代码拉取完成,页面将自动刷新
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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。