加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
interpreter.go 1.46 KB
一键复制 编辑 原始数据 按行查看 历史
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()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化