Browse Source

Add lookup of builtins

master
T. Meissner 6 years ago
parent
commit
821bc6250b
2 changed files with 23 additions and 1 deletions
  1. +22
    -1
      chapter_10/lispy.c
  2. +1
    -0
      chapter_10/lispy.h

+ 22
- 1
chapter_10/lispy.c View File

@ -289,7 +289,7 @@ lval* lval_eval_sexpr(lval* v) {
}
/* Call builtin with operator */
lval* result = builtin_op(v, f->sym);
lval* result = builtin(v, f->sym);
lval_del(f);
return result;
@ -338,6 +338,27 @@ lval* lval_take(lval* v, size_t i) {
}
lval* builtin(lval* a, char* func) {
/* Check for builtin functions */
if (strcmp("list", func) == 0) {return builtin_list(a);}
if (strcmp("head", func) == 0) {return builtin_head(a);}
if (strcmp("tail", func) == 0) {return builtin_tail(a);}
if (strcmp("join", func) == 0) {return builtin_join(a);}
if (strcmp("eval", func) == 0) {return builtin_eval(a);}
/* Check for operators */
if (strstr("+-*/%^", func) ||
strcmp("min", func) == 0 || strcmp("max", func) == 0) {
return builtin_op(a, func);
}
lval_del(a);
return lval_err("Unknown function!");
}
lval* builtin_op(lval* a, char* op) {
/* Ensure all arguments are numbers */


+ 1
- 0
chapter_10/lispy.h View File

@ -70,6 +70,7 @@ void lval_println(lval* v);
/* language built-in operators and functions */
long min(long x, long y);
long max(long x, long y);
lval* builtin(lval* a, char* func);
lval* builtin_op(lval* a, char* op);
lval* builtin_head(lval* a);
lval* builtin_tail(lval* a);


Loading…
Cancel
Save