basic expression interpreter
This commit is contained in:
parent
eebaadc16e
commit
b244f7e3b2
5 changed files with 186 additions and 16 deletions
10
ast/ast.go
10
ast/ast.go
|
|
@ -16,7 +16,7 @@ type ExprVisitor interface {
|
|||
}
|
||||
|
||||
type Expr interface {
|
||||
accept(v ExprVisitor) any
|
||||
Accept(v ExprVisitor) any
|
||||
}
|
||||
|
||||
type BinaryExpr struct {
|
||||
|
|
@ -25,7 +25,7 @@ type BinaryExpr struct {
|
|||
Right Expr
|
||||
}
|
||||
|
||||
func (b *BinaryExpr) accept(v ExprVisitor) any {
|
||||
func (b *BinaryExpr) Accept(v ExprVisitor) any {
|
||||
return v.VisitBinaryExpr(b)
|
||||
}
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ type GroupingExpr struct {
|
|||
Expr Expr
|
||||
}
|
||||
|
||||
func (g *GroupingExpr) accept(v ExprVisitor) any {
|
||||
func (g *GroupingExpr) Accept(v ExprVisitor) any {
|
||||
return v.VisitGroupingExpr(g)
|
||||
}
|
||||
|
||||
|
|
@ -41,7 +41,7 @@ type LiteralExpr struct {
|
|||
Value any
|
||||
}
|
||||
|
||||
func (l *LiteralExpr) accept(v ExprVisitor) any {
|
||||
func (l *LiteralExpr) Accept(v ExprVisitor) any {
|
||||
return v.VisitLiteralExpr(l)
|
||||
}
|
||||
|
||||
|
|
@ -50,6 +50,6 @@ type UnaryExpr struct {
|
|||
Right Expr
|
||||
}
|
||||
|
||||
func (u *UnaryExpr) accept(v ExprVisitor) any {
|
||||
func (u *UnaryExpr) Accept(v ExprVisitor) any {
|
||||
return v.VisitUnaryExpr(u)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ func (p *Printer) Parenthesize(name string, expressions ...Expr) string {
|
|||
val := "(" + name
|
||||
|
||||
for _, e := range expressions {
|
||||
exprStr, ok := (e.accept(p)).(string)
|
||||
exprStr, ok := (e.Accept(p)).(string)
|
||||
if !ok {
|
||||
panic("badly implemented visitor")
|
||||
}
|
||||
|
|
@ -58,7 +58,7 @@ func (p *Printer) Parenthesize(name string, expressions ...Expr) string {
|
|||
}
|
||||
|
||||
func (p *Printer) Print(e Expr) string {
|
||||
str, ok := (e.accept(p)).(string)
|
||||
str, ok := (e.Accept(p)).(string)
|
||||
if !ok {
|
||||
panic("badly implemented visitor")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue