Skip to content
Snippets Groups Projects
ast_interpreter.h 3.09 KiB
Newer Older
  • Learn to ignore specific revisions
  • #include "parser.h"
    
    typedef enum {
      VALUE_INTEGER,
      VALUE_BOOLEAN,
    
    Michal Štěpánek's avatar
    Michal Štěpánek committed
      VALUE_NULL,
    
      VALUE_FUNCTION,
      VALUE_ARRAY,
    
      VALUE_OBJECT,
      VALUE_INVALID
    
    } ValueType;
    
    typedef struct Heap {
      u8 * begin;
      u8 * next;
      u8 * end;
    } Heap;
    
    
    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;
    
    typedef struct Value {
      ValueType kind;
      union {
        i32 integer;
        bool boolean;
    
    Michal Štěpánek's avatar
    Michal Štěpánek committed
        AstFunction * function;
    
        void * address;
    
    typedef struct SimpleEntry {
      Str name;
    
    } SimpleEntry;
    
    
    typedef struct EnvironmentEntry {
      struct EnvironmentEntry * next;
      Str name;
    
    } 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_base ( Value * object );
    
    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 );