Add goroutines

This commit is contained in:
Arsen Musayelyan 2021-04-05 11:28:47 -07:00
parent e5b8521ba4
commit 836149ffc2
2 changed files with 31 additions and 19 deletions

46
ast.go
View File

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

View File

@ -22,7 +22,7 @@ var Funcs = FuncMap{
"append": appendArray, "append": appendArray,
"exit": scptExit, "exit": scptExit,
"return": setReturn, "return": setReturn,
"print": scptPrint, "print": scptPrint,
} }
// Default function to convert unnamed argument to a string using fmt.Sprint // 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 // Print message
fmt.Println(val) fmt.Println(val)
return nil, nil return nil, nil
} }