aboutsummaryrefslogtreecommitdiff
path: root/asm/lexer.c
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-11-11 10:16:06 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-11-11 10:16:37 +0000
commitc9f684cc7d145f334fd1dd40957e05c52133cbc4 (patch)
tree1c3ac2d9cdd1e5e5aff641cbdc6ea16a2c7b79f3 /asm/lexer.c
parentbd6fb54e3116b102670c980851af56461771545d (diff)
downloadovm-c9f684cc7d145f334fd1dd40957e05c52133cbc4.tar.gz
ovm-c9f684cc7d145f334fd1dd40957e05c52133cbc4.tar.bz2
ovm-c9f684cc7d145f334fd1dd40957e05c52133cbc4.zip
Added string literals in tokeniser
Doesn't do much, invalid for most operations.
Diffstat (limited to 'asm/lexer.c')
-rw-r--r--asm/lexer.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/asm/lexer.c b/asm/lexer.c
index 49756f5..5218d16 100644
--- a/asm/lexer.c
+++ b/asm/lexer.c
@@ -34,6 +34,8 @@ const char *token_type_as_cstr(token_type_t type)
return "GLOBAL";
case TOKEN_STAR:
return "STAR";
+ case TOKEN_LITERAL_STRING:
+ return "LITERAL_STRING";
case TOKEN_LITERAL_NUMBER:
return "LITERAL_NUMBER";
case TOKEN_LITERAL_CHAR:
@@ -461,6 +463,24 @@ token_t tokenise_char_literal(buffer_t *buffer, size_t *column)
return token;
}
+token_t tokenise_string_literal(buffer_t *buffer, size_t *column)
+{
+ ++buffer->used;
+ size_t string_size;
+ for (string_size = 0; string_size + buffer->used < buffer->available &&
+ buffer->data[buffer->used + string_size] != '\"';
+ ++string_size)
+ continue;
+ token_t t = {.type = TOKEN_LITERAL_STRING,
+ .column = *column,
+ .str = malloc(string_size + 1),
+ .str_size = string_size};
+ memcpy(t.str, buffer->data + (buffer->used - string_size), string_size);
+ t.str[string_size] = '\0';
+ *column += string_size + 1;
+ return t;
+}
+
lerr_t tokenise_buffer(buffer_t *buffer, token_stream_t *tokens_ptr)
{
size_t column = 0, line = 1;
@@ -507,6 +527,8 @@ lerr_t tokenise_buffer(buffer_t *buffer, token_stream_t *tokens_ptr)
t.str[0] = '\0';
++buffer->used;
}
+ else if (c == '\"')
+ t = tokenise_string_literal(buffer, &column);
else if (isdigit(c) || (space_left(buffer) > 1 && c == '-' &&
isdigit(buffer->data[buffer->used + 1])))
t = tokenise_number_literal(buffer, &column);