Browse Source

Add lookup of builtins

T. Meissner 7 months 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) {
289 289
     }
290 290
 
291 291
     /* Call builtin with operator */
292
-    lval* result = builtin_op(v, f->sym);
292
+    lval* result = builtin(v, f->sym);
293 293
     lval_del(f);
294 294
     return result;
295 295
 
@@ -338,6 +338,27 @@ lval* lval_take(lval* v, size_t i) {
338 338
 }
339 339
 
340 340
 
341
+lval* builtin(lval* a, char* func) {
342
+
343
+    /* Check for builtin functions */
344
+    if (strcmp("list", func) == 0) {return builtin_list(a);}
345
+    if (strcmp("head", func) == 0) {return builtin_head(a);}
346
+    if (strcmp("tail", func) == 0) {return builtin_tail(a);}
347
+    if (strcmp("join", func) == 0) {return builtin_join(a);}
348
+    if (strcmp("eval", func) == 0) {return builtin_eval(a);}
349
+
350
+    /* Check for operators */
351
+    if (strstr("+-*/%^", func) ||
352
+        strcmp("min", func) == 0 || strcmp("max", func) == 0) {
353
+        return builtin_op(a, func);
354
+    }
355
+
356
+    lval_del(a);
357
+    return lval_err("Unknown function!");
358
+
359
+}
360
+
361
+
341 362
 lval* builtin_op(lval* a, char* op) {
342 363
 
343 364
     /* Ensure all arguments are numbers */

+ 1
- 0
chapter_10/lispy.h View File

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