implemented a lot of stuff

This commit is contained in:
2025-07-21 13:34:20 +03:00
parent 5182a631c1
commit da3367de3f

83
lexer.c
View File

@@ -24,14 +24,24 @@ typedef enum{
TOKEN_MINUS, TOKEN_MINUS,
TOKEN_INTEGER, TOKEN_INTEGER,
TOKEN_SPACE, TOKEN_SPACE,
TOKEN_STRING,
intdef, intdef,
TOKEN_UNKNOWN, TOKEN_UNKNOWN,
} symbols; } symbols;
typedef enum{
BHV_STACK,
BHV_UNDEFINED,
BHV_NUMBER,
BHV_STRING,
} symbol_bhv;
typedef struct{ typedef struct{
symbols type; symbols type;
char* text; char* text;
size_t text_len; size_t text_len;
symbol_bhv behaviour;
uint cursor_skip;
} Token; } Token;
typedef struct{ typedef struct{
@@ -53,8 +63,12 @@ void lexer_next(Lexer *mylexer){
Token read_from_tok(char* text, uint cursor){ Token read_from_tok(char* text, uint cursor){
Token mytoks; Token mytoks;
static char buf[64]; static char buf[64];
size_t i = 0; size_t i = 0;
mytoks.cursor_skip = 1;
if (isdigit(text[cursor])) { if (isdigit(text[cursor])) {
size_t start = cursor; size_t start = cursor;
while (isdigit(text[cursor])) { while (isdigit(text[cursor])) {
@@ -62,9 +76,23 @@ Token read_from_tok(char* text, uint cursor){
} }
buf[i] = '\0'; buf[i] = '\0';
mytoks.type = TOKEN_INTEGER; mytoks.type = TOKEN_INTEGER;
mytoks.behaviour = BHV_NUMBER;
mytoks.cursor_skip = cursor - start;
mytoks.text = buf;
mytoks.text_len = i;
} else if (isalpha(text[cursor])){
size_t start = cursor;
while (isalpha(text[cursor])) {
buf[i++] = text[cursor++];
}
buf[i] = '\0';
mytoks.type = TOKEN_STRING;
mytoks.behaviour = BHV_STRING;
mytoks.cursor_skip = cursor - start;
mytoks.text = buf; mytoks.text = buf;
mytoks.text_len = i; mytoks.text_len = i;
} }
else { else {
buf[0] = text[cursor]; buf[0] = text[cursor];
buf[1] = '\0'; buf[1] = '\0';
@@ -73,12 +101,14 @@ Token read_from_tok(char* text, uint cursor){
switch (text[cursor]){ switch (text[cursor]){
case '+': case '+':
mytoks.type = TOKEN_PLUS; mytoks.type = TOKEN_PLUS;
// asigning text is not really needed unless for debug // asigning text is not really needed unless for debug. could however be useful for codegen later.
mytoks.text = "+"; mytoks.text = "+";
break; mytoks.behaviour = BHV_STACK;
break;
case '-': case '-':
mytoks.type = TOKEN_MINUS; mytoks.type = TOKEN_MINUS;
mytoks.text = "-"; mytoks.text = "-";
mytoks.behaviour = BHV_STACK;
break; break;
case ' ': case ' ':
mytoks.type = TOKEN_SPACE; mytoks.type = TOKEN_SPACE;
@@ -86,6 +116,8 @@ Token read_from_tok(char* text, uint cursor){
break; break;
default: default:
mytoks.type = TOKEN_UNKNOWN; mytoks.type = TOKEN_UNKNOWN;
mytoks.behaviour = BHV_UNDEFINED;
} }
} }
return mytoks; return mytoks;
@@ -94,22 +126,45 @@ Token read_from_tok(char* text, uint cursor){
// Token* c // Token* c
void parser(Token mytok, char* input){
int length1 = strlen(input);
int i=0;
while (i < length1) {
mytok = read_from_tok(input, i);
printf("Text: %s\n", mytok.text);
printf("Behaviour: %d\n", mytok.behaviour);
if (mytok.behaviour == BHV_STACK){
printf("this is stack lil bro\n");
}
i++;
}
}
// operators accepted in int/digit or whatever type def only when they have a digit before AND after them // operators accepted in int/digit or whatever type def only when they have a digit before AND after them
/*
int main(){ int main(){
Token newtok; Token newtok;
char* input = "32323 + 232"; char* input = "8";
int length1 = strlen(input);
int i = 0; parser(newtok, input);
while (i < length1) { }
Token result = read_from_tok(input, i); */
printf("text: %s\ntype: %u\n\n", result.text, result.type);
if (result.type == TOKEN_INTEGER) { int main(){
i += result.text_len; // to skip the whole integer Token newtok;
} else { char* input = "32323 + Hello world";
i++; int length1 = strlen(input);
} int i = 0;
} while (i < length1) {
Token result = read_from_tok(input, i);
printf("text: %s\ntype: %u\n\n", result.text, result.type);
i += result.cursor_skip;
}
} }