diff options
author | Preston Pan <preston@nullring.xyz> | 2023-01-04 12:03:26 -0800 |
---|---|---|
committer | Preston Pan <preston@nullring.xyz> | 2023-01-04 12:03:26 -0800 |
commit | 4fc423446c5f093483a4f20094904fa8b4ff88ba (patch) | |
tree | 54d07d80c6de6c661d8fa6397ccbdb3b9d97cbad | |
parent | 2fe28946e426e241e87e8381d7a62e73b9278385 (diff) |
parsing mostly works
-rw-r--r-- | Makefile | 11 | ||||
-rw-r--r-- | src/lexer.c | 2 | ||||
-rw-r--r-- | src/main.c | 36 | ||||
-rw-r--r-- | src/parser.c | 2 | ||||
-rw-r--r-- | src/print.c | 2 | ||||
-rw-r--r-- | src/visitor.c | 1 |
6 files changed, 43 insertions, 11 deletions
@@ -0,0 +1,11 @@ +# the compiler: gcc for C program, define as g++ for C++ +CC = gcc +# compiler flags: +# -g adds debugging information to the executable file +# -Wall turns on most, but not all, compiler warnings +CFLAGS = -g -Wall + +# the build target executable: +TARGET = myprog + +all: $(TARGET) diff --git a/src/lexer.c b/src/lexer.c index 938c4eb..97e6638 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -49,7 +49,7 @@ void lexer_skip_comment(lexer_t *lexer) { static bool is_valid_id_char(char c) { if (c == '(' || c == ')' || isdigit(c) || c == '"' || c == '\'' || c == '#' || - c == '.') + c == '.' || isspace(c)) return false; return true; } @@ -13,13 +13,15 @@ #include <string.h> int main(int argc, char **argv) { - /* Test Lexer */ - /* lexer_t *lexer = init_lexer("'(fasd \"aaaaaaaa\" 4)"); */ + /* TEST LEXER */ + /* lexer_t *lexer = init_lexer("'(fasd asdf)"); */ /* token_t *t = lexer_collect_next(lexer); */ /* while (t != NULL) { */ /* printf("%d: %s\n", t->type, t->value); */ /* t = lexer_collect_next(lexer); */ /* } */ + + /* TEST REPL POSSIBILITY */ /* printf("Welcome to the NXS REPL.\n"); */ /* char *buf = malloc(2); */ @@ -40,11 +42,31 @@ int main(int argc, char **argv) { /* printf("lmao\n"); */ /* } */ /* } */ - /* TEST PARSER, VISITOR, PRINTER */ - lexer_t *lexer = init_lexer("\"hello world\""); + + /* TEST PARSER, VISITOR, PRINTER (self evaluating types) */ + /* lexer_t *lexer = init_lexer("\"hello world\""); */ + /* parser_t *parser = init_parser(lexer); */ + /* visitor_t *visitor = init_visitor(parser); */ + /* ast_t *root = eval(visitor); */ + /* print_root(root); */ + + /* TEST PARSING LISTS */ + lexer_t *lexer = init_lexer("(hello world)"); parser_t *parser = init_parser(lexer); - visitor_t *visitor = init_visitor(parser); - ast_t *root = eval(visitor); - print_root(root); + ast_t *root = parse_all(parser); + ast_t *list = root->subnodes[0]; + ast_t *hello = list->car; + ast_t *world = list->cdr->car; + printf("%s\n", hello->string_value); + printf("%s\n", world->string_value); + + /* ast_t *e2 = list->cdr->car; */ + /* if (e2 == NULL) { */ + /* printf("something is wrong here...\n"); */ + /* exit(0); */ + /* } */ + + /* printf("%s\n", e2->string_value); */ + /* printf("number 3\n"); */ return 0; } diff --git a/src/parser.c b/src/parser.c index ac4d709..2f592b9 100644 --- a/src/parser.c +++ b/src/parser.c @@ -110,7 +110,6 @@ ast_t *parse_function_args(parser_t *parser) { } ast_t *parse_function(parser_t *parser) { - parser_move(parser); parser_eat(parser, TOKEN_LPAREN); /* TODO: actually write a helper function that also keeps track of the amount of arguments and checks that they are all identifiers.*/ @@ -166,7 +165,6 @@ ast_t *parse_list(parser_t *parser) { cur->cdr = init_ast_pair(NULL, NULL); cur = cur->cdr; first_entry = false; - parser_move(parser); current_token = parser->tokens[parser->i]; } parser_move(parser); diff --git a/src/print.c b/src/print.c index 49b6ec9..9339549 100644 --- a/src/print.c +++ b/src/print.c @@ -2,7 +2,7 @@ #include "./include/ast.h" #include <stdio.h> -void print_string(ast_t *str) { printf("=> %s\n", str->string_value); } +void print_string(ast_t *str) { printf("=> \"%s\"\n", str->string_value); } void print_int(ast_t *i) { printf("=> %d\n", i->int_value); } diff --git a/src/visitor.c b/src/visitor.c index 3c28245..a6fc040 100644 --- a/src/visitor.c +++ b/src/visitor.c @@ -41,6 +41,7 @@ bool is_built_in(ast_t *e) { strcmp(cmp, "cons") == 0) return true; + /* Comparison functions */ if (strcmp(cmp, "<") == 0 || strcmp(cmp, ">") == 0 || strcmp(cmp, "=") == 0 || strcmp(cmp, "<=") == 0 || strcmp(cmp, ">=") == 0 || strcmp(cmp, "eq") == 0) |