From 0771dd0e1a143c17920d65d5de8d010aa433ce1c Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Fri, 6 Jan 2023 18:53:42 -0800 Subject: add comments --- src/include/nxspp.h | 4 ++++ src/lexer.c | 10 ++++++---- src/main.c | 7 +++++++ src/visitor.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 src/include/nxspp.h (limited to 'src') 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)) diff --git a/src/main.c b/src/main.c index 8db3f77..bbe725c 100644 --- a/src/main.c +++ b/src/main.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); } } -- cgit