|
|
@ -170,7 +170,7 @@ lval* lval_read(mpc_ast_t* t) { |
|
|
|
|
|
|
|
/* Fill this list with any valid expression |
|
|
|
contained within */ |
|
|
|
for (size_t i = 0; i < t->children_num; i++) { |
|
|
|
for (size_t i = 0; i < (size_t) t->children_num; i++) { |
|
|
|
if (strcmp(t->children[i]->contents, "(") == 0) { |
|
|
|
continue; |
|
|
|
} |
|
|
@ -415,6 +415,66 @@ lval* builtin_op(lval* a, char* op) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
lval* builtin_head(lval* a) { |
|
|
|
|
|
|
|
/* Check error conditions */ |
|
|
|
if (a->count != 1) { |
|
|
|
lval_del(a); |
|
|
|
return lval_err("Function 'head' passed too many arguments"); |
|
|
|
} |
|
|
|
|
|
|
|
if (a->cell[0]->type != LVAL_QEXPR) { |
|
|
|
lval_del(a); |
|
|
|
return lval_err("Function 'head' passed incorrect types!"); |
|
|
|
} |
|
|
|
|
|
|
|
if (a->cell[0]->count == 0) { |
|
|
|
lval_del(a); |
|
|
|
return lval_err("Function 'head' passed empty {}!"); |
|
|
|
} |
|
|
|
|
|
|
|
/* Otherwise take first argument */ |
|
|
|
lval* v = lval_take(a, 0); |
|
|
|
|
|
|
|
/* Delete all elements that are not head and return */ |
|
|
|
while (v-> count > 1) { |
|
|
|
lval_del(lval_pop(v, 1)); |
|
|
|
} |
|
|
|
|
|
|
|
return v; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
lval* builtin_tail(lval* a) { |
|
|
|
|
|
|
|
/* Check error conditions */ |
|
|
|
if (a->count != 1) { |
|
|
|
lval_del(a); |
|
|
|
return lval_err("Function 'tail' passed too many arguments"); |
|
|
|
} |
|
|
|
|
|
|
|
if (a->cell[0]->type != LVAL_QEXPR) { |
|
|
|
lval_del(a); |
|
|
|
return lval_err("Function 'tail' passed incorrect types!"); |
|
|
|
} |
|
|
|
|
|
|
|
if (a->cell[0]->count == 0) { |
|
|
|
lval_del(a); |
|
|
|
return lval_err("Function 'tail' passed empty {}!"); |
|
|
|
} |
|
|
|
|
|
|
|
/* Otherwise take first argument */ |
|
|
|
lval* v = lval_take(a, 0); |
|
|
|
|
|
|
|
/* Delete first element and return */ |
|
|
|
lval_del(lval_pop(v, 0)); |
|
|
|
|
|
|
|
return v; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
long min(long x, long y) { |
|
|
|
if (x <= y) { |
|
|
|
return x; |
|
|
@ -433,7 +493,7 @@ long max(long x, long y) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char const *argv[]) { |
|
|
|
int main() { |
|
|
|
|
|
|
|
/* Create some parsers */ |
|
|
|
mpc_parser_t* Number = mpc_new("number"); |
|
|
@ -477,7 +537,7 @@ int main(int argc, char const *argv[]) { |
|
|
|
} |
|
|
|
|
|
|
|
/* Undefine and delete our parsers */ |
|
|
|
mpc_cleanup(5, Number, Symbol, Sexpr, Qexpr, Expr, Lispy); |
|
|
|
mpc_cleanup(6, Number, Symbol, Sexpr, Qexpr, Expr, Lispy); |
|
|
|
|
|
|
|
return 0; |
|
|
|
} |