add identifier to parser

This commit is contained in:
2025-06-01 02:28:27 +01:00
parent b7e9493171
commit d2518afb8e
20 changed files with 733 additions and 251 deletions

View File

@@ -1,30 +1,27 @@
#include "lex.yy.h"
#include "lexer.h"
#include "lex.yy.h"
void lexer(LexerState state) {
yyscan_t scanner;
yyscan_t scanner;
yylex_init(&scanner);
yylex_init(&scanner);
yyset_extra(&state, scanner);
yyset_extra(&state, scanner);
yyset_in(state.file, scanner);
yyset_in(state.file, scanner);
int token;
while ((token = yylex(scanner)) != 0) {
Token * token_struct = create_token(
token,
yyget_lineno(scanner),
state.current_column+1,
yyget_text(scanner)
);
darray_push(state.tokens, token_struct);
free(token_struct);
if (token == TOKEN_NEW_LINE) {
state.current_column = 0;
} else {
state.current_column += yyget_leng(scanner);
}
int token;
while ((token = yylex(scanner)) != 0) {
Token *token_struct =
create_token(token, yyget_lineno(scanner), state.current_column + 1,
yyget_text(scanner));
darray_push(state.tokens, token_struct);
free(token_struct);
if (token == TOKEN_NEW_LINE) {
state.current_column = 0;
} else {
state.current_column += yyget_leng(scanner);
}
yylex_destroy(scanner);
}
yylex_destroy(scanner);
}

View File

@@ -1,19 +1,18 @@
#ifndef LEXER_H
#define LEXER_H
#include "token.h"
#include "../dynamic_array/darray.h"
#include "token.h"
#include <stdio.h>
typedef struct {
const char *path;
FILE *file;
int current_column;
DArray* tokens;
// add more fields as needed
const char *path;
FILE *file;
int current_column;
DArray *tokens;
// add more fields as needed
} LexerState;
void lexer(LexerState state);
#endif // LEXER_H

View File

@@ -1,17 +1,17 @@
#include "token.h"
#include <stdlib.h>
#include "../string/string.h"
#include <stdlib.h>
Token *create_token(TokenType type, int line, int column, char *value) {
Token * token = malloc(sizeof(Token));
token->type = type;
token->line=line;
token->column=column;
token->value=cloneString(value);
return token;
Token *token = malloc(sizeof(Token));
token->type = type;
token->line = line;
token->column = column;
token->value = cloneString(value);
return token;
}
void free_token(void * ptr) {
Token* token = ptr;
free(token->value);
void free_token(void *ptr) {
Token *token = ptr;
free(token->value);
}

View File

@@ -20,15 +20,15 @@ typedef enum {
TOKEN_ASSIGN_CARET,
// Operators
TOKEN_AND, // &&
TOKEN_OR, // ||
TOKEN_NOT_IN, // not in
TOKEN_LE, // <=
TOKEN_GE, // >=
TOKEN_LT, // <
TOKEN_GT, // >
TOKEN_NE, // !=
TOKEN_EQ, // ==
TOKEN_AND, // &&
TOKEN_OR, // ||
TOKEN_NOT_IN, // not in
TOKEN_LE, // <=
TOKEN_GE, // >=
TOKEN_LT, // <
TOKEN_GT, // >
TOKEN_NE, // !=
TOKEN_EQ, // ==
TOKEN_PLUS, // +
TOKEN_MINUS, // -
TOKEN_MODULO, // %