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)...@@ -85,7 +85,7 @@ SwitchItem = Expression | (Expression "..." Expression)
8585
86WhileExpression = "while" "(" Expression ")" Expression86WhileExpression = "while" "(" Expression ")" Expression
8787
88ForExpression = "for" "(" "Symbol" "," Expression option("," "Symbol") ")" Expression88ForExpression = "for" "(" Expression ")" option("|" "Symbol" option("," "Symbol") "|") Expression
8989
90BoolOrExpression = BoolAndExpression "||" BoolOrExpression | BoolAndExpression90BoolOrExpression = BoolAndExpression "||" BoolOrExpression | BoolAndExpression
9191
src/parser.cpp+14-8
...@@ -1797,7 +1797,7 @@ static AstNode *ast_parse_symbol(ParseContext *pc, int *token_index) {...@@ -1797,7 +1797,7 @@ static AstNode *ast_parse_symbol(ParseContext *pc, int *token_index) {
1797}1797}
17981798
1799/*1799/*
1800ForExpression : token(For) token(LParen) Symbol token(Comma) Expression option(token(Comma) token(Symbol)) token(RParen) Expression1800ForExpression = "for" "(" Expression ")" option("|" "Symbol" option("," "Symbol") "|") Expression
1801*/1801*/
1802static AstNode *ast_parse_for_expr(ParseContext *pc, int *token_index, bool mandatory) {1802static AstNode *ast_parse_for_expr(ParseContext *pc, int *token_index, bool mandatory) {
1803 Token *token = &pc->tokens->at(*token_index);1803 Token *token = &pc->tokens->at(*token_index);
...@@ -1814,17 +1814,23 @@ static AstNode *ast_parse_for_expr(ParseContext *pc, int *token_index, bool mand...@@ -1814,17 +1814,23 @@ static AstNode *ast_parse_for_expr(ParseContext *pc, int *token_index, bool mand
1814 AstNode *node = ast_create_node(pc, NodeTypeForExpr, token);1814 AstNode *node = ast_create_node(pc, NodeTypeForExpr, token);
18151815
1816 ast_eat_token(pc, token_index, TokenIdLParen);1816 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);
1819 node->data.for_expr.array_expr = ast_parse_expression(pc, token_index, true);1817 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);1820 Token *maybe_bar = &pc->tokens->at(*token_index);
1822 if (comma->id == TokenIdComma) {1821 if (maybe_bar->id == TokenIdBinOr) {
1823 *token_index += 1;1822 *token_index += 1;
1824 node->data.for_expr.index_node = ast_parse_symbol(pc, token_index);1823 node->data.for_expr.elem_node = ast_parse_symbol(pc, token_index);
1825 }
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
1829 node->data.for_expr.body = ast_parse_expression(pc, token_index, true);1835 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 {...@@ -25,7 +25,7 @@ fn strlen(ptr: &const u8) -> isize {
2525
26fn call_main() -> unreachable {26fn call_main() -> unreachable {
27 var args: [argc][]u8 = undefined;27 var args: [argc][]u8 = undefined;
28 for (arg, args, i) {28 for (args) |arg, i| {
29 const ptr = argv[i];29 const ptr = argv[i];
30 args[i] = ptr[0...strlen(ptr)];30 args[i] = ptr[0...strlen(ptr)];
31 }31 }
std/rand.zig+1-1
...@@ -59,7 +59,7 @@ pub struct Rand {...@@ -59,7 +59,7 @@ pub struct Rand {
59 }59 }
6060
61 fn generate_numbers(r: &Rand) {61 fn generate_numbers(r: &Rand) {
62 for (item, r.array, i) {62 for (r.array) |item, i| {
63 const y : u32 = (item & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff);63 const y : u32 = (item & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff);
64 const untempered : u32 = r.array[(i + 397) % ARRAY_SIZE] ^ (y >> 1);64 const untempered : u32 = r.array[(i + 397) % ARRAY_SIZE] ^ (y >> 1);
65 r.array[i] = if ((y % 2) == 0) {65 r.array[i] = if ((y % 2) == 0) {
std/std.zig+1-1
...@@ -173,7 +173,7 @@ pub error Overflow;...@@ -173,7 +173,7 @@ pub error Overflow;
173pub fn parse_u64(buf: []u8, radix: u8) -> %u64 {173pub fn parse_u64(buf: []u8, radix: u8) -> %u64 {
174 var x : u64 = 0;174 var x : u64 = 0;
175175
176 for (c, buf) {176 for (buf) |c| {
177 const digit = char_to_digit(c);177 const digit = char_to_digit(c);
178178
179 if (digit > radix) {179 if (digit > radix) {
std/test_runner.zig+1-1
...@@ -8,7 +8,7 @@ struct TestFn {...@@ -8,7 +8,7 @@ struct TestFn {
8extern var zig_test_fn_list: []TestFn;8extern var zig_test_fn_list: []TestFn;
99
10pub fn run_tests() -> %void {10pub fn run_tests() -> %void {
11 for (test_fn, zig_test_fn_list, i) {11 for (zig_test_fn_list) |test_fn, i| {
12 %%stderr.print_str("Test ");12 %%stderr.print_str("Test ");
13 %%stderr.print_i64(i + 1);13 %%stderr.print_i64(i + 1);
14 %%stderr.print_str("/");14 %%stderr.print_str("/");
test/run_tests.cpp+6-6
...@@ -1120,20 +1120,20 @@ import "std.zig";...@@ -1120,20 +1120,20 @@ import "std.zig";
11201120
1121pub fn main(args: [][]u8) -> %void {1121pub fn main(args: [][]u8) -> %void {
1122 const array = []u8 {9, 8, 7, 6};1122 const array = []u8 {9, 8, 7, 6};
1123 for (item, array) {1123 for (array) |item| {
1124 %%stdout.print_u64(item);1124 %%stdout.print_u64(item);
1125 %%stdout.printf("\n");1125 %%stdout.printf("\n");
1126 }1126 }
1127 for (item, array, index) {1127 for (array) |item, index| {
1128 %%stdout.print_i64(index);1128 %%stdout.print_i64(index);
1129 %%stdout.printf("\n");1129 %%stdout.printf("\n");
1130 }1130 }
1131 const unknown_size: []u8 = array;1131 const unknown_size: []u8 = array;
1132 for (item, unknown_size) {1132 for (unknown_size) |item| {
1133 %%stdout.print_u64(item);1133 %%stdout.print_u64(item);
1134 %%stdout.printf("\n");1134 %%stdout.printf("\n");
1135 }1135 }
1136 for (item, unknown_size, index) {1136 for (unknown_size) |item, index| {
1137 %%stdout.print_i64(index);1137 %%stdout.print_i64(index);
1138 %%stdout.printf("\n");1138 %%stdout.printf("\n");
1139 }1139 }
...@@ -1145,7 +1145,7 @@ import "std.zig";...@@ -1145,7 +1145,7 @@ import "std.zig";
11451145
1146pub fn main(args: [][]u8) -> %void {1146pub fn main(args: [][]u8) -> %void {
1147 const fns = []@typeof(fn1) { fn1, fn2, fn3, fn4, };1147 const fns = []@typeof(fn1) { fn1, fn2, fn3, fn4, };
1148 for (f, fns) {1148 for (fns) |f| {
1149 %%stdout.print_u64(f());1149 %%stdout.print_u64(f());
1150 %%stdout.printf("\n");1150 %%stdout.printf("\n");
1151 }1151 }
...@@ -1434,7 +1434,7 @@ export fn main(args: c_int, argv: &&u8) -> c_int {...@@ -1434,7 +1434,7 @@ export fn main(args: c_int, argv: &&u8) -> c_int {
14341434
1435 qsort((&c_void)(array.ptr), c_ulong(array.len), @sizeof(i32), compare_fn);1435 qsort((&c_void)(array.ptr), c_ulong(array.len), @sizeof(i32), compare_fn);
14361436
1437 for (item, array, i) {1437 for (array) |item, i| {
1438 if (item != i) {1438 if (item != i) {
1439 abort();1439 abort();
1440 }1440 }
test/self_hosted.zig+1-1
...@@ -81,7 +81,7 @@ enum AnEnumWithPayload {...@@ -81,7 +81,7 @@ enum AnEnumWithPayload {
81fn continue_in_for_loop() {81fn continue_in_for_loop() {
82 const array = []i32 {1, 2, 3, 4, 5};82 const array = []i32 {1, 2, 3, 4, 5};
83 var sum : i32 = 0;83 var sum : i32 = 0;
84 for (x, array) {84 for (array) |x| {
85 sum += x;85 sum += x;
86 if (x < 3) {86 if (x < 3) {
87 continue;87 continue;