From 2d60d273afee72efb1805a51e2cf307296dd1000 Mon Sep 17 00:00:00 2001 From: akpaevj Date: Mon, 30 Sep 2024 22:19:01 +0300 Subject: [PATCH] closes #1448 --- .../Compiler/StackMachineCodeGenerator.cs | 91 ++++++------------- src/ScriptEngine/Machine/Core.cs | 18 ---- 2 files changed, 29 insertions(+), 80 deletions(-) diff --git a/src/ScriptEngine/Compiler/StackMachineCodeGenerator.cs b/src/ScriptEngine/Compiler/StackMachineCodeGenerator.cs index 0d25b176b..125b95a5f 100644 --- a/src/ScriptEngine/Compiler/StackMachineCodeGenerator.cs +++ b/src/ScriptEngine/Compiler/StackMachineCodeGenerator.cs @@ -33,7 +33,6 @@ public partial class StackMachineCodeGenerator : BslSyntaxWalker private readonly StackRuntimeModule _module; private SourceCode _sourceCode; private SymbolTable _ctx; - private List _constMap = new List(); private readonly List _forwardedMethods = new List(); private readonly Stack _nestedLoops = new Stack(); @@ -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 @@ -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; @@ -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); @@ -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); } @@ -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) @@ -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); } @@ -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) @@ -1166,49 +1146,36 @@ private static BslMethodBuilder NewMethod() => new BslMethodInfoFactory(() => 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; } diff --git a/src/ScriptEngine/Machine/Core.cs b/src/ScriptEngine/Machine/Core.cs index b74dda0b8..05ead6844 100644 --- a/src/ScriptEngine/Machine/Core.cs +++ b/src/ScriptEngine/Machine/Core.cs @@ -159,24 +159,6 @@ public enum DataType Boolean, Null } - - [Serializable] - public struct ConstDefinition : IEquatable - { - 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