Newer
Older
#include "parser.h"
typedef enum {
VALUE_INTEGER,
VALUE_BOOLEAN,
} ValueType;
typedef struct Heap {
u8 * begin;
u8 * next;
u8 * end;
} Heap;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
typedef struct Value {
ValueType kind;
} Value;
typedef struct Object {
struct Value * extends;
size_t member_cnt;
SimpleEntry members [];
} Object;
typedef struct Array {
i32 length;
Value * members [];
} Array;
typedef struct NullValue {
Value kind;
} NullValue;
typedef struct IntValue {
Value kind;
i32 integer;
} IntValue;
typedef struct BoolValue {
Value kind;
bool boolean;
} BoolValue;
typedef struct FunctionValue {
Value kind;
AstFunction * function;
} FunctionValue;
typedef struct ObjectValue {
Value kind;
struct Value * extends;
size_t member_cnt;
SimpleEntry members [];
} ObjectValue;
typedef struct ArrayValue {
Value kind;
Value * elements [];
} ArrayValue;
/*
typedef struct Value {
ValueType kind;
union {
i32 integer;
bool boolean;
Value * 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_alligned ( Heap * heap, size_t len, size_t align );
void * heap_alloc ( Heap * heap, size_t len );
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 );
NullValue * make_null ( ASTInterpreterState * state );
IntValue * make_int ( ASTInterpreterState * state, i32 val );
BoolValue * make_bool ( ASTInterpreterState * state, bool val );
FunctionValue * make_function ( ASTInterpreterState * state, AstFunction * function );
ArrayValue * make_array ( ASTInterpreterState * state, size_t len );
ObjectValue * make_object ( ASTInterpreterState * state, size_t member_cnt );
Value * get_object_field (Value * object, Str name );
Value * try_operator ( ASTInterpreterState * state, Value * object, Value ** arguments, size_t argc, Str * name );
Value * function_call ( ASTInterpreterState * state, Value * callee, bool is_function, Ast ** arguments, size_t argc, Str * name );
void print_value ( Value * value );
void fml_print ( Str format, Value ** args, size_t argc );
Value * evaluate ( ASTInterpreterState * state, Ast * ast );
int compare_entry ( const void * a, const void * b );