feat: implement AST walker
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user