diff options
author | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2024-09-18 22:13:57 +0330 |
---|---|---|
committer | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2024-09-18 22:13:57 +0330 |
commit | b6c2872fd2b4441e4c98b365e7401c5848d58154 (patch) | |
tree | a945d8abe499423b6f51a704689b440bb0b4f7e2 /src | |
parent | 7b735783ed9711d47411794d7d2051ffd3ade646 (diff) |
added nested block comments
added reminder to better implement parse order struct
Diffstat (limited to 'src')
-rw-r--r-- | src/compiler/lexer/lexer.c | 45 | ||||
-rw-r--r-- | src/compiler/parser/parser.h | 2 |
2 files changed, 39 insertions, 8 deletions
diff --git a/src/compiler/lexer/lexer.c b/src/compiler/lexer/lexer.c index 5c7c256..c216298 100644 --- a/src/compiler/lexer/lexer.c +++ b/src/compiler/lexer/lexer.c @@ -90,14 +90,45 @@ Nodes lexer(char const *restrict str) { } else if (follow == '*') { push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, str, i, TOKEN_NONE); - for (i += 2; str[i] != '\0' && str[i + 1] != '\0' && - (str[i] != '*' || str[i + 1] != '/'); - ++i); - if (str[i] == '\0' || str[i + 1] == '\0') { - perror("expected multi line comment to end\n"); - exit(1); + int in = 1; + for (i += 2;; ++i) { + switch (str[i]) { + case '\0': + fprintf(stderr, + "expected multi line comment to end at compiler line %d " + "and in=%d\n", + __LINE__, in); + exit(1); + case '*': + ++i; + if (str[i] == '/') { + --in; + if (in == 0) { + goto END_OF_BLOCK_COMMENT_LOOP; + } + } else if (str[i] == '\0') { + fprintf(stderr, + "expected multi line comment to end at compiler line " + "%d and in=%d\n", + __LINE__, in); + exit(1); + } + break; + case '/': + ++i; + if (str[i] == '*') { + ++in; + } else if (str[i] == '\0') { + fprintf(stderr, + "expected multi line comment to end at compiler line " + "%d and in=%d\n", + __LINE__, in); + exit(1); + } + break; + } } - i += 1; + END_OF_BLOCK_COMMENT_LOOP: push_if_not_empty(&nodes, &nodes_size, &nodes_inserted, &node, str, i, TOKEN_NONE); if (str[i] == '\0') { diff --git a/src/compiler/parser/parser.h b/src/compiler/parser/parser.h index 510e4e9..006fc59 100644 --- a/src/compiler/parser/parser.h +++ b/src/compiler/parser/parser.h @@ -16,7 +16,7 @@ extern const char *PARSED_TOKEN_STRINGS[]; typedef struct ParseOrder { bool ltr; size_t size; - Token tokens[10]; + Token tokens[10]; // TODO: change this } ParseOrder; typedef struct ScopeMetadata { |