Skip to content

Commit

Permalink
Add of LargeStringAllocationObserver
Browse files Browse the repository at this point in the history
Add of ExecuteObserver
Add of CreateObjectObserver
  • Loading branch information
sbruyere committed May 17, 2024
1 parent 922d302 commit c2ac709
Show file tree
Hide file tree
Showing 34 changed files with 189 additions and 97 deletions.
3 changes: 0 additions & 3 deletions Sources/vbSparkle.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ static void Main(string[] args)

private static void InitializeConsoleHeader()
{
Console.ForegroundColor = Color.WhiteSmoke;
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
Console.ResetColor();
Console.ReplaceAllColorsWithDefaults();
Console.Title = "vbSparkle " + version;

Console.WriteLine(
Expand Down
2 changes: 1 addition & 1 deletion Sources/vbSparkle.Console/vbSparkle.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<StartupObject>vbSparkle.CLI.Program</StartupObject>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<Authors>Sylvain Bruyere, Airbus CERT</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
using System.Text;
using MathNet.Symbolics;
using vbSparkle;
using vbSparkle.Options;

namespace vbSparkle.EvaluationObjects
{
internal class DComplexStringExpression
: DExpression, IStringExpression
{

public List<DExpression> ConcatExpressions { get; set; } = new List<DExpression>();

public override bool HasSideEffet { get; set; } = false;
public override bool IsValuable { get; set; } = true;

public DComplexStringExpression()
{

}

public DComplexStringExpression(DExpression leftExp)
Expand Down Expand Up @@ -80,7 +81,9 @@ public void Concat(DExpression expression)

if (curRightExp is DSimpleStringExpression && expression.IsValuable)
{
ConcatExpressions[ConcatExpressions.Count - 1] = new DSimpleStringExpression(curRightExp.ToValueString() + expression.ToValueString(), Encoding.Unicode);
var options = (curRightExp as DSimpleStringExpression).Options;

ConcatExpressions[ConcatExpressions.Count - 1] = new DSimpleStringExpression(curRightExp.ToValueString() + expression.ToValueString(), Encoding.Unicode, options);
//((DSimpleStringExpression)curRightExp).SetValue(curRightExp.ToValueString() + expression.ToValueString()); <= this was causing side effect
return;
}
Expand Down
15 changes: 14 additions & 1 deletion Sources/vbSparkle/EvaluationObjects/DStringExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,41 @@
using System.Text;
using MathNet.Symbolics;
using vbSparkle;
using vbSparkle.Options;

namespace vbSparkle.EvaluationObjects
{

internal class DSimpleStringExpression
: DExpression, IStringExpression
{
internal EvaluatorOptions Options { get; set; } = null;
string var;

public override bool HasSideEffet { get => false; set => throw new NotImplementedException(); }

public override bool IsValuable { get => true; set => throw new NotImplementedException(); }
public Encoding Encoding { get; set; }

public DSimpleStringExpression(string value, Encoding encoding)
public DSimpleStringExpression(string value, Encoding encoding, EvaluatorOptions options)
{
if (options == null)
(0).ToString();

Options = options;
var = value;
Encoding = encoding;
}

public override string ToExpressionString()
{
if (Options != null &&
Options.LargeStringAllocationObserver != null &&
Options.LargeStringAllocationObserver.MinSize < var.Length)
{
Options.LargeStringAllocationObserver.LargeStringAllocated.Add(var.Replace("\"\"","\""));
}

return VbUtils.StrValToExp(var);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public VbIdentifiedObject IdentifiedObject

public override DExpression Prettify(bool partialEvaluation = false)
{
var identifiedObject = IdentifiedObject;

if (identifiedObject is VbNativeFunction)
{
var funcArgs = CallArgs.ToArray();
return (identifiedObject as VbNativeFunction).TryEvaluate(funcArgs);
}

if (CallArgs.Any())
return new DCodeBlock($"{IdentifiedObject.Name} {string.Join(", ", CallArgs.Select(v => v.Exp(partialEvaluation)))}");
else
Expand Down
47 changes: 37 additions & 10 deletions Sources/vbSparkle/LanguageStatements/Functions/VBMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public VB_StrReverse(IVBScopeObject context)
: base(context, "StrReverse")
{
}

public override DExpression Evaluate(params DExpression[] args)
{
DExpression arg1 = args.FirstOrDefault();
Expand All @@ -106,12 +106,39 @@ public override DExpression Evaluate(params DExpression[] args)

if (!Converter.TryGetStringValue(arg1, out strArg))
return DefaultExpression(args);

string str = new string(strArg.ToCharArray().Reverse().ToArray());

return new DSimpleStringExpression(str, Encoding.Unicode);
return new DSimpleStringExpression(str, Encoding.Unicode, Context.Options);
}
}

public class VB_Execute
: VbNativeFunction
{
public VB_Execute(IVBScopeObject context)
: base(context, "Execute")
{
}

public override DExpression Evaluate(params DExpression[] args)
{
DExpression arg1 = args.FirstOrDefault();

string strArg;

if (!Converter.TryGetStringValue(arg1, out strArg))
{
return DefaultExpression(args);
}

if (Context?.Options?.ExecuteObserver != null)
{
Context.Options.ExecuteObserver.VBScriptExecuted.Add(strArg.Replace("\"\"", "\""));
}

return DefaultExpression(args);
}
}

public class VB_Replace
Expand Down Expand Up @@ -159,7 +186,7 @@ public override DExpression Evaluate(params DExpression[] args)

string str = findStr.Equals(replStr) ? expStr : expStr.Replace(findStr, replStr);

return new DSimpleStringExpression(str, Encoding.Unicode);
return new DSimpleStringExpression(str, Encoding.Unicode, Context.Options);
}

}
Expand Down Expand Up @@ -195,7 +222,7 @@ public override DExpression Evaluate(params DExpression[] args)
return DefaultExpression(args);

string str = strArg.Trim(' ');
return new DSimpleStringExpression(str, Encoding.Unicode);
return new DSimpleStringExpression(str, Encoding.Unicode, Context.Options);
}

}
Expand Down Expand Up @@ -233,7 +260,7 @@ public override DExpression Evaluate(params DExpression[] args)

string value = new string(' ', count);

return new DSimpleStringExpression(value, Encoding.Unicode);
return new DSimpleStringExpression(value, Encoding.Unicode, Context.Options);
}

}
Expand Down Expand Up @@ -265,7 +292,7 @@ public override DExpression Evaluate(params DExpression[] args)

//string value = Char.ConvertFromUtf32((int)ascii); //(byte) (UInt32)Math.Round(ascii) & 0x0000FFFF);

return new DSimpleStringExpression(value, Encoding.Unicode);
return new DSimpleStringExpression(value, Encoding.Unicode, Context.Options);
}

}
Expand Down Expand Up @@ -305,7 +332,7 @@ public override DExpression Evaluate(params DExpression[] args)
//string value = Encoding.ASCII.GetString(test);
string value = new string(new char[] { VbUtils.Chr(ascii) });

