made the 'proc' keyword optional in procedure types
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 22 Jan 2021 15:46:16 +0000 (09:46 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 22 Jan 2021 15:46:16 +0000 (09:46 -0600)
bin/onyx
onyx.exe
src/onyxparser.c
tests/multiple_returns_robustness
tests/named_arguments_test
tests/operator_overload
tests/polymorphic_array_lengths
tests/struct_robustness

index 310ed167d837aa61ca3c97cc093b76e0515fd643..cbc214887d5c0fbae532ca68e8e5fc8868ae5d62 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 41c79d7fa36d32821e20c890e8bede23a823c4c7..be1ae8ce692a0b0de34ac514b737efd94f6cd74c 100644 (file)
Binary files a/onyx.exe and b/onyx.exe differ
index 1b5054f6fb8987662a5c5cd853f052ae2024120a..0dda66fa4c2ffefc0f55094e7276e42fa30a104c 100644 (file)
@@ -1557,6 +1557,48 @@ static AstType* parse_compound_type(OnyxParser* parser) {
     }
 }
 
+static AstType* parse_function_type(OnyxParser* parser, OnyxToken* proc_token) {
+    bh_arr(AstType *) params = NULL;
+    bh_arr_new(global_scratch_allocator, params, 4);
+    bh_arr_set_length(params, 0);
+
+    expect_token(parser, '(');
+    while (parser->curr->type != ')') {
+        if (parser->hit_unexpected_token) return NULL;
+
+        if (peek_token(1)->type == ':') {
+            expect_token(parser, Token_Type_Symbol);
+            expect_token(parser, ':');
+        }
+
+        AstType* param_type = parse_type(parser);
+        bh_arr_push(params, param_type);
+
+        if (parser->curr->type != ')')
+            expect_token(parser, ',');
+    }
+    consume_token(parser);
+
+    AstType* return_type = (AstType *) &basic_type_void;
+    if (parser->curr->type == Token_Type_Right_Arrow) {
+        consume_token(parser);
+        return_type = parse_type(parser);
+    }
+
+    i64 param_count = bh_arr_length(params);
+    AstFunctionType* new = onyx_ast_node_new(parser->allocator,
+            sizeof(AstFunctionType) + sizeof(AstType*) * param_count,
+            Ast_Kind_Function_Type);
+    new->token = proc_token;
+    new->param_count = param_count;
+    new->return_type = return_type;
+
+    if (param_count > 0)
+        fori (i, 0, param_count) new->params[i] = params[i];
+
+    return (AstType *) new;
+}
+
 // <symbol>
 // '^' <type>
 static AstType* parse_type(OnyxParser* parser) {
@@ -1610,46 +1652,7 @@ static AstType* parse_type(OnyxParser* parser) {
 
             case Token_Type_Keyword_Proc: {
                 OnyxToken* proc_token = expect_token(parser, Token_Type_Keyword_Proc);
-
-                bh_arr(AstType *) params = NULL;
-                bh_arr_new(global_scratch_allocator, params, 4);
-                bh_arr_set_length(params, 0);
-
-                expect_token(parser, '(');
-                while (parser->curr->type != ')') {
-                    if (parser->hit_unexpected_token) return root;
-
-                    if (peek_token(1)->type == ':') {
-                        expect_token(parser, Token_Type_Symbol);
-                        expect_token(parser, ':');
-                    }
-
-                    AstType* param_type = parse_type(parser);
-                    bh_arr_push(params, param_type);
-
-                    if (parser->curr->type != ')')
-                        expect_token(parser, ',');
-                }
-                consume_token(parser);
-
-                AstType* return_type = (AstType *) &basic_type_void;
-                if (parser->curr->type == Token_Type_Right_Arrow) {
-                    consume_token(parser);
-                    return_type = parse_type(parser);
-                }
-
-                i64 param_count = bh_arr_length(params);
-                AstFunctionType* new = onyx_ast_node_new(parser->allocator,
-                        sizeof(AstFunctionType) + sizeof(AstType*) * param_count,
-                        Ast_Kind_Function_Type);
-                new->token = proc_token;
-                new->param_count = param_count;
-                new->return_type = return_type;
-
-                if (param_count > 0)
-                    fori (i, 0, param_count) new->params[i] = params[i];
-
-                *next_insertion = (AstType *) new;
+                *next_insertion = parse_function_type(parser, proc_token);
                 next_insertion = NULL;
                 break;
             }
@@ -1727,12 +1730,18 @@ static AstType* parse_type(OnyxParser* parser) {
             }
 
             case '(': {
-                expect_token(parser, '(');
-                
-                *next_insertion = parse_compound_type(parser);
-                next_insertion = NULL;
+                OnyxToken* matching = find_matching_paren(parser->curr);
 
-                expect_token(parser, ')');
+                if ((matching + 1)->type == Token_Type_Right_Arrow) {
+                    *next_insertion = parse_function_type(parser, parser->curr);
+
+                } else {
+                    expect_token(parser, '(');
+                    *next_insertion = parse_compound_type(parser);
+                    expect_token(parser, ')');
+                }
+
+                next_insertion = NULL;
                 break;
             }
 
index 64ec3eb15498266a810e5b3b3610b2d50c45023c..5fcb6e364aa3936209fefb487fe53cfa16d88221 100644 (file)
@@ -3,4 +3,4 @@
 110
 -10 to 200
 0 1 2 
-3.0 4.0 5.
+3.0000 4.0000 5.000
index 794350327fe5738f45de427ea66acdce22f26a59..2942784dcb16a991babf6a9e39d4a6e396675772 100644 (file)
@@ -1,14 +1,14 @@
 x is 10
-y is 20.0
+y is 20.0000
 x is 20
-y is 10.0
+y is 10.0000
 
 
 =========================
 10
 10 20 30
-10.0
-10.0 20.0 30.0
+10.0000
+10.0000 20.0000 30.0000
 
 
 =========================
@@ -20,7 +20,7 @@ MATCHED Z: 10
 =========================
 10
 20
-30.0 40.0 50.
+30.0000 40.0000 50.000
 
 
 =========================
index bad108f0e7652b6c2225b52558d146deec58f933..333bd6b1de8094fb26c9b78bc1593a784c315e8f 100644 (file)
@@ -1,7 +1,7 @@
-(2.0, 4.0)
-(2.0, 2.0)
-(-3.0, 2.0)
-(13.0, 24.0)
-2.0 3.0 5.0 7.0 13.0 24.
-(-5.0, 6.0)
+(2.0000, 4.0000)
+(2.0000, 2.0000)
+(-3.0000, 2.0000)
+(13.0000, 24.0000)
+2.0000 3.0000 5.0000 7.0000 13.0000 24.000
+(-5.0000, 6.0000)
 World!
index 6189705e8ad3c2dff6584a2b21933e33ace09c11..389fe62c5ed9ea60d943595bf96b238ff29c91ef 100644 (file)
@@ -1,20 +1,20 @@
-1 2 3 4 5 0.0
-1.0
+1 2 3 4 5 0.0000
+1.0000
 1.4142
 1.7320
-2.0
+2.0000
 2.2360
 2.4494
 2.6457
 2.8284
-3.0
+3.0000
 3.1622
 3.3166
 3.4641
 3.6055
 3.7416
 3.8729
-4.0
+4.0000
 4.1231
 4.2426
 4.3588
index 75163735104bb1f6152a9f02c65296bb38b84879..894dcadb46f94097ee243645c6ab267946ff8e1a 100644 (file)
@@ -7,22 +7,22 @@ Testing a simple union.
 
 
 Testing a struct with default values.
-DefaultedStruct(0, 1.0, 2, 3.0)
-DefaultedStruct(3, 1.0, 2, 0.0)
+DefaultedStruct(0, 1.0000, 2, 3.0000)
+DefaultedStruct(3, 1.0000, 2, 0.0000)
 
 
 Testing a struct with `use`.
-StructWithUse(1234, (1.0, 2.0), 5678)
-StructWithUse(1234, (1.0, 2.0), 5678)
+StructWithUse(1234, (1.0000, 2.0000), 5678)
+StructWithUse(1234, (1.0000, 2.0000), 5678)
 
 
 Testing a polymorphic struct.
-PolyStruct<i32, f32>(1234, 5678.0)
-PolyStruct<f32, i32>(1234.0, 5678)
+PolyStruct<i32, f32>(1234, 5678.0000)
+PolyStruct<f32, i32>(1234.0000, 5678)
 
 
 Testing a polymorphic struct with default values.
-PolyStruct<i32, f32>(1234, 5678.0)
+PolyStruct<i32, f32>(1234, 5678.0000)
 
 
 Testing a union with use.
@@ -37,4 +37,4 @@ PolyUnion(1234)
 
 Testing a polymorphic union with use.
 16 == 16
-0, 5678.0
+0, 5678.0000