代码拉取完成,页面将自动刷新
package main
import (
"fmt"
"reflect"
"runtime"
)
type Interpreter struct {
// 全局变量表
global Table
// 当前作用域变量表
local *Table
// 函数调用返回值保存栈
returnStack []interface{}
}
func _Interpreter() *Interpreter {
global := Table{nil, map[string]interface{}{}}
return &Interpreter{
global: global,
local: &global,
returnStack: []interface{}{},
}
}
// 解释器执行所有语句
func (interpreter *Interpreter) interpret(stmts []Stmt) {
for _, stmt := range stmts {
stmt.exec(interpreter)
}
}
// 进入或退出作用域
func (interpreter *Interpreter) enterScope(target *Table) {
interpreter.local = target
}
// 检查所有操作数的类型是否正确
func checkOperands(kind reflect.Kind, operator Token, operands ...interface{}) {
for _, operand := range operands {
if reflect.TypeOf(operand).Kind() != kind {
exitWithErr(operator.line, "Operator '"+operator.lexeme+"' expect right operands.")
}
}
}
// 真值判断
func isTrue(obj interface{}) bool {
if obj == nil || obj == false {
return false
}
return true
}
// 获得任意类型对应的字符串表示
func toString(obj interface{}) string {
if obj == nil {
return "nil"
}
if reflect.TypeOf(obj).Kind() == reflect.TypeOf(Function{}).Kind() {
return "<fun $" + obj.(Function).declaration.name.lexeme + ">"
}
return fmt.Sprint(obj)
}
func exitWithErr(line int, message string) {
out("[line %d] %s\n", line, message)
runtime.Goexit()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。