start adding maps

This commit is contained in:
2023-02-26 01:33:02 +00:00
parent 6ef6e051e6
commit f0620354c0
13 changed files with 117 additions and 11 deletions

68
src/map.go Normal file
View File

@@ -0,0 +1,68 @@
package main
import (
"fmt"
"strings"
)
type ArMap = map[any]any
var mapGetCompile = makeRegex(".+\\.([a-zA-Z_])([a-zA-Z0-9_])*( *)")
type ArMapGet struct {
VAL any
key any
line int
code string
path string
}
func mapGet(r ArMapGet, stack stack) (any, ArErr) {
resp, err := runVal(r.VAL, stack)
if err.EXISTS {
return nil, err
}
key, err := runVal(r.key, stack)
if err.EXISTS {
return nil, err
}
switch m := resp.(type) {
case ArMap:
if _, ok := m[key]; !ok {
return nil, ArErr{
"KeyError",
"key '" + fmt.Sprint(key) + "' not found",
r.line,
r.path,
r.code,
true,
}
}
return m[key], ArErr{}
}
return nil, ArErr{
"TypeError",
"cannot read " + anyToArgon(key, true) + " from type '" + typeof(resp) + "'",
r.line,
r.path,
r.code,
true,
}
}
func isMapGet(code UNPARSEcode) bool {
return mapGetCompile.MatchString(code.code)
}
func mapGetParse(code UNPARSEcode, index int, codelines []UNPARSEcode) (ArMapGet, bool, ArErr, int) {
trim := strings.TrimSpace(code.code)
split := strings.Split(trim, ".")
start := strings.Join(split[:len(split)-1], ".")
key := split[len(split)-1]
resp, worked, err, i := translateVal(UNPARSEcode{code: start, realcode: code.realcode, line: code.line, path: code.path}, index, codelines, false)
if !worked {
return ArMapGet{}, false, err, i
}
k := translateString{key, code.realcode, code.line}
return ArMapGet{resp, k, code.line, code.realcode, code.path}, true, ArErr{}, 1
}