Skip to content
Snippets Groups Projects
Commit 67cb0881 authored by Nathan PERIER's avatar Nathan PERIER
Browse files

Fixed name expression evaluation

parent 57d810d5
No related branches found
No related tags found
No related merge requests found
......@@ -30,7 +30,11 @@ public class NameExpression extends Expression {
@Override
public Expression evaluate(Context ctx) {
throw new UnsupportedOperationException("A name expression is not associated with a value and should never be evaluated (programmer messed up, please report)");
if(ctx.hasVariable(name)) {
VariableDef vd = ctx.getVariable(name);
return vd.getExpression().evaluate(ctx);
}
throw new RuntimeException("Name expression " + name + " was not associated with a value (programmer messed up, please report)");
}
@Override
......
......@@ -90,6 +90,10 @@ public class ExpressionParserTest {
Expression e = ExpressionParser.parse(it, ctx);
Expression exp = new NameExpression("var", new GenericType("a"));
Assertions.assertEquals(exp, e);
ValueDef vd = new ValueDef("var", SymbolPosition.begin(), new GenericType("int"));
vd.setExpression(new IntExpression(2));
ctx.addVariable(vd);
Assertions.assertEquals(vd.getExpression(), e.evaluate(ctx));
Assertions.assertTrue(it.junkIf(TokenType.TERMINATION));
Assertions.assertFalse(it.hasNext());
}
......@@ -122,5 +126,4 @@ public class ExpressionParserTest {
Assertions.assertThrows(ParsingException.class, () -> ExpressionParser.parse(it, ctx));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment