@@ -1008,6 +1008,29 @@ struct function_type_node
1008
1008
};
1009
1009
1010
1010
1011
+ // struct udt_type_node
1012
+ // {
1013
+ // source_position pos;
1014
+ // std::vector<id_expression_node> metaclass_names;
1015
+ //
1016
+ // udt_type_node( source_position pos ) : pos{pos} { }
1017
+ //
1018
+ // auto position() const -> source_position
1019
+ // {
1020
+ // return pos;
1021
+ // }
1022
+ //
1023
+ // auto visit(auto& v, int depth) -> void
1024
+ // {
1025
+ // v.start(*this, depth);
1026
+ // for (auto const& m : metaclass_names) {
1027
+ // m->visit(v, depth+1);
1028
+ // }
1029
+ // v.end(*this, depth);
1030
+ // }
1031
+ // };
1032
+
1033
+
1011
1034
struct declaration_node
1012
1035
{
1013
1036
// Declared first, because it should outlive any owned
@@ -1021,6 +1044,7 @@ struct declaration_node
1021
1044
std::variant<
1022
1045
std::unique_ptr<function_type_node>,
1023
1046
std::unique_ptr<type_id_node>
1047
+ // std::unique_ptr<udt_type_node>
1024
1048
> type;
1025
1049
1026
1050
source_position equal_sign = {};
@@ -1570,7 +1594,7 @@ class parser
1570
1594
// G primary-expression
1571
1595
// G postfix-expression postfix-operator [Note: without whitespace before the operator]
1572
1596
// G postfix-expression '[' expression-list ']'
1573
- // G postfix-expression '(' expression-list-opt ')'
1597
+ // G postfix-expression '(' expression-list? ')'
1574
1598
// G postfix-expression '.' id-expression
1575
1599
// G
1576
1600
auto postfix_expression ()
@@ -1981,7 +2005,7 @@ class parser
1981
2005
}
1982
2006
1983
2007
// G expression-list:
1984
- // G parameter-direction-opt expression
2008
+ // G parameter-direction? expression
1985
2009
// G expression-list ',' expression
1986
2010
// G
1987
2011
auto expression_list (source_position open_paren, bool inside_initializer = false ) -> std::unique_ptr<expression_list_node> {
@@ -2035,8 +2059,8 @@ class parser
2035
2059
2036
2060
2037
2061
// G type-id:
2038
- // G type-qualifier-seq-opt qualified-id
2039
- // G type-qualifier-seq-opt unqualified-id
2062
+ // G type-qualifier-seq? qualified-id
2063
+ // G type-qualifier-seq? unqualified-id
2040
2064
// G
2041
2065
// G type-qualifier-seq:
2042
2066
// G type-qualifier
@@ -2170,7 +2194,7 @@ class parser
2170
2194
// GTODO operator-function-id
2171
2195
// G
2172
2196
// G template-id:
2173
- // G identifier '<' template-argument-list-opt '>'
2197
+ // G identifier '<' template-argument-list? '>'
2174
2198
// G
2175
2199
// G template-argument-list:
2176
2200
// G template-argument-list ',' template-argument
@@ -2379,8 +2403,8 @@ class parser
2379
2403
2380
2404
2381
2405
// G selection-statement:
2382
- // G 'if' 'constexpr'-opt expression compound-statement
2383
- // G 'if' 'constexpr'-opt expression compound-statement 'else' compound-statement
2406
+ // G 'if' 'constexpr'? expression compound-statement
2407
+ // G 'if' 'constexpr'? expression compound-statement 'else' compound-statement
2384
2408
// G
2385
2409
auto selection_statement () -> std::unique_ptr<selection_statement_node>
2386
2410
{
@@ -2436,7 +2460,7 @@ class parser
2436
2460
2437
2461
2438
2462
// G return-statement:
2439
- // G return expression-opt ';'
2463
+ // G return expression? ';'
2440
2464
// G
2441
2465
auto return_statement () -> std::unique_ptr<return_statement_node>
2442
2466
{
@@ -2475,9 +2499,9 @@ class parser
2475
2499
2476
2500
2477
2501
// G iteration-statement:
2478
- // G 'while' logical-or-expression next-clause-opt compound-statement
2479
- // G 'do' compound-statement 'while' logical-or-expression next-clause-opt ';'
2480
- // G 'for' expression next-clause-opt 'do' unnamed-declaration
2502
+ // G 'while' logical-or-expression next-clause? compound-statement
2503
+ // G 'do' compound-statement 'while' logical-or-expression next-clause? ';'
2504
+ // G 'for' expression next-clause? 'do' unnamed-declaration
2481
2505
// G
2482
2506
// G next-clause:
2483
2507
// G 'next' assignment-expression
@@ -2602,9 +2626,9 @@ class parser
2602
2626
2603
2627
2604
2628
// G alternative:
2605
- // G alt-name-opt is-type-constraint '=' statement
2606
- // G alt-name-opt is-value-constraint '=' statement
2607
- // G alt-name-opt as-type-cast '=' statement
2629
+ // G alt-name? is-type-constraint '=' statement
2630
+ // G alt-name? is-value-constraint '=' statement
2631
+ // G alt-name? as-type-cast '=' statement
2608
2632
// G
2609
2633
// G alt-name:
2610
2634
// G unqualified-id :
@@ -2653,8 +2677,8 @@ class parser
2653
2677
2654
2678
2655
2679
// G inspect-expression:
2656
- // G 'inspect' 'constexpr'-opt expression '{' alternative-seq-opt '}'
2657
- // G 'inspect' 'constexpr'-opt expression '->' type-id '{' alternative-seq-opt '}'
2680
+ // G 'inspect' 'constexpr'? expression '{' alternative-seq? '}'
2681
+ // G 'inspect' 'constexpr'? expression '->' type-id '{' alternative-seq? '}'
2658
2682
// G
2659
2683
// G alternative-seq:
2660
2684
// G alternative
@@ -2831,7 +2855,7 @@ class parser
2831
2855
2832
2856
2833
2857
// G compound-statement:
2834
- // G '{' statement-seq-opt '}'
2858
+ // G '{' statement-seq? '}'
2835
2859
// G
2836
2860
// G statement-seq:
2837
2861
// G statement
@@ -2875,7 +2899,7 @@ class parser
2875
2899
2876
2900
2877
2901
// G parameter-declaration:
2878
- // G parameter-direction-opt declaration
2902
+ // G parameter-direction? declaration
2879
2903
// G
2880
2904
// G parameter-direction: one of
2881
2905
// G 'in' 'copy' 'inout' 'out' 'move' 'forward'
@@ -2971,7 +2995,7 @@ class parser
2971
2995
2972
2996
2973
2997
// G parameter-declaration-list
2974
- // G '(' parameter-declaration-seq-opt ')'
2998
+ // G '(' parameter-declaration-seq? ')'
2975
2999
// G
2976
3000
// G parameter-declaration-seq:
2977
3001
// G parameter-declaration
@@ -3018,8 +3042,8 @@ class parser
3018
3042
3019
3043
3020
3044
// G contract:
3021
- // G '[' '[' contract-kind id-expression-opt ':' logical-or-expression ']' ']'
3022
- // G '[' '[' contract-kind id-expression-opt ':' logical-or-expression ',' string-literal ']' ']'
3045
+ // G '[' '[' contract-kind id-expression? ':' logical-or-expression ']' ']'
3046
+ // G '[' '[' contract-kind id-expression? ':' logical-or-expression ',' string-literal ']' ']'
3023
3047
// G
3024
3048
// G contract-kind: one of
3025
3049
// G 'pre' 'post' 'assert'
@@ -3088,7 +3112,7 @@ class parser
3088
3112
3089
3113
3090
3114
// G function-type:
3091
- // G parameter-declaration-list throws-specifier-opt return-list-opt contract-seq-opt
3115
+ // G parameter-declaration-list throws-specifier? return-list? contract-seq?
3092
3116
// G
3093
3117
// G throws-specifier:
3094
3118
// G 'throws'
@@ -3154,8 +3178,13 @@ class parser
3154
3178
3155
3179
// G unnamed-declaration:
3156
3180
// G ':' function-type '=' statement
3157
- // G ':' type-id-opt '=' statement
3181
+ // G ':' type-id? '=' statement
3158
3182
// G ':' type-id
3183
+ // G ':' 'type' meta-constraints? '=' statement
3184
+ // G
3185
+ // G meta-constraints:
3186
+ // G 'is' id-expression
3187
+ // G meta-constraints ',' id-expression
3159
3188
// G
3160
3189
auto unnamed_declaration (source_position start, bool semicolon_required = true , bool captures_allowed = false ) -> std::unique_ptr<declaration_node>
3161
3190
{
@@ -3179,7 +3208,13 @@ class parser
3179
3208
3180
3209
// Next is an an optional type
3181
3210
3182
- // It could be a function type, declaring a function
3211
+ // // It could be "type," declaring a user-defined type
3212
+ // if (auto t = udt_type()) {
3213
+ // n->type = std::move(t);
3214
+ // assert (n->type.index() == declaration_node::udt);
3215
+ // }
3216
+
3217
+ // Or a function type, declaring a function
3183
3218
if (auto t = function_type ()) {
3184
3219
n->type = std::move (t);
3185
3220
assert (n->type .index () == declaration_node::function);
@@ -3316,6 +3351,15 @@ class parser
3316
3351
return {};
3317
3352
}
3318
3353
3354
+ if (n->parent_scope && n->is (declaration_node::function) && n->parent_scope ->is (declaration_node::function)) {
3355
+ assert (id->get_token ());
3356
+ auto name = id->get_token ()->to_string (true );
3357
+ errors.emplace_back (
3358
+ curr ().position (),
3359
+ " (temporary alpha limitation) local functions like '" + name + " : (/*params*/) = {/*body*/}' are not currently supported - write a local variable initialized with an unnamed function like '" + name + " := :(/*params*/) = {/*body*/};' instead (add ':=' and ';')"
3360
+ );
3361
+ }
3362
+
3319
3363
n->identifier = std::move (id);
3320
3364
return n;
3321
3365
}
@@ -3326,7 +3370,7 @@ class parser
3326
3370
// G declaration-seq declaration
3327
3371
// G
3328
3372
// G translation-unit:
3329
- // G declaration-seq-opt
3373
+ // G declaration-seq?
3330
3374
//
3331
3375
auto translation_unit () -> std::unique_ptr<translation_unit_node>
3332
3376
{
@@ -3416,6 +3460,11 @@ class parse_tree_printer : printing_visitor
3416
3460
o << pre (indent) << " prefix-expression\n " ;
3417
3461
}
3418
3462
3463
+ auto start (is_as_expression_node const &, int indent) -> void
3464
+ {
3465
+ o << pre (indent) << " is-as-expression\n " ;
3466
+ }
3467
+
3419
3468
template <String Name, typename Term>
3420
3469
auto start (binary_expression_node<Name, Term> const &, int indent) -> void
3421
3470
{
@@ -3504,6 +3553,11 @@ class parse_tree_printer : printing_visitor
3504
3553
}
3505
3554
}
3506
3555
3556
+ // auto start(udt_type_node const&, int indent) -> void
3557
+ // {
3558
+ // o << pre(indent) << "user-defined type\n";
3559
+ // }
3560
+
3507
3561
auto start (function_type_node const & n, int indent) -> void
3508
3562
{
3509
3563
o << pre (indent) << " function\n " ;
@@ -3522,7 +3576,8 @@ class parse_tree_printer : printing_visitor
3522
3576
3523
3577
auto start (declaration_node const & n, int indent) -> void
3524
3578
{
3525
- o << pre (indent) << " declaration\n " ;
3579
+ o << pre (indent) << " declaration [" << &n << " ]\n " ;
3580
+ o << pre (indent+1 ) << " parent: [" << n.parent_scope << " ]\n " ;
3526
3581
if (!n.captures .members .empty ()) {
3527
3582
o << pre (indent+1 ) << " captures: " << n.captures .members .size () << " \n " ;
3528
3583
}
0 commit comments