2021-03-01 23:01:21 +00:00
|
|
|
// Package scpt provides an interpreter for my simple applescript-like
|
|
|
|
// scripting language
|
2021-03-01 17:11:22 +00:00
|
|
|
package scpt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"github.com/alecthomas/participle"
|
2021-03-01 21:43:10 +00:00
|
|
|
"github.com/antonmedv/expr"
|
2021-03-01 17:11:22 +00:00
|
|
|
"io"
|
2021-03-01 21:43:10 +00:00
|
|
|
"reflect"
|
2021-03-01 17:11:22 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// AddFuncs adds all functions from the provided FuncMap into
|
|
|
|
// the Funcs variable
|
2021-03-01 17:11:22 +00:00
|
|
|
func AddFuncs(fnMap FuncMap) {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Add each function to Funcs
|
2021-03-01 17:11:22 +00:00
|
|
|
for name, fn := range fnMap {
|
|
|
|
Funcs[name] = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// AddVars adds all functions from the provided map into
|
|
|
|
// the Vars variable
|
2021-03-01 17:11:22 +00:00
|
|
|
func AddVars(varMap map[string]interface{}) {
|
|
|
|
for name, val := range varMap {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Add each variable to Vars
|
2021-03-01 17:11:22 +00:00
|
|
|
Vars[name] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// Parse uses participle to parse a script from r into a new AST
|
2021-03-01 17:11:22 +00:00
|
|
|
func Parse(r io.Reader) (*AST, error) {
|
2021-03-02 03:43:06 +00:00
|
|
|
// Build parser from empty AST struct with custom lexer
|
|
|
|
parser, err := participle.Build(
|
|
|
|
&AST{},
|
|
|
|
participle.Lexer(scptLexer),
|
|
|
|
participle.Elide("Whitespace", "Comment"),
|
2021-03-02 04:18:52 +00:00
|
|
|
)
|
2021-03-01 17:11:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-01 23:01:21 +00:00
|
|
|
// Create new empty AST struct
|
2021-03-01 17:11:22 +00:00
|
|
|
ast := &AST{}
|
2021-03-01 23:01:21 +00:00
|
|
|
// Parse script from provided reader into ast
|
2021-03-01 17:22:00 +00:00
|
|
|
err = parser.Parse(r, ast)
|
2021-03-01 17:11:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-01 23:01:21 +00:00
|
|
|
// Return filled AST struct
|
2021-03-01 17:11:22 +00:00
|
|
|
return ast, nil
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// ParseValue parses a Value struct into a go value
|
|
|
|
func ParseValue(val *Value) (interface{}, error) {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Determine which value was provided and return it
|
2021-03-01 17:11:22 +00:00
|
|
|
if val.String != nil {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Return unquoted string
|
2021-03-01 21:43:10 +00:00
|
|
|
return strings.Trim(*val.String, `"`), nil
|
2021-03-01 17:11:22 +00:00
|
|
|
} else if val.Bool != nil {
|
2021-03-02 00:44:33 +00:00
|
|
|
// Return dereferenced Bool converted to bool
|
|
|
|
return bool(*val.Bool), nil
|
2021-03-02 03:43:06 +00:00
|
|
|
} else if val.Number != nil {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Return dereferenced float
|
2021-03-02 03:43:06 +00:00
|
|
|
return *val.Number, nil
|
2021-03-01 17:11:22 +00:00
|
|
|
} else if val.SubCmd != nil {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Return reference to subcommand
|
2021-03-01 21:43:10 +00:00
|
|
|
return val.SubCmd, nil
|
2021-03-01 17:11:22 +00:00
|
|
|
} else if val.VarVal != nil {
|
2021-03-05 03:30:08 +00:00
|
|
|
// If variable access contains index
|
|
|
|
if val.VarVal.Index != nil {
|
|
|
|
// Get index value
|
|
|
|
index, err := callIfFunc(ParseValue(val.VarVal.Index))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Get requested variable and attempt to assert as []interface{}
|
|
|
|
slc, ok := Vars[*val.VarVal.Name].([]interface{})
|
|
|
|
// If assertion successful
|
|
|
|
if ok {
|
|
|
|
// Attempt to assert index as a 64-bit float
|
|
|
|
indexFlt, ok := index.(float64)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("array index must be a number")
|
|
|
|
}
|
|
|
|
// If requested index is out of range, return error
|
|
|
|
if int64(len(slc)) <= int64(indexFlt) {
|
|
|
|
return nil, fmt.Errorf("index %d is out of range with length %d", *val.VarVal.Index, len(slc))
|
|
|
|
}
|
|
|
|
// Return value at requested index of requested variable
|
|
|
|
return slc[int64(indexFlt)], nil
|
|
|
|
} else {
|
|
|
|
// If assertion unsuccessful, attempt to assert as a map[interface{}]interface{}
|
|
|
|
iMap, ok := Vars[*val.VarVal.Name].(map[interface{}]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("variable " + *val.VarVal.Name + " does not exist or is not a map")
|
|
|
|
}
|
|
|
|
// Attempt to get value at requested key
|
|
|
|
val, ok := iMap[index]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("index %v does not exist in map", index)
|
|
|
|
}
|
|
|
|
// Return value at key with no error
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If index is absent, attempt to get variable value from Vars
|
|
|
|
value, ok := Vars[*val.VarVal.Name]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("variable " + *val.VarVal.Name + " does not exist")
|
|
|
|
}
|
|
|
|
// Return value with no error
|
|
|
|
return value, nil
|
|
|
|
}
|
2021-03-01 21:43:10 +00:00
|
|
|
} else if val.Expr != nil {
|
2021-03-05 03:30:08 +00:00
|
|
|
// If value is an expression, return evaluated expression
|
2021-03-02 08:27:28 +00:00
|
|
|
return evalExpr(*val.Expr)
|
2021-03-05 03:30:08 +00:00
|
|
|
} else if val.Array != nil {
|
|
|
|
// If value is an array, create new nil []interface{}
|
|
|
|
var iSlice []interface{}
|
|
|
|
// For each value in array
|
|
|
|
for _, value := range val.Array {
|
|
|
|
// Recursively parse value
|
2021-03-06 03:38:05 +00:00
|
|
|
iVal, err := callIfFunc(ParseValue(value))
|
2021-03-05 03:30:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Append value to []interface{}
|
|
|
|
iSlice = append(iSlice, iVal)
|
|
|
|
}
|
|
|
|
// Return []interface{]
|
|
|
|
return iSlice, nil
|
|
|
|
} else if val.Map != nil {
|
|
|
|
// If value is a map, create new empty map[interface{}]interface{}
|
|
|
|
iMap := map[interface{}]interface{}{}
|
|
|
|
// For each value in map
|
|
|
|
for _, value := range val.Map {
|
|
|
|
// Recursively parse value
|
|
|
|
iVal, err := ParseValue(value.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Recursively parse key
|
|
|
|
iKey, err := ParseValue(value.Key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Set key of map to value
|
|
|
|
iMap[iKey] = iVal
|
|
|
|
}
|
|
|
|
// Return map[interface{}]interface{}
|
|
|
|
return iMap, nil
|
2021-03-08 19:21:50 +00:00
|
|
|
} else if val.Opposite != nil {
|
|
|
|
value, err := callIfFunc(ParseValue(val.Opposite))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
boolean, ok := value.(bool)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("cannot take opposite of a non-boolean value")
|
|
|
|
}
|
|
|
|
return !boolean, nil
|
2021-03-02 08:27:28 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Evaluate given expression, returning its value and optionally an error
|
|
|
|
func evalExpr(expression Expression) (interface{}, error) {
|
|
|
|
// Parse value of left side of expression
|
|
|
|
left, _ := callIfFunc(ParseValue(expression.Left))
|
|
|
|
// If value is string, requote
|
|
|
|
if isStr(left) {
|
2021-03-05 03:30:08 +00:00
|
|
|
left = quoteStr(left.(string))
|
2021-03-02 08:27:28 +00:00
|
|
|
}
|
|
|
|
// Create new nil string
|
|
|
|
var right string
|
|
|
|
// For every right gsegment
|
|
|
|
for _, segment := range expression.RightSegs {
|
|
|
|
// Parse value of right segment, calling it if it is a function
|
|
|
|
rVal, _ := callIfFunc(ParseValue(segment.Right))
|
2021-03-01 23:01:21 +00:00
|
|
|
// If value is string, requote
|
2021-03-02 08:27:28 +00:00
|
|
|
if isStr(rVal) {
|
2021-03-05 03:30:08 +00:00
|
|
|
rVal = quoteStr(rVal)
|
2021-03-01 21:43:10 +00:00
|
|
|
}
|
2021-03-02 08:27:28 +00:00
|
|
|
// Append right segment to right string
|
|
|
|
right = right + fmt.Sprintf(
|
|
|
|
" %s %v",
|
|
|
|
segment.Op,
|
|
|
|
rVal,
|
2021-03-01 21:43:10 +00:00
|
|
|
)
|
|
|
|
}
|
2021-03-02 08:27:28 +00:00
|
|
|
// Create string expression from segments and operator
|
|
|
|
exp := fmt.Sprintf(
|
|
|
|
"%v %s",
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
)
|
|
|
|
// Compile string expression
|
|
|
|
program, err := expr.Compile(strings.ReplaceAll(exp, "^", "**"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Run expression
|
|
|
|
out, err := expr.Run(program, Vars)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Return expression output value
|
|
|
|
return out, nil
|
2021-03-01 21:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add quotes to an unquoted string
|
2021-03-05 03:30:08 +00:00
|
|
|
func quoteStr(s interface{}) string {
|
|
|
|
// If s is nil
|
|
|
|
if s == nil {
|
|
|
|
// Return empty quotes
|
|
|
|
return `""`
|
|
|
|
} else {
|
|
|
|
// Otherwise return formatted string using %v (any value)
|
|
|
|
return fmt.Sprintf(`"%v"`, s)
|
|
|
|
}
|
2021-03-01 21:43:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if i is a string
|
|
|
|
func isStr(i interface{}) bool {
|
2021-03-05 03:30:08 +00:00
|
|
|
if i == nil {
|
|
|
|
return true
|
|
|
|
}
|
2021-03-01 23:01:21 +00:00
|
|
|
// if type of input is string, return true
|
2021-03-01 21:43:10 +00:00
|
|
|
if reflect.TypeOf(i).String() == "string" {
|
|
|
|
return true
|
2021-03-01 17:11:22 +00:00
|
|
|
}
|
2021-03-01 21:43:10 +00:00
|
|
|
return false
|
2021-03-01 17:11:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-02 00:44:33 +00:00
|
|
|
// Call val if it is a function, otherwise pass through return values
|
|
|
|
func callIfFunc(val interface{}, err error) (interface{}, error) {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// If val is a pointer to a FuncCall
|
|
|
|
if IsFuncCall(val) {
|
|
|
|
// Pass through return values of function call
|
|
|
|
return CallFunction(val.(*FuncCall))
|
|
|
|
}
|
|
|
|
// Return given value
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// UnwrapArgs takes a slice of Arg structs and returns a map
|
|
|
|
// storing the argument name and its value. If the argument has
|
|
|
|
// no name, its key will be an empty string
|
|
|
|
func UnwrapArgs(args []*Arg) (map[string]interface{}, error) {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Create new empty map of strings to any type
|
2021-03-01 17:11:22 +00:00
|
|
|
argMap := map[string]interface{}{}
|
2021-03-01 23:01:21 +00:00
|
|
|
// For each argument
|
2021-03-01 17:11:22 +00:00
|
|
|
for _, arg := range args {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Parse value into interface{}
|
2021-03-01 21:43:10 +00:00
|
|
|
val, err := ParseValue(arg.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-01 23:01:21 +00:00
|
|
|
// If value is function call
|
2021-03-01 21:43:10 +00:00
|
|
|
if IsFuncCall(val) {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Call function, setting its value as the argument's value
|
2021-03-01 21:43:10 +00:00
|
|
|
argMap[arg.Key], err = CallFunction(val.(*FuncCall))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-01 23:01:21 +00:00
|
|
|
// Skip further code and start next loop
|
2021-03-01 21:43:10 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-01 23:01:21 +00:00
|
|
|
// Set argument value to parsed value
|
2021-03-01 21:43:10 +00:00
|
|
|
argMap[arg.Key] = val
|
2021-03-01 17:11:22 +00:00
|
|
|
}
|
2021-03-01 23:01:21 +00:00
|
|
|
// Return map of arguments
|
2021-03-01 21:43:10 +00:00
|
|
|
return argMap, nil
|
2021-03-01 17:11:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// IsFuncCall checks if val is a FuncCall struct
|
|
|
|
func IsFuncCall(val interface{}) bool {
|
2021-03-02 00:44:33 +00:00
|
|
|
// If type of val is a pointer to FuncCall, return true
|
2021-03-08 01:25:16 +00:00
|
|
|
return reflect.TypeOf(val) == reflect.TypeOf(&FuncCall{})
|
2021-03-01 17:11:22 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 21:43:10 +00:00
|
|
|
// CallFunction executes a given function call in the form of
|
|
|
|
// a FuncCall struct
|
|
|
|
func CallFunction(call *FuncCall) (interface{}, error) {
|
2021-03-01 23:01:21 +00:00
|
|
|
// Unwrap provided arguments
|
2021-03-01 21:43:10 +00:00
|
|
|
argMap, err := UnwrapArgs(call.Args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-03-01 17:11:22 +00:00
|
|
|
}
|
2021-03-01 23:01:21 +00:00
|
|
|
// Attempt to get function from Funcs map
|
2021-03-01 21:43:10 +00:00
|
|
|
fn, ok := Funcs[call.Name]
|
2021-03-01 17:11:22 +00:00
|
|
|
if !ok {
|
2021-03-01 21:43:10 +00:00
|
|
|
return nil, errors.New("no such function: " + call.Name)
|
2021-03-01 17:11:22 +00:00
|
|
|
}
|
2021-03-01 23:01:21 +00:00
|
|
|
// Return value received from function
|
2021-03-01 21:43:10 +00:00
|
|
|
return fn(argMap)
|
2021-03-01 17:11:22 +00:00
|
|
|
}
|