Browse Source

Add command

master
Arsen Musayelyan 1 year ago
parent
commit
cffba2f085
  1. 1
      .gitignore
  2. 25
      ast.go
  3. 23
      scpt.go

1
.gitignore

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

25
ast.go

@ -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

@ -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, `"`)

Loading…
Cancel
Save