authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-05 17:15:19-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-05 17:15:19-07:00
log4339d555626197f4b8c9598b602f098b76488c2d
tree2773f3dbace6d3603f7fddaddee8f2683b645d37
parent4208435f662030abf24929c90a4bb8c7fb0d2908

update for loop syntax

it matches more closely the %% binary operator syntax See #51

8 files changed, 26 insertions(+), 20 deletions(-)

doc/langref.md+1-1
......@@ -85,7 +85,7 @@ SwitchItem = Expression | (Expression "..." Expression)
8585
8686WhileExpression = "while" "(" Expression ")" Expression
8787
88ForExpression = "for" "(" "Symbol" "," Expression option("," "Symbol") ")" Expression
88ForExpression = "for" "(" Expression ")" option("|" "Symbol" option("," "Symbol") "|") Expression
8989
9090BoolOrExpression = BoolAndExpression "||" BoolOrExpression | BoolAndExpression
9191
src/parser.cpp+14-8
......@@ -1797,7 +1797,7 @@ static AstNode *ast_parse_symbol(ParseContext *pc, int *token_index) {
17971797}
17981798
17991799/*
1800ForExpression : token(For) token(LParen) Symbol token(Comma) Expression option(token(Comma) token(Symbol)) token(RParen) Expression
1800ForExpression = "for" "(" Expression ")" option("|" "Symbol" option("," "Symbol") "|") Expression
18011801*/
18021802static AstNode *ast_parse_for_expr(ParseContext *pc, int *token_index, bool mandatory) {
18031803 Token *token = &pc->tokens->at(*token_index);
......@@ -1814,17 +1814,23 @@ static AstNode *ast_parse_for_expr(ParseContext *pc, int *token_index, bool mand
18141814 AstNode *node = ast_create_node(pc, NodeTypeForExpr, token);
18151815
18161816 ast_eat_token(pc, token_index, TokenIdLParen);
1817 node->data.for_expr.elem_node = ast_parse_symbol(pc, token_index);
1818 ast_eat_token(pc, token_index, TokenIdComma);
18191817 node->data.for_expr.array_expr = ast_parse_expression(pc, token_index, true);
1818 ast_eat_token(pc, token_index, TokenIdRParen);
18201819
1821 Token *comma = &pc->tokens->at(*token_index);
1822 if (comma->id == TokenIdComma) {
1820 Token *maybe_bar = &pc->tokens->at(*token_index);
1821 if (maybe_bar->id == TokenIdBinOr) {
18231822 *token_index += 1;
1824 node->data.for_expr.index_node = ast_parse_symbol(pc, token_index);
1825 }
1823 node->data.for_expr.elem_node = ast_parse_symbol(pc, token_index);
18261824
1827 ast_eat_token(pc, token_index, TokenIdRParen);
1825 Token *maybe_comma = &pc->tokens->at(*token_index);
1826 if (maybe_comma->id == TokenIdComma) {
1827 *token_index += 1;
1828
1829 node->data.for_expr.index_node = ast_parse_symbol(pc, token_index);
1830 }
1831
1832 ast_eat_token(pc, token_index, TokenIdBinOr);
1833 }
18281834
18291835 node->data.for_expr.body = ast_parse_expression(pc, token_index, true);
18301836
std/bootstrap.zig+1-1
......@@ -25,7 +25,7 @@ fn strlen(ptr: &const u8) -> isize {
2525
2626fn call_main() -> unreachable {
2727 var args: [argc][]u8 = undefined;
28 for (arg, args, i) {
28 for (args) |arg, i| {
2929 const ptr = argv[i];
3030 args[i] = ptr[0...strlen(ptr)];
3131 }
std/rand.zig+1-1
......@@ -59,7 +59,7 @@ pub struct Rand {
5959 }
6060
6161 fn generate_numbers(r: &Rand) {
62 for (item, r.array, i) {
62 for (r.array) |item, i| {
6363 const y : u32 = (item & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff);
6464 const untempered : u32 = r.array[(i + 397) % ARRAY_SIZE] ^ (y >> 1);
6565 r.array[i] = if ((y % 2) == 0) {
std/std.zig+1-1
......@@ -173,7 +173,7 @@ pub error Overflow;
173173pub fn parse_u64(buf: []u8, radix: u8) -> %u64 {
174174 var x : u64 = 0;
175175
176 for (c, buf) {
176 for (buf) |c| {
177177 const digit = char_to_digit(c);
178178
179179 if (digit > radix) {
std/test_runner.zig+1-1
......@@ -8,7 +8,7 @@ struct TestFn {
88extern var zig_test_fn_list: []TestFn;
99
1010pub fn run_tests() -> %void {
11 for (test_fn, zig_test_fn_list, i) {
11 for (zig_test_fn_list) |test_fn, i| {
1212 %%stderr.print_str("Test ");
1313 %%stderr.print_i64(i + 1);
1414 %%stderr.print_str("/");
test/run_tests.cpp+6-6
......@@ -1120,20 +1120,20 @@ import "std.zig";
11201120
11211121pub fn main(args: [][]u8) -> %void {
11221122 const array = []u8 {9, 8, 7, 6};
1123 for (item, array) {
1123 for (array) |item| {
11241124 %%stdout.print_u64(item);
11251125 %%stdout.printf("\n");
11261126 }
1127 for (item, array, index) {
1127 for (array) |item, index| {
11281128 %%stdout.print_i64(index);
11291129 %%stdout.printf("\n");
11301130 }
11311131 const unknown_size: []u8 = array;
1132 for (item, unknown_size) {
1132 for (unknown_size) |item| {
11331133 %%stdout.print_u64(item);
11341134 %%stdout.printf("\n");
11351135 }
1136 for (item, unknown_size, index) {
1136 for (unknown_size) |item, index| {
11371137 %%stdout.print_i64(index);
11381138 %%stdout.printf("\n");
11391139 }
......@@ -1145,7 +1145,7 @@ import "std.zig";
11451145
11461146pub fn main(args: [][]u8) -> %void {
11471147 const fns = []@typeof(fn1) { fn1, fn2, fn3, fn4, };
1148 for (f, fns) {
1148 for (fns) |f| {
11491149 %%stdout.print_u64(f());
11501150 %%stdout.printf("\n");
11511151 }
......@@ -1434,7 +1434,7 @@ export fn main(args: c_int, argv: &&u8) -> c_int {
14341434
14351435 qsort((&c_void)(array.ptr), c_ulong(array.len), @sizeof(i32), compare_fn);
14361436
1437 for (item, array, i) {
1437 for (array) |item, i| {
14381438 if (item != i) {
14391439 abort();
14401440 }
test/self_hosted.zig+1-1
......@@ -81,7 +81,7 @@ enum AnEnumWithPayload {
8181fn continue_in_for_loop() {
8282 const array = []i32 {1, 2, 3, 4, 5};
8383 var sum : i32 = 0;
84 for (x, array) {
84 for (array) |x| {
8585 sum += x;
8686 if (x < 3) {
8787 continue;