#pragma once #include "parser.h" #include "heap.h" #include "string.h" #define INIT_STRING_LENGTH 512 #define MAX_SCOPE_VARIABLES 128 typedef struct String { u32 len; u32 capacity; u8 * str; } String; typedef struct BCFunction { u8 parameters; u16 locals; String bc; } BCFunction; typedef struct BCClass { u16 fields; u16 * indexes; } BCClass; typedef struct BCConstant { ConstantType kind; union { i32 integer; bool boolean; Str string; BCFunction function; BCClass class; }; } BCConstant; void string_init ( String * str ); void string_destroy ( String * str ); void string_write_byte ( String * str, const u8 data ); void string_write_u16 ( String * str, const u16 data ); void string_write_i32 ( String * str, const i32 data ); typedef struct Constants { BCConstant constants [ 256 * 256 ]; u16 constant_count; i32 null_pos; i32 bool_pos [2]; } Constants; typedef struct Globals { Str names [ 256 * 256 ]; u16 global_count; } Globals; typedef struct LocalScope { struct LocalScope * prev; u16 used_locals; // locals at use in this scope u16 local_count; // locals at use at this time Str locals []; } LocalScope; typedef struct BCCompilerState { Constants constants; Globals globals; LocalScope * scope; u16 fp; u16 ep; } BCCompilerState; BCConstant create_function ( Str name, u8 parameters ); LocalScope * create_function_scope ( u8 parameters ); u16 get_string_index ( BCCompilerState * state, Str str ); u16 get_local_index ( BCCompilerState * state, Str name ); void gen_bc_constant ( BCCompilerState * state, u16 index ); void insert_constant ( BCCompilerState * state, Constant constant ); void bc_state_init ( BCCompilerState * state ); void bc_state_destroy ( BCCompilerState * state ); void ast_to_bc ( BCCompilerState * state, Ast * ast ); void generate_bc ( Ast * ast );