shithub: femtolisp

Download patch

ref: e365cb1d33f1642da25d46c76b44dee842357b37
parent: ae2a4dd1561db4e404aa5c86f0706d703a648f58
author: JeffBezanson <jeff.bezanson@gmail.com>
date: Wed May 13 22:53:04 EDT 2009

adding divide by 0 check in div


--- a/femtolisp/cvalues.c
+++ b/femtolisp/cvalues.c
@@ -1264,6 +1264,12 @@
     return 1;
 }
 
+static void DivideByZeroError() __attribute__ ((__noreturn__));
+static void DivideByZeroError()
+{
+    lerror(DivideError, "/: division by zero");
+}
+
 static value_t fl_div2(value_t a, value_t b)
 {
     double da, db;
@@ -1280,7 +1286,7 @@
     db = conv_to_double(bptr, tb);
 
     if (db == 0 && tb < T_FLOAT)  // exact 0
-        lerror(DivideError, "/: division by zero");
+        DivideByZeroError();
 
     da = da/db;
 
@@ -1330,7 +1336,7 @@
 
     return return_from_int64(conv_to_int64(aptr, ta) / b64);
  div_error:
-    lerror(DivideError, "/: division by zero");
+    DivideByZeroError();
 }
 
 static value_t fl_bitwise_op(value_t a, value_t b, int opcode, char *fname)
--- a/femtolisp/flisp.c
+++ b/femtolisp/flisp.c
@@ -1139,8 +1139,10 @@
             goto next_op;
         case OP_IDIV:
             v = Stack[SP-2]; e = Stack[SP-1];
-            if (bothfixnums(v, e))
+            if (bothfixnums(v, e)) {
+                if (e==0) DivideByZeroError();
                 v = fixnum(numval(v) / numval(e));
+            }
             else
                 v = fl_idiv2(v, e);
             POPN(1);
--- a/femtolisp/todo
+++ b/femtolisp/todo
@@ -1024,13 +1024,13 @@
 * make (for ...) a special form
 * trycatch should require 2nd arg to be a lambda expression
 * immediate load int8 instruction
-- fix equal? on functions
-- store function name and signature
-- maxstack calculation, replace Stack with C stack, alloca
+- maxstack calculation, make Stack growable
   - stack traces and better debugging support
+- let eversion
 - lambda lifting
 * let optimization
-- let eversion
+- fix equal? on functions
+- store function name and signature
 * have macroexpand use its own global syntax table
 * be able to create/load an image file
 * fix trace and untrace