scpt/ast.go
2021-03-01 09:22:00 -08:00

67 lines
1.1 KiB
Go

package scpt
import (
"reflect"
"strings"
)
type AST struct {
Commands []*Command `@@*`
}
func (ast *AST) Execute() {
for _, cmd := range ast.Commands {
if cmd.Vars != nil {
for _, Var := range cmd.Vars {
val := ParseValue(Var.Value)
if strings.Contains(reflect.TypeOf(val).String(), ".FuncCall") {
Call := val.(*FuncCall)
Vars[Var.Key], _ = CallFunction(Call)
} else {
Vars[Var.Key] = val
}
}
} else if cmd.Calls != nil {
for _, Call := range cmd.Calls {
_, _ = CallFunction(Call)
}
}
}
}
type Command struct {
Vars []*Var `( @@`
Calls []*FuncCall `| @@ )`
}
type FuncCall struct {
Name string `@Ident`
Args []*Arg `@@*`
}
type Arg struct {
Key string `"with" @Ident`
Value *Value `@@`
}
type Var struct {
Key string `"set" @Ident "to"`
Value *Value `@@`
}
type Value struct {
String *string ` @String`
Float *float64 `| @Float`
Integer *int64 `| @Int`
Bool *Bool `| @("true" | "false")`
SubCmd *FuncCall `| "(" @@ ")"`
VarVal *string `| "$" @Ident`
}
type Bool bool
func (b *Bool) Capture(values []string) error {
*b = values[0] == "true"
return nil
}