mirror of
https://github.com/Open-Argon/argon-v3.git
synced 2025-12-06 00:46:07 +00:00
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
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
|
|
}
|