#ifndef AMALGAM_AST_H #define AMALGAM_AST_H #include "defs.h" #include "std/defs.h" #include "std/buffer_view.h" #include "std/buffer.h" #include "std/misc.h" #include "std/scoped_allocator.h" #include "std/hash_map.h" #include "binop_type.h" #include "ssa/ssa.h" #include /* General error */ #define AST_ERR -1 #define AST_ERR_DEF_DUP -20 typedef struct FunctionCall FunctionCall; typedef struct StructDecl StructDecl; typedef struct StructField StructField; typedef struct LhsExpr LhsExpr; typedef struct Import Import; typedef struct String String; typedef struct Variable Variable; typedef struct Number Number; typedef struct Binop Binop; typedef union { void *data; FunctionDecl *func_decl; FunctionCall *func_call; StructDecl *struct_decl; StructField *struct_field; LhsExpr *lhs_expr; Import *import; String *string; Number *number; Variable *variable; Binop *binop; } AstValue; typedef enum { AST_FUNCTION_DECL, AST_FUNCTION_CALL, AST_STRUCT_DECL, AST_STRUCT_FIELD, AST_LHS, AST_IMPORT, AST_STRING, AST_NUMBER, AST_VARIABLE, AST_BINOP } AstType; typedef enum { AST_NOT_RESOLVED, AST_RESOLVING, AST_RESOLVED, AST_SSA_RESOLVED } AstResolveStatus; typedef struct { LhsExpr *type; AstResolveStatus status; } AstResolveData; typedef struct { AstValue value; AstType type; AstResolveData resolve_data; SsaRegister ssa_reg; } Ast; struct Scope { Buffer/**/ ast_objects; HashMap/*(key=BufferView, value=Ast*)*/ named_objects; Scope *parent; /* Is null unless the scope is a file scope, in which case this is the parser that owns the scope */ Parser *parser; }; struct FileScopeReference { Parser *parser; Buffer canonical_path; }; struct Variable { BufferView name; }; struct FunctionDecl { Scope body; SsaFuncIndex ssa_func_index; }; struct FunctionCall { Variable func; Buffer/**/ args; }; struct StructDecl { Scope body; }; struct StructField { BufferView name; Variable type; }; struct LhsExpr { bool is_pub; bool is_const; BufferView var_name; Variable type; Ast *rhs_expr; /* TODO: Find a better way to store this. This most likely will use too much memory. */ amal_mutex *mutex; }; struct Import { BufferView path; FileScopeReference *file_scope; }; struct String { BufferView str; }; struct Number { union { i64 integer; f64 floating; } value; bool is_integer; BufferView code_ref; }; struct Binop { Ast *lhs; Ast *rhs; BinopType type; /* Is the binop already ordered - no need to reorder it */ bool grouped; }; typedef struct { jmp_buf env; amal_compiler *compiler; Scope *scope; } AstCompilerContext; CHECK_RESULT int ast_create(ScopedAllocator *allocator, void *value, AstType type, Ast **result); BufferView ast_get_name(Ast *self); CHECK_RESULT int funcdecl_init(FunctionDecl *self, Scope *parent, ScopedAllocator *allocator); CHECK_RESULT int funccall_init(FunctionCall *self, BufferView name, ScopedAllocator *allocator); CHECK_RESULT int structdecl_init(StructDecl *self, Scope *parent, ScopedAllocator *allocator); void structfield_init(StructField *self, BufferView name, BufferView type_name); CHECK_RESULT int lhsexpr_init(LhsExpr *self, bool is_pub, bool is_const, BufferView var_name, ScopedAllocator *allocator); void import_init(Import *self, BufferView path); CHECK_RESULT int string_init(String *self, BufferView str); void number_init(Number *self, i64 value, bool is_integer, BufferView code_ref); void variable_init(Variable *self, BufferView name); void binop_init(Binop *self); CHECK_RESULT int scope_init(Scope *self, Scope *parent, ScopedAllocator *allocator); CHECK_RESULT int file_scope_reference_init(FileScopeReference *self, BufferView canonical_path, ScopedAllocator *allocator); CHECK_RESULT int scope_add_child(Scope *self, Ast *child); /* longjump to compiler env on failure */ void scope_resolve(Scope *self, AstCompilerContext *context); #endif