Fixed unit tests

This commit is contained in:
Michael Schimmel
2025-09-29 12:00:56 +02:00
parent 823ea0e8f1
commit 2b34046efc
2 changed files with 65 additions and 177 deletions
+14 -9
View File
@@ -46,13 +46,18 @@ implementation
function TInterpreterScopeTests.Execute(const ANode: IAstNode): TDataValue;
var
boundScope: IExecutionScope;
visitor: IAstVisitor;
binder: IAstBinder;
boundNode: IAstNode;
descriptor: IScopeDescriptor;
runtimeScope: IExecutionScope;
visitor: IEvaluatorVisitor;
begin
// Helper to encapsulate the Bind -> CreateScope -> Evaluate pattern.
boundScope := TAstBinder.Bind(ANode, FGlobalScope).CreateScope(FGlobalScope);
visitor := TEvaluatorVisitor.Create(boundScope);
Result := ANode.Accept(visitor);
binder := TAstBinder.Create(FGlobalScope);
boundNode := binder.Execute(ANode, descriptor);
runtimeScope := descriptor.CreateScope(FGlobalScope);
visitor := TEvaluatorVisitor.Create(runtimeScope);
Result := visitor.Execute(boundNode);
end;
procedure TInterpreterScopeTests.Setup;
@@ -95,7 +100,7 @@ begin
TAst.Identifier('x'),
TAst.BinaryExpr(
TAst.Identifier('x'),
boAdd,
TScalar.TBinaryOp.Add,
TAst.Constant(TScalar.FromInt64(5))
)
)
@@ -138,7 +143,7 @@ begin
[], // Inner closure that is returned
TAst.Assign(
TAst.Identifier('a'),
TAst.BinaryExpr(TAst.Identifier('a'), boAdd, TAst.Constant(TScalar.FromInt64(5)))
TAst.BinaryExpr(TAst.Identifier('a'), TScalar.TBinaryOp.Add, TAst.Constant(TScalar.FromInt64(5)))
)
)
)
@@ -169,7 +174,7 @@ begin
[TAst.Identifier('p')], // Parameter 'p'
TAst.LambdaExpr(
[], // Returned lambda captures 'p'
TAst.BinaryExpr(TAst.Identifier('p'), boMultiply, TAst.Constant(TScalar.FromInt64(2)))
TAst.BinaryExpr(TAst.Identifier('p'), TScalar.TBinaryOp.Multiply, TAst.Constant(TScalar.FromInt64(2)))
)
)
),
@@ -225,7 +230,7 @@ var
begin
// Defines a variable in the global scope and ensures a simple script can access it.
FGlobalScope.Define('g', TScalar.FromInt64(99));
mainBlock := TAst.BinaryExpr(TAst.Identifier('g'), boAdd, TAst.Constant(TScalar.FromInt64(1)));
mainBlock := TAst.BinaryExpr(TAst.Identifier('g'), TScalar.TBinaryOp.Add, TAst.Constant(TScalar.FromInt64(1)));
resultValue := Execute(mainBlock);
Assert.AreEqual<Int64>(100, resultValue.AsScalar.Value.AsInt64, 'Should be able to access variables from the parent scope.');