add __dir__ function for maps

This commit is contained in:
2023-06-19 12:32:51 +01:00
parent dc2e62bbdb
commit 3f048ce645
4 changed files with 36 additions and 5 deletions

View File

@@ -163,6 +163,26 @@ func makeGlobal() ArObject {
for key := range x.obj { for key := range x.obj {
newarray = append(newarray, key) newarray = append(newarray, key)
} }
if callable, ok := x.obj["__dir__"]; ok {
resp, err := runCall(
call{
callable: callable,
args: []any{},
},
stack{newscope()},
0,
)
if err.EXISTS {
return nil, err
}
resp = ArValidToAny(resp)
switch x := resp.(type) {
case []any:
newarray = append(newarray, x...)
default:
return nil, ArErr{TYPE: "TypeError", message: "__dir__ returned type '" + typeof(x) + "'", EXISTS: true}
}
}
return ArArray(newarray), ArErr{} return ArArray(newarray), ArErr{}
} }
return ArArray([]any{}), ArErr{} return ArArray([]any{}), ArErr{}

View File

@@ -6,7 +6,7 @@ import (
"sync" "sync"
) )
var mapCompiled = makeRegex(`( *)\{(((( *).+( *):( *).+( *))|(` + spacelessVariable + `))(( *)\,(( *).+( *):( *).+( *))|(` + spacelessVariable + `)))*\}( *)`) var mapCompiled = makeRegex(`( )*<( |\n)*(((.|\n)+)(,(.|\n)+)*)?( |\n)*>( )*`)
type createMap struct { type createMap struct {
body anymap body anymap
@@ -19,11 +19,11 @@ func isMap(code UNPARSEcode) bool {
return mapCompiled.MatchString(code.code) return mapCompiled.MatchString(code.code)
} }
func parseMap(code UNPARSEcode) (any, UNPARSEcode) { func parseMap(code UNPARSEcode, index int, codelines []UNPARSEcode) (any, bool, ArErr, int) {
trimmed := strings.Trim(code.code, " ") trimmed := strings.Trim(code.code, " ")
trimmed = trimmed[1 : len(trimmed)-1] trimmed = trimmed[1 : len(trimmed)-1]
debugPrintln(trimmed) debugPrintln(trimmed)
return nil, UNPARSEcode{} return Map(anymap{}), true, ArErr{}, 1
} }
func Map(m anymap) ArObject { func Map(m anymap) ArObject {
@@ -211,5 +211,14 @@ func Map(m anymap) ArObject {
return true, ArErr{} return true, ArErr{}
}, },
} }
obj.obj["__dir__"] = builtinFunc{
"__dir__",
func(args ...any) (any, ArErr) {
x := []any{}
for k := range m {
x = append(x, k)
}
return x, ArErr{}
}}
return obj return obj
} }

View File

@@ -76,7 +76,7 @@ func anyToArgon(x any, quote bool, simplify bool, depth int, indent int, colored
} }
case anymap: case anymap:
if len(x) == 0 { if len(x) == 0 {
return "{}" return "<>"
} }
keys := make([]any, len(x)) keys := make([]any, len(x))
sort.Slice(keys, func(i, j int) bool { sort.Slice(keys, func(i, j int) bool {
@@ -107,7 +107,7 @@ func anyToArgon(x any, quote bool, simplify bool, depth int, indent int, colored
} }
output = append(output, keyval+": "+anyToArgon(x[key], true, true, depth-1, indent+1, colored, plain)) output = append(output, keyval+": "+anyToArgon(x[key], true, true, depth-1, indent+1, colored, plain))
} }
return "{" + maybenewline + (strings.Repeat(" ", (indent+1)*plain)) + strings.Join(output, ","+maybenewline+(strings.Repeat(" ", (indent+1)*plain))) + maybenewline + (strings.Repeat(" ", indent*plain)) + "}" return "<" + maybenewline + (strings.Repeat(" ", (indent+1)*plain)) + strings.Join(output, ","+maybenewline+(strings.Repeat(" ", (indent+1)*plain))) + maybenewline + (strings.Repeat(" ", indent*plain)) + ">"
case []any: case []any:
singleline := len(x) <= 3 singleline := len(x) <= 3
output := []string{} output := []string{}

View File

@@ -103,6 +103,8 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i
if worked { if worked {
return resp, worked, err, i return resp, worked, err, i
} }
} else if isMap(code) {
resp, worked, err, i = parseMap(code, index, codelines)
} }
{ {
operation, worked, err, step := parseOperations(code, index, codelines) operation, worked, err, step := parseOperations(code, index, codelines)