diff options
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | Makefile | 17 | ||||
-rw-r--r-- | doc/main.nxs | 2 | ||||
-rw-r--r-- | src/lexer.c | 20 | ||||
-rw-r--r-- | src/main.c | 50 | ||||
-rw-r--r-- | src/parser.c | 1 |
6 files changed, 72 insertions, 22 deletions
@@ -1 +1,3 @@ -src/a.out +nxs +diffs/** +hash_table_new.c @@ -1,11 +1,14 @@ -# 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 +nxs: + cc src/*.c $(CFLAGS) -o nxs -all: $(TARGET) +clean: + -rm nxs + +install: + cp nxs /usr/local/bin/ + +uninstall: + -rm /usr/local/bin/nxs diff --git a/doc/main.nxs b/doc/main.nxs index e09c304..e0839b5 100644 --- a/doc/main.nxs +++ b/doc/main.nxs @@ -1,2 +1,4 @@ (bind factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) + +(factorial 3) diff --git a/src/lexer.c b/src/lexer.c index 97e6638..db4c44b 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -38,8 +38,11 @@ void lexer_move(lexer_t *lexer) { } void lexer_ignore_whitespace(lexer_t *lexer) { - while (isspace(lexer->c)) + while (isspace(lexer->c)) { + if (lexer->c == '\0') + return; lexer_move(lexer); + } } void lexer_skip_comment(lexer_t *lexer) { @@ -120,13 +123,18 @@ static token_t *lexer_move_with(lexer_t *lexer, token_t *token) { } token_t *lexer_collect_next(lexer_t *lexer) { - if (lexer->c == '\0') { + if (lexer->c == '\0' || lexer->c == EOF) { lexer->finished = true; return NULL; } - if (isspace(lexer->c)) - lexer_ignore_whitespace(lexer); + if (isspace(lexer->c)) { + lexer_ignore_whitespace(lexer); + } + if (lexer->c == '\0' || lexer->c == EOF) { + lexer->finished = true; + return NULL; + } if (isdigit(lexer->c)) return lexer_collect_num(lexer); else if (is_valid_id_char(lexer->c)) @@ -147,6 +155,8 @@ token_t *lexer_collect_next(lexer_t *lexer) { else if (lexer->c == '.') return lexer_move_with( lexer, init_token(TOKEN_PERIOD, ".", lexer->row, lexer->col)); - else + else { + printf("returns null\n"); return NULL; + } } @@ -12,6 +12,9 @@ #include <stdlib.h> #include <string.h> +// Read the file into allocated memory. +// Return NULL on error. + int main(int argc, char **argv) { /* DONE: TEST LEXER */ /* lexer_t *lexer = init_lexer("'(fasd asdf)"); */ @@ -84,15 +87,16 @@ int main(int argc, char **argv) { /* ast_t *root = eval(visitor); */ /* print_root(root); */ - /* TODO: TEST RECURSION */ + /* DONE: TEST RECURSION */ /* char *expr = "(if (= 0 0) 1 2)"; */ - char *expr = "(bind factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n " - "1)))))) (factorial 3)"; - lexer_t *lexer = init_lexer(expr); - parser_t *parser = init_parser(lexer); - visitor_t *visitor = init_visitor(parser); - ast_t *root = eval(visitor); - print_root(root); + /* char *expr = "(bind factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- + * n " */ + /* "1)))))) (factorial 3)"; */ + /* lexer_t *lexer = init_lexer(expr); */ + /* parser_t *parser = init_parser(lexer); */ + /* visitor_t *visitor = init_visitor(parser); */ + /* ast_t *root = eval(visitor); */ + /* print_root(root); */ /* lexer_t *lexer = init_lexer("((lambda (x y) (+ x y)) (+ 3 4) 4) (+ 3 * 4)"); */ @@ -122,5 +126,35 @@ int main(int argc, char **argv) { /* lexer_reset(lexer, buf); */ /* } */ + /* DONE: BE ABLE TO READ IN FILE AS INPUT */ + if (argc < 2) { + printf("TOO FEW ARGUMENTS.\n"); + exit(1); + } + char *filename = argv[1]; + char *buffer = 0; + long length; + FILE *f = fopen(filename, "rb"); + + if (f) { + fseek(f, 0, SEEK_END); + length = ftell(f); + fseek(f, 0, SEEK_SET); + buffer = malloc(length); + if (buffer) { + fread(buffer, 1, length, f); + } + fclose(f); + } + + if (buffer) { + /* printf("%s", buffer); */ + lexer_t *lexer = init_lexer(buffer); + parser_t *parser = init_parser(lexer); + visitor_t *visitor = init_visitor(parser); + ast_t *root = eval(visitor); + print_root(root); + } + return 0; } diff --git a/src/parser.c b/src/parser.c index 0997f18..6a77e87 100644 --- a/src/parser.c +++ b/src/parser.c @@ -31,7 +31,6 @@ parser_t *init_parser(lexer_t *lexer) { if (t == NULL) break; } - p->size = size; return p; } |