Skip to content

Commit 5aaf91f

Browse files
authored
Helper function to convert tokens to passing_styles. (#198)
Also use is_literal() in an appropriate place.
1 parent 9890447 commit 5aaf91f

File tree

1 file changed

+34
-60
lines changed

1 file changed

+34
-60
lines changed

source/parse.h

+34-60
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,18 @@ struct expression_node
241241
}
242242
};
243243

244-
enum class passing_style { in=0, copy, inout, out, move, forward };
244+
enum class passing_style { in=0, copy, inout, out, move, forward, invalid };
245+
auto to_passing_style(token const& t) -> passing_style {
246+
if (t.type() == lexeme::Identifier) {
247+
if (t == "in" ) { return passing_style::in; }
248+
if (t == "copy" ) { return passing_style::copy; }
249+
if (t == "inout" ) { return passing_style::inout; }
250+
if (t == "out" ) { return passing_style::out; }
251+
if (t == "move" ) { return passing_style::move; }
252+
if (t == "forward") { return passing_style::forward; }
253+
}
254+
return passing_style::invalid;
255+
}
245256
auto to_string_view(passing_style pass) -> std::string_view {
246257
switch (pass) {
247258
break;case passing_style::in : return "in";
@@ -1549,14 +1560,7 @@ class parser
15491560
return n;
15501561
}
15511562

1552-
if (curr().type() == lexeme::DecimalLiteral ||
1553-
curr().type() == lexeme::FloatLiteral ||
1554-
curr().type() == lexeme::StringLiteral ||
1555-
curr().type() == lexeme::CharacterLiteral ||
1556-
curr().type() == lexeme::BinaryLiteral ||
1557-
curr().type() == lexeme::HexadecimalLiteral
1558-
)
1559-
{
1563+
if (is_literal(curr().type())) {
15601564
n->expr = &curr();
15611565
next();
15621566
return n;
@@ -2047,16 +2051,11 @@ class parser
20472051
n->open_paren = open_paren;
20482052
n->inside_initializer = inside_initializer;
20492053

2050-
if (curr().type() == lexeme::Identifier && curr() == "out") {
2051-
pass = passing_style::out;
2052-
next();
2053-
}
2054-
else if (curr().type() == lexeme::Identifier && curr() == "move") {
2055-
pass = passing_style::move;
2056-
next();
2057-
}
2058-
else if (curr().type() == lexeme::Identifier && curr() == "forward") {
2059-
pass = passing_style::forward;
2054+
if (auto dir = to_passing_style(curr());
2055+
dir == passing_style::out || dir == passing_style::move || dir == passing_style::forward
2056+
)
2057+
{
2058+
pass = dir;
20602059
next();
20612060
}
20622061
auto x = expression();
@@ -2072,16 +2071,11 @@ class parser
20722071
while (curr().type() == lexeme::Comma) {
20732072
next();
20742073
pass = passing_style::in;
2075-
if (curr().type() == lexeme::Identifier && curr() == "out") {
2076-
pass = passing_style::out;
2077-
next();
2078-
}
2079-
else if (curr().type() == lexeme::Identifier && curr() == "move") {
2080-
pass = passing_style::move;
2081-
next();
2082-
}
2083-
else if (curr().type() == lexeme::Identifier && curr() == "forward") {
2084-
pass = passing_style::forward;
2074+
if (auto dir = to_passing_style(curr());
2075+
dir == passing_style::out || dir == passing_style::move || dir == passing_style::forward
2076+
)
2077+
{
2078+
pass = dir;
20852079
next();
20862080
}
20872081
auto expr = expression();
@@ -2959,51 +2953,31 @@ class parser
29592953
n->pass = returns ? passing_style::out : passing_style::in;
29602954
n->pos = curr().position();
29612955

2962-
if (curr().type() == lexeme::Identifier) {
2963-
if (curr() == "in") {
2964-
if (returns) {
2956+
if (auto dir = to_passing_style(curr()); dir != passing_style::invalid) {
2957+
if (returns) {
2958+
if (dir == passing_style::in) {
29652959
error("a return value cannot be 'in'");
29662960
return {};
29672961
}
2968-
n->pass = passing_style::in;
2969-
next();
2970-
}
2971-
else if (curr() == "copy") {
2972-
if (returns) {
2962+
if (dir == passing_style::copy) {
29732963
error("a return value cannot be 'copy'");
29742964
return {};
29752965
}
2976-
n->pass = passing_style::copy;
2977-
next();
2978-
}
2979-
else if (curr() == "inout") {
2980-
if (returns) {
2966+
if (dir == passing_style::inout) {
29812967
error("a return value cannot be 'inout'");
29822968
return {};
29832969
}
2984-
n->pass = passing_style::inout;
2985-
next();
2986-
}
2987-
else if (curr() == "out") {
2988-
if (!named) {
2989-
error("(temporary alpha limitation) an unnamed function cannot have an 'out' parameter");
2990-
return {};
2991-
}
2992-
n->pass = passing_style::out;
2993-
next();
2994-
}
2995-
else if (curr() == "move") {
2996-
if (returns) {
2970+
if (dir == passing_style::move) {
29972971
error("a return value cannot be 'move' (it is implicitly 'move'-out)");
29982972
return {};
29992973
}
3000-
n->pass = passing_style::move;
3001-
next();
30022974
}
3003-
else if (curr() == "forward") {
3004-
n->pass = passing_style::forward;
3005-
next();
2975+
if (!named && dir == passing_style::out) {
2976+
error("(temporary alpha limitation) an unnamed function cannot have an 'out' parameter");
2977+
return {};
30062978
}
2979+
n->pass = dir;
2980+
next();
30072981
}
30082982

30092983
if (curr().type() == lexeme::Identifier) {

0 commit comments

Comments
 (0)