shithub: femtolisp

Download patch

ref: 27120b0ce4d33ae58a0d3878293c4e3f0aee1d1f
parent: 291b5f86e063dce60db3c826c166103cc98b3fcc
author: JeffBezanson <jeff.bezanson@gmail.com>
date: Wed Apr 22 11:02:49 EDT 2009

fixing bug in truncate
simplifying nconc loop


--- a/femtolisp/builtins.c
+++ b/femtolisp/builtins.c
@@ -33,12 +33,17 @@
     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 @@
             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]);
--- a/femtolisp/flisp.c
+++ b/femtolisp/flisp.c
@@ -814,7 +814,19 @@
         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 @@
                     }
                 }
             }
-            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 @@
         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 @@
         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]);