|
|
|
@ -34,33 +34,45 @@ func (ap *Printer) VisitExpressionStmt(stmt *ExpressionStmt) any {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ap *Printer) VisitPrintStmt(stmt *PrintStmt) any {
|
|
|
|
func (ap *Printer) VisitPrintStmt(stmt *PrintStmt) any {
|
|
|
|
return ap.parenthesize("print", stmt.Expression)
|
|
|
|
return ap.parenthesizeExpr("print", stmt.Expression)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ap *Printer) VisitVarStmt(stmt *VarStmt) any {
|
|
|
|
func (ap *Printer) VisitIfStmt(stmt *IfStmt) any {
|
|
|
|
return ap.parenthesize("var", &LiteralExpr{stmt.Name}, stmt.Initializer)
|
|
|
|
str := "(if"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (ap *Printer) VisitBlockStmt(stmt *BlockStmt) any {
|
|
|
|
if stmt.Condition != nil {
|
|
|
|
str := "(block\n"
|
|
|
|
str += " " + stmt.Condition.Accept(ap).(string)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, s := range stmt.Statements {
|
|
|
|
if stmt.ThenBranch != nil {
|
|
|
|
str += " " + s.Accept(ap).(string) + "\n"
|
|
|
|
str += " " + ap.bracesizeStmt(stmt.ThenBranch)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if stmt.ElseBranch != nil {
|
|
|
|
|
|
|
|
str += " else " + ap.bracesizeStmt(stmt.ElseBranch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return str + ")"
|
|
|
|
return str + ")"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (ap *Printer) VisitVarStmt(stmt *VarStmt) any {
|
|
|
|
|
|
|
|
return ap.parenthesizeExpr("var", &LiteralExpr{stmt.Name}, stmt.Initializer)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (ap *Printer) VisitBlockStmt(stmt *BlockStmt) any {
|
|
|
|
|
|
|
|
return ap.bracesizeStmt(stmt.Statements...)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ap *Printer) VisitVariableExpr(expr *VariableExpr) any {
|
|
|
|
func (ap *Printer) VisitVariableExpr(expr *VariableExpr) any {
|
|
|
|
return expr.Name.Lexeme
|
|
|
|
return expr.Name.Lexeme
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ap *Printer) VisitBinaryExpr(expr *BinaryExpr) any {
|
|
|
|
func (ap *Printer) VisitBinaryExpr(expr *BinaryExpr) any {
|
|
|
|
return ap.parenthesize(expr.Operator.Lexeme, expr.Left, expr.Right)
|
|
|
|
return ap.parenthesizeExpr(expr.Operator.Lexeme, expr.Left, expr.Right)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ap *Printer) VisitGroupingExpr(expr *GroupingExpr) any {
|
|
|
|
func (ap *Printer) VisitGroupingExpr(expr *GroupingExpr) any {
|
|
|
|
return ap.parenthesize("group", expr.Expression)
|
|
|
|
return ap.parenthesizeExpr("group", expr.Expression)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ap *Printer) VisitLiteralExpr(expr *LiteralExpr) any {
|
|
|
|
func (ap *Printer) VisitLiteralExpr(expr *LiteralExpr) any {
|
|
|
|
@ -71,7 +83,7 @@ func (ap *Printer) VisitLiteralExpr(expr *LiteralExpr) any {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ap *Printer) VisitUnaryExpr(expr *UnaryExpr) any {
|
|
|
|
func (ap *Printer) VisitUnaryExpr(expr *UnaryExpr) any {
|
|
|
|
return ap.parenthesize(expr.Operator.Lexeme, expr.Right)
|
|
|
|
return ap.parenthesizeExpr(expr.Operator.Lexeme, expr.Right)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ap *Printer) VisitErrorExpr(expr *ErrorExpr) any {
|
|
|
|
func (ap *Printer) VisitErrorExpr(expr *ErrorExpr) any {
|
|
|
|
@ -79,10 +91,10 @@ func (ap *Printer) VisitErrorExpr(expr *ErrorExpr) any {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ap *Printer) VisitAssignExpr(expr *AssignExpr) any {
|
|
|
|
func (ap *Printer) VisitAssignExpr(expr *AssignExpr) any {
|
|
|
|
return ap.parenthesize("=", &VariableExpr{expr.Name}, expr.Value)
|
|
|
|
return ap.parenthesizeExpr("=", &VariableExpr{expr.Name}, expr.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ap *Printer) parenthesize(name string, exprs ...Expr) string {
|
|
|
|
func (ap *Printer) parenthesizeExpr(name string, exprs ...Expr) string {
|
|
|
|
str := "(" + name
|
|
|
|
str := "(" + name
|
|
|
|
|
|
|
|
|
|
|
|
for _, expr := range exprs {
|
|
|
|
for _, expr := range exprs {
|
|
|
|
@ -94,3 +106,13 @@ func (ap *Printer) parenthesize(name string, exprs ...Expr) string {
|
|
|
|
|
|
|
|
|
|
|
|
return str + ")"
|
|
|
|
return str + ")"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (ap *Printer) bracesizeStmt(stmts ...Stmt) string {
|
|
|
|
|
|
|
|
str := "{\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, s := range stmts {
|
|
|
|
|
|
|
|
str += " " + s.Accept(ap).(string) + "\n"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return str + "}"
|
|
|
|
|
|
|
|
}
|
|
|
|
|