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
cmd/scpt/scpt
.idea/

25
ast.go
View File

@ -1,9 +1,34 @@
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 `| @@ )`

23
scpt.go
View File

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