diff options
author | Preston Pan <preston@nullring.xyz> | 2023-01-06 20:48:56 -0800 |
---|---|---|
committer | Preston Pan <preston@nullring.xyz> | 2023-01-06 20:48:56 -0800 |
commit | c620d528fb9d9efbac559002d23857623e71df05 (patch) | |
tree | d8d6ee788cc4688a3c4a4401906fe3ce25307855 | |
parent | 0771dd0e1a143c17920d65d5de8d010aa433ce1c (diff) |
whoops, fixed concat. Include in the making
-rw-r--r-- | doc/fib.nxs | 6 | ||||
-rw-r--r-- | src/include/nxspp.h | 8 | ||||
-rw-r--r-- | src/nxspp.c | 24 | ||||
-rw-r--r-- | src/parser.c | 8 | ||||
-rw-r--r-- | src/visitor.c | 18 |
5 files changed, 62 insertions, 2 deletions
diff --git a/doc/fib.nxs b/doc/fib.nxs index 130f99d..e09e7a4 100644 --- a/doc/fib.nxs +++ b/doc/fib.nxs @@ -1,4 +1,8 @@ ;; Author: Andrei S (bind fib (lambda (x) ;; xth fib number -(if (< x 2) x (+ (fib (- x 1)) (fib (- x 2)))))) + (if (< x 2) x (+ (fib (- x 1)) (fib (- x 2)))))) + (fib 7) + +(bind fibseq (lambda (n) +(if (= 0 n) 0 (begin (+ n 1) (fib n))))) diff --git a/src/include/nxspp.h b/src/include/nxspp.h index d60832f..d88ab57 100644 --- a/src/include/nxspp.h +++ b/src/include/nxspp.h @@ -1,4 +1,12 @@ #ifndef NXSPP_H #define NXSPP_H +typedef struct { + char *source; + char c; + int i; +} npp_t; + +npp_t *init_npp(char *source); +char *process_string(npp_t *p); #endif diff --git a/src/nxspp.c b/src/nxspp.c new file mode 100644 index 0000000..7888cbb --- /dev/null +++ b/src/nxspp.c @@ -0,0 +1,24 @@ +#include "./include/nxspp.h" +#include "./include/macros.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +npp_t *init_npp(char *source) { + npp_t *n = malloc(sizeof(npp_t)); + if (n == NULL) + die("malloc on nxspp"); + n->source = source; + n->i = 0; + n->c = source[n->i]; + return n; +} + +char *nxspp_collect_id(char *source, int i) {} + +void npp_move(npp_t *p) { + if (p->c != '\0') { + p->i++; + p->c = p->source[p->i]; + } +} diff --git a/src/parser.c b/src/parser.c index 6a77e87..0a2b213 100644 --- a/src/parser.c +++ b/src/parser.c @@ -133,6 +133,8 @@ void parse_bind(parser_t *parser) { hash table transfers to visitor JIT */ if (expr == NULL) parser_error(parser); + if (expr->type == AST_ROOT) + parser_error(parser); hash_table_add(parser->symbol_table, name, expr); if (parser->tokens[parser->i]->type != TOKEN_RPAREN) parser_error(parser); @@ -140,6 +142,8 @@ void parse_bind(parser_t *parser) { parser_move(parser); } +ast_t *parse_include(parser_t *parser) { parser_eat(parser, TOKEN_STRING); } + ast_t *parse_list(parser_t *parser) { ast_t *car; ast_t *head = init_ast_pair(NULL, NULL); @@ -162,6 +166,8 @@ ast_t *parse_list(parser_t *parser) { car = parse_expr(parser); if (car == NULL) parser_error(parser); + if (car->type == AST_ROOT) + parser_error(parser); } cur->car = car; cur->cdr = init_ast_pair(NULL, NULL); @@ -179,6 +185,8 @@ ast_t *parse_quote(parser_t *parser) { ast_t *expr = parse_expr(parser); if (expr == NULL) parser_error(parser); + if (expr->type == AST_ROOT) + parser_error(parser); ast_t *ret = init_ast_pair( car, init_ast_pair( expr, init_ast_pair(NULL, NULL))); /* Converts ' to `quote` */ diff --git a/src/visitor.c b/src/visitor.c index 14ea404..25fb7cd 100644 --- a/src/visitor.c +++ b/src/visitor.c @@ -60,6 +60,10 @@ bool is_built_in(ast_t *e) { strcmp(cmp, "atof") == 0 || strcmp(cmp, "ftoa") == 0) return true; + /* I/O and other operating system stuff */ + if (strcmp(cmp, "exit") == 0 || strcmp(cmp, "readin") == 0 || + strcmp(cmp, "read") == 0) + return true; return false; } @@ -422,10 +426,22 @@ ast_t *eval_list(visitor_t *v, ast_t *e) { char *ret = malloc((string_len1 + string_len2) * sizeof(char)); strcat(ret, arg1->string_value); strcat(ret, arg2->string_value); - return ret; + return init_ast_string(ret); + } else + eval_error(v, e); + } else if (strcmp(function->string_value, "exit") == 0) { + if (cmp != 1) + eval_error(v, e); + + ast_t *arg1 = eval_expr(v, args->car); + + if (arg1->type == AST_INT) { + int code = arg1->int_value; + exit(code); } else eval_error(v, e); } + return NULL; } /* printf("debug 2\n"); */ |