diff options
-rw-r--r-- | doc/fib.nxs | 4 | ||||
-rw-r--r-- | doc/main.nxs | 3 | ||||
-rw-r--r-- | src/include/nxspp.h | 4 | ||||
-rw-r--r-- | src/lexer.c | 10 | ||||
-rw-r--r-- | src/main.c | 7 | ||||
-rw-r--r-- | src/visitor.c | 28 |
6 files changed, 51 insertions, 5 deletions
diff --git a/doc/fib.nxs b/doc/fib.nxs new file mode 100644 index 0000000..130f99d --- /dev/null +++ b/doc/fib.nxs @@ -0,0 +1,4 @@ +;; Author: Andrei S +(bind fib (lambda (x) ;; xth fib number +(if (< x 2) x (+ (fib (- x 1)) (fib (- x 2)))))) +(fib 7) diff --git a/doc/main.nxs b/doc/main.nxs index 274067c..212f098 100644 --- a/doc/main.nxs +++ b/doc/main.nxs @@ -1,5 +1,6 @@ + (bind factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) -(* (factorial 4) 24) +(+ (factorial 5) 10) "hello world!" diff --git a/src/include/nxspp.h b/src/include/nxspp.h new file mode 100644 index 0000000..d60832f --- /dev/null +++ b/src/include/nxspp.h @@ -0,0 +1,4 @@ +#ifndef NXSPP_H +#define NXSPP_H + +#endif diff --git a/src/lexer.c b/src/lexer.c index db4c44b..29c6542 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -123,18 +123,20 @@ static token_t *lexer_move_with(lexer_t *lexer, token_t *token) { } token_t *lexer_collect_next(lexer_t *lexer) { +start: if (lexer->c == '\0' || lexer->c == EOF) { lexer->finished = true; return NULL; } - if (isspace(lexer->c)) { lexer_ignore_whitespace(lexer); + goto start; } - if (lexer->c == '\0' || lexer->c == EOF) { - lexer->finished = true; - return NULL; + if (lexer->c == ';') { + lexer_skip_comment(lexer); + goto start; } + if (isdigit(lexer->c)) return lexer_collect_num(lexer); else if (is_valid_id_char(lexer->c)) @@ -131,6 +131,7 @@ int main(int argc, char **argv) { printf("TOO FEW ARGUMENTS.\n"); exit(1); } + char *filename = argv[1]; char *buffer = 0; long length; @@ -148,6 +149,12 @@ int main(int argc, char **argv) { } if (buffer) { + /* lexer_t *lexer = init_lexer(buffer); */ + /* token_t *t = lexer_collect_next(lexer); */ + /* while (t != NULL) { */ + /* printf("%d: %s\n", t->type, t->value); */ + /* t = lexer_collect_next(lexer); */ + /* } */ /* printf("%s", buffer); */ lexer_t *lexer = init_lexer(buffer); parser_t *parser = init_parser(lexer); diff --git a/src/visitor.c b/src/visitor.c index ba29f3f..14ea404 100644 --- a/src/visitor.c +++ b/src/visitor.c @@ -397,6 +397,34 @@ ast_t *eval_list(visitor_t *v, ast_t *e) { ast_t *arg2 = eval_expr(v, args->cdr->car); ast_t *ret = init_ast_pair(arg1, arg2); return ret; + } else if (strcmp(function->string_value, "eq") == 0) { + if (cmp != 2) + eval_error(v, e); + + ast_t *arg1 = eval_expr(v, args->car); + ast_t *arg2 = eval_expr(v, args->cdr->car); + + if ((arg1->type == AST_STRING) && (arg2->type == AST_STRING)) { + return init_ast_bool(strcmp(arg1->string_value, arg2->string_value) == + 0); + } else + eval_error(v, e); + } else if (strcmp(function->string_value, "concat") == 0) { + if (cmp != 2) + eval_error(v, e); + + ast_t *arg1 = eval_expr(v, args->car); + ast_t *arg2 = eval_expr(v, args->cdr->car); + + if ((arg1->type == AST_STRING) && (arg2->type == AST_STRING)) { + int string_len1 = strlen(arg1->string_value); + int string_len2 = strlen(arg2->string_value); + char *ret = malloc((string_len1 + string_len2) * sizeof(char)); + strcat(ret, arg1->string_value); + strcat(ret, arg2->string_value); + return ret; + } else + eval_error(v, e); } } |