Skip to content

Commit

Permalink
fixing loop dynamic expression
Browse files Browse the repository at this point in the history
  • Loading branch information
arakov committed Jul 14, 2021
1 parent f72b5e8 commit 39df7c0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src50/system/dynamic/expressions/expressions.l
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ namespace expressions
//
// static Expression Assigning(ScopeVariable variable, Expression expr)
// = new AssigningExpression(variable, expr);
//
// static Expression Assigning(string variable, Expression expr)
// = new AssigningExpression(new ScopeVariable(variable), expr);

static Expression Assigning(string variable, Expression expr)
= new AssigningExpression(variable, expr);

static Expression DeclareAndAssigning(string variable, Expression expr)
= new DeclaringAndAssigningExpression(variable, expr);
Expand Down Expand Up @@ -691,6 +691,9 @@ namespace expressions

declare(ScopeIdentifier variable)
{
int index := variables.Length + 1;
scope.reserve(index);

variables.append(variable)
}
};
Expand Down Expand Up @@ -1393,6 +1396,12 @@ namespace expressions
this expression := expression
}

constructor(string variable, Expression expression)
{
this variable := ScopeIdentifier.Variable(variable);
this expression := expression
}

bool IsOperation = false;

int prepare(ExpressionScope scope)
Expand Down
1 change: 1 addition & 0 deletions tests/system/main.l
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public program()
//templateExtensionTest();

ifExpressionTest();
loopExpressionTest();

console.writeLine:"--- Passed ---"
}
55 changes: 55 additions & 0 deletions tests/system/test_arithm.l
Original file line number Diff line number Diff line change
Expand Up @@ -834,3 +834,58 @@ public ifExpressionTest()

console.writeLine("done")
}

public loopExpressionTest()
{
console.write("loopExpressionTest..");

auto c := DynamicSingleton.new(
Expression.Method(
"eval",
Expression.CodeBlock(
Expression.DeclareAndAssigning(
"i",
Expression.Constant(0)
),
Expression.DeclareAndAssigning(
"k",
Expression.Constant(0)
),
Expression.Loop(
Expression.MessageCall(
new Message("less[2]"),
Expression.Variable("i"),
Expression.Variable("n")
),
Expression.CodeBlock(
Expression.Assigning(
"k",
Expression.MessageCall(
new Message("add[2]"),
Expression.Variable("k"),
Expression.Constant(2)
)
),
Expression.Assigning(
"i",
Expression.MessageCall(
new Message("add[2]"),
Expression.Variable("i"),
Expression.Constant(1)
)
)
)
),
Expression.Variable("k")
),
ScopeIdentifier.Variable("n")
)
);

var o := c.compile();

Assert.ifEqual(o.eval(4), 8);
Assert.ifEqual(o.eval(0), 0);

console.writeLine("done")
}

0 comments on commit 39df7c0

Please sign in to comment.