glox/ast.go

54 lines
798 B
Go
Raw Normal View History

2024-10-03 13:07:17 +03:00
package main
2024-10-03 22:12:40 +03:00
type Visitor interface {
2024-10-04 15:24:01 +03:00
visitUnary(u *Unary) any
visitBinary(b *Binary) any
visitLiteral(l *Literal) any
visitGrouping(g *Grouping) any
2024-10-03 22:12:40 +03:00
}
2024-10-03 13:07:17 +03:00
type Expr interface {
expr()
2024-10-04 15:24:01 +03:00
accept(v Visitor) any
2024-10-03 13:07:17 +03:00
}
2024-10-04 15:24:01 +03:00
type Unary struct {
2024-10-03 13:07:17 +03:00
op Token
right Expr
}
2024-10-04 15:24:01 +03:00
type Binary struct {
left Expr
2024-10-03 22:12:40 +03:00
op Token
right Expr
2024-10-03 13:07:17 +03:00
}
2024-10-03 22:12:40 +03:00
type Literal struct {
value any
}
2024-10-04 15:24:01 +03:00
type Grouping struct {
expression Expr
2024-10-03 22:12:40 +03:00
}
2024-10-04 15:24:01 +03:00
func (u *Unary) expr() {}
func (b *Binary) expr() {}
func (l *Literal) expr() {}
func (g *Grouping) expr() {}
2024-10-03 22:12:40 +03:00
2024-10-04 15:24:01 +03:00
func (u *Unary) accept(v Visitor) any {
return v.visitUnary(u)
2024-10-03 22:12:40 +03:00
}
2024-10-04 15:24:01 +03:00
func (b *Binary) accept(v Visitor) any {
return v.visitBinary(b)
2024-10-03 22:12:40 +03:00
}
2024-10-04 15:24:01 +03:00
func (l *Literal) accept(v Visitor) any {
return v.visitLiteral(l)
2024-10-03 22:12:40 +03:00
}
2024-10-04 15:24:01 +03:00
func (g *Grouping) accept(v Visitor) any {
return v.visitGrouping(g)
2024-10-03 22:12:40 +03:00
}