wrote dsl for ast boiler plate generation
This commit is contained in:
parent
b244f7e3b2
commit
e0dd8ff9d5
16 changed files with 915 additions and 60 deletions
54
ast/ast.go
54
ast/ast.go
|
|
@ -1,55 +1,3 @@
|
|||
package ast
|
||||
|
||||
import "git.red-panda.pet/pandaware/lox-go/lexer"
|
||||
|
||||
// todo: find something better than any here
|
||||
// we can't use generics on either the visitor itself or
|
||||
// each individual method because the adding it to the
|
||||
// visitor itself infects every expr you use with it
|
||||
// and methods cannot have generic parameters
|
||||
|
||||
type ExprVisitor interface {
|
||||
VisitBinaryExpr(b *BinaryExpr) any
|
||||
VisitGroupingExpr(g *GroupingExpr) any
|
||||
VisitLiteralExpr(g *LiteralExpr) any
|
||||
VisitUnaryExpr(g *UnaryExpr) any
|
||||
}
|
||||
|
||||
type Expr interface {
|
||||
Accept(v ExprVisitor) any
|
||||
}
|
||||
|
||||
type BinaryExpr struct {
|
||||
Left Expr
|
||||
Operator *lexer.Token
|
||||
Right Expr
|
||||
}
|
||||
|
||||
func (b *BinaryExpr) Accept(v ExprVisitor) any {
|
||||
return v.VisitBinaryExpr(b)
|
||||
}
|
||||
|
||||
type GroupingExpr struct {
|
||||
Expr Expr
|
||||
}
|
||||
|
||||
func (g *GroupingExpr) Accept(v ExprVisitor) any {
|
||||
return v.VisitGroupingExpr(g)
|
||||
}
|
||||
|
||||
type LiteralExpr struct {
|
||||
Value any
|
||||
}
|
||||
|
||||
func (l *LiteralExpr) Accept(v ExprVisitor) any {
|
||||
return v.VisitLiteralExpr(l)
|
||||
}
|
||||
|
||||
type UnaryExpr struct {
|
||||
Operator *lexer.Token
|
||||
Right Expr
|
||||
}
|
||||
|
||||
func (u *UnaryExpr) Accept(v ExprVisitor) any {
|
||||
return v.VisitUnaryExpr(u)
|
||||
}
|
||||
//go:generate go run ./gen -f expr.ast
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue