altos/scheme: Add separate floor-quotient builtin
authorKeith Packard <keithp@keithp.com>
Sun, 24 Dec 2017 22:28:29 +0000 (14:28 -0800)
committerKeith Packard <keithp@keithp.com>
Sun, 24 Dec 2017 22:31:31 +0000 (14:31 -0800)
Does what 'quotient' did before, now quotient rounds towards zero
while floor-quotient rounds down.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/scheme/ao_scheme_builtin.c
src/scheme/ao_scheme_builtin.txt

index 81fd901058fa7967332328d773e1c5297148a48c..e2532c98746a331ae248ace4f825ab41c4d1d874 100644 (file)
@@ -393,6 +393,11 @@ ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
                        case builtin_quotient:
                                if (c == 0)
                                        return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "quotient by zero");
+                               r = r / c;
+                               break;
+                       case builtin_floor_quotient:
+                               if (c == 0)
+                                       return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "floor-quotient by zero");
                                if (r % c != 0 && (c < 0) != (r < 0))
                                        r = r / c - 1;
                                else
@@ -436,6 +441,7 @@ ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
                                r /= c;
                                break;
                        case builtin_quotient:
+                       case builtin_floor_quotient:
                        case builtin_remainder:
                        case builtin_modulo:
                                return ao_scheme_error(AO_SCHEME_INVALID, "non-integer value in integer divide");
@@ -491,6 +497,12 @@ ao_scheme_do_quotient(struct ao_scheme_cons *cons)
        return ao_scheme_math(cons, builtin_quotient);
 }
 
+ao_poly
+ao_scheme_do_floor_quotient(struct ao_scheme_cons *cons)
+{
+       return ao_scheme_math(cons, builtin_floor_quotient);
+}
+
 ao_poly
 ao_scheme_do_modulo(struct ao_scheme_cons *cons)
 {
index 23adf6eddbb276d74fcefbbdbf64607c5c9a6b7f..bdadbd6ac6d3705aa3a92593a791f5e8ce8a36a6 100644 (file)
@@ -31,6 +31,7 @@ all   f_lambda        divide          /
 all    f_lambda        modulo          modulo  %
 all    f_lambda        remainder
 all    f_lambda        quotient
+all    f_lambda        floor_quotient  floor-quotient
 all    f_lambda        equal           =       eq?     eqv?
 all    f_lambda        less            <       string<?
 all    f_lambda        greater         >       string>?