From c620d528fb9d9efbac559002d23857623e71df05 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Fri, 6 Jan 2023 20:48:56 -0800 Subject: whoops, fixed concat. Include in the making --- src/include/nxspp.h | 8 ++++++++ src/nxspp.c | 24 ++++++++++++++++++++++++ src/parser.c | 8 ++++++++ src/visitor.c | 18 +++++++++++++++++- 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/nxspp.c (limited to 'src') 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 +#include +#include + +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"); */ -- cgit