Skip to content

Commit

Permalink
closes #1448
Browse files Browse the repository at this point in the history
  • Loading branch information
akpaevj committed Sep 30, 2024
1 parent e091463 commit 2d60d27
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 80 deletions.
91 changes: 29 additions & 62 deletions src/ScriptEngine/Compiler/StackMachineCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public partial class StackMachineCodeGenerator : BslSyntaxWalker
private readonly StackRuntimeModule _module;
private SourceCode _sourceCode;
private SymbolTable _ctx;
private List<ConstDefinition> _constMap = new List<ConstDefinition>();

private readonly List<ForwardedMethodDecl> _forwardedMethods = new List<ForwardedMethodDecl>();
private readonly Stack<NestedLoopInfo> _nestedLoops = new Stack<NestedLoopInfo>();
Expand Down Expand Up @@ -260,7 +259,7 @@ protected override void VisitMethod(MethodNode methodNode)

if (paramNode.HasDefaultValue)
{
var constDef = CreateConstDefinition(paramNode.DefaultValue);
var constDef = CreateConstValue(paramNode.DefaultValue);
var defValueIndex = GetConstNumber(constDef);

parameter
Expand Down Expand Up @@ -306,15 +305,7 @@ protected override void VisitMethodBody(MethodNode methodNode)
base.VisitMethodBody(methodNode);

if (methodNode.Signature.IsFunction)
{
var undefConst = new ConstDefinition()
{
Type = DataType.Undefined,
Presentation = "Неопределено"
};

AddCommand(OperationCode.PushConst, GetConstNumber(undefConst));
}
AddCommand(OperationCode.PushConst, GetConstNumber(DataType.Undefined, "Неопределено"));

var codeEnd = _module.Code.Count;

Expand Down Expand Up @@ -690,10 +681,7 @@ private void ResolveObjectMethod(BslSyntaxNode callNode, bool asFunction)

PushCallArguments(args);

var cDef = new ConstDefinition();
cDef.Type = DataType.String;
cDef.Presentation = name.GetIdentifier();
int lastIdentifierConst = GetConstNumber(cDef);
int lastIdentifierConst = GetConstNumber(DataType.String, name.GetIdentifier());

if (asFunction)
AddCommand(OperationCode.ResolveMethodFunc, lastIdentifierConst);
Expand All @@ -703,10 +691,7 @@ private void ResolveObjectMethod(BslSyntaxNode callNode, bool asFunction)

private void ResolveProperty(string identifier)
{
var cDef = new ConstDefinition();
cDef.Type = DataType.String;
cDef.Presentation = identifier;
var identifierConstIndex = GetConstNumber(cDef);
var identifierConstIndex = GetConstNumber(DataType.String, identifier);
AddCommand(OperationCode.ResolveProp, identifierConstIndex);
}

Expand Down Expand Up @@ -989,12 +974,7 @@ private void MakeNewObjectDynamic(NewObjectNode node)

private void MakeNewObjectStatic(NewObjectNode node)
{
var cDef = new ConstDefinition()
{
Type = DataType.String,
Presentation = node.TypeNameNode.GetIdentifier()
};
AddCommand(OperationCode.PushConst, GetConstNumber(cDef));
AddCommand(OperationCode.PushConst, GetConstNumber(DataType.String, node.TypeNameNode.GetIdentifier()));

var callArgs = 0;
if (node.ConstructorArguments != default)
Expand Down Expand Up @@ -1108,7 +1088,7 @@ protected override void VisitConstant(TerminalNode node)

private void VisitConstant(in Lexem constant)
{
var cDef = CreateConstDefinition(constant);
var cDef = CreateConstValue(constant);
var num = GetConstNumber(cDef);
AddCommand(OperationCode.PushConst, num);
}
Expand Down Expand Up @@ -1138,7 +1118,7 @@ private BslAnnotationParameter MakeAnnotationParameter(AnnotationParameterNode p
BslAnnotationParameter result;
if (param.Value.Type != LexemType.NotALexem)
{
var constDef = CreateConstDefinition(param.Value);
var constDef = CreateConstValue(param.Value);
var constNumber = GetConstNumber(constDef);
var runtimeValue = _module.Constants[constNumber];
result = new BslAnnotationParameter(param.Name, runtimeValue)
Expand Down Expand Up @@ -1166,49 +1146,36 @@ private static BslMethodBuilder<MachineMethodInfo> NewMethod() =>
new BslMethodInfoFactory<MachineMethodInfo>(() => new MachineMethodInfo())
.NewMethod();

private static ConstDefinition CreateConstDefinition(in Lexem lex)
private static BslPrimitiveValue CreateConstValue(in Lexem lex)
{
DataType constType;
switch (lex.Type)
{
case LexemType.BooleanLiteral:
constType = DataType.Boolean;
break;
case LexemType.DateLiteral:
constType = DataType.Date;
break;
case LexemType.NumberLiteral:
constType = DataType.Number;
break;
case LexemType.StringLiteral:
constType = DataType.String;
break;
case LexemType.NullLiteral:
constType = DataType.Null;
break;
case LexemType.UndefinedLiteral:
constType = DataType.Undefined;
break;
default:
throw new ArgumentException($"Can't create constant for literal from {lex.ToString()}");
}

ConstDefinition cDef = new ConstDefinition()
var constType = lex.Type switch
{
Type = constType,
Presentation = lex.Content
LexemType.BooleanLiteral => DataType.Boolean,
LexemType.DateLiteral => DataType.Date,
LexemType.NumberLiteral => DataType.Number,
LexemType.StringLiteral => DataType.String,
LexemType.NullLiteral => DataType.Null,
LexemType.UndefinedLiteral => DataType.Undefined,
_ => throw new ArgumentException($"Can't create constant for literal from {lex}"),
};
return cDef;

return (BslPrimitiveValue)ValueFactory.Parse(lex.Content, constType);
}

private int GetConstNumber(in ConstDefinition cDef)
private int GetConstNumber(DataType type, string presentation)
{
var value = (BslPrimitiveValue)ValueFactory.Parse(presentation, type);

return GetConstNumber(value);
}

private int GetConstNumber(BslPrimitiveValue value)
{
var idx = _constMap.IndexOf(cDef);
var idx = _module.Constants.IndexOf(value);
if (idx < 0)
{
idx = _constMap.Count;
_constMap.Add(cDef);
_module.Constants.Add((BslPrimitiveValue)ValueFactory.Parse(cDef.Presentation, cDef.Type));
_module.Constants.Add(value);
idx = _module.Constants.Count - 1;
}
return idx;
}
Expand Down
18 changes: 0 additions & 18 deletions src/ScriptEngine/Machine/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,24 +159,6 @@ public enum DataType
Boolean,
Null
}

[Serializable]
public struct ConstDefinition : IEquatable<ConstDefinition>
{
public DataType Type;
public string Presentation;

public override string ToString()
{
return Enum.GetName(typeof(DataType), Type) + ":" + Presentation;
}

public bool Equals(ConstDefinition other)
{
return Type == other.Type && string.Equals(Presentation, other.Presentation, StringComparison.Ordinal);
}

}

[Serializable]
public struct AnnotationDefinition
Expand Down

0 comments on commit 2d60d27

Please sign in to comment.