Statements and State 8
This commit is contained in:
parent
f4338ba51f
commit
e749367886
7 changed files with 250 additions and 69 deletions
|
|
@ -12,10 +12,12 @@ import jlox.util;
|
|||
|
||||
abstract class Expr{
|
||||
interface Visitor(R){
|
||||
R visit(Assign expr);
|
||||
R visit(Binary expr);
|
||||
R visit(Grouping expr);
|
||||
R visit(Literal expr);
|
||||
R visit(Unary expr);
|
||||
R visit(Variable expr);
|
||||
}
|
||||
private alias rTypes = AliasSeq!(string, TValue);
|
||||
static foreach(T; rTypes)
|
||||
|
|
@ -26,63 +28,61 @@ abstract class Expr{
|
|||
override T accept(Visitor!T visitor) => visitor.visit(this);
|
||||
}
|
||||
|
||||
static class Binary : Expr{
|
||||
static class Assign : typeof(this){
|
||||
Token name;
|
||||
Expr value;
|
||||
mixin defCtorAndAccept;
|
||||
}
|
||||
static class Binary : typeof(this){
|
||||
Expr left;
|
||||
Token operator;
|
||||
Expr right;
|
||||
mixin defCtorAndAccept;
|
||||
}
|
||||
static class Grouping : Expr{
|
||||
static class Grouping : typeof(this){
|
||||
Expr expression;
|
||||
mixin defCtorAndAccept;
|
||||
}
|
||||
static class Literal : Expr{
|
||||
static class Literal : typeof(this){
|
||||
TValue value;
|
||||
mixin defCtorAndAccept;
|
||||
}
|
||||
static class Unary : Expr{
|
||||
static class Unary : typeof(this){
|
||||
Token operator;
|
||||
Expr right;
|
||||
mixin defCtorAndAccept;
|
||||
}
|
||||
}
|
||||
|
||||
class AstPrinter : Expr.Visitor!string{
|
||||
string print(Expr expr){
|
||||
return expr.accept(this);
|
||||
}
|
||||
string parenthesize(Args...)(string name, Args args){
|
||||
string s = "(" ~ name;
|
||||
static foreach(expr; args){
|
||||
s ~= " ";
|
||||
s ~= expr.accept(this);
|
||||
}
|
||||
return s ~ ")";
|
||||
}
|
||||
string visit(Expr.Binary expr){
|
||||
return parenthesize(expr.operator.lexeme, expr.left, expr.right);
|
||||
}
|
||||
string visit(Expr.Grouping expr){
|
||||
return parenthesize("group", expr.expression);
|
||||
}
|
||||
string visit(Expr.Literal expr){
|
||||
if(expr.value.isNil)
|
||||
return "nil";
|
||||
return expr.value.to!string;
|
||||
}
|
||||
string visit(Expr.Unary expr){
|
||||
return parenthesize(expr.operator.lexeme, expr.right);
|
||||
static class Variable : typeof(this){
|
||||
Token name;
|
||||
mixin defCtorAndAccept;
|
||||
}
|
||||
}
|
||||
|
||||
unittest{
|
||||
auto expression = new Expr.Binary(
|
||||
new Expr.Unary(
|
||||
new Token(TokenType.MINUS, "-", TValue.nil(0), 1),
|
||||
new Expr.Literal(123)),
|
||||
new Token(TokenType.STAR, "*", TValue.nil(0), 1),
|
||||
new Expr.Grouping(
|
||||
new Expr.Literal(45.67)));
|
||||
assert(new AstPrinter().print(expression));
|
||||
}
|
||||
/* class AstPrinter : Expr.Visitor!string{ */
|
||||
/* string print(Expr expr){ */
|
||||
/* return expr.accept(this); */
|
||||
/* } */
|
||||
/* string parenthesize(Args...)(string name, Args args){ */
|
||||
/* string s = "(" ~ name; */
|
||||
/* static foreach(expr; args){ */
|
||||
/* s ~= " "; */
|
||||
/* s ~= expr.accept(this); */
|
||||
/* } */
|
||||
/* return s ~ ")"; */
|
||||
/* } */
|
||||
/* string visit(Expr.Binary expr){ */
|
||||
/* return parenthesize(expr.operator.lexeme, expr.left, expr.right); */
|
||||
/* } */
|
||||
/* string visit(Expr.Grouping expr){ */
|
||||
/* return parenthesize("group", expr.expression); */
|
||||
/* } */
|
||||
/* string visit(Expr.Literal expr){ */
|
||||
/* if(expr.value.isNil) */
|
||||
/* return "nil"; */
|
||||
/* return expr.value.to!string; */
|
||||
/* } */
|
||||
/* string visit(Expr.Unary expr){ */
|
||||
/* return parenthesize(expr.operator.lexeme, expr.right); */
|
||||
/* } */
|
||||
/* } */
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue