Add goroutines

This commit is contained in:
Elara 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)
}
}
} 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

View File

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