feat: implement AST walker

This commit is contained in:
samsonium
2026-10-10 02:40:53 +03:00
parent 2fabd0ec2d
commit e979a22739
9 changed files with 469 additions and 17 deletions
+9
View File
@@ -0,0 +1,9 @@
#ifndef FZL_BUILTINS_H
#define FZL_BUILTINS_H
typedef enum
{
FZL_BUILTIN_PRINTLINE // Print message in arg with newline at end
} fzl_builtin_t;
#endif // FZL_BUILTINS_H
+12
View File
@@ -0,0 +1,12 @@
#ifndef FZL_OPCODES_H
#define FZL_OPCODES_H
// Available opcodes
typedef enum
{
FZL_OPCODE_LOADCONST, // Load const into T (temp) reg
FZL_OPCODE_PUSHARG, // Push argument from T into args stack
FZL_OPCODE_CALL_BUILTIN // Call builtin function
} fzl_opcode_t;
#endif // FZL_OPCODES_H
+34
View File
@@ -0,0 +1,34 @@
#ifndef FZL_WALKER_H
#define FZL_WALKER_H
#include <common/fzl_opcodes.h>
#include <compiler/fzl_parser.h>
#include <compiler/utils/fzl_const_pool.h>
#include <stddef.h>
// Walker descriptor
typedef struct
{
fzl_node_t *root;
fzl_const_hash_pool_t *consts;
uint64_t *ops;
size_t op_count;
size_t op_remainder;
} fzl_walker_t;
// Allocate new walker
fzl_walker_t *fzl_mkwalker(fzl_node_t *root);
// Walk throught the entire AST and build:
// 1. Constants table
// 2. Operations
int fzl_walk(fzl_walker_t *walker);
// Get last error message
char *fzl_walker_geterror();
// Free walker
void fzl_freewalker(fzl_walker_t *walker);
#endif // FZL_WALKER_H
+64
View File
@@ -0,0 +1,64 @@
#ifndef FZL_CONST_SET_H
#define FZL_CONST_SET_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
// Constant types
typedef enum
{
FZL_CONSTTYPE_STRING
} fzl_consttype_t;
// Constant descriptor
typedef struct
{
fzl_consttype_t type;
union
{
struct
{
const char *chars;
size_t length;
} string;
} as;
} fzl_const_t;
typedef struct
{
size_t pool_index;
} fzl_const_hash_slot_t;
// Constant pool descriptor
typedef struct
{
fzl_const_t *array;
size_t size;
size_t capacity;
fzl_const_hash_slot_t *slots;
size_t slots_count;
size_t slots_capacity;
} fzl_const_hash_pool_t;
// Compare two constants for equality
bool fzl_consts_equal(fzl_const_t a, fzl_const_t b);
// Get hash of specified constant
static uint32_t fzl_hash_const(fzl_const_t c);
// Allocate constants hash pool
fzl_const_hash_pool_t *fzl_mkconstpool();
// Resize the hash table
static void fzl_consts_pool_resize_hash(fzl_const_hash_pool_t *pool);
// Push constant. Returns unique index
size_t fzl_consts_pool_add(fzl_const_hash_pool_t *pool, fzl_const_t c);
// Free constants hash pool
void fzl_consts_pool_free(fzl_const_hash_pool_t *pool);
#endif // FZL_CONST_SET_H