Skip to content

Commit 264a41d

Browse files
committed
1 parent d2a72fa commit 264a41d

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

src/ScriptEngine/Compiler/StackMachineCodeGenerator.cs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This Source Code Form is subject to the terms of the
88
using System;
99
using System.Collections.Generic;
1010
using System.Diagnostics;
11+
using System.Globalization;
1112
using System.Linq;
1213
using System.Reflection;
1314
using System.Runtime.CompilerServices;
@@ -307,13 +308,7 @@ protected override void VisitMethodBody(MethodNode methodNode)
307308

308309
if (methodNode.Signature.IsFunction)
309310
{
310-
var undefConst = new ConstDefinition()
311-
{
312-
Type = DataType.Undefined,
313-
Presentation = "Неопределено"
314-
};
315-
316-
AddCommand(OperationCode.PushConst, GetConstNumber(undefConst));
311+
AddCommand(OperationCode.PushUndef);
317312
}
318313

319314
var codeEnd = _module.Code.Count;
@@ -1108,9 +1103,33 @@ protected override void VisitConstant(TerminalNode node)
11081103

11091104
private void VisitConstant(in Lexem constant)
11101105
{
1111-
var cDef = CreateConstDefinition(constant);
1112-
var num = GetConstNumber(cDef);
1113-
AddCommand(OperationCode.PushConst, num);
1106+
if (constant.Type == LexemType.BooleanLiteral)
1107+
{
1108+
AddCommand(OperationCode.PushBool, (bool)(BslValue)BslBooleanValue.Parse(constant.Content) ? 1 : 0);
1109+
}
1110+
else if (constant.Type == LexemType.NumberLiteral && ParseIntegerString(constant.Content, out var integer))
1111+
{
1112+
AddCommand(OperationCode.PushInt, integer);
1113+
}
1114+
else if (constant.Type == LexemType.UndefinedLiteral)
1115+
{
1116+
AddCommand(OperationCode.PushUndef);
1117+
}
1118+
else if (constant.Type == LexemType.NullLiteral)
1119+
{
1120+
AddCommand(OperationCode.PushNull);
1121+
}
1122+
else
1123+
{
1124+
var cDef = CreateConstDefinition(constant);
1125+
var num = GetConstNumber(cDef);
1126+
AddCommand(OperationCode.PushConst, num);
1127+
}
1128+
}
1129+
1130+
private bool ParseIntegerString(string constantContent, out int integer)
1131+
{
1132+
return int.TryParse(constantContent, NumberStyles.None, NumberFormatInfo.InvariantInfo, out integer);
11141133
}
11151134

11161135
private IEnumerable<BslAnnotationAttribute> GetAnnotationAttributes(AnnotatableNode node)

src/ScriptEngine/Machine/Core.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public enum OperationCode
1414
Nop,
1515
PushVar,
1616
PushConst,
17+
PushInt,
18+
PushBool,
19+
PushUndef,
20+
PushNull,
1721
PushLoc,
1822
PushRef,
1923
LoadVar,

src/ScriptEngine/Machine/MachineInstance.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,10 @@ private void InitCommands()
533533
(i)=>{NextInstruction();},
534534
PushVar,
535535
PushConst,
536+
PushInt,
537+
PushBool,
538+
PushUndef,
539+
PushNull,
536540
PushLoc,
537541
PushRef,
538542
LoadVar,
@@ -671,6 +675,30 @@ private void PushConst(int arg)
671675
_operationStack.Push(_module.Constants[arg]);
672676
NextInstruction();
673677
}
678+
679+
private void PushBool(int arg)
680+
{
681+
_operationStack.Push(BslBooleanValue.Create(arg == 1));
682+
NextInstruction();
683+
}
684+
685+
private void PushInt(int arg)
686+
{
687+
_operationStack.Push(BslNumericValue.Create(arg));
688+
NextInstruction();
689+
}
690+
691+
private void PushUndef(int arg)
692+
{
693+
_operationStack.Push(BslUndefinedValue.Instance);
694+
NextInstruction();
695+
}
696+
697+
private void PushNull(int arg)
698+
{
699+
_operationStack.Push(BslNullValue.Instance);
700+
NextInstruction();
701+
}
674702

675703
private void PushLoc(int arg)
676704
{

0 commit comments

Comments
 (0)