示例#1
0
文件: main.go 项目: kyeongdong/3
func main() {
	log.SetFlags(0)
	flag.Parse()
	world = script.NewWorld()
	world.Func("exit", exit)
	script.Debug = *debug

	if flag.NArg() > 1 {
		check(fmt.Errorf("need 0 or 1 input files"))
	}

	if flag.NArg() == 1 {
		src, err := os.Open(flag.Arg(0))
		check(err)
		ps1 = ">"
		interpret(src)
	} else {
		ps1 = ""
		interpret(os.Stdin)
	}
}
示例#2
0
文件: script.go 项目: jsampaio/3
func Eval1Line(code string) interface{} {
	tree, err := World.Compile(code)
	if err != nil {
		LogErr(err.Error())
		return nil
	}
	if len(tree.Children) != 1 {
		LogErr("expected single statement:" + code)
		return nil
	}
	return tree.Children[0].Eval()
}

// holds the script state (variables etc)
var World = script.NewWorld()

// Add a function to the script world
func DeclFunc(name string, f interface{}, doc string) {
	World.Func(name, f, doc)
}

// Add a constant to the script world
func DeclConst(name string, value float64, doc string) {
	World.Const(name, value, doc)
}

// Add a read-only variable to the script world.
// It can be changed, but not by the user.
func DeclROnly(name string, value interface{}, doc string) {
	World.ROnly(name, value, doc)