Skip to content
Snippets Groups Projects
bc_interpreter.h 1.62 KiB
Newer Older
#pragma once
#include "parser.h"
#include "heap.h"

#define OPERAND_STACK_INIT_CAPACITY 512
#define FRAME_STACK_CAPACITY 512

typedef struct Class {
  u16 member_count;
  u16 * members;
} Class;

typedef struct Constant {
  ConstantType kind;
  union {
    i32 integer;
    bool boolean;
    Str string;
    ConstantFunction function;
  };
} Constant;

typedef struct Program {
  u16 constant_count;
  Constant * constant_pool;
  u16 entry_point;
typedef struct OperandStack {
  Value ** elements;
  size_t size;
  size_t capacity;
} OperandStack;

void ostack_init ( OperandStack * stack );
Value * ostack_pop ( OperandStack * stack  );
Value * ostack_top ( OperandStack * stack  );
void ostack_push ( OperandStack * stack , Value * value );
void ostack_destroy ( OperandStack * stack );

typedef struct Frame {
  struct Frame * prev;
  size_t return_ip;
  Value * locals [];
} Frame;

typedef struct VM {
  Program program;
  Heap heap;
  size_t instruction_pointer;
} VM;

void vm_init ( VM * vm, size_t heap_size );
void vm_destroy ( VM * vm );

Value * alloc_constant ( VM * vm, u16 index );
ObjectValue * create_object ( VM * vm, Class * class );

void print_operand_value ( Value * value );
void print_call ( VM * vm, u16 index, u8 arguments );

void read_header ( Str input, size_t * pos );
void read_constant_pool ( Program * program, Str input, size_t * pos );
void parse_program ( Program * program, Str input );
void bc_function_call ( VM * vm, u8 arguments, bool is_function, Str * name );