diff --git a/object.c b/object.c index 388a12a..0a00816 100644 --- a/object.c +++ b/object.c @@ -518,6 +518,25 @@ oop map_append(oop map, oop value) return map_set(map, makeInteger(map_size(map)), value); } +oop makeArrayFromElement(oop elem, int repeat) +{ + oop array= makeMapCapacity(repeat); + for(int i=0; i < repeat; ++i) { + map_append(array, elem); + } + return array; +} + +oop makeArrayFromString(char *str) +{ + size_t len= strlen(str); + oop array= makeMapCapacity(len); + for(int i=0; i < len; ++i) { + map_append(array, makeInteger(str[i])); + } + return array; +} + bool isHidden(oop obj) { if (is(Symbol, obj)) { char *s = get(obj, Symbol, name); diff --git a/parse.leg b/parse.leg index 795dcbb..a3a6c4d 100644 --- a/parse.leg +++ b/parse.leg @@ -2062,6 +2062,37 @@ oop prim_Map(oop scope, oop params) return null; } +oop prim_Array(oop scope, oop params) +{ + if (!map_hasIntegerKey(params, 0)) return makeMap(); + oop arg= get(params, Map, elements)[0].value; + switch (getType(arg)) { + case Undefined: { + return makeMap(); + } + case Integer: { + int repeat= getInteger(arg); + oop array= NULL; + if (map_hasIntegerKey(params, 1)) { + array= makeArrayFromElement(get(params, Map, elements)[1].value, repeat); + } else { + array= makeArrayFromElement(null, repeat); + } + return array; + } + case Symbol: { + return makeArrayFromString(get(arg, Symbol, name)); + } + case String: { + return makeArrayFromString(get(arg, String, value)); + } + case Map: { + return clone(arg); + } + } + return null; +} + oop prim_scope(oop scope, oop params) { return fixScope(scope); @@ -2097,8 +2128,9 @@ int main(int argc, char **argv) map_set(globals, intern("microseconds"), makeFunction(prim_microseconds, intern("microseconds"), null, null, globals, null)); map_set(globals, intern("String" ), makeFunction(prim_String , intern("String" ), null, null, globals, null)); map_set(globals, intern("Integer" ), makeFunction(prim_Integer , intern("Integer" ), null, null, globals, null)); - map_set(globals, intern("Map" ), makeFunction(prim_Map , intern("Map" ), null, null, globals, null)); map_set(globals, intern("Symbol" ), makeFunction(prim_Symbol , intern("Symbol" ), null, null, globals, null)); + map_set(globals, intern("Map" ), makeFunction(prim_Map , intern("Map" ), null, null, globals, null)); + map_set(globals, intern("Array" ), makeFunction(prim_Array , intern("Array" ), null, null, globals, null)); map_set(globals, intern("scope"), makeFunction(prim_scope, intern("scope"), null, null, globals, null));