Integrated with newest TracingHeap, refactoring for functions

This commit is contained in:
VegOwOtenks 2025-01-12 17:40:09 +01:00
parent dfd2f81f3d
commit 702bb6f850
5 changed files with 67 additions and 52 deletions

9
src/heap.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef FLUP_HEAP_H
#define FLUP_HEAP_H
enum HeapDataKind {
HEAPDATAKIND_OBJECT,
HEAPDATAKIND_OBJECTTYPE,
};
#endif

View file

@ -2,6 +2,7 @@
#include "builtin-functions.h" #include "builtin-functions.h"
#include "object-type.h" #include "object-type.h"
#include "error-message.h" #include "error-message.h"
#include "heap.h"
#include <stdio.h> #include <stdio.h>
#include <stdalign.h> #include <stdalign.h>
@ -306,7 +307,7 @@ int Interpreter_GetParenthesizedRange(Interpreter* self, CallFrame* top_frame, s
Object* _Interpreter_MakeObject(Interpreter* self, ObjectType* otype) Object* _Interpreter_MakeObject(Interpreter* self, ObjectType* otype)
{ {
size_t object_size = Object_AllocationSizeForType(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; return new_object;
} }

View file

@ -26,63 +26,13 @@
#include "interpreter.h" #include "interpreter.h"
#include "tokenizer.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) int tokenize_all(StringView source, DynamicArray* a)
{ {
TokenContext context = {.row = 1, .col = 1}; TokenContext context = {.row = 1, .col = 1};
Token t; Token t;
while ((t = Tokenizer_NextToken(&source, &context)).type != TOKENTYPE_NONE) { while ((t = Tokenizer_NextToken(&source, &context)).type != TOKENTYPE_NONE) {
//Debug // Debug Token_Print(&t);
print_token(&t);
int append_code = DynamicArray_Append(a, &t); int append_code = DynamicArray_Append(a, &t);
if (append_code) return append_code; if (append_code) return append_code;
if (t.type == TOKENTYPE_ERROR) break; if (t.type == TOKENTYPE_ERROR) break;

View file

@ -1,6 +1,8 @@
#include "tokenizer.h" #include "tokenizer.h"
#include <ctype.h> #include <ctype.h>
#include <math.h> #include <math.h>
#include <stdio.h>
#include <inttypes.h>
static StringView StringViewOfNumberTillNextNonDigit(StringView* source) { static StringView StringViewOfNumberTillNextNonDigit(StringView* source) {
StringView stringViewOfNumber = StringView_Slice(*source, 0, 0); StringView stringViewOfNumber = StringView_Slice(*source, 0, 0);
@ -310,3 +312,54 @@ const char* TokenType_ToString(enum TokenType type)
return "INVALID"; 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);
}

View file

@ -60,4 +60,6 @@ typedef struct Token_s {
Token Tokenizer_NextToken(StringView* source, TokenContext* context); Token Tokenizer_NextToken(StringView* source, TokenContext* context);
const char* TokenType_ToString(enum TokenType type); const char* TokenType_ToString(enum TokenType type);
void Token_Print(Token* token);
#endif //header guard #endif //header guard