diff --git a/Makefile b/Makefile index e95bc05..a5dd923 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,10 @@ $(BINARY): $(CFILES) $(LEXER_C) $(LEXER_H) mkdir -p bin gcc -O3 -o $(BINARY) $(CFILES) -lm -Wall -Wextra -Wno-unused-function +debug: $(CFILES) $(LEXER_C) $(LEXER_H) + mkdir -p bin + gcc -g -O0 -o $(BINARY) $(CFILES) -lm -Wall -Wextra -Wno-unused-function + clean: rm -rf bin rm -f $(LEXER_C) $(LEXER_H) \ No newline at end of file diff --git a/src/lexer/lex.l b/src/lexer/lex.l index 166168d..5285df5 100644 --- a/src/lexer/lex.l +++ b/src/lexer/lex.l @@ -1,9 +1,14 @@ +%option reentrant + %{ #include "token.h" -int current_line = 1; -int current_column = 1; +#include "lexer.h" +#define GET_STATE LexerState *state = (LexerState *)yyget_extra(yyscanner); +#define GET_ADD_COLUMN COLUMN_NO += yyleng; +#define LINE_NO yylineno+1 +#define COLUMN_NO state->current_column -int yywrap() { +int yywrap(void *) { return 1; } %} @@ -11,37 +16,43 @@ int yywrap() { %% \"(\\[a-z\"'`]|[^\\"])*\" { - add_token(TOKEN_STRING, yytext, current_line, current_column); - current_column += yyleng; + GET_STATE + add_token(TOKEN_STRING, yytext, LINE_NO, COLUMN_NO); + GET_ADD_COLUMN } [0-9]+ { - add_token(TOKEN_NUMBER, yytext, current_line, current_column); - current_column += yyleng; + GET_STATE + add_token(TOKEN_NUMBER, yytext, LINE_NO, COLUMN_NO); + GET_ADD_COLUMN } [a-zA-Z_][a-zA-Z0-9_]* { - add_token(TOKEN_IDENTIFIER, yytext, current_line, current_column); - current_column += yyleng; + GET_STATE + add_token(TOKEN_IDENTIFIER, yytext, LINE_NO, COLUMN_NO); + GET_ADD_COLUMN } "." { - add_token(TOKEN_DOT, yytext, current_line, current_column); - current_column += yyleng; + GET_STATE + add_token(TOKEN_DOT, yytext, LINE_NO, COLUMN_NO); + GET_ADD_COLUMN } \n { - add_token(TOKEN_NEW_LINE, yytext, current_line, current_column); - current_line++; - current_column = 1; + GET_STATE + add_token(TOKEN_NEW_LINE, yytext, LINE_NO, COLUMN_NO); + COLUMN_NO = 1; } [ \t]+ { - current_column += yyleng; // Advance column for whitespace + GET_STATE + GET_ADD_COLUMN // Advance column for whitespace } -. { - fprintf(stderr, "Error: Unexpected character '%c' at line %d\n", *yytext, yylineno); +. { + GET_STATE + fprintf(stderr, "Error in file %s on line %d column %d: unexpected character '%s'\n", state->filename, LINE_NO, COLUMN_NO, yytext); exit(1); } %% \ No newline at end of file diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 38bc85c..58eef98 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -1,16 +1,27 @@ #include "lex.yy.h" #include "token.h" +#include "lexer.h" int lexer() { - const char *input = "term.log\n"; + yyscan_t scanner; + LexerState state = { "file1.src", 1 }; + const char *input = "let x = 10"; + yylex_init(&scanner); - void* buffer = yy_scan_string(input); - yy_switch_to_buffer(buffer); - yylex(); // This fills the token array - yy_delete_buffer(buffer); + // Set the extra data *before* scanning + yyset_extra(&state, scanner); + void* buffer = yy_scan_string(input, scanner); + yy_switch_to_buffer(buffer, scanner); + + yylex(scanner); // This fills the token array + + yy_delete_buffer(buffer, scanner); + yylex_destroy(scanner); + + // print tokens etc. for (int i = 0; i < token_count; i++) { printf("Token(type=%d, value='%s')\n", tokens[i].type, tokens[i].value); } diff --git a/src/lexer/lexer.h b/src/lexer/lexer.h index c03af10..5646f0e 100644 --- a/src/lexer/lexer.h +++ b/src/lexer/lexer.h @@ -1 +1,7 @@ -int lexer(); \ No newline at end of file +typedef struct { + const char *filename; + int current_column; + // add more fields as needed +} LexerState; + +int lexer(); diff --git a/src/main.c b/src/main.c index 7e0f830..4705d7e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,9 +1,4 @@ -#include "string/string.h" #include "number/number.h" - -#include -#include -#include #include "lexer/lexer.h" void initialize() {