Add command

This commit is contained in:
Elara 2021-03-01 09:22:00 -08:00
parent 62571a8ae4
commit cffba2f085
3 changed files with 26 additions and 23 deletions

1
.gitignore vendored
View File

@ -1,3 +1,2 @@
scpt scpt
cmd/scpt/scpt
.idea/ .idea/

25
ast.go
View File

@ -1,9 +1,34 @@
package scpt package scpt
import (
"reflect"
"strings"
)
type AST struct { type AST struct {
Commands []*Command `@@*` 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 { type Command struct {
Vars []*Var `( @@` Vars []*Var `( @@`
Calls []*FuncCall `| @@ )` Calls []*FuncCall `| @@ )`

23
scpt.go
View File

@ -8,7 +8,6 @@ import (
"io" "io"
"os" "os"
"os/exec" "os/exec"
"reflect"
"strings" "strings"
) )
@ -39,33 +38,13 @@ func Parse(r io.Reader) (*AST, error) {
return nil, err return nil, err
} }
ast := &AST{} ast := &AST{}
err = parser.Parse("", r, ast) err = parser.Parse(r, ast)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return ast, nil return ast, nil
} }
func Execute(ast AST) {
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)
}
}
}
}
func ParseValue(val *Value) interface{} { func ParseValue(val *Value) interface{} {
if val.String != nil { if val.String != nil {
return strings.Trim(*val.String, `"`) return strings.Trim(*val.String, `"`)