From: Brendan Hansen Date: Fri, 26 Feb 2021 16:19:33 +0000 (-0600) Subject: random little changes; starting package rewrite X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=f752cf160ceb9428588e8c3dafd8fe8b8e209377;p=onyx.git random little changes; starting package rewrite --- diff --git a/bin/onyx b/bin/onyx index eec96002..c2858465 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/docs/bugs b/docs/bugs index 44ea7228..463b007e 100644 --- a/docs/bugs +++ b/docs/bugs @@ -35,4 +35,24 @@ List of things to change: // x.x = 1 // x.y = -[ ] Anonymous struct, enum and array literals. +[ ] Add functionality to #solidify that resolves to a procedure given a set of types. For example: + overloaded :: proc { + (x: str) { println("String function!"); }, + (x: i32) { println("Int function!"); }, + (x: f32) { println("Float function!"); }, + (x: $T) { println("Other function!"); }, + (z: $T) { println("Other function ZZZ!"); }, + } + + int_function :: #solidify overloaded(i32); + int_function(); // Int function! + + other_function :: #solidify overloaded(range); + other_function(); // Other function! + + wont_work :: #solidify overloaded(y = i32); // This won't work because overloaded doesn't have a 'y' + will_work :: #solidify overloaded(z = range); // This will work because overloaded does have a 'z' + + Obviously this will also work on a non-overloaded, only polymorphic procedure. + +[X] Anonymous struct, enum and array literals. diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index e36cbf5b..5ef1184d 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -4,79 +4,85 @@ #include "onyxlex.h" #include "onyxtypes.h" -typedef struct AstNode AstNode; -typedef struct AstTyped AstTyped; - -typedef struct AstNamedValue AstNamedValue; -typedef struct AstBinaryOp AstBinaryOp; -typedef struct AstUnaryOp AstUnaryOp; -typedef struct AstNumLit AstNumLit; -typedef struct AstStrLit AstStrLit; -typedef struct AstLocal AstLocal; -typedef struct AstCall AstCall; -typedef struct AstArgument AstArgument; -typedef struct AstAddressOf AstAddressOf; -typedef struct AstDereference AstDereference; -typedef struct AstArrayAccess AstArrayAccess; -typedef struct AstFieldAccess AstFieldAccess; -typedef struct AstSizeOf AstSizeOf; -typedef struct AstAlignOf AstAlignOf; -typedef struct AstFileContents AstFileContents; -typedef struct AstStructLiteral AstStructLiteral; -typedef struct AstArrayLiteral AstArrayLiteral; -typedef struct AstRangeLiteral AstRangeLiteral; -typedef struct AstCompound AstCompound; - -typedef struct AstDirectiveSolidify AstDirectiveSolidify; -typedef struct AstStaticIf AstStaticIf; -typedef struct AstDirectiveError AstDirectiveError; - -typedef struct AstReturn AstReturn; -typedef struct AstJump AstJump; -typedef struct AstUse AstUse; - -typedef struct AstBlock AstBlock; -typedef struct AstIfWhile AstIfWhile; -typedef struct AstFor AstFor; -typedef struct AstDefer AstDefer; -typedef struct AstSwitchCase AstSwitchCase; -typedef struct AstSwitch AstSwitch; - -typedef struct AstType AstType; -typedef struct AstBasicType AstBasicType; -typedef struct AstPointerType AstPointerType; -typedef struct AstFunctionType AstFunctionType; -typedef struct AstArrayType AstArrayType; -typedef struct AstSliceType AstSliceType; -typedef struct AstDynArrType AstDynArrType; -typedef struct AstVarArgType AstVarArgType; -typedef struct AstStructType AstStructType; -typedef struct AstStructMember AstStructMember; -typedef struct AstPolyStructType AstPolyStructType; -typedef struct AstPolyStructParam AstPolyStructParam; -typedef struct AstPolyCallType AstPolyCallType; -typedef struct AstEnumType AstEnumType; -typedef struct AstEnumValue AstEnumValue; -typedef struct AstTypeAlias AstTypeAlias; -typedef struct AstTypeRawAlias AstTypeRawAlias; -typedef struct AstCompoundType AstCompoundType; - -typedef struct AstBinding AstBinding; -typedef struct AstMemRes AstMemRes; -typedef struct AstInclude AstInclude; -typedef struct AstUsePackage AstUsePackage; -typedef struct AstAlias AstAlias; -typedef struct AstGlobal AstGlobal; -typedef struct AstParam AstParam; -typedef struct AstFunction AstFunction; -typedef struct AstOverloadedFunction AstOverloadedFunction; - -typedef struct AstPolyParam AstPolyParam; -typedef struct AstPolySolution AstPolySolution; -typedef struct AstSolidifiedFunction AstSolidifiedFunction; -typedef struct AstPolyProc AstPolyProc; - -typedef struct AstPackage AstPackage; +#define AST_NODES \ + NODE(Node) \ + NODE(Typed) \ + \ + NODE(NamedValue) \ + NODE(BinaryOp) \ + NODE(UnaryOp) \ + NODE(NumLit) \ + NODE(StrLit) \ + NODE(Local) \ + NODE(Call) \ + NODE(Argument) \ + NODE(AddressOf) \ + NODE(Dereference) \ + NODE(ArrayAccess) \ + NODE(FieldAccess) \ + NODE(SizeOf) \ + NODE(AlignOf) \ + NODE(FileContents) \ + NODE(StructLiteral) \ + NODE(ArrayLiteral) \ + NODE(RangeLiteral) \ + NODE(Compound) \ + \ + NODE(DirectiveSolidify) \ + NODE(StaticIf) \ + NODE(DirectiveError) \ + \ + NODE(Return) \ + NODE(Jump) \ + NODE(Use) \ + \ + NODE(Block) \ + NODE(IfWhile) \ + NODE(For) \ + NODE(Defer) \ + NODE(SwitchCase) \ + NODE(Switch) \ + \ + NODE(Type) \ + NODE(BasicType) \ + NODE(PointerType) \ + NODE(FunctionType) \ + NODE(ArrayType) \ + NODE(SliceType) \ + NODE(DynArrType) \ + NODE(VarArgType) \ + NODE(StructType) \ + NODE(StructMember) \ + NODE(PolyStructType) \ + NODE(PolyStructParam) \ + NODE(PolyCallType) \ + NODE(EnumType) \ + NODE(EnumValue) \ + NODE(TypeAlias) \ + NODE(TypeRawAlias) \ + NODE(CompoundType) \ + \ + NODE(Binding) \ + NODE(MemRes) \ + NODE(Include) \ + NODE(UsePackage) \ + NODE(Alias) \ + NODE(Global) \ + NODE(Param) \ + NODE(Function) \ + NODE(OverloadedFunction) \ + \ + NODE(PolyParam) \ + NODE(PolySolution) \ + NODE(SolidifiedFunction) \ + NODE(PolyProc) \ + \ + NODE(Package) + +#define NODE(name) typedef struct Ast ## name Ast ## name; +AST_NODES +#undef NODE + typedef struct Package Package; typedef struct Scope { @@ -86,12 +92,9 @@ typedef struct Scope { bh_table(AstNode *) symbols; } Scope; -extern Scope* scope_create(bh_allocator a, Scope* parent, OnyxFilePos created_at); - typedef enum AstKind { Ast_Kind_Error, - Ast_Kind_Program, Ast_Kind_Package, Ast_Kind_Load_File, Ast_Kind_Load_Path, @@ -792,6 +795,9 @@ struct AstOverloadedFunction { struct AstPackage { AstNode_base; + // Allocated in the ast arena + char* package_name; + Package* package; }; @@ -1138,15 +1144,15 @@ i64 get_expression_integer_value(AstTyped* node); b32 cast_is_legal(Type* from_, Type* to_, char** err_msg); char* get_function_name(AstFunction* func); -AstNumLit* make_int_literal(bh_allocator a, i64 value); -AstNumLit* make_float_literal(bh_allocator a, f64 value); +AstNumLit* make_int_literal(bh_allocator a, i64 value); +AstNumLit* make_float_literal(bh_allocator a, f64 value); AstRangeLiteral* make_range_literal(bh_allocator a, AstTyped* low, AstTyped* high); -AstBinaryOp* make_binary_op(bh_allocator a, BinaryOp operation, AstTyped* left, AstTyped* right); -AstArgument* make_argument(bh_allocator a, AstTyped* value); -AstFieldAccess* make_field_access(bh_allocator a, AstTyped* node, char* field); -AstAddressOf* make_address_of(bh_allocator a, AstTyped* node); -AstLocal* make_local(bh_allocator a, OnyxToken* token, AstType* type_node); -AstNode* make_symbol(bh_allocator a, OnyxToken* sym); +AstBinaryOp* make_binary_op(bh_allocator a, BinaryOp operation, AstTyped* left, AstTyped* right); +AstArgument* make_argument(bh_allocator a, AstTyped* value); +AstFieldAccess* make_field_access(bh_allocator a, AstTyped* node, char* field); +AstAddressOf* make_address_of(bh_allocator a, AstTyped* node); +AstLocal* make_local(bh_allocator a, OnyxToken* token, AstType* type_node); +AstNode* make_symbol(bh_allocator a, OnyxToken* sym); void arguments_initialize(Arguments* args); b32 fill_in_arguments(Arguments* args, AstNode* provider, char** err_msg); @@ -1209,10 +1215,7 @@ static inline b32 node_is_auto_cast(AstNode* node) { static inline CallingConvention type_function_get_cc(Type* type) { if (type == NULL) return CC_Undefined; if (type->kind != Type_Kind_Function) return CC_Undefined; - if (type->Function.return_type->kind == Type_Kind_Struct) return CC_Return_Stack; - if (type->Function.return_type->kind == Type_Kind_Slice) return CC_Return_Stack; - if (type->Function.return_type->kind == Type_Kind_DynArray) return CC_Return_Stack; - if (type->Function.return_type->kind == Type_Kind_Compound) return CC_Return_Stack; + if (type_is_compound(type->Function.return_type)) return CC_Return_Stack; return CC_Return_Wasm; } diff --git a/include/onyxutils.h b/include/onyxutils.h index 37fa8d64..d1369e86 100644 --- a/include/onyxutils.h +++ b/include/onyxutils.h @@ -15,11 +15,12 @@ Package* package_lookup_or_create(char* package_name, Scope* parent_scope, bh_al void package_track_use_package(Package* package, Entity* entity); void package_reinsert_use_packages(Package* package); +Scope* scope_create(bh_allocator a, Scope* parent, OnyxFilePos created_at); void scope_include(Scope* target, Scope* source, OnyxFilePos pos); b32 symbol_introduce(Scope* scope, OnyxToken* tkn, AstNode* symbol); b32 symbol_raw_introduce(Scope* scope, char* tkn, OnyxFilePos pos, AstNode* symbol); void symbol_builtin_introduce(Scope* scope, char* sym, AstNode *node); -void symbol_subpackage_introduce(Scope* scope, OnyxToken* sym, AstPackage *node); +void symbol_subpackage_introduce(Scope* scope, char* sym, AstPackage *node); AstNode* symbol_raw_resolve(Scope* start_scope, char* sym); AstNode* symbol_resolve(Scope* start_scope, OnyxToken* tkn); AstNode* try_symbol_raw_resolve_from_node(AstNode* node, char* symbol); diff --git a/src/onyxastnodes.c b/src/onyxastnodes.c index d6061506..5bef0fcc 100644 --- a/src/onyxastnodes.c +++ b/src/onyxastnodes.c @@ -6,7 +6,6 @@ AstNode empty_node = { Ast_Kind_Error, 0, NULL, NULL }; static const char* ast_node_names[] = { "ERROR", - "PROGRAM", "PACKAGE", "INCLUDE FILE", "INCLUDE FOLDER", diff --git a/src/onyxclone.c b/src/onyxclone.c index 15ba968f..60cae924 100644 --- a/src/onyxclone.c +++ b/src/onyxclone.c @@ -25,7 +25,6 @@ static inline b32 should_clone(AstNode* node) { static inline i32 ast_kind_to_size(AstNode* node) { switch (node->kind) { case Ast_Kind_Error: return sizeof(AstNode); - case Ast_Kind_Program: return sizeof(AstNode); case Ast_Kind_Package: return sizeof(AstPackage); case Ast_Kind_Load_File: return sizeof(AstInclude); case Ast_Kind_Load_Path: return sizeof(AstInclude); @@ -468,4 +467,4 @@ void clone_function_body(bh_allocator a, AstFunction* dest, AstFunction* source) dest->allocate_exprs = NULL; bh_arr_new(global_heap_allocator, dest->allocate_exprs, 4); -} \ No newline at end of file +} diff --git a/src/onyxparser.c b/src/onyxparser.c index 66bdad6d..8663f251 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -461,6 +461,12 @@ static AstTyped* parse_factor(OnyxParser* parser) { break; } + // case Token_Type_Keyword_Package: { + // AstPackage* package_node = make_node(AstPackage, Ast_Kind_Package); + // package_node->token + // break; + // } + // :TypeValueInterchange case '<': { AstTypeAlias* alias = make_node(AstTypeAlias, Ast_Kind_Type_Alias); @@ -1166,18 +1172,12 @@ static AstNode* parse_use_stmt(OnyxParser* parser) { OnyxToken* package_name = expect_token(parser, Token_Type_Symbol); // CLEANUP: This is just gross. - if (consume_token_if_next(parser, '.')) { + while (consume_token_if_next(parser, '.')) { + if (parser->hit_unexpected_token) break; package_name->length += 1; - while (1) { - if (parser->hit_unexpected_token) break; - - OnyxToken* symbol = expect_token(parser, Token_Type_Symbol); - package_name->length += symbol->length; - - if (consume_token_if_next(parser, '.')) package_name->length += 1; - else break; - } + OnyxToken* symbol = expect_token(parser, Token_Type_Symbol); + package_name->length += symbol->length; } upack->package_name = package_name; @@ -2387,7 +2387,9 @@ static AstPackage* parse_package_name(OnyxParser* parser) { pnode->token = symbol; pnode->package = newpackage; - symbol_subpackage_introduce(package->scope, symbol, pnode); + token_toggle_end(symbol); + symbol_subpackage_introduce(package->scope, symbol->text, pnode); + token_toggle_end(symbol); } package = newpackage; diff --git a/src/onyxutils.c b/src/onyxutils.c index a11a35f8..f176a1b2 100644 --- a/src/onyxutils.c +++ b/src/onyxutils.c @@ -116,17 +116,15 @@ void symbol_builtin_introduce(Scope* scope, char* sym, AstNode *node) { bh_table_put(AstNode *, scope->symbols, sym, node); } -void symbol_subpackage_introduce(Scope* scope, OnyxToken* sym, AstPackage* package) { - token_toggle_end(sym); - - if (bh_table_has(AstNode *, scope->symbols, sym->text)) { - AstNode* maybe_package = bh_table_get(AstNode *, scope->symbols, sym->text); +void symbol_subpackage_introduce(Scope* scope, char* sym, AstPackage* package) { + if (bh_table_has(AstNode *, scope->symbols, sym)) { + AstNode* maybe_package = bh_table_get(AstNode *, scope->symbols, sym); + + // CLEANUP: Make this assertion an actual error message. assert(maybe_package->kind == Ast_Kind_Package); } else { - bh_table_put(AstNode *, scope->symbols, sym->text, (AstNode *) package); + bh_table_put(AstNode *, scope->symbols, sym, (AstNode *) package); } - - token_toggle_end(sym); } AstNode* symbol_raw_resolve(Scope* start_scope, char* sym) {