return new DSimpleStringExpression(value, Encoding.Unicode);
return new DSimpleStringExpression(value, Encoding.Unicode, Context.Options);
}
}

Expand Down Expand Up @@ -342,7 +369,7 @@ public override DExpression Evaluate(params DExpression[] args)

string value = new string(new char[]{ VbUtils.Chr(ascii) });

return new DSimpleStringExpression(value, Encoding.Unicode);
return new DSimpleStringExpression(value, Encoding.Unicode, Context.Options);
}

}
Expand Down Expand Up @@ -735,7 +762,7 @@ public override DExpression Evaluate(params DExpression[] args)

string hexStr = $"{input:X}";

return new DSimpleStringExpression(hexStr, null);
return new DSimpleStringExpression(hexStr, null, Context.Options);
}
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/vbSparkle/LanguageStatements/Literals/VBLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public abstract class VBLiteral
{
public DExpression Value { get; set; }
public IVBScopeObject Context { get; set; }

public abstract string Prettify();
}
Expand All @@ -12,9 +13,10 @@ public class VBLiteral<T> : VBLiteral
{
public T Object { get; set; }

public VBLiteral(T @object)
public VBLiteral(IVBScopeObject context, T @object)
{
Object = @object;
Context = context;
Value = new DCodeBlock(@object?.GetText());
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/vbSparkle/LanguageStatements/Literals/VbLtBoolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace vbSparkle
{
public class VbLtBoolean : VBLiteral<LtBooleanContext>
{
public VbLtBoolean(LtBooleanContext @object)
: base(@object)
public VbLtBoolean(IVBScopeObject context, LtBooleanContext @object)
: base(context, @object)
{
string quoted = @object.GetText();
if (quoted.Equals("True", StringComparison.InvariantCultureIgnoreCase))
Expand Down
4 changes: 2 additions & 2 deletions Sources/vbSparkle/LanguageStatements/Literals/VbLtColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace vbSparkle
{
public class VbLtColor : VBLiteral<LtColorContext>
{
public VbLtColor(LtColorContext @object)
: base(@object)
public VbLtColor(IVBScopeObject context, LtColorContext @object)
: base(context, @object)
{
string quoted = @object.GetText();
Value = new DMathExpression<Int32>( Convert.ToInt32(quoted.Substring(2).Replace("&",""), 16));
Expand Down
4 changes: 2 additions & 2 deletions Sources/vbSparkle/LanguageStatements/Literals/VbLtDateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace vbSparkle
{
public class VbLtDateTime : VBLiteral<LtDateContext>
{
public VbLtDateTime(LtDateContext @object)
: base(@object)
public VbLtDateTime(IVBScopeObject context, LtDateContext @object)
: base(context, @object)
{
string date = @object.GetText();
date = date.Substring(1, date.Length - 2);
Expand Down
4 changes: 2 additions & 2 deletions Sources/vbSparkle/LanguageStatements/Literals/VbLtDouble.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace vbSparkle
{
public class VbLtDouble : VBLiteral<LtDoubleContext>
{
public VbLtDouble(LtDoubleContext @object)
: base(@object)
public VbLtDouble(IVBScopeObject context, LtDoubleContext @object)
: base(context, @object)
{
string quoted = @object.GetText();
Value = new DMathExpression<double>(double.Parse(quoted));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace vbSparkle
{
public class VbLtFileNumber : VBLiteral<LtFilenumberContext>
{
public VbLtFileNumber(LtFilenumberContext @object)
: base(@object)
public VbLtFileNumber(IVBScopeObject context, LtFilenumberContext @object)
: base(context, @object)
{
string quoted = @object.GetText();
Value = new DMathExpression<Int32>(int.Parse(quoted.Replace("#", "")));
Expand Down
4 changes: 2 additions & 2 deletions Sources/vbSparkle/LanguageStatements/Literals/VbLtInteger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace vbSparkle
{
public class VbLtInteger : VBLiteral<LtIntegerContext>
{
public VbLtInteger(LtIntegerContext @object)
: base(@object)
public VbLtInteger(IVBScopeObject context, LtIntegerContext @object)
: base(context, @object)
{
string quoted = @object.GetText();
if (quoted.EndsWith("#"))
Expand Down
4 changes: 2 additions & 2 deletions Sources/vbSparkle/LanguageStatements/Literals/VbLtNothing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace vbSparkle
{
public class VbLtNothing : VBLiteral<LtNothingContext>
{
public VbLtNothing(LtNothingContext @object)
: base(@object)
public VbLtNothing(IVBScopeObject context, LtNothingContext @object)
: base(context, @object)
{
Value = new DCodeBlock("Nothing");
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/vbSparkle/LanguageStatements/Literals/VbLtNull.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace vbSparkle
{
public class VbLtNull : VBLiteral<LtNullContext>
{
public VbLtNull(LtNullContext @object)
: base(@object)
public VbLtNull(IVBScopeObject context, LtNullContext @object)
: base(context, @object)
{
Value = new DCodeBlock("Null");
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/vbSparkle/LanguageStatements/Literals/VbLtOctal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace vbSparkle
{
public class VbLtOctal : VBLiteral<LtOctalContext>
{
public VbLtOctal(LtOctalContext @object)
: base(@object)
public VbLtOctal(IVBScopeObject context, LtOctalContext @object)
: base(context, @object)
{
string quoted = @object.GetText();
Value = new DMathExpression<Int32>(Convert.ToInt32(quoted.Substring(2, quoted.Length - 2), 8));
Expand Down
6 changes: 3 additions & 3 deletions Sources/vbSparkle/LanguageStatements/Literals/VbLtString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace vbSparkle
{
public class VbLtString : VBLiteral<LtStringContext>
{
public VbLtString(LtStringContext @object)
: base(@object)
public VbLtString(IVBScopeObject context, LtStringContext @object)
: base(context, @object)
{
string quoted = @object.GetText();
Value = new DSimpleStringExpression(quoted.Substring(1, quoted.Length -2), Encoding.Unicode);
Value = new DSimpleStringExpression(quoted.Substring(1, quoted.Length -2), Encoding.Unicode, context.Options);
}

public override string Prettify()
Expand Down
Loading

0 comments on commit c2ac709

Please sign in to comment.