added additon to the language. but in a shit way. will generalize it later

This commit is contained in:
2025-07-28 17:30:45 +03:00
parent 9874f19ec6
commit 30c95ef81d

44
lexer.c
View File

@@ -19,6 +19,11 @@ typedef struct{
float myfloat; float myfloat;
} mfloat; } mfloat;
int str_to_int(char *strint){
int new_int = atoi(strint);
return new_int;
}
typedef enum{ typedef enum{
TOKEN_PLUS, TOKEN_PLUS,
@@ -51,6 +56,8 @@ typedef struct{
symbols previous_token; symbols previous_token;
} Token; } Token;
// since I now have tokenize all I dont really need previous_token. I can just ast walk it without each individual token carrying all data
typedef struct{ typedef struct{
Token* unit; Token* unit;
size_t size; size_t size;
@@ -93,10 +100,12 @@ typedef struct{
// Lexer // Lexer
void lexer_new(char *content, size_t content_len){ void lexer_new(char *content, size_t content_len){
(void) content;
(void) content_len;
} }
// Token // Token
void lexer_next(Lexer *mylexer){ void lexer_next(Lexer *mylexer){
(void) mylexer;
} }
// will implement a stack for arithmetic later. do I want a compiler or interpreter? since this is a learning experience im gonna do the easier thing first // will implement a stack for arithmetic later. do I want a compiler or interpreter? since this is a learning experience im gonna do the easier thing first
@@ -271,7 +280,28 @@ void main2() {
int main() { void astparser(const char* input){
TokenArr stack = tokenize_all(input);
size_t j=0;
for (size_t i=0; i < stack.size; ++i){
Token stack_save = stack.unit[i];
//printf("current token: %s\nCurrent i: %d\n\n", stack_save.text, i);
if (stack_save.behaviour == BHV_STACK){
assert(i < stack.size-1);
assert(i > 0);
assert(stack.unit[i+1].type == TOKEN_INTEGER);
assert(stack.unit[i-1].type == TOKEN_INTEGER);
printf("%d\n", str_to_int(stack.unit[i+1].text) + str_to_int(stack.unit[i-1].text));
// may switch to atoi later even here.
}
}
}
int main4() {
char* input = "print(5) hello"; char* input = "print(5) hello";
printf("input: %s\n\n", input); printf("input: %s\n\n", input);
@@ -295,3 +325,13 @@ int main() {
free(arr.unit); free(arr.unit);
return 0; return 0;
} }
int main(){
char* input = "1+2";
printf("input: %s\n\n", input);
astparser(input);
}