diff --git a/src/built-ins.go b/src/built-ins.go index 81baebc..6cf3d52 100644 --- a/src/built-ins.go +++ b/src/built-ins.go @@ -163,6 +163,26 @@ func makeGlobal() ArObject { for key := range x.obj { 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([]any{}), ArErr{} diff --git a/src/map.go b/src/map.go index be38b83..7fc754f 100644 --- a/src/map.go +++ b/src/map.go @@ -6,7 +6,7 @@ import ( "sync" ) -var mapCompiled = makeRegex(`( *)\{(((( *).+( *):( *).+( *))|(` + spacelessVariable + `))(( *)\,(( *).+( *):( *).+( *))|(` + spacelessVariable + `)))*\}( *)`) +var mapCompiled = makeRegex(`( )*<( |\n)*(((.|\n)+)(,(.|\n)+)*)?( |\n)*>( )*`) type createMap struct { body anymap @@ -19,11 +19,11 @@ func isMap(code UNPARSEcode) bool { 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 = trimmed[1 : len(trimmed)-1] debugPrintln(trimmed) - return nil, UNPARSEcode{} + return Map(anymap{}), true, ArErr{}, 1 } func Map(m anymap) ArObject { @@ -211,5 +211,14 @@ func Map(m anymap) ArObject { 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 } diff --git a/src/to-argon.go b/src/to-argon.go index c45307f..d174825 100644 --- a/src/to-argon.go +++ b/src/to-argon.go @@ -76,7 +76,7 @@ func anyToArgon(x any, quote bool, simplify bool, depth int, indent int, colored } case anymap: if len(x) == 0 { - return "{}" + return "<>" } keys := make([]any, len(x)) 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)) } - 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: singleline := len(x) <= 3 output := []string{} diff --git a/src/translate.go b/src/translate.go index 89c3ab9..e5e70c3 100644 --- a/src/translate.go +++ b/src/translate.go @@ -103,6 +103,8 @@ func translateVal(code UNPARSEcode, index int, codelines []UNPARSEcode, isLine i if worked { 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)