Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add % (modulus) and // (integer division) primitives #23

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/lang.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,9 @@ Multiplies all its arguments.

##### (/ ...)
Divides all its arguments, left-to-right.

##### (% ...)
Sequentially gets the remainder of the division of its arguments, left-to-right.

##### (// ...)
Integer divides all its arguments, left-to-right.
29 changes: 17 additions & 12 deletions src/fe.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@


enum {
P_LET, P_SET, P_IF, P_FN, P_MAC, P_WHILE, P_QUOTE, P_AND, P_OR, P_DO, P_CONS,
P_CAR, P_CDR, P_SETCAR, P_SETCDR, P_LIST, P_NOT, P_IS, P_ATOM, P_PRINT, P_LT,
P_LTE, P_ADD, P_SUB, P_MUL, P_DIV, P_MAX
P_LET, P_SET, P_IF, P_FN, P_MAC, P_WHILE, P_QUOTE, P_AND, P_OR, P_DO, P_CONS,
P_CAR, P_CDR, P_SETCAR, P_SETCDR, P_LIST, P_NOT, P_IS, P_ATOM, P_PRINT, P_LT,
P_LTE, P_ADD, P_SUB, P_MUL, P_DIV, P_MOD, P_IDIV, P_MAX
};

static const char *primnames[] = {
"let", "=", "if", "fn", "mac", "while", "quote", "and", "or", "do", "cons",
"car", "cdr", "setcar", "setcdr", "list", "not", "is", "atom", "print", "<",
"<=", "+", "-", "*", "/"
"<=", "+", "-", "*", "/", "%", "//"
};

static const char *typenames[] = {
Expand Down Expand Up @@ -597,12 +597,13 @@ static fe_Object* argstoenv(fe_Context *ctx, fe_Object *prm, fe_Object *arg, fe_

#define evalarg() eval(ctx, fe_nextarg(ctx, &arg), env, NULL)

#define arithop(op) { \
fe_Number x = fe_tonumber(ctx, evalarg()); \
#define arithop(expr) { \
fe_Number a = fe_tonumber(ctx, evalarg()); \
while (!isnil(arg)) { \
x = x op fe_tonumber(ctx, evalarg()); \
fe_Number b = fe_tonumber(ctx, evalarg()); \
a = expr; \
} \
res = fe_number(ctx, x); \
res = fe_number(ctx, a); \
}

#define numcmpop(op) { \
Expand Down Expand Up @@ -738,10 +739,14 @@ static fe_Object* eval(fe_Context *ctx, fe_Object *obj, fe_Object *env, fe_Objec

case P_LT: numcmpop(<); break;
case P_LTE: numcmpop(<=); break;
case P_ADD: arithop(+); break;
case P_SUB: arithop(-); break;
case P_MUL: arithop(*); break;
case P_DIV: arithop(/); break;
case P_ADD: arithop(a + b); break;
case P_SUB: arithop(a - b); break;
case P_MUL: arithop(a * b); break;
case P_DIV: arithop(a / b); break;
case P_MOD: arithop(a - b * (long) (a / b)); break;
case P_IDIV:
arithop(b ? (long) (a / b) : (fe_error(ctx, "divide by zero"), a));
break;
}
break;

Expand Down