This commit is contained in:
attilavs2 2025-03-05 16:04:20 +01:00
parent e0d78813d0
commit 5f2511e0f2
6 changed files with 121 additions and 11 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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;

47
src/parser.c Normal file
View file

@ -0,0 +1,47 @@
#include <stdio.h>
#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;
}

5
src/parser.h Normal file
View file

@ -0,0 +1,5 @@
#include "types.h"
#include "byte_defs.h"
#include "code_defs.h"
#pragma once

View file

@ -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
};