summaryrefslogtreecommitdiff
path: root/lisp.c
diff options
context:
space:
mode:
authorFelix Perktold <Felix.Perktold@student.uibk.ac.at>2025-12-20 21:59:28 +0100
committerFelix Perktold <Felix.Perktold@student.uibk.ac.at>2025-12-20 21:59:28 +0100
commit5eeb355805e0d5d91981dbe7eacb078f41da0437 (patch)
treed4d2a76e7988cf63aa9438d2b42d0a4a4656ee42 /lisp.c
init commit
Diffstat (limited to 'lisp.c')
-rw-r--r--lisp.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/lisp.c b/lisp.c
new file mode 100644
index 0000000..e6dc02e
--- /dev/null
+++ b/lisp.c
@@ -0,0 +1,95 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <readline/readline.h>
+#include <readline/history.h>
+#include "lisp.h"
+#include "lex.yy.h"
+#include "lisp.tab.h"
+
+value *make_int(int i) {
+ value *val = malloc(sizeof(value));
+ val->type = VALTYPE_INT;
+ val->as.i = i;
+ return val;
+}
+
+value *make_double(double d) {
+ value *val = malloc(sizeof(value));
+ val->type = VALTYPE_DOUBLE;
+ val->as.d = d;
+ return val;
+}
+
+value *make_symbol(const char *s) {
+ value *val = malloc(sizeof(value));
+ val->type = VALTYPE_SYMBOL;
+ val->as.sym = s;
+ return val;
+}
+
+value *make_string(const char *s) {
+ value *val = malloc(sizeof(value));
+ val->type = VALTYPE_STRING;
+ val->as.str = s;
+ return val;
+}
+
+value *make_nil() {
+ value *val = malloc(sizeof(value));
+ val->type = VALTYPE_NIL;
+ return val;
+}
+
+value *cons(value *car, value *cdr) {
+ value *val = malloc(sizeof(value));
+ val->type = VALTYPE_PAIR;
+ val->as.pair.car = car;
+ val->as.pair.cdr = cdr;
+ return val;
+}
+
+void *print_val(value *val){
+ char *str;
+ switch (val->type) {
+ case VALTYPE_INT:
+ printf("%d", val->as.i);
+ break;
+
+ case VALTYPE_DOUBLE:
+ printf("%f", val->as.d);
+ break;
+
+ case VALTYPE_SYMBOL:
+ printf("%s", val->as.sym);
+ break;
+
+ case VALTYPE_STRING:
+ printf("\"%s\"", val->as.str);
+ break;
+
+ case VALTYPE_NIL:
+ printf("()");
+ break;
+
+ case VALTYPE_PAIR:
+ printf("(");
+ print_val(val->as.pair.car);
+ printf(" . ");
+ print_val(val->as.pair.cdr);
+ printf(")");
+ default:
+ }
+
+ return val;
+}
+
+int main(void) {
+ char* line;
+ while ((line = readline("λ > ")) != NULL) {
+ if (*line) add_history(line);
+ yy_scan_string(line); // feed input to Flex/Bison
+ yyparse();
+ free(line);
+ }
+ return 0;
+}