diff --git a/femtolisp/builtins.c b/femtolisp/builtins.c index 8e9ea19..87590ce 100644 --- a/femtolisp/builtins.c +++ b/femtolisp/builtins.c @@ -33,12 +33,17 @@ static value_t fl_nconc(value_t *args, u_int32_t nargs) value_t lst, first=NIL; value_t *pcdr = &first; cons_t *c; - int a; - FOR_ARGS(a, 0, lst, args) { - // skip last - if ((nargs > MAX_ARGS && !iscons(args[MAX_ARGS])) || - (nargs <= MAX_ARGS && a == nargs-1)) - break; + int i=0; + while (1) { + if (i >= MAX_ARGS) { + lst = car_(args[MAX_ARGS]); + args[MAX_ARGS] = cdr_(args[MAX_ARGS]); + if (!iscons(args[MAX_ARGS])) break; + } + else { + lst = args[i++]; + if (i >= nargs) break; + } if (iscons(lst)) { *pcdr = lst; c = (cons_t*)ptr(lst); @@ -254,8 +259,13 @@ static value_t fl_truncate(value_t *args, u_int32_t nargs) d = *(double*)data; else return args[0]; - if (d > 0) + if (d > 0) { + if (d > (double)U64_MAX) + return args[0]; return return_from_uint64((uint64_t)d); + } + if (d > (double)S64_MAX || d < (double)S64_MIN) + return args[0]; return return_from_int64((int64_t)d); } type_error("truncate", "number", args[0]); diff --git a/femtolisp/flisp.c b/femtolisp/flisp.c index 9e4c98c..c39e692 100644 --- a/femtolisp/flisp.c +++ b/femtolisp/flisp.c @@ -814,7 +814,19 @@ static value_t apply_cl(uint32_t nargs) do_call: s = SP; func = Stack[SP-i-1]; - if (isbuiltinish(func)) { + if (isfunction(func)) { + if (op == OP_TCALL) { + for(s=-1; s < (fixnum_t)i; s++) + Stack[bp+s] = Stack[SP-i+s]; + SP = bp+i; + nargs = i; + goto apply_cl_top; + } + else { + v = apply_cl(i); + } + } + else if (isbuiltinish(func)) { op = uintval(func); if (op > N_BUILTINS) { v = ((builtin_t)ptr(func))(&Stack[SP-i], i); @@ -842,18 +854,6 @@ static value_t apply_cl(uint32_t nargs) } } } - else if (isfunction(func)) { - if (op == OP_TCALL) { - for(s=-1; s < (fixnum_t)i; s++) - Stack[bp+s] = Stack[SP-i+s]; - SP = bp+i; - nargs = i; - goto apply_cl_top; - } - else { - v = apply_cl(i); - } - } else { type_error("apply", "function", func); } @@ -1021,7 +1021,6 @@ static value_t apply_cl(uint32_t nargs) case OP_SUB: n = code[ip++]; apply_sub: - if (__unlikely(n < 1)) lerror(ArgError, "-: too few arguments"); i = SP-n; if (n == 1) { if (__likely(isfixnum(Stack[i]))) @@ -1084,7 +1083,6 @@ static value_t apply_cl(uint32_t nargs) case OP_DIV: n = code[ip++]; apply_div: - if (__unlikely(n < 1)) lerror(ArgError, "/: too few arguments"); i = SP-n; if (n == 1) { Stack[SP-1] = fl_div2(fixnum(1), Stack[i]);