Skip to content

Commit e6ec953

Browse files
hsutterzaucy
authored andcommitted
Update: simplify terse syntax further by omitting -> too
And use it in test cases See discussion hsutter#714
1 parent 3418836 commit e6ec953

11 files changed

+49
-135
lines changed

regression-tests/pure2-more-wildcards.cpp2

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

2-
less_than: (value:_) -> _ =
3-
:(x:_) -> _ = x < value$;
2+
less_than: (value) :(x) x < value$;
43

54
main: () -> int
65
= {

regression-tests/pure2-template-parameter-lists.cpp2

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11

2-
f1: <T, U> (t:T, u:U) -> _ = {
3-
return t+u;
4-
}
5-
6-
f2: <T:type, U:type> (t:T, u:U) -> _ = {
7-
return t+u;
8-
}
9-
10-
f3: <T:_, U:_> () -> _ = {
11-
return T+U;
12-
}
13-
14-
f4: <T: i8, U: i16> () -> _ = {
15-
return T+U;
16-
}
2+
f1: <T, U> (t:T, u:U) t+u;
3+
f2: <T:type, U:type> (t:T, u:U) t+u;
4+
f3: <T:_, U:_> () T+U;
5+
f4: <T: i8, U: i16> () T+U;
176

187
main: () = {
198
std::cout << "f1: (f1(1,1))$\n";

regression-tests/pure2-types-basics.cpp2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ myclass : type = {
5454
g: () = std::cout << "N::myclass::nested::g\n";
5555
}
5656

57-
f1: <T , U > (t:T, u:U) -> _ = t+u;
58-
f2: <T:type, U:type> (t:T, u:U) -> _ = t+u;
59-
f3: <T:_ , U:_ > () -> _ = T+U;
60-
f4: <T:i8 , U:i16 > () -> _ = T+U;
57+
f1: <T , U > (t:T, u:U) t+u;
58+
f2: <T:type, U:type> (t:T, u:U) t+u;
59+
f3: <T:_ , U:_ > () T+U;
60+
f4: <T:i8 , U:i16 > () T+U;
6161

6262
}
6363

regression-tests/pure2-variadics.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ all: <Args...: type> (args...: Args) -> bool =
1313
// Unary left fold expression
1414
(... && args);
1515

16-
make_string: <Args...: type> (forward args...: Args) -> _ = :std::string = args...;
16+
make_string: <Args...> (forward args...: Args) :std::string = args...;
1717

18-
make: <T, Args...: type> (forward args...: Args) -> _ = :T = args...;
18+
make: <T, Args...> (forward args...: Args) :T = args...;
1919

2020
main: ()
2121
= {

regression-tests/test-results/pure2-more-wildcards.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
#line 2 "pure2-more-wildcards.cpp2"
1515
[[nodiscard]] auto less_than(auto const& value) -> auto;
16-
1716

1817
[[nodiscard]] auto main() -> int;
1918

@@ -22,8 +21,7 @@
2221

2322

2423
#line 2 "pure2-more-wildcards.cpp2"
25-
[[nodiscard]] auto less_than(auto const& value) -> auto {
26-
return [_0 = value](auto const& x) -> auto { return cpp2::cmp_less(x,_0); }; }
24+
[[nodiscard]] auto less_than(auto const& value) -> auto { return [_0 = value](auto const& x) -> auto { return cpp2::cmp_less(x,_0); }; }
2725

2826
[[nodiscard]] auto main() -> int
2927
{

regression-tests/test-results/pure2-template-parameter-lists.cpp

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,21 @@
1313

1414
#line 2 "pure2-template-parameter-lists.cpp2"
1515
template<typename T, typename U> [[nodiscard]] auto f1(T const& t, U const& u) -> auto;
16-
17-
18-
#line 6 "pure2-template-parameter-lists.cpp2"
1916
template<typename T, typename U> [[nodiscard]] auto f2(T const& t, U const& u) -> auto;
20-
21-
22-
#line 10 "pure2-template-parameter-lists.cpp2"
2317
template<auto T, auto U> [[nodiscard]] auto f3() -> auto;
24-
25-
26-
#line 14 "pure2-template-parameter-lists.cpp2"
2718
template<cpp2::i8 T, cpp2::i16 U> [[nodiscard]] auto f4() -> auto;
28-
2919

30-
#line 18 "pure2-template-parameter-lists.cpp2"
3120
auto main() -> int;
3221

3322

3423
//=== Cpp2 function definitions =================================================
3524

3625

3726
#line 2 "pure2-template-parameter-lists.cpp2"
38-
template<typename T, typename U> [[nodiscard]] auto f1(T const& t, U const& u) -> auto{
39-
return t + u;
40-
}
41-
42-
template<typename T, typename U> [[nodiscard]] auto f2(T const& t, U const& u) -> auto{
43-
return t + u;
44-
}
45-
46-
template<auto T, auto U> [[nodiscard]] auto f3() -> auto{
47-
return T + U;
48-
}
49-
50-
template<cpp2::i8 T, cpp2::i16 U> [[nodiscard]] auto f4() -> auto{
51-
return T + U;
52-
}
27+
template<typename T, typename U> [[nodiscard]] auto f1(T const& t, U const& u) -> auto { return t + u; }
28+
template<typename T, typename U> [[nodiscard]] auto f2(T const& t, U const& u) -> auto { return t + u; }
29+
template<auto T, auto U> [[nodiscard]] auto f3() -> auto { return T + U; }
30+
template<cpp2::i8 T, cpp2::i16 U> [[nodiscard]] auto f4() -> auto { return T + U; }
5331

5432
auto main() -> int{
5533
std::cout << "f1: " + cpp2::to_string(f1(1, 1)) + "\n";

regression-tests/test-results/pure2-types-basics.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ namespace N {
182182

183183
#line 57 "pure2-types-basics.cpp2"
184184
template<typename T, typename U> [[nodiscard]] auto myclass::f1(T const& t, U const& u) -> auto { return t + u; }
185-
template<typename T, typename U> [[nodiscard]] auto myclass::f2(T const& t, U const& u) -> auto { return t + u; }
185+
template<typename T, typename U> [[nodiscard]] auto myclass::f2(T const& t, U const& u) -> auto { return t + u; }
186186
template<auto T, auto U> [[nodiscard]] auto myclass::f3() -> auto { return T + U; }
187187
template<cpp2::i8 T, cpp2::i16 U> [[nodiscard]] auto myclass::f4() -> auto { return T + U; }
188188

regression-tests/test-results/pure2-variadics.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ template<typename ...Args> [[nodiscard]] auto all(Args const& ...args) -> bool;
3333

3434

3535
#line 16 "pure2-variadics.cpp2"
36-
template<typename ...Args> [[nodiscard]] auto make_string(Args&& ...args) -> auto;
36+
template <typename ...Args> [[nodiscard]] auto make_string(Args&& ...args) -> auto;
3737

38-
template<typename T, typename ...Args> [[nodiscard]] auto make(Args&& ...args) -> auto;
38+
template <typename T, typename ...Args> [[nodiscard]] auto make(Args&& ...args) -> auto;
3939

4040
auto main() -> int;
4141

@@ -53,9 +53,9 @@ template<typename ...Args> [[nodiscard]] auto all(Args const& ...args) -> bool {
5353
// Unary left fold expression
5454
return (... && args); }
5555

56-
template<typename ...Args> [[nodiscard]] auto make_string(Args&& ...args) -> auto { return std::string{CPP2_FORWARD(args)...}; }
56+
template <typename ...Args> [[nodiscard]] auto make_string(Args&& ...args) -> auto { return std::string{CPP2_FORWARD(args)...}; }
5757

58-
template<typename T, typename ...Args> [[nodiscard]] auto make(Args&& ...args) -> auto { return T{CPP2_FORWARD(args)...}; }
58+
template <typename T, typename ...Args> [[nodiscard]] auto make(Args&& ...args) -> auto { return T{CPP2_FORWARD(args)...}; }
5959

6060
auto main() -> int
6161
{

regression-tests/test-results/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
cppfront compiler v0.2.1 Build 8930:1743
2+
cppfront compiler v0.2.1 Build 8A01:0916
33
Copyright(c) Herb Sutter All rights reserved
44

55
SPDX-License-Identifier: CC-BY-NC-ND-4.0

source/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"8930:1743"
1+
"8A01:0916"

source/parse.h

Lines changed: 26 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5808,8 +5808,7 @@ class parser
58085808
//G assignment-expression assignment-operator logical-or-expression
58095809
//G
58105810
auto assignment_expression(
5811-
bool allow_angle_operators = true,
5812-
bool allow_assignment_or_equality_expression = true
5811+
bool allow_angle_operators = true
58135812
)
58145813
-> std::unique_ptr<assignment_expression_node>
58155814
{
@@ -5834,10 +5833,7 @@ class parser
58345833
return nullptr;
58355834
},
58365835
[=,this]{
5837-
return logical_or_expression(
5838-
allow_angle_operators,
5839-
allow_assignment_or_equality_expression
5840-
);
5836+
return logical_or_expression(allow_angle_operators);
58415837
}
58425838
);
58435839
}
@@ -5846,10 +5842,7 @@ class parser
58465842
ret = binary_expression<assignment_expression_node> (
58475843
[](token const&, token const&) -> token const* { return nullptr; },
58485844
[=,this]{
5849-
return logical_or_expression(
5850-
allow_angle_operators,
5851-
allow_assignment_or_equality_expression
5852-
);
5845+
return logical_or_expression(allow_angle_operators);
58535846
}
58545847
);
58555848
}
@@ -5859,10 +5852,6 @@ class parser
58595852
return {};
58605853
}
58615854

5862-
if (ret && ret->terms_size() > 0 && !allow_assignment_or_equality_expression) {
5863-
return {};
5864-
}
5865-
58665855
return ret;
58675856
}
58685857

@@ -5871,9 +5860,8 @@ class parser
58715860
//GTODO try expression
58725861
//G
58735862
auto expression(
5874-
bool allow_angle_operators = true,
5875-
bool check_arrow = true,
5876-
bool allow_assignment_or_equality_expression = true
5863+
bool allow_angle_operators = true,
5864+
bool check_arrow = true
58775865
)
58785866
-> std::unique_ptr<expression_node>
58795867
{
@@ -5883,7 +5871,7 @@ class parser
58835871
expression_node::current_expressions.push_back(n.get());
58845872
auto guard = finally([&]{ expression_node::current_expressions.pop_back(); });
58855873

5886-
if (!(n->expr = assignment_expression(allow_angle_operators, allow_assignment_or_equality_expression))) {
5874+
if (!(n->expr = assignment_expression(allow_angle_operators))) {
58875875
return {};
58885876
}
58895877

@@ -6360,8 +6348,7 @@ class parser
63606348
//G expression
63616349
//G
63626350
auto expression_statement(
6363-
bool semicolon_required,
6364-
bool allow_assignment_or_equality_statement = true
6351+
bool semicolon_required
63656352
)
63666353
-> std::unique_ptr<expression_statement_node>
63676354
{
@@ -6370,7 +6357,7 @@ class parser
63706357
expression_statement_node::current_expression_statements.push_back(n.get());
63716358
auto guard = finally([&]{ expression_statement_node::current_expression_statements.pop_back(); });
63726359

6373-
if (!(n->expr = expression(true, true, allow_assignment_or_equality_statement))) {
6360+
if (!(n->expr = expression(true, true))) {
63746361
return {};
63756362
}
63766363

@@ -7468,7 +7455,8 @@ class parser
74687455
//G 'throws'
74697456
//G
74707457
//G return-list:
7471-
//G '->' type-id?
7458+
//G expression-statement
7459+
//G '->' type-id
74727460
//G '->' parameter_declaration_list
74737461
//G
74747462
//G contract-seq:
@@ -7510,68 +7498,30 @@ class parser
75107498
next();
75117499
}
75127500

7513-
// Optional returns
7514-
if (curr().type() == lexeme::Arrow)
7515-
{
7516-
next();
75177501

7518-
// Experiment: If we're not at a '(' or '*' and there isn't a '[' '[' contract
7519-
// start before the next semicolon, check for a ":(params) -> expr;" shorthand
7520-
// syntax by speculatively parsing an expression-statement that isn't an assignment
7521-
//
7522-
// If this experiment is interesting, it would be easy to relax the `(` restriction
7523-
// to allow parenthesized expressions. This is just a simple implementation to try
7524-
// out the idea
7525-
auto at_an_expression_statement = false;
7526-
if (
7527-
curr().type() != lexeme::LeftParen
7528-
&& curr().type() != lexeme::Multiply
7529-
)
7530-
{
7531-
auto i = 0;
7532-
auto ptok = peek(0);
7533-
while (ptok = peek(i))
7534-
{
7535-
if (
7536-
ptok->type() == lexeme::LeftBracket
7537-
&& peek(i+1)
7538-
&& peek(i+1)->type() == lexeme::LeftBracket
7539-
)
7540-
{
7541-
break;
7542-
}
7543-
if (ptok->type() == lexeme::Semicolon)
7544-
{
7545-
break;
7546-
}
7547-
++i;
7548-
}
7549-
if (ptok && ptok->type() == lexeme::Semicolon)
7550-
{
7551-
auto start_pos = pos;
7552-
auto expr = expression_statement(true, false);
7553-
at_an_expression_statement =
7554-
expr
7555-
&& expr->expr
7556-
&& !(expr->expr->is_identifier() || expr->expr->is_id_expression())
7557-
;
7558-
pos = start_pos; // backtrack no matter what, we're just peeking here
7559-
}
7560-
}
7502+
// If we're not at a '->' and what follows is an expression-statement,
7503+
// this is a ":(params) expr;" shorthand function syntax
7504+
if (curr().type() != lexeme::Arrow)
7505+
{
7506+
auto start_pos = pos;
7507+
auto at_an_expression_statement = expression_statement(true) != nullptr;
7508+
pos = start_pos; // backtrack no matter what, we're just peeking here
75617509

7562-
if (at_an_expression_statement)
7563-
{
7564-
// This is the ":(params) -> expr;" shorthand syntax for when the programmer
7565-
// accepts the defaults they would other spell out longhand as "-> _ = expr;"
7566-
// (i.e., a deduced return type, no contracts, and single-return-statement body)
7510+
if (at_an_expression_statement) {
75677511
n->returns = function_type_node::single_type_id{ std::make_unique<type_id_node>() };
75687512
assert(n->returns.index() == function_type_node::id);
7569-
75707513
n->my_decl->terse_no_equals = true;
75717514
return n;
75727515
}
7516+
}
7517+
7518+
7519+
// Optional returns
7520+
if (curr().type() == lexeme::Arrow)
7521+
{
7522+
next();
75737523

7574-
else if (auto pass = to_passing_style(curr());
7524+
if (auto pass = to_passing_style(curr());
75757525
pass != passing_style::invalid
75767526
)
75777527
{

0 commit comments

Comments
 (0)