Skip to content

Commit dcf482c

Browse files
committed
Start initial name: as id-expression inspect alternative support
And make the 'todo' grammar comments clearer
1 parent c599bc7 commit dcf482c

File tree

4 files changed

+67
-35
lines changed

4 files changed

+67
-35
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ For now, I'm not posting a lot of written documentation because that would imply
128128

129129
Here's where to find out more about my 'syntax #2' experiment:
130130

131-
- **My CppCon 2022 talk** [link coming soon] — this is the primary documentation right now. See also every talk I've given and paper I've written since 2015, each of which details an individual part of this design experiment, but presented in today's C++ syntax as a standalone C++ evolution proposal.
131+
- **My CppCon 2022 talk, "C++ simplicity, safety, and toolability: Can C++ be 10x simpler and safer ...?**" [link coming soon] — this is the primary documentation right now. See also every talk I've given and paper I've written since 2015, each of which details an individual part of this design experiment, but presented in today's C++ syntax as a standalone C++ evolution proposal.
132132
- **The [cppfront regression tests](https://github.com/hsutter/cppfront/tree/main/regression-tests/test-results)** which show dozens of working examples, each with a`.cpp2` file and the `.cpp` file it is translated to. Each filename briefly describes the language features the test demonstrates (e.g., contracts, parameter passing, bounds safety, type-safe `is` queries and `as` casts, initialization safety, and generalized value capture including in function expressions ('lambdas'), postconditions, and string interpolation).
133133

134134
## List of my papers and talks since 2015 (all from this work, but presented in today's syntax)
@@ -233,8 +233,8 @@ Cppfront currently has basic support for `is` and `as`, including for dynamic ty
233233
I still need to validate the `is` and `as` implementations with more cases (I'm sure there are still some that break that I need to fix), flesh out allowing `is` and `as` in `requires`-clauses to constrain templates (as shown in the paper), and non-basic `inspect` pattern matching examples.
234234

235235
### 2022: CppCon 2022 talk and cppfront
236-
237-
- **CppCon 2022**: "Can C++ be 10x simpler & safer?" (link coming soon).
236+
237+
- **CppCon 2022**: "C++ simplicity, safety, and toolability: Can C++ be 10x simpler and safer ...?" [link coming soon]
238238
- This repo.
239239

240240
# Epilog: 2016 roadmap diagram

source/cppfront.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ class cppfront
11921192

11931193
for (auto first = true; auto&& alt : n.alternatives)
11941194
{
1195-
assert(alt);
1195+
assert(alt && alt->is_as_keyword);
11961196
if (!first) {
11971197
printer.print_cpp2("else ", alt->position());
11981198
}
@@ -1204,7 +1204,10 @@ class cppfront
12041204
emit(*alt->id_expression);
12051205
printer.emit_to_string();
12061206

1207-
if (*alt->identifier == "is")
1207+
assert (*alt->is_as_keyword == "is" || *alt->is_as_keyword == "as");
1208+
// TODO: pick up 'as' next, for now just do 'is'
1209+
1210+
if (*alt->is_as_keyword == "is")
12081211
{
12091212
// Stringize the expression-statement now...
12101213
auto statement = std::string{};

source/lex.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,8 @@ auto lex_line(
667667
//G character-literal
668668
//G floating-point-literal
669669
//G string-literal
670-
//GT boolean-literal
671-
//GT pointer-literal
670+
//GTODO boolean-literal
671+
//GTODO pointer-literal
672672
//G
673673
//G integer-literal:
674674
//G binary-literal
@@ -728,7 +728,7 @@ auto lex_line(
728728
//G
729729
//G floating-point-literal:
730730
//G digit { ' | digit }* . digit { ' | digit }*
731-
//GT TODO: full grammar
731+
//GTODO TODO: full grammar
732732
//G
733733
else if (is_digit(line[i])) {
734734
auto j = 1;

source/parse.h

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -597,15 +597,16 @@ struct return_statement_node
597597

598598
struct alternative_node
599599
{
600-
token const* identifier;
601-
std::unique_ptr<id_expression_node> id_expression;
602-
source_position equal_sign;
603-
std::unique_ptr<statement_node> statement;
600+
std::unique_ptr<unqualified_id_node> name;
601+
token const* is_as_keyword;
602+
std::unique_ptr<id_expression_node> id_expression;
603+
source_position equal_sign;
604+
std::unique_ptr<statement_node> statement;
604605

605606
auto position() const -> source_position
606607
{
607-
assert(identifier);
608-
return identifier->position();
608+
assert(is_as_keyword);
609+
return is_as_keyword->position();
609610
}
610611

611612
auto visit(auto& v, int depth) -> void;
@@ -708,8 +709,11 @@ struct statement_node
708709
auto alternative_node::visit(auto& v, int depth) -> void
709710
{
710711
v.start(*this, depth);
711-
assert (identifier);
712-
v.start(*identifier, depth+1);
712+
if (name) {
713+
v.start(*name, depth+1);
714+
}
715+
assert (is_as_keyword);
716+
v.start(*is_as_keyword, depth+1);
713717
assert (id_expression);
714718
id_expression->visit(v, depth+1);
715719
assert (statement);
@@ -1458,11 +1462,11 @@ class parser
14581462
//G prefix-expression:
14591463
//G postfix-expression
14601464
//G prefix-operator prefix-expression
1461-
//GT await-expression
1462-
//GT sizeof ( type-id )
1463-
//GT sizeof ... ( identifier )
1464-
//GT alignof ( type-id )
1465-
//GT throws-expression
1465+
//GTODO await-expression
1466+
//GTODO sizeof ( type-id )
1467+
//GTODO sizeof ... ( identifier )
1468+
//GTODO alignof ( type-id )
1469+
//GTODO throws-expression
14661470
//G
14671471
auto prefix_expression()
14681472
-> std::unique_ptr<prefix_expression_node>
@@ -1516,9 +1520,9 @@ class parser
15161520

15171521
//G is-as-expression:
15181522
//G prefix-expression
1519-
//GT is-as-expression is-expression-constraint
1520-
//GT is-as-expression as-type-cast
1521-
//GT type-id is-type-constraint
1523+
//GTODO is-as-expression is-expression-constraint
1524+
//GTODO is-as-expression as-type-cast
1525+
//GTODO type-id is-type-constraint
15221526
//G
15231527
auto is_as_expression() {
15241528
return binary_expression<is_as_expression_node> (
@@ -1682,7 +1686,7 @@ class parser
16821686

16831687
//G expression: // eliminated condition: - use expression:
16841688
//G assignment-expression
1685-
//GT try expression
1689+
//GTODO try expression
16861690
//G
16871691
auto expression(bool allow_relational_comparison = true) -> std::unique_ptr<expression_node> {
16881692
auto n = std::make_unique<expression_node>();
@@ -1744,7 +1748,7 @@ class parser
17441748
//G unqualified-id:
17451749
//G const-opt identifier
17461750
//G const-opt template-id
1747-
//GT operator-function-id
1751+
//GTODO operator-function-id
17481752
//G
17491753
//G template-id:
17501754
//G identifier < template-argument-list-opt >
@@ -2180,21 +2184,46 @@ class parser
21802184

21812185

21822186
//G alternative:
2183-
//G is-expression-constraint = statement
2187+
//G alt-name-opt is-type-constraint = statement
2188+
//G alt-name-opt as-type-cast = statement
2189+
//GTODO alt-name-opt is-expression-constraint = statement
21842190
//G
2185-
//G is-expression-constraint
2191+
//G is-type-constraint
21862192
//G is id-expression
21872193
//G
2194+
//G as-type-cast
2195+
//G as id-expression
2196+
//G
2197+
//G alt-name:
2198+
//G unqualified-id :
2199+
//G
21882200
auto alternative() -> std::unique_ptr<alternative_node>
21892201
{
2190-
// Initial partial implementation: Just "is id-expression"
2191-
// (note: that covers "_" as an unqualified-id)
2192-
if (curr() != "is") {
2202+
auto n = std::make_unique<alternative_node>();
2203+
2204+
//// Check for an optional name (just one unqualified-id, no decomposition yet)
2205+
//if (curr() != "is" && curr() != "as") {
2206+
// if (auto id = unqualified_id()) {
2207+
// n->name = std::move(id);
2208+
// }
2209+
// else {
2210+
// error("expected unqualified-id, 'is', or 'as' to start an inspect alternative");
2211+
// return {};
2212+
// }
2213+
// if (curr().type() != lexeme::Colon) {
2214+
// error("expected : after the introduced name in an inspect alternative");
2215+
// return {};
2216+
// }
2217+
// next();
2218+
//}
2219+
2220+
// Now we should be as "is" or "as"
2221+
// (initial partial implementation, just "is/as id-expression")
2222+
if (curr() != "is" && curr() != "as") {
21932223
return {};
21942224
}
21952225

2196-
auto n = std::make_unique<alternative_node>();
2197-
n->identifier = &curr();
2226+
n->is_as_keyword = &curr();
21982227
next();
21992228

22002229
if (auto id = id_expression()) {
@@ -2325,8 +2354,8 @@ class parser
23252354
//G inspect-expression
23262355
//G let parameter-list statement
23272356
//
2328-
//GT jump-statement
2329-
//GT try-block
2357+
//GTODO jump-statement
2358+
//GTODO try-block
23302359
//G
23312360
auto statement(bool semicolon_required, source_position equal_sign = source_position{})
23322361
-> std::unique_ptr<statement_node>

0 commit comments

Comments
 (0)