glox/ast_string.go

52 lines
895 B
Go
Raw Normal View History

2024-10-04 15:24:01 +03:00
package main
import (
"fmt"
"strings"
)
type AstStringer struct {
str strings.Builder
}
func (as AstStringer) String(expr Expr) string {
if expr == nil {
return ""
}
expr.accept(&as)
return as.str.String()
}
func (as *AstStringer) visitBinary(b *Binary) any {
as.str.WriteString("(")
as.str.WriteString(b.op.lexeme)
as.str.WriteString(" ")
b.left.accept(as)
as.str.WriteString(" ")
b.right.accept(as)
as.str.WriteString(")")
return nil
}
func (as *AstStringer) visitLiteral(l *Literal) any {
as.str.WriteString(fmt.Sprintf("%v", l.value))
return nil
}
func (as *AstStringer) visitGrouping(g *Grouping) any {
as.str.WriteString("(group ")
g.expression.accept(as)
as.str.WriteString(")")
return nil
}
func (as *AstStringer) visitUnary(u *Unary) any {
as.str.WriteString(fmt.Sprintf("(%s ", u.op.lexeme))
u.right.accept(as)
as.str.WriteString(")")
return nil
}