-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathast_printer.go
46 lines (35 loc) · 1.14 KB
/
ast_printer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package glox
type AstPrinter struct{}
// func (ap *AstPrinter) Print(expr Expr) (string, error) {
// val, err := expr.Accept(ap)
// if err != nil {
// return "", err
// }
// return val.(string), nil
// }
// func (ap *AstPrinter) VisitBinaryExpr(expr *Binary) (interface{}, error) {
// return ap.parenthesize(expr.Operator.Lexeme, expr.Left, expr.Right), nil
// }
// func (ap *AstPrinter) VisitGroupingExpr(expr *Grouping) (interface{}, error) {
// return ap.parenthesize("group", expr.Expression), nil
// }
// func (ap *AstPrinter) VisitLiteralExpr(expr *Literal) (interface{}, error) {
// if expr.Value == nil {
// return "nil", nil
// }
// return fmt.Sprintf("%v", expr.Value), nil
// }
// func (ap *AstPrinter) VisitUnaryExpr(expr *Unary) (interface{}, error) {
// return ap.parenthesize(expr.Operator.Lexeme, expr.Right), nil
// }
// func (ap *AstPrinter) parenthesize(name string, exprs ...Expr) string {
// s := strings.Builder{}
// s.WriteString("(" + name)
// for _, expr := range exprs {
// s.WriteString(" ")
// val,_ := expr.Accept(ap)
// s.WriteString(val.(string))
// }
// s.WriteString(")")
// return s.String()
// }