Move default maps/variables to default.go and add print default function

This commit is contained in:
Elara 2021-03-07 19:01:43 -08:00
parent 1c99345af1
commit 36d1eee759
2 changed files with 30 additions and 17 deletions

View File

@ -21,6 +21,24 @@ import (
"strconv"
)
// Vars stores any variables set during script runtime
var Vars = map[string]interface{}{}
// FuncMap is a map of strings mapped to suitable script functions
type FuncMap map[string]func(map[string]interface{}) (interface{}, error)
// Funcs stores the functions allowed for use in a script
var Funcs = FuncMap{
"str": toString,
"num": parseNumber,
"bool": parseBool,
"break": setBreakLoop,
"append": appendArray,
"exit": scptExit,
"return": setReturn,
"print": scptPrint,
}
// Default function to convert unnamed argument to a string using fmt.Sprint
func toString(args map[string]interface{}) (interface{}, error) {
val, ok := args[""]
@ -101,3 +119,15 @@ func appendArray(args map[string]interface{}) (interface{}, error) {
// Return appended unnamed argument
return val, nil
}
// Print message via fmt.Println
func scptPrint(args map[string]interface{}) (interface{}, error) {
// Get message
val, ok := args[""]
if !ok {
return nil, errors.New("print requires an unnamed argument")
}
// Print message
fmt.Println(val)
return nil, nil
}

17
scpt.go
View File

@ -26,23 +26,6 @@ import (
"strings"
)
// Vars stores any variables set during script runtime
var Vars = map[string]interface{}{}
// FuncMap is a map of strings mapped to suitable script functions
type FuncMap map[string]func(map[string]interface{}) (interface{}, error)
// Funcs stores the functions allowed for use in a script
var Funcs = FuncMap{
"str": toString,
"num": parseNumber,
"bool": parseBool,
"break": setBreakLoop,
"append": appendArray,
"exit": scptExit,
"return": setReturn,
}
// AddFuncs adds all functions from the provided FuncMap into
// the Funcs variable
func AddFuncs(fnMap FuncMap) {