aboutsummaryrefslogtreecommitdiff
path: root/src/lexer.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2024-08-30 17:01:28 +0300
committerkartofen <mladenovnasko0@gmail.com>2024-08-30 17:01:28 +0300
commite1ceef73192f0300ff9b10ba9a16475fbebeaa5f (patch)
tree0cb2bc5336a522b965c1d171b433044591721e20 /src/lexer.c
parentde3a062bfc206bf0373f96f4f6cc8c74ffcbab48 (diff)
proper repl, stylistic changes, removed trailing whitespace
Diffstat (limited to 'src/lexer.c')
-rw-r--r--src/lexer.c26
1 files changed, 16 insertions, 10 deletions
diff --git a/src/lexer.c b/src/lexer.c
index b546fda..93c7f44 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -7,7 +7,7 @@
#define CH(lexer) (lexer)->str[(lexer)->str_idx]
#define TOKEN_SEPARATOR_TABLE(X, l) \
- X((';' == CH(l)), CH(l) = '\0') \
+ X((';' == CH(l)), lexer_clear_line(l)) \
X(('(' == CH(l)), on_separator(l, TOKEN_LP)) \
X((')' == CH(l)), on_separator(l, TOKEN_RP)) \
X(('\''== CH(l)), on_separator(l, TOKEN_QUOTE)) \
@@ -78,7 +78,7 @@ lexer_t lexer_create(FILE *fp)
lexer->acc_idx = 0;
memset(lexer->acc, 0, sizeof(lexer->acc));
memset(lexer->str, 0, sizeof(lexer->str));
-
+
lexer->token.type = TOKEN_NONE;
return lexer;
@@ -90,12 +90,18 @@ void lexer_destroy(lexer_t lexer)
free(lexer);
}
+int lexer_clear_line(lexer_t lexer)
+{
+ lexer->str[lexer->str_idx] = '\0';
+ return 0;
+}
+
int lexer_token_next(lexer_t lexer, struct token *token)
{
if(lexer->acc_idx == 0 && lexer->acc[0] != '\0') {
memset(lexer->acc, 0, sizeof(lexer->acc));
}
-
+
while(lexer->token.type == TOKEN_NONE)
{
if(lexer->str[lexer->str_idx] == '\0') {
@@ -103,10 +109,10 @@ int lexer_token_next(lexer_t lexer, struct token *token)
lexer->str_idx = 0;
lexer->line++;
}
-
+
TOKEN_SEPARATOR_TABLE(CALLBACK, lexer) TABLE_END;
}
-
+
*token = lexer->token;
lexer->token.type = TOKEN_NONE;
return 0;
@@ -116,7 +122,7 @@ int token_value_string(struct token *token, size_t buf_sz, char *buf)
{
#define AS_STRING(ttype, ...) \
case ttype: return snprintf(buf, buf_sz, __VA_ARGS__);
-
+
switch(token->type) {
TOKEN_VALUE_STRING_TABLE(AS_STRING, token->value);
}
@@ -175,7 +181,7 @@ static int acc_add_char(lexer_t lexer, char ch)
if(lexer->acc_idx >= LEN(lexer->acc) - 1) {
return -ENAMETOOLONG;
}
-
+
lexer->acc[lexer->acc_idx++] = ch;
lexer->str_idx++;
@@ -183,10 +189,10 @@ static int acc_add_char(lexer_t lexer, char ch)
}
static int acc_empty(lexer_t lexer)
-{
+{
TOKEN_VALUE_TABLE(CALLBACK_BLIND, lexer) TABLE_END;
lexer->acc_idx = 0;
-
+
return 0;
}
@@ -210,6 +216,6 @@ static int is_special(char *str, enum token_type *type)
} else
TOKEN_SPECIALS_TABLE(IS_SPECIAL) TABLE_END;
-
+
return 0;
}