scpt/ast.go

67 lines
1.1 KiB
Go
Raw Normal View History

2021-03-01 17:11:22 +00:00
package scpt
2021-03-01 17:22:00 +00:00
import (
"reflect"
"strings"
)
2021-03-01 17:11:22 +00:00
type AST struct {
Commands []*Command `@@*`
}
2021-03-01 17:22:00 +00:00
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)
}
}
}
}
2021-03-01 17:11:22 +00:00
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
}