aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-03-09 11:37:23 +0100
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commit255aa20f6d68a71c9eedd47998480a8b14a3be36 (patch)
treeca4803e1b1fcff8f1e3fa1af460bde7e0feb9c97
parent6421dd86886280e0a6b79228c0269304704be5f5 (diff)
Add single line, multiline comments
-rw-r--r--src/ast.c1
-rw-r--r--src/tokenizer.c57
-rw-r--r--tests/main.amal5
3 files changed, 56 insertions, 7 deletions
diff --git a/src/ast.c b/src/ast.c
index 2154531..5314e90 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -115,6 +115,7 @@ void scope_resolve(Scope *self) {
static void lhs_resolve(LhsExpr *self) {
amal_log_debug("Lhs resolve var name: %.*s", self->var_name.size, self->var_name.data);
+ /*ast_resolve(&self->rhs_expr);*/
}
void ast_resolve(Ast *self) {
diff --git a/src/tokenizer.c b/src/tokenizer.c
index 9077bb9..b2bd6c5 100644
--- a/src/tokenizer.c
+++ b/src/tokenizer.c
@@ -20,6 +20,11 @@ static int isAlphaDigit(int c) {
return isAlpha(c) || isDigit(c);
}
+static int tokenizer_get_start_of_line_from_index(Tokenizer *self, int index);
+static int tokenizer_get_end_of_line_from_index(Tokenizer *self, int index);
+/* Returns -1 if end of multiline comment was not found */
+static int tokenizer_get_end_of_multiline_comment(Tokenizer *self, int index);
+
int tokenizer_init(Tokenizer *self, ScopedAllocator *allocator, BufferView code, BufferView code_name) {
assert(code.size <= INT_MAX);
self->code = code;
@@ -242,6 +247,23 @@ static CHECK_RESULT int __tokenizer_next(Tokenizer *self, Token *token) {
SET_BINOP(BINOP_MUL);
} else if(c == '/') {
++self->index;
+ if(self->index < (int)self->code.size) {
+ c = tokenizer_get_char(self);
+ /* Single line comment */
+ if(c == '/') {
+ ++self->index;
+ self->index = tokenizer_get_end_of_line_from_index(self, self->index);
+ return __tokenizer_next(self, token);
+ } else if(c == '*') {
+ ++self->index;
+ self->index = tokenizer_get_end_of_multiline_comment(self, self->index);
+ if(self->index == -1) {
+ tokenizer_print_error(self, "End of multiline comment not found");
+ return TOKENIZER_ERR;
+ }
+ return __tokenizer_next(self, token);
+ }
+ }
SET_BINOP(BINOP_DIV);
} else if(c == '=') {
++self->index;
@@ -503,10 +525,10 @@ int tokenizer_consume_if(Tokenizer *self, Token expected_token, bool *result) {
return TOKENIZER_OK;
}
-static int tokenizer_get_start_of_line_from_index(Tokenizer *self, int index) {
- int c;
+int tokenizer_get_start_of_line_from_index(Tokenizer *self, int index) {
+ char c;
while(index >= 0) {
- c = self->code.data[(usize)index];
+ c = self->code.data[index];
if(c == '\n' || c == '\r') {
return index + 1;
}
@@ -515,10 +537,10 @@ static int tokenizer_get_start_of_line_from_index(Tokenizer *self, int index) {
return 0;
}
-static int tokenizer_get_end_of_line_from_index(Tokenizer *self, int index) {
- int c;
+int tokenizer_get_end_of_line_from_index(Tokenizer *self, int index) {
+ char c;
while(index < (int)self->code.size) {
- c = self->code.data[(usize)index];
+ c = self->code.data[index];
if(c == '\n' || c == '\r')
break;
++index;
@@ -526,6 +548,29 @@ static int tokenizer_get_end_of_line_from_index(Tokenizer *self, int index) {
return index;
}
+int tokenizer_get_end_of_multiline_comment(Tokenizer *self, int index) {
+ char c;
+ int comment_count;
+ comment_count = 1;
+
+ while(index < (int)self->code.size) {
+ c = self->code.data[index];
+ if(c == '*') {
+ if(index - 1 >= 0 && self->code.data[index - 1] == '/') {
+ ++comment_count;
+ }
+ } else if(c == '/') {
+ if(index - 1 >= 0 && self->code.data[index - 1] == '*') {
+ --comment_count;
+ if(comment_count == 0)
+ return index + 1;
+ }
+ }
+ ++index;
+ }
+ return -1;
+}
+
void tokenizer_print_error(Tokenizer *self, const char *fmt, ...) {
va_list args;
int line_start;
diff --git a/tests/main.amal b/tests/main.amal
index 786724c..cd0fc60 100644
--- a/tests/main.amal
+++ b/tests/main.amal
@@ -10,7 +10,10 @@ const main = fn {
const num2 = 23232;
const num3 = num1 + num2 * 30;
const num4 = (num1 + num2) * num3 * ((34 + 32) / 234.345);
- const num4 = 23;
+ //const num4 = 23;
+ /*
+ episfjpseifipesf
+ */
}
const print = fn {