Exemplo n.º 1
0
// Parse and evaluate a given script for the scope.
// Returns a set of default vars.
// If a set of predefined vars is provided, they may effect the default var values.
func Evaluate(script string, scope *stateful.Scope, predefinedVars map[string]Var, ignoreMissingVars bool) (_ map[string]Var, err error) {
	defer func(errP *error) {
		r := recover()
		if r == ErrEmptyStack {
			trace := make([]byte, 1024)
			n := runtime.Stack(trace, false)
			*errP = fmt.Errorf("evaluation caused stack error: %v Go Trace: %s", r, string(trace[:n]))
		} else if r != nil {
			panic(r)
		}

	}(&err)

	root, err := ast.Parse(script)
	if err != nil {
		return nil, err
	}

	// Use a stack machine to evaluate the AST
	stck := &stack{}
	// Collect any defined defaultVars
	defaultVars := make(map[string]Var)
	err = eval(root, scope, stck, predefinedVars, defaultVars, ignoreMissingVars)
	if err != nil {
		return nil, err
	}
	return defaultVars, nil
}
Exemplo n.º 2
0
// Formats a TICKscript according to the standard.
func Format(script string) (string, error) {
	root, err := ast.Parse(script)
	if err != nil {
		return "", err
	}
	var buf bytes.Buffer
	buf.Grow(len(script))
	root.Format(&buf, "", false)
	return buf.String(), nil
}