|
@ -19,9 +19,9 @@ |
|
|
_DO(PostIncVariable) _DO(PostIncMember) _DO(PostIncIndex) \ |
|
|
_DO(PostIncVariable) _DO(PostIncMember) _DO(PostIncIndex) \ |
|
|
_DO(PreDecVariable) _DO(PreDecMember) _DO(PreDecIndex) \ |
|
|
_DO(PreDecVariable) _DO(PreDecMember) _DO(PreDecIndex) \ |
|
|
_DO(PostDecVariable) _DO(PostDecMember) _DO(PostDecIndex) \ |
|
|
_DO(PostDecVariable) _DO(PostDecMember) _DO(PostDecIndex) \ |
|
|
_DO(GetVariable) _DO(GetMember) _DO(SetMember) _DO(GetIndex) _DO(SetIndex) \ |
|
|
|
|
|
|
|
|
_DO(GetVariable) _DO(GetMember) _DO(SetMember) _DO(GetIndex) _DO(SetIndex) _DO(Slice) \ |
|
|
_DO(Return) _DO(Break) _DO(Continue) _DO(Throw) _DO(Try) \ |
|
|
_DO(Return) _DO(Break) _DO(Continue) _DO(Throw) _DO(Try) \ |
|
|
_DO(Quasiquote) _DO(Unquote) _DO(Unsplice) |
|
|
|
|
|
|
|
|
_DO(Quasiquote) _DO(Unquote) _DO(Unsplice) _DO(Splice) |
|
|
|
|
|
|
|
|
typedef enum { |
|
|
typedef enum { |
|
|
t_UNDEFINED=0, |
|
|
t_UNDEFINED=0, |
|
@ -70,7 +70,8 @@ oop globals= 0; |
|
|
_DO(lhs) _DO(rhs) _DO(scope) _DO(args) _DO(expression) _DO(labels) _DO(statements) _DO(initialise) \ |
|
|
_DO(lhs) _DO(rhs) _DO(scope) _DO(args) _DO(expression) _DO(labels) _DO(statements) _DO(initialise) \ |
|
|
_DO(update) _DO(this) _DO(fixed) _DO(operator) _DO(map) _DO(func) \ |
|
|
_DO(update) _DO(this) _DO(fixed) _DO(operator) _DO(map) _DO(func) \ |
|
|
_DO(try) _DO(catch) _DO(finally) _DO(exception) \ |
|
|
_DO(try) _DO(catch) _DO(finally) _DO(exception) \ |
|
|
_DO(__line__) _DO(__file__) |
|
|
|
|
|
|
|
|
_DO(__line__) _DO(__file__) \ |
|
|
|
|
|
_DO(start) _DO(stop) |
|
|
|
|
|
|
|
|
#define _DO(NAME) oop NAME##_symbol; |
|
|
#define _DO(NAME) oop NAME##_symbol; |
|
|
DO_SYMBOLS() |
|
|
DO_SYMBOLS() |
|
@ -560,6 +561,15 @@ oop newContinue(void) |
|
|
return obj; |
|
|
return obj; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
oop newSlice(oop value, oop start, oop stop) |
|
|
|
|
|
{ |
|
|
|
|
|
oop obj= newObject(Slice_proto); |
|
|
|
|
|
map_set(obj, value_symbol, value); |
|
|
|
|
|
map_set(obj, start_symbol, start); |
|
|
|
|
|
map_set(obj, stop_symbol, stop); |
|
|
|
|
|
return obj; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
oop newTry(oop try, oop exception, oop catch, oop finally) |
|
|
oop newTry(oop try, oop exception, oop catch, oop finally) |
|
|
{ |
|
|
{ |
|
|
oop obj = newObject(Try_proto); |
|
|
oop obj = newObject(Try_proto); |
|
@ -678,23 +688,23 @@ cond = c:logor QUERY t:exp COLON f:cond { $$ = newIf(c, t, |
|
|
|
|
|
|
|
|
logor = l:logand |
|
|
logor = l:logand |
|
|
( LOGOR r:logand { l = newBinary(Logor_proto, l, r) } |
|
|
( LOGOR r:logand { l = newBinary(Logor_proto, l, r) } |
|
|
)* { $$ = l } |
|
|
|
|
|
|
|
|
)* { $$ = l } |
|
|
|
|
|
|
|
|
logand = l:bitor |
|
|
logand = l:bitor |
|
|
( LOGAND r:bitor { l = newBinary(Logand_proto, l, r) } |
|
|
( LOGAND r:bitor { l = newBinary(Logand_proto, l, r) } |
|
|
)* { $$ = l } |
|
|
|
|
|
|
|
|
)* { $$ = l } |
|
|
|
|
|
|
|
|
bitor = l:bitxor |
|
|
bitor = l:bitxor |
|
|
( BITOR r:bitxor { l = newBinary(Bitor_proto, l, r) } |
|
|
( BITOR r:bitxor { l = newBinary(Bitor_proto, l, r) } |
|
|
)* { $$ = l } |
|
|
|
|
|
|
|
|
)* { $$ = l } |
|
|
|
|
|
|
|
|
bitxor = l:bitand |
|
|
bitxor = l:bitand |
|
|
( BITXOR r:bitand { l = newBinary(Bitxor_proto, l, r) } |
|
|
|
|
|
)* { $$ = l } |
|
|
|
|
|
|
|
|
( BITXOR r:bitand { l = newBinary(Bitxor_proto, l, r) } |
|
|
|
|
|
)* { $$ = l } |
|
|
|
|
|
|
|
|
bitand = l:eq |
|
|
bitand = l:eq |
|
|
( BITAND r:eq { l = newBinary(Bitand_proto, l, r) } |
|
|
|
|
|
)* { $$ = l } |
|
|
|
|
|
|
|
|
( BITAND r:eq { l = newBinary(Bitand_proto, l, r) } |
|
|
|
|
|
)* { $$ = l } |
|
|
|
|
|
|
|
|
eq = l:ineq |
|
|
eq = l:ineq |
|
|
( EQUAL r:ineq { l = newBinary(Equal_proto, l, r) } |
|
|
( EQUAL r:ineq { l = newBinary(Equal_proto, l, r) } |
|
@ -714,8 +724,8 @@ shift = l:sum |
|
|
)* { $$ = l } |
|
|
)* { $$ = l } |
|
|
|
|
|
|
|
|
sum = l:prod |
|
|
sum = l:prod |
|
|
( PLUS r:prod { l = newBinary(Add_proto, l, r) } |
|
|
|
|
|
| MINUS r:prod { l = newBinary(Sub_proto, l, r) } |
|
|
|
|
|
|
|
|
( PLUS r:prod { l = newBinary(Add_proto, l, r) } |
|
|
|
|
|
| MINUS r:prod { l = newBinary(Sub_proto, l, r) } |
|
|
)* { $$ = l } |
|
|
)* { $$ = l } |
|
|
|
|
|
|
|
|
prod = l:prefix |
|
|
prod = l:prefix |
|
@ -734,6 +744,10 @@ prefix = PLUS n:prefix { $$= n } |
|
|
|
|
|
|
|
|
postfix = i:value ( DOT s:IDENT a:argumentList { i = newInvoke(i, s, a) } |
|
|
postfix = i:value ( DOT s:IDENT a:argumentList { i = newInvoke(i, s, a) } |
|
|
| DOT s:IDENT !assignOp { i = newGetMap(GetMember_proto, i, s) } |
|
|
| DOT s:IDENT !assignOp { i = newGetMap(GetMember_proto, i, s) } |
|
|
|
|
|
| LBRAC e1:exp COLON e2:exp RBRAC !assignOp { i = newSlice(i, e1, e2) } |
|
|
|
|
|
| LBRAC e1:exp COLON RBRAC !assignOp { i = newSlice(i, e1, null) } |
|
|
|
|
|
| LBRAC COLON e2:exp RBRAC !assignOp { i = newSlice(i, null, e2) } |
|
|
|
|
|
| LBRAC COLON RBRAC !assignOp { i = newSlice(i, null, null) } |
|
|
| LBRAC p:exp RBRAC !assignOp { i = newGetMap(GetIndex_proto, i, p) } |
|
|
| LBRAC p:exp RBRAC !assignOp { i = newGetMap(GetIndex_proto, i, p) } |
|
|
| a:argumentList { i = (null != getSyntax(1, i)) ? apply(globals, globals, getSyntax(1, i), a, i) : newCall(i, a) } |
|
|
| a:argumentList { i = (null != getSyntax(1, i)) ? apply(globals, globals, getSyntax(1, i), a, i) : newCall(i, a) } |
|
|
| PLUSPLUS { i = newPostIncrement(i) } |
|
|
| PLUSPLUS { i = newPostIncrement(i) } |
|
@ -748,18 +762,18 @@ paramList = LPAREN m:makeMap |
|
|
RPAREN { $$ = m } |
|
|
RPAREN { $$ = m } |
|
|
|
|
|
|
|
|
parameter = ATAT p:value { $$ = newUnary(Unsplice_proto, p) } |
|
|
parameter = ATAT p:value { $$ = newUnary(Unsplice_proto, p) } |
|
|
| AT p:value { $$ = newUnary(Unquote_proto, p) } |
|
|
|
|
|
| p:IDENT { $$ = p } |
|
|
|
|
|
|
|
|
| p:ident { $$ = p } |
|
|
|
|
|
|
|
|
argumentList = LPAREN m:makeMap |
|
|
argumentList = LPAREN m:makeMap |
|
|
( a:argument { map_append(m, a) } |
|
|
|
|
|
|
|
|
( a:argument { map_append(m, a) } |
|
|
( COMMA a:argument { map_append(m, a) } |
|
|
( COMMA a:argument { map_append(m, a) } |
|
|
) * |
|
|
) * |
|
|
) ? |
|
|
) ? |
|
|
RPAREN { $$ = m } |
|
|
RPAREN { $$ = m } |
|
|
|
|
|
|
|
|
argument = ATAT a:value { $$ = newUnary(Unsplice_proto, a) } |
|
|
|
|
|
| a:exp { $$ = a } |
|
|
|
|
|
|
|
|
argument = ATAT e:value { $$ = newUnary(Unsplice_proto, e) } |
|
|
|
|
|
| MULTI e:exp { $$ = newUnary(Splice_proto, e) } |
|
|
|
|
|
| e:exp { $$ = e } |
|
|
|
|
|
|
|
|
value = BACKTICK n:value { $$ = newUnary(Quasiquote_proto, n) } |
|
|
value = BACKTICK n:value { $$ = newUnary(Quasiquote_proto, n) } |
|
|
| AT n:value { $$ = newUnary(Unquote_proto, n) } |
|
|
| AT n:value { $$ = newUnary(Unquote_proto, n) } |
|
@ -916,7 +930,6 @@ oop clone(oop obj) |
|
|
switch(getType(obj)) { |
|
|
switch(getType(obj)) { |
|
|
case Undefined: |
|
|
case Undefined: |
|
|
case Integer: |
|
|
case Integer: |
|
|
case Function: |
|
|
|
|
|
case Symbol: |
|
|
case Symbol: |
|
|
return obj; |
|
|
return obj; |
|
|
case String: |
|
|
case String: |
|
@ -929,6 +942,11 @@ oop clone(oop obj) |
|
|
set(map, Map, elements, elements); |
|
|
set(map, Map, elements, elements); |
|
|
return map; |
|
|
return map; |
|
|
} |
|
|
} |
|
|
|
|
|
case Function: { |
|
|
|
|
|
oop fun= malloc(sizeof(*obj)); |
|
|
|
|
|
memcpy(fun, obj, sizeof(*obj)); |
|
|
|
|
|
return fun; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
return obj; |
|
|
return obj; |
|
|
} |
|
|
} |
|
@ -1498,6 +1516,9 @@ oop eval(oop scope, oop ast) |
|
|
} |
|
|
} |
|
|
return apply(scope, this, func, args, ast); |
|
|
return apply(scope, this, func, args, ast); |
|
|
} |
|
|
} |
|
|
|
|
|
case t_Splice: { |
|
|
|
|
|
runtimeError("* outside of argument list"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
case t_Return: { |
|
|
case t_Return: { |
|
|
assert(jbs); |
|
|
assert(jbs); |
|
@ -1600,12 +1621,24 @@ oop eval(oop scope, oop ast) |
|
|
oop key = eval(scope, map_get(ast, key_symbol)); |
|
|
oop key = eval(scope, map_get(ast, key_symbol)); |
|
|
switch (getType(map)) { |
|
|
switch (getType(map)) { |
|
|
case String: |
|
|
case String: |
|
|
if (getInteger(key) >= get(map, String, size)) { |
|
|
|
|
|
runtimeError("GetIndex out of range on String"); |
|
|
|
|
|
|
|
|
if (!isInteger(key)) { |
|
|
|
|
|
runtimeError("non-integer index"); |
|
|
} |
|
|
} |
|
|
return makeInteger(unescape(get(map, String, value))[getInteger(key)]); |
|
|
|
|
|
|
|
|
ssize_t i= getInteger(key); |
|
|
|
|
|
size_t len= string_size(map); |
|
|
|
|
|
if (i < 0) i+= len; |
|
|
|
|
|
if (i < 0 || i >= len) { |
|
|
|
|
|
runtimeError("GetIndex out of bounds on String"); |
|
|
|
|
|
} |
|
|
|
|
|
return makeInteger(get(map, String, value)[i]); |
|
|
case Map: |
|
|
case Map: |
|
|
return getVariable(map, key); |
|
|
|
|
|
|
|
|
if (isInteger(key) && getInteger(key) < 0) { |
|
|
|
|
|
size_t size= map_size(map); |
|
|
|
|
|
if (size > 0 && map_hasIntegerKey(map, size - 1)) { |
|
|
|
|
|
key= makeInteger(getInteger(key) + size); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return map_get(map, key); |
|
|
default: |
|
|
default: |
|
|
runtimeError("GetIndex on non Map or String"); |
|
|
runtimeError("GetIndex on non Map or String"); |
|
|
} |
|
|
} |
|
@ -1618,7 +1651,7 @@ oop eval(oop scope, oop ast) |
|
|
switch (getType(map)) { |
|
|
switch (getType(map)) { |
|
|
case String: |
|
|
case String: |
|
|
if (getInteger(key) >= get(map, String, size)) { |
|
|
if (getInteger(key) >= get(map, String, size)) { |
|
|
runtimeError("SetIndex out of range on String"); |
|
|
|
|
|
|
|
|
runtimeError("SetIndex out of bounds on String"); |
|
|
} |
|
|
} |
|
|
get(map, String, value)[getInteger(key)] = getInteger(value); |
|
|
get(map, String, value)[getInteger(key)] = getInteger(value); |
|
|
return value; |
|
|
return value; |
|
@ -1630,6 +1663,35 @@ oop eval(oop scope, oop ast) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
case t_Slice: { |
|
|
|
|
|
oop pre= eval(scope, map_get(ast, value_symbol)); |
|
|
|
|
|
oop start= eval(scope, map_get(ast, start_symbol)); |
|
|
|
|
|
oop stop= eval(scope, map_get(ast, stop_symbol)); |
|
|
|
|
|
ssize_t first= start == null ? 0 : getInteger(start); |
|
|
|
|
|
|
|
|
|
|
|
if (start == null) { |
|
|
|
|
|
start= makeInteger(0); |
|
|
|
|
|
} |
|
|
|
|
|
switch (getType(pre)) { |
|
|
|
|
|
case String: { |
|
|
|
|
|
ssize_t last= stop == null ? string_size(pre) : getInteger(stop); |
|
|
|
|
|
oop res= string_slice(pre, first, last); |
|
|
|
|
|
if (NULL == res) { |
|
|
|
|
|
runtimeError("index out of bounds"); |
|
|
|
|
|
} |
|
|
|
|
|
return res; |
|
|
|
|
|
} |
|
|
|
|
|
case Map: { |
|
|
|
|
|
ssize_t last= stop == null ? map_size(pre) : getInteger(stop); |
|
|
|
|
|
oop res= map_slice(pre, first, last); |
|
|
|
|
|
if (NULL == res) { |
|
|
|
|
|
runtimeError("index out of bounds"); |
|
|
|
|
|
} |
|
|
|
|
|
return res; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
runtimeError("slicing a non-String or non-Map"); |
|
|
|
|
|
} |
|
|
case t_Symbol: |
|
|
case t_Symbol: |
|
|
case t_Integer: |
|
|
case t_Integer: |
|
|
case t_String: { |
|
|
case t_String: { |
|
@ -1883,16 +1945,27 @@ oop prim_print(oop scope, oop params) |
|
|
return params; |
|
|
return params; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
oop evalArgs(oop scope, oop args) |
|
|
|
|
|
{ |
|
|
|
|
|
int i = 0; |
|
|
|
|
|
oop params = makeMap(); |
|
|
|
|
|
oop index; |
|
|
|
|
|
while ((index = makeInteger(i)), map_hasKey(args, index)) { |
|
|
|
|
|
map_set(params, index, eval(scope, map_get(args, index))); |
|
|
|
|
|
i++; |
|
|
|
|
|
} |
|
|
|
|
|
return params; |
|
|
|
|
|
|
|
|
oop evalArgs(oop scope, oop asts) |
|
|
|
|
|
{ |
|
|
|
|
|
oop args= makeMap(); |
|
|
|
|
|
size_t nargs= map_size(asts); |
|
|
|
|
|
for (size_t i= 0; i < nargs; ++i) { |
|
|
|
|
|
oop ast= get(asts, Map, elements)[i].value; |
|
|
|
|
|
if (is(Map, ast) && (Splice_proto == map_get(ast, __proto___symbol))) { |
|
|
|
|
|
oop splice= eval(scope, map_get(ast, rhs_symbol)); |
|
|
|
|
|
if (!is(Map, splice)) map_append(args, splice); |
|
|
|
|
|
else { |
|
|
|
|
|
size_t nsplice= map_size(splice); |
|
|
|
|
|
for (size_t j= 0; j < nsplice; ++j) { |
|
|
|
|
|
map_append(args, get(splice, Map, elements)[j].value); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
map_append(args, eval(scope, ast)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return args; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
oop AST= NULL; |
|
|
oop AST= NULL; |
|
@ -1952,8 +2025,218 @@ oop prim_import(oop scope, oop params) |
|
|
|
|
|
|
|
|
oop prim_String(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) |
|
|
oop prim_scope(oop scope, oop params) |
|
@ -1992,6 +2275,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("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("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("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)); |
|
|
map_set(globals, intern("scope"), makeFunction(prim_scope, intern("scope"), null, null, globals, null)); |
|
|
|
|
|
|
|
|