2024-10-05 18:58:49 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
type ExprVisitor interface {
|
2024-10-09 19:57:52 +03:00
|
|
|
visitCall(c *Call) any
|
2024-10-05 18:58:49 +03:00
|
|
|
visitUnary(u *Unary) any
|
2024-10-12 14:48:27 +03:00
|
|
|
visitLambda(l *Lambda) any
|
2024-10-05 18:58:49 +03:00
|
|
|
visitBinary(b *Binary) any
|
|
|
|
visitLiteral(l *Literal) any
|
|
|
|
visitGrouping(g *Grouping) any
|
|
|
|
visitVariable(v *Variable) any
|
2024-10-06 17:15:50 +03:00
|
|
|
visitLogical(l *Logical) any
|
2024-10-05 18:58:49 +03:00
|
|
|
visitAssignment(a *Assign) any
|
|
|
|
}
|
|
|
|
|
|
|
|
type Expr interface {
|
|
|
|
expr()
|
|
|
|
accept(v ExprVisitor) any
|
|
|
|
}
|
|
|
|
|
|
|
|
type Unary struct {
|
|
|
|
op Token
|
|
|
|
right Expr
|
|
|
|
}
|
|
|
|
|
|
|
|
type Binary struct {
|
|
|
|
left Expr
|
|
|
|
op Token
|
|
|
|
right Expr
|
|
|
|
}
|
|
|
|
|
|
|
|
type Literal struct {
|
|
|
|
value any
|
|
|
|
}
|
|
|
|
|
|
|
|
type Grouping struct {
|
|
|
|
expression Expr
|
|
|
|
}
|
|
|
|
|
|
|
|
type Variable struct {
|
|
|
|
name Token
|
|
|
|
}
|
|
|
|
|
|
|
|
type Assign struct {
|
|
|
|
variable Token
|
|
|
|
value Expr
|
|
|
|
}
|
|
|
|
|
2024-10-06 17:15:50 +03:00
|
|
|
type Logical struct {
|
|
|
|
left Expr
|
|
|
|
operator Token
|
|
|
|
right Expr
|
2024-10-06 16:30:57 +03:00
|
|
|
}
|
|
|
|
|
2024-10-09 19:57:52 +03:00
|
|
|
type Call struct {
|
2024-10-11 17:01:12 +03:00
|
|
|
callee Expr
|
|
|
|
paren Token
|
|
|
|
args []Expr
|
2024-10-09 19:57:52 +03:00
|
|
|
}
|
|
|
|
|
2024-10-12 14:48:27 +03:00
|
|
|
type Lambda struct {
|
|
|
|
name Token
|
|
|
|
args []Token
|
2024-10-14 22:53:26 +03:00
|
|
|
body []Stmt
|
2024-10-12 14:48:27 +03:00
|
|
|
}
|
|
|
|
|
2024-10-09 19:57:52 +03:00
|
|
|
func (c *Call) expr() {}
|
2024-10-06 17:15:50 +03:00
|
|
|
func (u *Unary) expr() {}
|
|
|
|
func (a *Assign) expr() {}
|
|
|
|
func (b *Binary) expr() {}
|
2024-10-12 14:48:27 +03:00
|
|
|
func (l *Lambda) expr() {}
|
2024-10-06 17:15:50 +03:00
|
|
|
func (l *Literal) expr() {}
|
|
|
|
func (g *Grouping) expr() {}
|
|
|
|
func (v *Variable) expr() {}
|
|
|
|
func (l *Logical) expr() {}
|
2024-10-05 18:58:49 +03:00
|
|
|
|
|
|
|
func (u *Unary) accept(v ExprVisitor) any {
|
|
|
|
return v.visitUnary(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Binary) accept(v ExprVisitor) any {
|
|
|
|
return v.visitBinary(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Literal) accept(v ExprVisitor) any {
|
|
|
|
return v.visitLiteral(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *Grouping) accept(v ExprVisitor) any {
|
|
|
|
return v.visitGrouping(g)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (va *Variable) accept(v ExprVisitor) any {
|
|
|
|
return v.visitVariable(va)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Assign) accept(v ExprVisitor) any {
|
|
|
|
return v.visitAssignment(a)
|
|
|
|
}
|
2024-10-06 16:30:57 +03:00
|
|
|
|
2024-10-06 17:15:50 +03:00
|
|
|
func (l *Logical) accept(v ExprVisitor) any {
|
|
|
|
return v.visitLogical(l)
|
2024-10-06 16:30:57 +03:00
|
|
|
}
|
2024-10-09 19:57:52 +03:00
|
|
|
|
|
|
|
func (c *Call) accept(v ExprVisitor) any {
|
|
|
|
return v.visitCall(c)
|
|
|
|
}
|
2024-10-12 14:48:27 +03:00
|
|
|
|
|
|
|
func (l *Lambda) accept(v ExprVisitor) any {
|
|
|
|
return v.visitLambda(l)
|
|
|
|
}
|