Ordering
For simplicity’s sake I’m going to re-use our number data type to represent the result of comparisons. I’ll make a rule similar to C, to say that any number that isn’t 0
evaluates to true in an if
statement, while 0
always evaluates to false.
Therefore our ordering functions are a little like a simplified version of our arithmetic functions. They’ll only work on numbers, and we only want them to work on two arguments.
If these error conditions are met the maths is simple. We want to return a number lval
either 0
or 1
depending on the equality comparison between the two input lval
. We can use C’s comparison operators to do this. Like our arithmetic functions we’ll make use of a single function to do all of the comparisons.
First we check the error conditions, then we compare the numbers in each of the arguments to get some result. Finally we return this result as a number value.
lval* builtin_gt(lenv* e, lval* a) {
return builtin_ord(e, a, ">");
}
lval* builtin_lt(lenv* e, lval* a) {
return builtin_ord(e, a, "<");
}
lval* builtin_ge(lenv* e, lval* a) {
return builtin_ord(e, a, ">=");
}
lval* builtin_le(lenv* e, lval* a) {
return builtin_ord(e, a, "<=");
}
lval* builtin_ord(lenv* e, lval* a, char* op) {
LASSERT_NUM(op, a, 2);
LASSERT_TYPE(op, a, 0, LVAL_NUM);
LASSERT_TYPE(op, a, 1, LVAL_NUM);
int r;
if (strcmp(op, ">") == 0) {
r = (a->cell[0]->num > a->cell[1]->num);
}
if (strcmp(op, "<") == 0) {
r = (a->cell[0]->num < a->cell[1]->num);
}
if (strcmp(op, ">=") == 0) {
r = (a->cell[0]->num >= a->cell[1]->num);
}
if (strcmp(op, "<=") == 0) {
r = (a->cell[0]->num <= a->cell[1]->num);
}
lval_del(a);
return lval_num(r);
}