From 836149ffc24fc6424d5902d0e1f025445fb80654 Mon Sep 17 00:00:00 2001 From: Arsen Musayelyan Date: Mon, 5 Apr 2021 11:28:47 -0700 Subject: [PATCH] Add goroutines --- ast.go | 46 +++++++++++++++++++++++++++++----------------- defaults.go | 4 ++-- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/ast.go b/ast.go index 1c17552..03d9a74 100644 --- a/ast.go +++ b/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 diff --git a/defaults.go b/defaults.go index 4df6249..fcdd832 100644 --- a/defaults.go +++ b/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 -} \ No newline at end of file +}