mattn / anko

Scriptable interpreter written in golang

Home Page:http://play-anko.appspot.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Execute function instead of whole script

vulpivia opened this issue · comments

Is it possible to execute a specific function in a script instead of the entire script? Something like vm.ExecuteFunction with the name of the function as a parameter would be nice.

I think there are other ways to solve this issue. For example, first run in the script with all the functions, then can run a script with just the wanted function. Could also look into using modules.

I'm not sure I understand. I can run a whole script, but how would I then run the wanted function only?

Modules look basically like classes to me. Unless I can interact with them from outside Anko, I'm not sure that modules would help me.

Split the script into two part. First run in the part with all the functions. Then run a script with just the function you want to call.

Are the variables shared between the two parts?

Basically I have a function (or in this case script) that gets called in regular intervals and with each execution of the script, all variables are reset. My thought was that if I just call a function and not the whole script, I can have variables outside of that function that don't reset between calls.

Any script can be run in any env, the env is where the script runs. Can keep running scripts as long as you want in the same env.

package main

import (
	"fmt"
	"log"

	"github.com/mattn/anko/env"
	"github.com/mattn/anko/vm"
)

func main() {
	e := env.NewEnv()

	err := e.Define("println", fmt.Println)
	if err != nil {
		log.Fatalf("Define error: %v\n", err)
	}

	script := `
a = 1
	`

	_, err = vm.Execute(e, nil, script)
	if err != nil {
		log.Fatalf("Execute error: %v\n", err)
	}

	script = `
a = a + 1
println(a)
`

	_, err = vm.Execute(e, nil, script)
	if err != nil {
		log.Fatalf("Execute error: %v\n", err)
	}

	_, err = vm.Execute(e, nil, script)
	if err != nil {
		log.Fatalf("Execute error: %v\n", err)
	}

	_, err = vm.Execute(e, nil, script)
	if err != nil {
		log.Fatalf("Execute error: %v\n", err)
	}

}

Output:

2
3
4

Okay, that solves this issue. Thanks!