From 5f2511e0f2833cf3ef39816053fe99147f5f05db Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Wed, 5 Mar 2025 16:04:20 +0100 Subject: [PATCH] Zeubi --- src/byte_defs.h | 26 ++++++++++++++++---------- src/code_defs.h | 42 ++++++++++++++++++++++++++++++++++++++++++ src/hash.h | 3 ++- src/parser.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/parser.h | 5 +++++ src/types.h | 9 +++++++++ 6 files changed, 121 insertions(+), 11 deletions(-) create mode 100644 src/parser.c create mode 100644 src/parser.h diff --git a/src/byte_defs.h b/src/byte_defs.h index 756c6b3..899f56c 100755 --- a/src/byte_defs.h +++ b/src/byte_defs.h @@ -37,14 +37,6 @@ enum OpTypes { OP_vpsh = 29 }; -enum ValTypes { - T_null = 0, - T_int = 1, - T_fix = 2, - T_float = 3, - T_str = 4, - T_fn = 5 -}; enum TypeFlags { F_int = 0x1, @@ -64,6 +56,21 @@ typedef struct { } OpDef; +typedef struct { + + uint is_array : 1; + uint type : 15; + +} PACKED Tag; + +typedef struct { + + Tag params[8]; + i32 n_param; + + +} FnSignature; + typedef struct { u8 type; // OpTypes @@ -106,8 +113,7 @@ typedef struct { ValueArray varray; }; - uint is_array : 1; - uint type : 15; + Tag tag; } PACKED Value; diff --git a/src/code_defs.h b/src/code_defs.h index db8c0fb..161b0f6 100755 --- a/src/code_defs.h +++ b/src/code_defs.h @@ -1,4 +1,46 @@ +#include "byte_defs.h" #include "types.h" +#include "hash.h" #pragma once +#define SYMBOL_MAP_S (1024*16) + +enum StatementTypes { + ST_None = 0, // Not processed yet + ST_Call = 1, // Any kind of function call + ST_Builtin = 2, // All special builtins (var, declarations, control flow...) + ST_Const = 3, // Constant + ST_Var = 4 // Variable +}; + +enum BuiltinStatements { + +}; + +struct Statement; + +typedef struct Statement { + + i32 type; + i32 is_const; // Statement is constant, != is a constant + struct Statement *children; + i32 child_n; + enum { + i32 fn_id; + i32 builtin_type; + Value cons; + } + +} Statement; + +// In practice, this is allocated in parse.c +typedef struct { + + i32 stack_size; + i32 curr_statement; + HashMap *symbol_map; + char **pos_stack; // Pointers to text pos of statements, freed after parsing + Statement statements[]; + +} ASTStack; diff --git a/src/hash.h b/src/hash.h index ab1c302..939cf04 100755 --- a/src/hash.h +++ b/src/hash.h @@ -21,9 +21,10 @@ typedef struct { MapItem *buffer; u32 *bit_free; // Bit map to track usage - i32 curr_len; + i32 curr_len; // In items i32 item_n; i32 curr_id; + i32 is_heap; // TODO } HashMap; diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..196cba2 --- /dev/null +++ b/src/parser.c @@ -0,0 +1,47 @@ +#include + +#include "types.h" +#include "byte_defs.h" +#include "code_defs.h" +#include "parser.h" +#include "hash.h" + +ASTStack *make_stack(){ + ASTStack *ret = malloc(sizeof(ASTStack)+sizeof(Statement)*2048); + if(!ret) + return NULL; + ret->pos_stack = malloc(sizeof(void*)*2048); + if(!ret->pos_stack) + return NULL; + ret->symbol_map = heap_hashmap(SYMBOL_MAP_S); + ret->stack_size = 2048; + return ret; +} + +i32 parenth_stack[256]; +i32 parenth_d; + +// Does the final transformation -> recursion end +int parse_statement_final(StatementStack *stack, Statement *parent, char *str){ + +} + +// Recursively extracts statements +int parse_statement(StatementStack *stack, Statement *parent, char *str){ + +} + +ASTStack *parse(char *text){ + ASTStack *stack = make_stack(); + if(!stack){ + printf("Couldn't allocate parse buffer !\n"); + return NULL; + } + + i32 ret = 0; + do { + ret = parse_statement(stack, NULL, text); + } while(!ret); + + return stack; +} diff --git a/src/parser.h b/src/parser.h new file mode 100644 index 0000000..0d0a58e --- /dev/null +++ b/src/parser.h @@ -0,0 +1,5 @@ +#include "types.h" +#include "byte_defs.h" +#include "code_defs.h" + +#pragma once diff --git a/src/types.h b/src/types.h index c6ebdd9..4b0a3ef 100755 --- a/src/types.h +++ b/src/types.h @@ -19,3 +19,12 @@ typedef unsigned int uint; #define PACKED __attribute__((__packed__)) #endif +enum ValTypes { + T_null = 0, + T_int = 1, + T_fix = 2, + T_float = 3, + T_str = 4, + T_fn = 5 +}; +