2024-10-05 18:58:49 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
type StmtVisitor interface {
|
2024-10-06 16:30:57 +03:00
|
|
|
visitIfStmt(i *IfStmt)
|
2024-10-05 18:58:49 +03:00
|
|
|
visitVarStmt(v *VarStmt)
|
2024-10-09 23:36:18 +03:00
|
|
|
visitEnvStmt(e *EnvStmt)
|
|
|
|
visitFunStmt(f *FunStmt)
|
2024-10-05 23:27:00 +03:00
|
|
|
visitExprStmt(es *ExprStmt)
|
|
|
|
visitPrintStmt(p *PrintStmt)
|
|
|
|
visitBlockStmt(b *BlockStmt)
|
2024-10-07 21:06:23 +03:00
|
|
|
visitWhileStmt(w *WhileStmt)
|
2024-10-07 21:47:55 +03:00
|
|
|
visitBreakStmt(b *BreakStmt)
|
2024-10-05 18:58:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Stmt interface {
|
|
|
|
stmt()
|
|
|
|
accept(v StmtVisitor)
|
|
|
|
}
|
|
|
|
|
|
|
|
type PrintStmt struct {
|
|
|
|
val Expr
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExprStmt struct {
|
|
|
|
expr Expr
|
|
|
|
}
|
|
|
|
|
|
|
|
type VarStmt struct {
|
|
|
|
name Token
|
|
|
|
initializer Expr
|
|
|
|
}
|
|
|
|
|
2024-10-05 23:27:00 +03:00
|
|
|
type BlockStmt struct {
|
|
|
|
stmts []Stmt
|
|
|
|
}
|
|
|
|
|
2024-10-06 16:30:57 +03:00
|
|
|
type EnvStmt struct{}
|
|
|
|
|
|
|
|
type IfStmt struct {
|
|
|
|
name Token
|
|
|
|
expr Expr
|
|
|
|
then Stmt
|
|
|
|
or Stmt
|
|
|
|
}
|
|
|
|
|
2024-10-07 21:06:23 +03:00
|
|
|
type WhileStmt struct {
|
|
|
|
cond Expr
|
|
|
|
body Stmt
|
|
|
|
}
|
|
|
|
|
2024-10-07 21:47:55 +03:00
|
|
|
type BreakStmt struct{}
|
|
|
|
|
2024-10-09 23:36:18 +03:00
|
|
|
type FunStmt struct {
|
|
|
|
name Token
|
|
|
|
args []Token
|
|
|
|
body *BlockStmt
|
|
|
|
}
|
|
|
|
|
2024-10-06 16:30:57 +03:00
|
|
|
func (i *IfStmt) stmt() {}
|
2024-10-09 23:36:18 +03:00
|
|
|
func (f *FunStmt) stmt() {}
|
2024-10-07 21:06:23 +03:00
|
|
|
func (e *EnvStmt) stmt() {}
|
2024-10-05 18:58:49 +03:00
|
|
|
func (vs *VarStmt) stmt() {}
|
2024-10-05 23:27:00 +03:00
|
|
|
func (es *ExprStmt) stmt() {}
|
|
|
|
func (p *PrintStmt) stmt() {}
|
|
|
|
func (b *BlockStmt) stmt() {}
|
2024-10-07 21:06:23 +03:00
|
|
|
func (w *WhileStmt) stmt() {}
|
2024-10-07 21:47:55 +03:00
|
|
|
func (b *BreakStmt) stmt() {}
|
2024-10-05 18:58:49 +03:00
|
|
|
|
|
|
|
func (p *PrintStmt) accept(v StmtVisitor) {
|
|
|
|
v.visitPrintStmt(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (se *ExprStmt) accept(v StmtVisitor) {
|
|
|
|
v.visitExprStmt(se)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vs *VarStmt) accept(v StmtVisitor) {
|
|
|
|
v.visitVarStmt(vs)
|
|
|
|
}
|
2024-10-05 23:27:00 +03:00
|
|
|
|
|
|
|
func (b *BlockStmt) accept(v StmtVisitor) {
|
|
|
|
v.visitBlockStmt(b)
|
|
|
|
}
|
2024-10-06 16:30:57 +03:00
|
|
|
|
|
|
|
func (i *IfStmt) accept(v StmtVisitor) {
|
|
|
|
v.visitIfStmt(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EnvStmt) accept(v StmtVisitor) {
|
|
|
|
v.visitEnvStmt(e)
|
|
|
|
}
|
2024-10-07 21:06:23 +03:00
|
|
|
|
|
|
|
func (w *WhileStmt) accept(v StmtVisitor) {
|
|
|
|
v.visitWhileStmt(w)
|
|
|
|
}
|
2024-10-07 21:47:55 +03:00
|
|
|
|
|
|
|
func (b *BreakStmt) accept(v StmtVisitor) {
|
|
|
|
v.visitBreakStmt(b)
|
|
|
|
}
|
2024-10-09 23:36:18 +03:00
|
|
|
|
|
|
|
func (f *FunStmt) accept(v StmtVisitor) {
|
|
|
|
v.visitFunStmt(f)
|
|
|
|
}
|