summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2019-11-06 10:45:02 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2019-11-06 10:45:02 -0500
commitb3e5b99840d5ead250239565aedeb41443cc1102 (patch)
treeef04c6b76892d4bc5af9c7ed0219ace58bb0eb84
parent532287ffe97392c5b2b9f24646c5ec99095acd93 (diff)
allowed just each <array or range>
-rw-r--r--parse.c65
-rw-r--r--test.toc10
2 files changed, 43 insertions, 32 deletions
diff --git a/parse.c b/parse.c
index 414fe09..b545667 100644
--- a/parse.c
+++ b/parse.c
@@ -1065,43 +1065,50 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) {
ea->value = NULL;
ea->index = NULL;
t->token++;
- if (t->token->kind == TOKEN_IDENT) {
- ea->value = t->token->ident;
- if (ident_eq_str(ea->value, "_")) /* ignore value */
- ea->value = NULL;
- t->token++;
- if (token_is_kw(t->token, KW_COMMA)) {
+ if (token_is_kw(t->token, KW_COLON)
+ || (t->token->kind == TOKEN_IDENT
+ && (token_is_kw(t->token + 1, KW_COLON)
+ || (token_is_kw(t->token + 1, KW_COMMA)
+ && t->token[2].kind == TOKEN_IDENT
+ && token_is_kw(t->token + 3, KW_COLON))))) {
+ if (t->token->kind == TOKEN_IDENT) {
+ ea->value = t->token->ident;
+ if (ident_eq_str(ea->value, "_")) /* ignore value */
+ ea->value = NULL;
t->token++;
- if (t->token->kind == TOKEN_IDENT) {
- ea->index = t->token->ident;
- if (ident_eq_str(ea->index, "_")) /* ignore index */
- ea->index = NULL;
+ if (token_is_kw(t->token, KW_COMMA)) {
t->token++;
- } else {
- tokr_err(t, "Expected identifier after , in each statement.");
- return false;
+ if (t->token->kind == TOKEN_IDENT) {
+ ea->index = t->token->ident;
+ if (ident_eq_str(ea->index, "_")) /* ignore index */
+ ea->index = NULL;
+ t->token++;
+ } else {
+ tokr_err(t, "Expected identifier after , in each statement.");
+ return false;
+ }
}
}
- }
- if (token_is_kw(t->token, KW_AT)) {
- tokr_err(t, "The variable(s) in a for loop cannot be constant.");
- return false;
- }
- if (!token_is_kw(t->token, KW_COLON)) {
- tokr_err(t, "Expected : following identifiers in for statement.");
- return false;
- }
- t->token++;
- if (!token_is_kw(t->token, KW_EQ)) {
- ea->flags |= EACH_ANNOTATED_TYPE;
- if (!parse_type(p, &ea->type))
+ if (token_is_kw(t->token, KW_AT)) {
+ tokr_err(t, "The variable(s) in a for loop cannot be constant.");
return false;
- if (!token_is_kw(t->token, KW_EQ)) {
- tokr_err(t, "Expected = in for statement.");
+ }
+ if (!token_is_kw(t->token, KW_COLON)) {
+ tokr_err(t, "Expected : following identifiers in for statement.");
return false;
}
+ t->token++;
+ if (!token_is_kw(t->token, KW_EQ)) {
+ ea->flags |= EACH_ANNOTATED_TYPE;
+ if (!parse_type(p, &ea->type))
+ return false;
+ if (!token_is_kw(t->token, KW_EQ)) {
+ tokr_err(t, "Expected = in for statement.");
+ return false;
+ }
+ }
+ t->token++;
}
- t->token++;
Token *first_end = expr_find_end(p, EXPR_CAN_END_WITH_COMMA|EXPR_CAN_END_WITH_DOTDOT|EXPR_CAN_END_WITH_LBRACE, NULL);
Expression *first = parser_new_expr(p);
if (!parse_expr(p, first, first_end))
diff --git a/test.toc b/test.toc
index 82603e0..2bd8e0f 100644
--- a/test.toc
+++ b/test.toc
@@ -45,8 +45,10 @@ putf @= fn(x: float) {
// };
g @= fn() int {
- // foo : [10]int; // = new(int, 10);
- // each _, i := foo {
+ foo : [10]int; // = new(int, 10);
+ total := 0;
+ each foo { total = total + 1; }
+// each _, i := foo {
// foo[i] = i;
// };
// total := 0;
@@ -55,7 +57,6 @@ g @= fn() int {
// }
// total
- total := 0;
each i := 1..10 {
total = total + i;
total
@@ -71,4 +72,7 @@ main @= fn() {
puti(g());
X @= g();
puti(X);
+ each i, j := 1..10 {
+ puti(i + j);
+ }
};