Skip to content
Snippets Groups Projects
ast_interpreter.h 1.48 KiB
Newer Older
#include "parser.h"

typedef enum {
  VALUE_INTEGER,
  VALUE_BOOLEAN,
Michal Štěpánek's avatar
Michal Štěpánek committed
  VALUE_NULL,
  VALUE_FUNCTION
} ValueType;

typedef struct Heap {
  u8 * begin;
  u8 * next;
  u8 * end;
} Heap;

typedef struct Value {
  ValueType kind;
  union {
    i32 integer;
    bool boolean;
Michal Štěpánek's avatar
Michal Štěpánek committed
    AstFunction * function;
  };
} Value;

typedef struct EnvironmentEntry {
  struct EnvironmentEntry * next;
  Str name;
  Value value;
} EnvironmentEntry;

typedef struct Environment {
  struct Environment * prev;
  EnvironmentEntry * start;
} Environment;

typedef struct ASTInterpreterState {
  Heap * heap;
  Environment global_env;
  Environment * current_env;
} ASTInterpreterState;

bool value_to_bool ( Value value );

void env_push ( ASTInterpreterState * state );
void env_pop ( ASTInterpreterState * state );
Value env_get ( ASTInterpreterState * state, Str name );
void env_put ( ASTInterpreterState * state, Str name, Value value );
void env_def ( ASTInterpreterState * state, Str name, Value value );
void * heap_alloc ( Heap * heap, size_t len, size_t align );
void heap_init ( Heap * heap, size_t heap_size );
void heap_destroy ( Heap * heap );
void state_init ( ASTInterpreterState * state, Heap * heap );
void state_destroy ( ASTInterpreterState * state );

Michal Štěpánek's avatar
Michal Štěpánek committed
Value function_call ( ASTInterpreterState * state, Value function, bool is_function, Ast ** arguments, size_t argc );
void print_value ( Value value );
void fml_print ( Str format, Value * args, size_t argc );
Value evaluate ( ASTInterpreterState * state, Ast * ast );