加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
stmt.go 1.82 KB
一键复制 编辑 原始数据 按行查看 历史
package main
type (
Stmt interface {
exec(interpreter *Interpreter)
}
exprStmt struct {
expr Expr
}
printStmt struct {
expr Expr
}
varStmt struct {
name Token
initializer Expr
}
blockStmt struct {
stmts []Stmt
}
ifStmt struct {
condition Expr
thenBranch Stmt
elseBranch Stmt
}
whileStmt struct {
condition Expr
body Stmt
}
functionStmt struct {
name Token
params []Token
stmts []Stmt
}
returnStmt struct {
keyword Token
value Expr
}
)
func (e exprStmt) exec(interpreter *Interpreter) {
e.expr.eval(interpreter)
}
func (p printStmt) exec(interpreter *Interpreter) {
value := p.expr.eval(interpreter)
out(toString(value) + "\n")
}
func (v varStmt) exec(interpreter *Interpreter) {
var value interface{}
if v.initializer != nil {
value = v.initializer.eval(interpreter)
}
interpreter.local.define(v.name.lexeme, value)
}
func (b blockStmt) exec(interpreter *Interpreter) {
father := interpreter.local
child := &Table{
father: father,
values: map[string]interface{}{},
}
interpreter.enterScope(child)
defer interpreter.enterScope(father)
for _, stmt := range b.stmts {
stmt.exec(interpreter)
}
}
func (i ifStmt) exec(interpreter *Interpreter) {
if isTrue(i.condition.eval(interpreter)) {
i.thenBranch.exec(interpreter)
} else {
if i.elseBranch != nil {
i.elseBranch.exec(interpreter)
}
}
}
func (w whileStmt) exec(interpreter *Interpreter) {
for isTrue(w.condition.eval(interpreter)) {
w.body.exec(interpreter)
}
}
func (f functionStmt) exec(interpreter *Interpreter) {
fun := Function{f}
interpreter.local.define(f.name.lexeme, fun)
}
func (r returnStmt) exec(interpreter *Interpreter) {
var result interface{}
if r.value != nil {
result = r.value.eval(interpreter)
}
interpreter.returnStack = append(interpreter.returnStack, result)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化