Evaluating Expressions 7

This commit is contained in:
nazrin 2025-05-31 17:44:11 +00:00
parent d937226553
commit f4338ba51f
6 changed files with 182 additions and 63 deletions

View file

@ -17,7 +17,7 @@ abstract class Expr{
R visit(Literal expr);
R visit(Unary expr);
}
private alias rTypes = AliasSeq!(string);
private alias rTypes = AliasSeq!(string, TValue);
static foreach(T; rTypes)
abstract T accept(Visitor!T visitor);
private template defCtorAndAccept(){
@ -37,7 +37,7 @@ abstract class Expr{
mixin defCtorAndAccept;
}
static class Literal : Expr{
TTokenValue value;
TValue value;
mixin defCtorAndAccept;
}
static class Unary : Expr{
@ -78,9 +78,9 @@ class AstPrinter : Expr.Visitor!string{
unittest{
auto expression = new Expr.Binary(
new Expr.Unary(
new Token(TokenType.MINUS, "-", TTokenValue.nil(0), 1),
new Token(TokenType.MINUS, "-", TValue.nil(0), 1),
new Expr.Literal(123)),
new Token(TokenType.STAR, "*", TTokenValue.nil(0), 1),
new Token(TokenType.STAR, "*", TValue.nil(0), 1),
new Expr.Grouping(
new Expr.Literal(45.67)));
assert(new AstPrinter().print(expression));