random little changes; starting package rewrite
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 26 Feb 2021 16:19:33 +0000 (10:19 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 26 Feb 2021 16:19:33 +0000 (10:19 -0600)
bin/onyx
docs/bugs
include/onyxastnodes.h
include/onyxutils.h
src/onyxastnodes.c
src/onyxclone.c
src/onyxparser.c
src/onyxutils.c

index eec960022798d5999eda7eb6af044dedb2fed879..c285846521a4539c4d13abd97b7cb0ebc4820d96 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 44ea72289d3c6942f0c3d595aa7013b646481c1f..463b007e727a0f24432bba4bf3304568c5072c5a 100644 (file)
--- a/docs/bugs
+++ b/docs/bugs
@@ -35,4 +35,24 @@ List of things to change:
     // x.x = 1
     // x.y = <uninitialized>
 
-[ ] 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.
index e36cbf5bf9d201639791bbba77d11cfc024ce021..5ef1184d4e19f833832081de219751022d79d612 100644 (file)
@@ -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;
 }
 
index 37fa8d640b8a73e6cf6fdb8a9e5ee57dd56e2fee..d1369e8630be6be148f830d4e0e42e218ec6c5d0 100644 (file)
@@ -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);
index d60615063724f25494feacd153bf28de426698a8..5bef0fccd947fcca2054205ea5c1c4f864bc9743 100644 (file)
@@ -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",
index 15ba968ff20423789fd88f789d137187ec0d8842..60cae9241a41f07e337d44eb2717d2161e4fbe61 100644 (file)
@@ -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
+}
index 66bdad6db159f37066fb04c6c2602f8c563bfc8b..8663f251470957a98630e73398a1c98f645018c2 100644 (file)
@@ -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;
index a11a35f8e0200a2b5dfef98e52a80641e7b6db81..f176a1b224d7f46d99b2c28a00ddd8de21987f0b 100644 (file)
@@ -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) {