summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lexer.c20
-rw-r--r--src/main.c50
-rw-r--r--src/parser.c1
3 files changed, 57 insertions, 14 deletions
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;
+ }
}
diff --git a/src/main.c b/src/main.c
index 73885ad..8db3f77 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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;
}