From 702bb6f85087cf06fd1261daa0828f718efd5509 Mon Sep 17 00:00:00 2001 From: VegOwOtenks Date: Sun, 12 Jan 2025 17:40:09 +0100 Subject: [PATCH] Integrated with newest TracingHeap, refactoring for functions --- src/heap.h | 9 ++++++++ src/interpreter.c | 3 ++- src/main.c | 52 +--------------------------------------------- src/tokenizer.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++ src/tokenizer.h | 2 ++ 5 files changed, 67 insertions(+), 52 deletions(-) create mode 100644 src/heap.h diff --git a/src/heap.h b/src/heap.h new file mode 100644 index 0000000..e871960 --- /dev/null +++ b/src/heap.h @@ -0,0 +1,9 @@ +#ifndef FLUP_HEAP_H +#define FLUP_HEAP_H + +enum HeapDataKind { + HEAPDATAKIND_OBJECT, + HEAPDATAKIND_OBJECTTYPE, +}; + +#endif diff --git a/src/interpreter.c b/src/interpreter.c index 022bfc5..cc3cada 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -2,6 +2,7 @@ #include "builtin-functions.h" #include "object-type.h" #include "error-message.h" +#include "heap.h" #include #include @@ -306,7 +307,7 @@ int Interpreter_GetParenthesizedRange(Interpreter* self, CallFrame* top_frame, s Object* _Interpreter_MakeObject(Interpreter* self, ObjectType* otype) { size_t object_size = Object_AllocationSizeForType(otype); - Object* new_object = TracingHeap_Allocate(&self->heap, object_size); + Object* new_object = TracingHeap_Allocate(&self->heap, object_size, HEAPDATAKIND_OBJECT); return new_object; } diff --git a/src/main.c b/src/main.c index 8f48405..832ed77 100644 --- a/src/main.c +++ b/src/main.c @@ -26,63 +26,13 @@ #include "interpreter.h" #include "tokenizer.h" -//Debug -//Temp - -void tokencontent_tostring(Token* token) { - switch (token->type) { - case TOKENTYPE_INTEGER: - printf("%" PRIi64, token->get.integer); - break; - case TOKENTYPE_DOUBLE: - printf("%f", token->get.decimal); - break; - case TOKENTYPE_IDENTIFIER: - case TOKENTYPE_COMMENT: - case TOKENTYPE_STRING: - case TOKENTYPE_ERROR: - printf("%s", token->get.identifier); - break; - case TOKENTYPE_NONE: - case TOKENTYPE_LEFT_BRACE: - case TOKENTYPE_RIGHT_BRACE: - case TOKENTYPE_AMPERSAND: - case TOKENTYPE_PLUS: - case TOKENTYPE_MINUS: - case TOKENTYPE_MULTIPLY: - case TOKENTYPE_DIVIDE: - case TOKENTYPE_PIPE: - case TOKENTYPE_ARROW: - case TOKENTYPE_COLON: - case TOKENTYPE_SEMICOLON: - case TOKENTYPE_EQUALITY: - case TOKENTYPE_INEQUALITY: - case TOKENTYPE_LESSTHAN: - case TOKENTYPE_LESSEQUAL: - case TOKENTYPE_GREATERTHAN: - case TOKENTYPE_GREATEREQUAL: - case TOKENTYPE_COMMA: - case TOKENTYPE_BIND: - case TOKENTYPE_AS: - case TOKENTYPE_LEFT_PAREN: - case TOKENTYPE_RIGHT_PAREN: - break; - } -} - -void print_token(Token* token) { - printf("Token: type: %s, TokenContent: ", TokenType_ToString(token->type)); - tokencontent_tostring(token); - printf(" , contex: row: %i, col: %i\n", token->context.row, token->context.col); -} int tokenize_all(StringView source, DynamicArray* a) { TokenContext context = {.row = 1, .col = 1}; Token t; while ((t = Tokenizer_NextToken(&source, &context)).type != TOKENTYPE_NONE) { - //Debug - print_token(&t); + // Debug Token_Print(&t); int append_code = DynamicArray_Append(a, &t); if (append_code) return append_code; if (t.type == TOKENTYPE_ERROR) break; diff --git a/src/tokenizer.c b/src/tokenizer.c index db4a535..b6b3e0a 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -1,6 +1,8 @@ #include "tokenizer.h" #include #include +#include +#include static StringView StringViewOfNumberTillNextNonDigit(StringView* source) { StringView stringViewOfNumber = StringView_Slice(*source, 0, 0); @@ -310,3 +312,54 @@ const char* TokenType_ToString(enum TokenType type) return "INVALID"; } + +void _TokenContent_Print(Token* token) +{ + switch (token->type) { + case TOKENTYPE_INTEGER: + printf("%" PRIi64, token->get.integer); + break; + case TOKENTYPE_DOUBLE: + printf("%f", token->get.decimal); + break; + case TOKENTYPE_IDENTIFIER: + case TOKENTYPE_COMMENT: + case TOKENTYPE_STRING: + case TOKENTYPE_ERROR: + printf("%s", token->get.identifier.source); + break; + case TOKENTYPE_NONE: + case TOKENTYPE_LEFT_BRACE: + case TOKENTYPE_RIGHT_BRACE: + case TOKENTYPE_AMPERSAND: + case TOKENTYPE_PLUS: + case TOKENTYPE_MINUS: + case TOKENTYPE_MULTIPLY: + case TOKENTYPE_DIVIDE: + case TOKENTYPE_PIPE: + case TOKENTYPE_ARROW: + case TOKENTYPE_COLON: + case TOKENTYPE_SEMICOLON: + case TOKENTYPE_EQUALITY: + case TOKENTYPE_INEQUALITY: + case TOKENTYPE_LESSTHAN: + case TOKENTYPE_LESSEQUAL: + case TOKENTYPE_GREATERTHAN: + case TOKENTYPE_GREATEREQUAL: + case TOKENTYPE_COMMA: + case TOKENTYPE_BIND: + case TOKENTYPE_AS: + case TOKENTYPE_LEFT_PAREN: + case TOKENTYPE_RIGHT_PAREN: + break; + } +} + +void Token_Print(Token* token) +{ + printf("Token: type: %s, TokenContent: ", TokenType_ToString(token->type)); + _TokenContent_Print(token); + printf(" , contex: row: %i, col: %i\n", token->context.row, token->context.col); +} + + diff --git a/src/tokenizer.h b/src/tokenizer.h index bb05337..c05fceb 100644 --- a/src/tokenizer.h +++ b/src/tokenizer.h @@ -60,4 +60,6 @@ typedef struct Token_s { Token Tokenizer_NextToken(StringView* source, TokenContext* context); const char* TokenType_ToString(enum TokenType type); +void Token_Print(Token* token); + #endif //header guard