Browse Source

Add goroutines

master
Arsen Musayelyan 7 months ago
parent
commit
836149ffc2
  1. 46
      ast.go
  2. 4
      defaults.go

46
ast.go

@ -268,6 +268,12 @@ func executeCmd(cmd *Command) error {
return fmt.Errorf("%s: %s", Call.Pos, err)
}
}
} else if cmd.Goroutines != nil {
// For each function call
for _, goroutine := range cmd.Goroutines {
// Attempt to call function
go CallFunction(goroutine.Call)
}
} else if cmd.Ifs != nil {
// For each if statement
for _, If := range cmd.Ifs {
@ -310,27 +316,28 @@ func executeCmd(cmd *Command) error {
// Command stores any commands encountered while parsing a script
type Command struct {
Pos lexer.Position
Vars []*Var `( @@`
Ifs []*If `| @@`
RptLoops []*RptLoop `| @@`
WhlLoops []*WhlLoop `| @@`
Defs []*FuncDef `| @@`
Calls []*FuncCall `| @@)`
Pos lexer.Position
Vars []*Var `( @@`
Ifs []*If `| @@`
RptLoops []*RptLoop `| @@`
WhlLoops []*WhlLoop `| @@`
Defs []*FuncDef `| @@`
Goroutines []*Goroutine `| @@`
Calls []*FuncCall `| @@ )`
}
// Value stores any literal values encountered while parsing a script
type Value struct {
Pos lexer.Position
String *string ` @String`
Number *float64 `| @Number`
Bool *Bool `| @("true" | "false")`
SubCmd *FuncCall `| "(" @@ ")"`
VarVal *VarVal `| @@`
Expr *Expression `| "{" @@ "}"`
Map []*MapKVPair `| "[" (@@ ("," @@)* )? "]"`
Array []*Value `| "[" (@@ ("," @@)* )? "]"`
Opposite *Value `| "!" @@`
Pos lexer.Position
String *string ` @String`
Number *float64 `| @Number`
Bool *Bool `| @("true" | "false")`
SubCmd *FuncCall `| "(" @@ ")"`
VarVal *VarVal `| @@`
Expr *Expression `| "{" @@ "}"`
Map []*MapKVPair `| "[" (@@ ("," @@)* )? "]"`
Array []*Value `| "[" (@@ ("," @@)* )? "]"`
Opposite *Value `| "!" @@`
}
// Bool stores boolean values encountered while parsing a script.
@ -352,6 +359,11 @@ type FuncCall struct {
Args []*Arg `@@*`
}
type Goroutine struct {
Pos lexer.Position
Call *FuncCall `"go" @@`
}
// Arg stores arguments for function calls
type Arg struct {
Pos lexer.Position

4
defaults.go

@ -22,7 +22,7 @@ var Funcs = FuncMap{
"append": appendArray,
"exit": scptExit,
"return": setReturn,
"print": scptPrint,
"print": scptPrint,
}
// Default function to convert unnamed argument to a string using fmt.Sprint
@ -116,4 +116,4 @@ func scptPrint(args map[string]interface{}) (interface{}, error) {
// Print message
fmt.Println(val)
return nil, nil
}
}

Loading…
Cancel
Save