|
|
@ -922,7 +922,6 @@ oop clone(oop obj) |
|
|
|
switch(getType(obj)) { |
|
|
|
case Undefined: |
|
|
|
case Integer: |
|
|
|
case Function: |
|
|
|
case Symbol: |
|
|
|
return obj; |
|
|
|
case String: |
|
|
@ -935,6 +934,11 @@ oop clone(oop obj) |
|
|
|
set(map, Map, elements, elements); |
|
|
|
return map; |
|
|
|
} |
|
|
|
case Function: { |
|
|
|
oop fun= malloc(sizeof(*obj)); |
|
|
|
memcpy(fun, obj, sizeof(*obj)); |
|
|
|
return fun; |
|
|
|
} |
|
|
|
} |
|
|
|
return obj; |
|
|
|
} |
|
|
@ -1948,8 +1952,218 @@ oop prim_import(oop scope, oop params) |
|
|
|
|
|
|
|
oop prim_String(oop scope, oop params) |
|
|
|
{ |
|
|
|
if (!map_hasIntegerKey(params, 0)) return null; |
|
|
|
return makeString(printString(get(params, Map, elements)[0].value)); |
|
|
|
if (!map_hasIntegerKey(params, 0)) return makeString(""); |
|
|
|
oop arg= get(params, Map, elements)[0].value; |
|
|
|
switch (getType(arg)) { |
|
|
|
case Undefined: { |
|
|
|
return makeString(""); |
|
|
|
} |
|
|
|
case Integer: { |
|
|
|
int repeat= getInteger(arg); |
|
|
|
if (!map_hasIntegerKey(params, 1)) { |
|
|
|
return makeStringFromChar('\0', repeat); |
|
|
|
} |
|
|
|
char c= getInteger(get(params, Map, elements)[1].value); |
|
|
|
return makeStringFromChar(c, repeat); |
|
|
|
} |
|
|
|
case String: { |
|
|
|
return clone(arg); |
|
|
|
} |
|
|
|
case Map: { |
|
|
|
if (map_isArray(arg)) { |
|
|
|
size_t len= map_size(arg); |
|
|
|
char *str= malloc(sizeof(char) * len + 1); |
|
|
|
for (size_t i=0; i < len; ++i) { |
|
|
|
str[i]= getInteger(get(arg, Map, elements)[i].value); |
|
|
|
} |
|
|
|
return makeStringFrom(str, len); |
|
|
|
} |
|
|
|
} |
|
|
|
case Symbol: { |
|
|
|
return makeString(get(arg, Symbol, name)); |
|
|
|
} |
|
|
|
} |
|
|
|
runtimeError("cannot make string from: %s", printString(arg)); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
oop prim_Symbol(oop scope, oop params) |
|
|
|
{ |
|
|
|
oop arg= null; |
|
|
|
if (map_hasIntegerKey(params, 0)) { |
|
|
|
arg= get(params, Map, elements)[0].value; |
|
|
|
switch (getType(arg)) { |
|
|
|
case Integer: { |
|
|
|
if (map_hasIntegerKey(params, 1)) { |
|
|
|
int repeat= getInteger(arg); |
|
|
|
char c= getInteger(get(params, Map, elements)[1].value); |
|
|
|
return makeSymbolFromChar(c, repeat); |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
case String: { |
|
|
|
return makeSymbol(get(arg, String, value)); |
|
|
|
} |
|
|
|
case Map: { |
|
|
|
if (map_isArray(arg)) { |
|
|
|
size_t len= map_size(arg); |
|
|
|
char *str= malloc(sizeof(char) * len + 1); |
|
|
|
for (size_t i=0; i < len; ++i) { |
|
|
|
str[i]= getInteger(get(arg, Map, elements)[i].value); |
|
|
|
} |
|
|
|
return makeSymbolFrom(str); |
|
|
|
} |
|
|
|
} |
|
|
|
case Symbol: { |
|
|
|
return arg; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
runtimeError("cannot make symbol from: %s", printString(arg)); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
oop prim_Integer(oop scope, oop params) |
|
|
|
{ |
|
|
|
oop arg= null; |
|
|
|
if (map_hasIntegerKey(params, 0)) { |
|
|
|
arg= get(params, Map, elements)[0].value; |
|
|
|
switch (getType(arg)) { |
|
|
|
case Undefined: { |
|
|
|
return makeInteger(0); |
|
|
|
} |
|
|
|
case Integer: { |
|
|
|
return arg; |
|
|
|
} |
|
|
|
case String: { |
|
|
|
if (!map_hasIntegerKey(params, 1)) { |
|
|
|
return makeInteger(strtoll(get(arg, String, value), NULL, 0)); |
|
|
|
} |
|
|
|
int base= getInteger(get(params, Map, elements)[1].value); |
|
|
|
if (base > 36 || base < 2) { |
|
|
|
runtimeError("base must be between 2 and 36 inclusive"); |
|
|
|
} |
|
|
|
return makeInteger(strtoll(get(arg, String, value), NULL, base)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
runtimeError("cannot make integer from: %s", printString(arg)); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
oop prim_Map(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: { |
|
|
|
return makeMapCapacity(getInteger(arg)); |
|
|
|
} |
|
|
|
case Map: { |
|
|
|
return clone(arg); |
|
|
|
} |
|
|
|
} |
|
|
|
runtimeError("cannot make map from: %s", printString(arg)); |
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
runtimeError("cannot make array from: %s", printString(arg)); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
oop prim_Function(oop scope, oop params) |
|
|
|
{ |
|
|
|
oop arg= null; |
|
|
|
if (map_hasIntegerKey(params, 0)) { |
|
|
|
arg= get(params, Map, elements)[0].value; |
|
|
|
switch (getType(arg)) { |
|
|
|
case Function: { |
|
|
|
if (isTrue(get(arg, Function, fixed))) { |
|
|
|
oop unfix= clone(arg); |
|
|
|
set(unfix, Function, fixed, makeInteger(0)); |
|
|
|
return unfix; |
|
|
|
} else { |
|
|
|
return clone(arg); |
|
|
|
} |
|
|
|
} |
|
|
|
case Map: { |
|
|
|
if (map_hasIntegerKey(params, 1) && map_hasIntegerKey(params, 2) && map_hasIntegerKey(params, 3)) { |
|
|
|
oop param= arg; |
|
|
|
oop body= get(params, Map, elements)[1].value; |
|
|
|
oop parentScope= get(params, Map, elements)[2].value; |
|
|
|
oop name= get(params, Map, elements)[3].value; |
|
|
|
if (is(Map, body) && is(Map, parentScope) && is(Map, name)) { |
|
|
|
return makeFunction(NULL, name, param, body, parentScope, makeInteger(0)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
runtimeError("cannot make function from: %s", printString(arg)); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
oop prim_Syntax(oop scope, oop params) |
|
|
|
{ |
|
|
|
oop arg= null; |
|
|
|
if (map_hasIntegerKey(params, 0)) { |
|
|
|
arg= get(params, Map, elements)[0].value; |
|
|
|
switch (getType(arg)) { |
|
|
|
case Function: { |
|
|
|
if (isFalse(get(arg, Function, fixed))) { |
|
|
|
oop fix= clone(arg); |
|
|
|
set(fix, Function, fixed, makeInteger(1)); |
|
|
|
return fix; |
|
|
|
} else { |
|
|
|
return clone(arg); |
|
|
|
} |
|
|
|
} |
|
|
|
case Map: { |
|
|
|
if (map_hasIntegerKey(params, 1) && map_hasIntegerKey(params, 2) && map_hasIntegerKey(params, 3)) { |
|
|
|
oop param= arg; |
|
|
|
oop body= get(params, Map, elements)[1].value; |
|
|
|
oop parentScope= get(params, Map, elements)[2].value; |
|
|
|
oop name= get(params, Map, elements)[3].value; |
|
|
|
if (is(Map, body) && is(Map, parentScope) && is(Map, name)) { |
|
|
|
return makeFunction(NULL, name, param, body, parentScope, makeInteger(1)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
runtimeError("cannot make syntax from: %s", printString(arg)); |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
|
|
|
|
oop prim_scope(oop scope, oop params) |
|
|
@ -1986,6 +2200,12 @@ int main(int argc, char **argv) |
|
|
|
map_set(globals, intern("import" ), makeFunction(prim_import, intern("import" ), null, null, globals, null)); |
|
|
|
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("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("Function" ), makeFunction(prim_Function , intern("Function" ), null, null, globals, null)); |
|
|
|
map_set(globals, intern("Syntax" ), makeFunction(prim_Syntax , intern("Syntax" ), null, null, globals, null)); |
|
|
|
|
|
|
|
map_set(globals, intern("scope"), makeFunction(prim_scope, intern("scope"), null, null, globals, null)); |
|
|
|
|
|
|
|