aboutsummaryrefslogtreecommitdiff
path: root/src/tokenizer.c
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-03-03 13:18:08 +0100
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commitb3b0c807a75c4f854495b547d8e00a598979cbf6 (patch)
treee42017c2f5b87a939d103f48d5f90dd93193ebcc /src/tokenizer.c
parent4c5ffb35d50d514e3df4788e7cf38245c0127883 (diff)
Add arithmetic (binop) parsing
Diffstat (limited to 'src/tokenizer.c')
-rw-r--r--src/tokenizer.c42
1 files changed, 37 insertions, 5 deletions
diff --git a/src/tokenizer.c b/src/tokenizer.c
index afaeb8e..152aec4 100644
--- a/src/tokenizer.c
+++ b/src/tokenizer.c
@@ -116,6 +116,8 @@ static CHECK_RESULT int string_to_float_unchecked(BufferView str, f64 *result) {
return 0;
}
+#define SET_BINOP(_binop_type) do { *token = TOK_BINOP; self->value.binop_type = (_binop_type); } while(0)
+
static CHECK_RESULT int tokenizer_next(Tokenizer *self, Token *token);
static CHECK_RESULT int __tokenizer_next(Tokenizer *self, Token *token) {
@@ -182,7 +184,6 @@ static CHECK_RESULT int __tokenizer_next(Tokenizer *self, Token *token) {
self->value.string.size = string_end - self->index;
self->index = string_end + 1;
*token = TOK_STRING;
- return TOKENIZER_OK;
} else if(isDigit(c)) {
int number_start;
int dot_index;
@@ -225,10 +226,21 @@ static CHECK_RESULT int __tokenizer_next(Tokenizer *self, Token *token) {
self->number_is_integer = bool_false;
}
*token = TOK_NUMBER;
- return TOKENIZER_OK;
} else if(c == '.') {
++self->index;
- *token = TOK_DOT;
+ SET_BINOP(BINOP_DOT);
+ } else if(c == '+') {
+ ++self->index;
+ SET_BINOP(BINOP_ADD);
+ } else if(c == '-') {
+ ++self->index;
+ SET_BINOP(BINOP_SUB);
+ } else if(c == '*') {
+ ++self->index;
+ SET_BINOP(BINOP_MUL);
+ } else if(c == '/') {
+ ++self->index;
+ SET_BINOP(BINOP_DIV);
} else if(c == '=') {
++self->index;
*token = TOK_EQUALS;
@@ -250,6 +262,9 @@ static CHECK_RESULT int __tokenizer_next(Tokenizer *self, Token *token) {
} else if(c == ';') {
++self->index;
*token = TOK_SEMICOLON;
+ } else if(c == ':') {
+ ++self->index;
+ *token = TOK_COLON;
} else if(c == '@') {
const char *err_msg;
++self->index;
@@ -313,6 +328,19 @@ static usize strlen(const char *str) {
return len;
}
+/*
+static const char* binop_to_string(BinopType binop_type) {
+ switch(binop_type) {
+ case BINOP_DOT: return ".";
+ case BINOP_ADD: return "+";
+ case BINOP_SUB: return "-";
+ case BINOP_MUL: return "*";
+ case BINOP_DIV: return "/";
+ }
+ assert(bool_false && "binop_to_string not implemented for binop_type");
+}
+*/
+
static BufferView tokenizer_expected_token_as_string(Token token) {
const char *str;
switch(token) {
@@ -361,12 +389,16 @@ static BufferView tokenizer_expected_token_as_string(Token token) {
case TOK_NUMBER:
str = "number";
break;
- case TOK_DOT:
- str = ".";
+ case TOK_BINOP:
+ /* TODO: binop_to_string */
+ str = "binop";
break;
case TOK_SEMICOLON:
str = ";";
break;
+ case TOK_COLON:
+ str = ":";
+ break;
default:
str = "Unknown token";
break;