From b43fd46260b2fc87fc64f2293355b5301c4f2cb6 Mon Sep 17 00:00:00 2001 From: Jacob Signorovitch Date: Sat, 9 Nov 2024 04:37:56 -0500 Subject: [PATCH] Changes were made. --- .clangd | 2 ++ src/lexer.c | 39 ++++++++++++++++++++------------------- 2 files changed, 22 insertions(+), 19 deletions(-) create mode 100644 .clangd diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..8f7929a --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Add: [-xc] diff --git a/src/lexer.c b/src/lexer.c index 8f09e9e..0a20f69 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -1,10 +1,10 @@ #include +#include #include #include -#include -#include "include/lexer.h" #include "include/dstr.h" +#include "include/lexer.h" #include "include/token.h" #include "include/util.h" @@ -26,7 +26,8 @@ void lexer_init(char* src) { void lexer_destroy() { // Does not free lexer->src. - for (int i = 0; i < thelexer->ntokens; i++) token_destroy(thelexer->tokens[i]); + for (int i = 0; i < thelexer->ntokens; i++) + token_destroy(thelexer->tokens[i]); } void lexer_lex() { @@ -86,8 +87,8 @@ void lexer_do_call() { // Where the call string starts. char* start = thelexer->cchar; - for (callln = 0; - *thelexer->cchar && (!isdigit(*thelexer->cchar) && !isspace(*thelexer->cchar)); + for (callln = 0; *thelexer->cchar && + (!isdigit(*thelexer->cchar) && !isspace(*thelexer->cchar)); callln++) lexer_inc(); @@ -100,9 +101,7 @@ void lexer_do_call() { thelexer->state = LEXER_STATE_CONFUSED; } -void lexer_inc() { - thelexer->cchar += sizeof(char); -} +void lexer_inc() { thelexer->cchar += sizeof(char); } void lexer_add_token(Token* token) { assert(thelexer->ntokens < TOKENS_MAX); @@ -121,13 +120,14 @@ void lexer_print_i(int ilvl) { INDENT_BEGIN(ilvl); INDENT_TITLE("Lexer", thelexer); INDENT_FIELD_NONL_START("state") - lexerstate_print_raw(); + lexerstate_print_raw(); INDENT_FIELD_NONL_END INDENT_FIELD("srcln", "%ld", thelexer->srcln); INDENT_FIELD_NL("src", "\"%s\"", thelexer->src); INDENT_FIELD("cchar", "'%c'", *thelexer->cchar); INDENT_FIELD("ntokens", "%ld", thelexer->ntokens); - INDENT_FIELD_LIST("tokens", thelexer->tokens, thelexer->ntokens, token_print_i); + INDENT_FIELD_LIST("tokens", thelexer->tokens, thelexer->ntokens, + token_print_i); } void lexerstate_print_raw() { @@ -140,31 +140,32 @@ void lexerstate_print_raw() { #include "../build/grammars/grammar.tab.h" +extern YYSTYPE yylval; + int yylex(void) { if (*thelexer->cchar == '\0') return YYEOF; switch (*thelexer->cchar) { - case ' ': - case '\t': - thelexer->cchar++; + case ' ': + case '\t': thelexer->cchar++; } // Assign & consume current character. int c = *thelexer->cchar++; switch (c) { - case '+': - return PLUS; + case '+': return PLUS; + default: return CALL; } if (isdigit(c)) { - int value = c - '0'; // Start with the first digit + int value = c - '0'; // Start with the first digit while (isdigit(*thelexer->cchar)) { - value = value * 10 + (*thelexer->cchar - '0'); // Accumulate value + value = value * 10 + (*thelexer->cchar - '0'); // Accumulate value thelexer++; } - yylval.intval = value; // Set the token value - return NUM; // Return the INTEGER token type + yylval.intval = value; // Set the token value + return NUM; // Return the INTEGER token type } fprintf(stderr, "Unexpected character: %c\n", c);