authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-03 18:12:07-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-03 18:12:07-04:00
log698829b772fe39c4311a75b20324256b8d7392b1
treec1434e819ef38698a79a711c164ca62d9418a9c7
parent644ea2dde9fbb1f948cf12115df2a15e908f3c29

change while syntax

Old: ``` while (condition; expression) {} ``` New: ``` while (condition) : (expression) {} ``` This is in preparation to allow nullable and error union types as the condition. See #357

25 files changed, 97 insertions(+), 65 deletions(-)

doc/langref.md+2-2
...@@ -79,8 +79,6 @@ SwitchProng = (list(SwitchItem, ",") | "else") "=>" option("|" option("*") Symbo...@@ -79,8 +79,6 @@ SwitchProng = (list(SwitchItem, ",") | "else") "=>" option("|" option("*") Symbo
7979
80SwitchItem = Expression | (Expression "..." Expression)80SwitchItem = Expression | (Expression "..." Expression)
8181
82WhileExpression(body) = "while" "(" Expression option(";" Expression) ")" body
83
84ForExpression(body) = "for" "(" Expression ")" option("|" option("*") Symbol option("," Symbol) "|") body82ForExpression(body) = "for" "(" Expression ")" option("|" option("*") Symbol option("," Symbol) "|") body
8583
86BoolOrExpression = BoolAndExpression "or" BoolOrExpression | BoolAndExpression84BoolOrExpression = BoolAndExpression "or" BoolOrExpression | BoolAndExpression
...@@ -95,6 +93,8 @@ TryExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|")...@@ -95,6 +93,8 @@ TryExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|")
9593
96TestExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" BlockExpression(body))94TestExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" BlockExpression(body))
9795
96WhileExpression(body) = "while" "(" Expression ")" option("|" option("*") Symbol "|") option(":" "(" Expression ")") body option("else" option("|" Symbol "|") BlockExpression(body))
97
98BoolAndExpression = ComparisonExpression "and" BoolAndExpression | ComparisonExpression98BoolAndExpression = ComparisonExpression "and" BoolAndExpression | ComparisonExpression
9999
100ComparisonExpression = BinaryOrExpression ComparisonOperator BinaryOrExpression | BinaryOrExpression100ComparisonExpression = BinaryOrExpression ComparisonOperator BinaryOrExpression | BinaryOrExpression
example/cat/main.zig+1-1
...@@ -7,7 +7,7 @@ pub fn main() -> %void {...@@ -7,7 +7,7 @@ pub fn main() -> %void {
7 const exe = os.args.at(0);7 const exe = os.args.at(0);
8 var catted_anything = false;8 var catted_anything = false;
9 var arg_i: usize = 1;9 var arg_i: usize = 1;
10 while (arg_i < os.args.count(); arg_i += 1) {10 while (arg_i < os.args.count()) : (arg_i += 1) {
11 const arg = os.args.at(arg_i);11 const arg = os.args.at(arg_i);
12 if (mem.eql(u8, arg, "-")) {12 if (mem.eql(u8, arg, "-")) {
13 catted_anything = true;13 catted_anything = true;
src/all_types.hpp+4
...@@ -609,8 +609,12 @@ struct AstNodeTestExpr {...@@ -609,8 +609,12 @@ struct AstNodeTestExpr {
609609
610struct AstNodeWhileExpr {610struct AstNodeWhileExpr {
611 AstNode *condition;611 AstNode *condition;
612 Buf *var_symbol;
613 bool var_is_ptr;
612 AstNode *continue_expr;614 AstNode *continue_expr;
613 AstNode *body;615 AstNode *body;
616 AstNode *else_node;
617 Buf *err_symbol;
614 bool is_inline;618 bool is_inline;
615};619};
616620
src/parser.cpp+37-9
...@@ -1580,7 +1580,7 @@ static AstNode *ast_parse_bool_or_expr(ParseContext *pc, size_t *token_index, bo...@@ -1580,7 +1580,7 @@ static AstNode *ast_parse_bool_or_expr(ParseContext *pc, size_t *token_index, bo
1580}1580}
15811581
1582/*1582/*
1583WhileExpression(body) = option("inline") "while" "(" Expression option(";" Expression) ")" body1583WhileExpression(body) = option("inline") "while" "(" Expression ")" option("|" option("*") Symbol "|") option(":" "(" Expression ")") body option("else" option("|" Symbol "|") BlockExpression(body))
1584*/1584*/
1585static AstNode *ast_parse_while_expr(ParseContext *pc, size_t *token_index, bool mandatory) {1585static AstNode *ast_parse_while_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
1586 Token *first_token = &pc->tokens->at(*token_index);1586 Token *first_token = &pc->tokens->at(*token_index);
...@@ -1613,21 +1613,49 @@ static AstNode *ast_parse_while_expr(ParseContext *pc, size_t *token_index, bool...@@ -1613,21 +1613,49 @@ static AstNode *ast_parse_while_expr(ParseContext *pc, size_t *token_index, bool
16131613
1614 ast_eat_token(pc, token_index, TokenIdLParen);1614 ast_eat_token(pc, token_index, TokenIdLParen);
1615 node->data.while_expr.condition = ast_parse_expression(pc, token_index, true);1615 node->data.while_expr.condition = ast_parse_expression(pc, token_index, true);
1616 ast_eat_token(pc, token_index, TokenIdRParen);
16161617
1617 Token *semi_or_rparen = &pc->tokens->at(*token_index);1618 Token *open_bar_tok = &pc->tokens->at(*token_index);
16181619 if (open_bar_tok->id == TokenIdBinOr) {
1619 if (semi_or_rparen->id == TokenIdRParen) {
1620 *token_index += 1;1620 *token_index += 1;
1621 node->data.while_expr.body = ast_parse_block_or_expression(pc, token_index, true);1621
1622 } else if (semi_or_rparen->id == TokenIdSemicolon) {1622 Token *star_tok = &pc->tokens->at(*token_index);
1623 if (star_tok->id == TokenIdStar) {
1624 *token_index += 1;
1625 node->data.while_expr.var_is_ptr = true;
1626 }
1627
1628 Token *var_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
1629 node->data.while_expr.var_symbol = token_buf(var_name_tok);
1630 ast_eat_token(pc, token_index, TokenIdBinOr);
1631 }
1632
1633 Token *colon_tok = &pc->tokens->at(*token_index);
1634 if (colon_tok->id == TokenIdColon) {
1623 *token_index += 1;1635 *token_index += 1;
1636 ast_eat_token(pc, token_index, TokenIdLParen);
1624 node->data.while_expr.continue_expr = ast_parse_expression(pc, token_index, true);1637 node->data.while_expr.continue_expr = ast_parse_expression(pc, token_index, true);
1625 ast_eat_token(pc, token_index, TokenIdRParen);1638 ast_eat_token(pc, token_index, TokenIdRParen);
1626 node->data.while_expr.body = ast_parse_block_or_expression(pc, token_index, true);
1627 } else {
1628 ast_invalid_token_error(pc, semi_or_rparen);
1629 }1639 }
16301640
1641 node->data.while_expr.body = ast_parse_block_or_expression(pc, token_index, true);
1642
1643 Token *else_tok = &pc->tokens->at(*token_index);
1644 if (else_tok->id == TokenIdKeywordElse) {
1645 *token_index += 1;
1646
1647 Token *else_bar_tok = &pc->tokens->at(*token_index);
1648 if (else_bar_tok->id == TokenIdBinOr) {
1649 *token_index += 1;
1650
1651 Token *err_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
1652 node->data.while_expr.err_symbol = token_buf(err_name_tok);
1653
1654 ast_eat_token(pc, token_index, TokenIdBinOr);
1655 }
1656
1657 node->data.while_expr.body = ast_parse_block_or_expression(pc, token_index, true);
1658 }
16311659
1632 return node;1660 return node;
1633}1661}
std/base64.zig+1-1
...@@ -17,7 +17,7 @@ pub fn encodeWithAlphabet(dest: []u8, source: []const u8, alphabet: []const u8)...@@ -17,7 +17,7 @@ pub fn encodeWithAlphabet(dest: []u8, source: []const u8, alphabet: []const u8)
1717
18 var i: usize = 0;18 var i: usize = 0;
19 var out_index: usize = 0;19 var out_index: usize = 0;
20 while (i + 2 < source.len; i += 3) {20 while (i + 2 < source.len) : (i += 3) {
21 dest[out_index] = alphabet[(source[i] >> 2) & 0x3f];21 dest[out_index] = alphabet[(source[i] >> 2) & 0x3f];
22 out_index += 1;22 out_index += 1;
2323
std/cstr.zig+2-2
...@@ -3,13 +3,13 @@ const assert = debug.assert;...@@ -3,13 +3,13 @@ const assert = debug.assert;
33
4pub fn len(ptr: &const u8) -> usize {4pub fn len(ptr: &const u8) -> usize {
5 var count: usize = 0;5 var count: usize = 0;
6 while (ptr[count] != 0; count += 1) {}6 while (ptr[count] != 0) : (count += 1) {}
7 return count;7 return count;
8}8}
99
10pub fn cmp(a: &const u8, b: &const u8) -> i8 {10pub fn cmp(a: &const u8, b: &const u8) -> i8 {
11 var index: usize = 0;11 var index: usize = 0;
12 while (a[index] == b[index] and a[index] != 0; index += 1) {}12 while (a[index] == b[index] and a[index] != 0) : (index += 1) {}
13 if (a[index] > b[index]) {13 if (a[index] > b[index]) {
14 return 1;14 return 1;
15 } else if (a[index] < b[index]) {15 } else if (a[index] < b[index]) {
std/debug.zig+4-4
...@@ -79,7 +79,7 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty...@@ -79,7 +79,7 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty
79 var ignored_count: usize = 0;79 var ignored_count: usize = 0;
8080
81 var fp = usize(@frameAddress());81 var fp = usize(@frameAddress());
82 while (fp != 0; fp = *@intToPtr(&const usize, fp)) {82 while (fp != 0) : (fp = *@intToPtr(&const usize, fp)) {
83 if (ignored_count < ignore_frame_count) {83 if (ignored_count < ignore_frame_count) {
84 ignored_count += 1;84 ignored_count += 1;
85 continue;85 continue;
...@@ -108,7 +108,7 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty...@@ -108,7 +108,7 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty
108 if (line_info.column == 0) {108 if (line_info.column == 0) {
109 %return out_stream.write("\n");109 %return out_stream.write("\n");
110 } else {110 } else {
111 {var col_i: usize = 1; while (col_i < line_info.column; col_i += 1) {111 {var col_i: usize = 1; while (col_i < line_info.column) : (col_i += 1) {
112 %return out_stream.writeByte(' ');112 %return out_stream.writeByte(' ');
113 }}113 }}
114 %return out_stream.write(GREEN ++ "^" ++ RESET ++ "\n");114 %return out_stream.write(GREEN ++ "^" ++ RESET ++ "\n");
...@@ -594,7 +594,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe...@@ -594,7 +594,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
594 var this_offset = st.debug_line.offset;594 var this_offset = st.debug_line.offset;
595 var this_index: usize = 0;595 var this_index: usize = 0;
596596
597 while (this_offset < debug_line_end; this_index += 1) {597 while (this_offset < debug_line_end) : (this_index += 1) {
598 %return in_stream.seekTo(this_offset);598 %return in_stream.seekTo(this_offset);
599599
600 var is_64: bool = undefined;600 var is_64: bool = undefined;
...@@ -628,7 +628,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe...@@ -628,7 +628,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
628628
629 const standard_opcode_lengths = %return st.allocator().alloc(u8, opcode_base - 1);629 const standard_opcode_lengths = %return st.allocator().alloc(u8, opcode_base - 1);
630630
631 {var i: usize = 0; while (i < opcode_base - 1; i += 1) {631 {var i: usize = 0; while (i < opcode_base - 1) : (i += 1) {
632 standard_opcode_lengths[i] = %return in_stream.readByte();632 standard_opcode_lengths[i] = %return in_stream.readByte();
633 }}633 }}
634634
std/fmt.zig+1-1
...@@ -200,7 +200,7 @@ pub fn formatBuf(buf: []const u8, width: usize,...@@ -200,7 +200,7 @@ pub fn formatBuf(buf: []const u8, width: usize,
200200
201 var leftover_padding = if (width > buf.len) (width - buf.len) else return true;201 var leftover_padding = if (width > buf.len) (width - buf.len) else return true;
202 const pad_byte: u8 = ' ';202 const pad_byte: u8 = ' ';
203 while (leftover_padding > 0; leftover_padding -= 1) {203 while (leftover_padding > 0) : (leftover_padding -= 1) {
204 if (!output(context, (&pad_byte)[0...1]))204 if (!output(context, (&pad_byte)[0...1]))
205 return false;205 return false;
206 }206 }
std/hash_map.zig+5-5
...@@ -43,7 +43,7 @@ pub fn HashMap(comptime K: type, comptime V: type,...@@ -43,7 +43,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
43 assert(it.initial_modification_count == it.hm.modification_count); // concurrent modification43 assert(it.initial_modification_count == it.hm.modification_count); // concurrent modification
44 }44 }
45 if (it.count >= it.hm.size) return null;45 if (it.count >= it.hm.size) return null;
46 while (it.index < it.hm.entries.len; it.index += 1) {46 while (it.index < it.hm.entries.len) : (it.index += 1) {
47 const entry = &it.hm.entries[it.index];47 const entry = &it.hm.entries[it.index];
48 if (entry.used) {48 if (entry.used) {
49 it.index += 1;49 it.index += 1;
...@@ -112,7 +112,7 @@ pub fn HashMap(comptime K: type, comptime V: type,...@@ -112,7 +112,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
112 pub fn remove(hm: &Self, key: K) -> ?&Entry {112 pub fn remove(hm: &Self, key: K) -> ?&Entry {
113 hm.incrementModificationCount();113 hm.incrementModificationCount();
114 const start_index = hm.keyToIndex(key);114 const start_index = hm.keyToIndex(key);
115 {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index; roll_over += 1) {115 {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
116 const index = (start_index + roll_over) % hm.entries.len;116 const index = (start_index + roll_over) % hm.entries.len;
117 var entry = &hm.entries[index];117 var entry = &hm.entries[index];
118118
...@@ -121,7 +121,7 @@ pub fn HashMap(comptime K: type, comptime V: type,...@@ -121,7 +121,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
121121
122 if (!eql(entry.key, key)) continue;122 if (!eql(entry.key, key)) continue;
123123
124 while (roll_over < hm.entries.len; roll_over += 1) {124 while (roll_over < hm.entries.len) : (roll_over += 1) {
125 const next_index = (start_index + roll_over + 1) % hm.entries.len;125 const next_index = (start_index + roll_over + 1) % hm.entries.len;
126 const next_entry = &hm.entries[next_index];126 const next_entry = &hm.entries[next_index];
127 if (!next_entry.used or next_entry.distance_from_start_index == 0) {127 if (!next_entry.used or next_entry.distance_from_start_index == 0) {
...@@ -169,7 +169,7 @@ pub fn HashMap(comptime K: type, comptime V: type,...@@ -169,7 +169,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
169 const start_index = hm.keyToIndex(key);169 const start_index = hm.keyToIndex(key);
170 var roll_over: usize = 0;170 var roll_over: usize = 0;
171 var distance_from_start_index: usize = 0;171 var distance_from_start_index: usize = 0;
172 while (roll_over < hm.entries.len; {roll_over += 1; distance_from_start_index += 1}) {172 while (roll_over < hm.entries.len) : ({roll_over += 1; distance_from_start_index += 1}) {
173 const index = (start_index + roll_over) % hm.entries.len;173 const index = (start_index + roll_over) % hm.entries.len;
174 const entry = &hm.entries[index];174 const entry = &hm.entries[index];
175175
...@@ -215,7 +215,7 @@ pub fn HashMap(comptime K: type, comptime V: type,...@@ -215,7 +215,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
215215
216 fn internalGet(hm: &Self, key: K) -> ?&Entry {216 fn internalGet(hm: &Self, key: K) -> ?&Entry {
217 const start_index = hm.keyToIndex(key);217 const start_index = hm.keyToIndex(key);
218 {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index; roll_over += 1) {218 {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
219 const index = (start_index + roll_over) % hm.entries.len;219 const index = (start_index + roll_over) % hm.entries.len;
220 const entry = &hm.entries[index];220 const entry = &hm.entries[index];
221221
std/list.zig+2-2
...@@ -78,11 +78,11 @@ test "basic list test" {...@@ -78,11 +78,11 @@ test "basic list test" {
78 var list = List(i32).init(&debug.global_allocator);78 var list = List(i32).init(&debug.global_allocator);
79 defer list.deinit();79 defer list.deinit();
8080
81 {var i: usize = 0; while (i < 10; i += 1) {81 {var i: usize = 0; while (i < 10) : (i += 1) {
82 %%list.append(i32(i + 1));82 %%list.append(i32(i + 1));
83 }}83 }}
8484
85 {var i: usize = 0; while (i < 10; i += 1) {85 {var i: usize = 0; while (i < 10) : (i += 1) {
86 assert(list.items[i] == i32(i + 1));86 assert(list.items[i] == i32(i + 1));
87 }}87 }}
8888
std/mem.zig+5-5
...@@ -123,7 +123,7 @@ pub fn set(comptime T: type, dest: []T, value: T) {...@@ -123,7 +123,7 @@ pub fn set(comptime T: type, dest: []T, value: T) {
123pub fn cmp(comptime T: type, a: []const T, b: []const T) -> Cmp {123pub fn cmp(comptime T: type, a: []const T, b: []const T) -> Cmp {
124 const n = math.min(a.len, b.len);124 const n = math.min(a.len, b.len);
125 var i: usize = 0;125 var i: usize = 0;
126 while (i < n; i += 1) {126 while (i < n) : (i += 1) {
127 if (a[i] == b[i]) continue;127 if (a[i] == b[i]) continue;
128 return if (a[i] > b[i]) Cmp.Greater else if (a[i] < b[i]) Cmp.Less else Cmp.Equal;128 return if (a[i] > b[i]) Cmp.Greater else if (a[i] < b[i]) Cmp.Less else Cmp.Equal;
129 }129 }
...@@ -164,7 +164,7 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) -> ?usi...@@ -164,7 +164,7 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) -> ?usi
164164
165 var i: usize = 0;165 var i: usize = 0;
166 const end = haystack.len - needle.len;166 const end = haystack.len - needle.len;
167 while (i <= end; i += 1) {167 while (i <= end) : (i += 1) {
168 if (eql(T, haystack[i...i + needle.len], needle))168 if (eql(T, haystack[i...i + needle.len], needle))
169 return i;169 return i;
170 }170 }
...@@ -263,14 +263,14 @@ const SplitIterator = struct {...@@ -263,14 +263,14 @@ const SplitIterator = struct {
263263
264 pub fn next(self: &SplitIterator) -> ?[]const u8 {264 pub fn next(self: &SplitIterator) -> ?[]const u8 {
265 // move to beginning of token265 // move to beginning of token
266 while (self.index < self.s.len and self.s[self.index] == self.c; self.index += 1) {}266 while (self.index < self.s.len and self.s[self.index] == self.c) : (self.index += 1) {}
267 const start = self.index;267 const start = self.index;
268 if (start == self.s.len) {268 if (start == self.s.len) {
269 return null;269 return null;
270 }270 }
271271
272 // move to end of token272 // move to end of token
273 while (self.index < self.s.len and self.s[self.index] != self.c; self.index += 1) {}273 while (self.index < self.s.len and self.s[self.index] != self.c) : (self.index += 1) {}
274 const end = self.index;274 const end = self.index;
275275
276 return self.s[start...end];276 return self.s[start...end];
...@@ -280,7 +280,7 @@ const SplitIterator = struct {...@@ -280,7 +280,7 @@ const SplitIterator = struct {
280 pub fn rest(self: &const SplitIterator) -> []const u8 {280 pub fn rest(self: &const SplitIterator) -> []const u8 {
281 // move to beginning of token281 // move to beginning of token
282 var index: usize = self.index;282 var index: usize = self.index;
283 while (index < self.s.len and self.s[index] == self.c; index += 1) {}283 while (index < self.s.len and self.s[index] == self.c) : (index += 1) {}
284 return self.s[index...];284 return self.s[index...];
285 }285 }
286};286};
std/os/index.zig+5-5
...@@ -276,7 +276,7 @@ pub fn posixExecve(exe_path: []const u8, argv: []const []const u8, env_map: &con...@@ -276,7 +276,7 @@ pub fn posixExecve(exe_path: []const u8, argv: []const []const u8, env_map: &con
276 {276 {
277 var it = env_map.iterator();277 var it = env_map.iterator();
278 var i: usize = 0;278 var i: usize = 0;
279 while (true; i += 1) {279 while (true) : (i += 1) {
280 const pair = it.next() ?? break;280 const pair = it.next() ?? break;
281281
282 const env_buf = %return allocator.alloc(u8, pair.key.len + pair.value.len + 2);282 const env_buf = %return allocator.alloc(u8, pair.key.len + pair.value.len + 2);
...@@ -354,11 +354,11 @@ pub fn getEnvMap(allocator: &Allocator) -> %BufMap {...@@ -354,11 +354,11 @@ pub fn getEnvMap(allocator: &Allocator) -> %BufMap {
354354
355 for (environ_raw) |ptr| {355 for (environ_raw) |ptr| {
356 var line_i: usize = 0;356 var line_i: usize = 0;
357 while (ptr[line_i] != 0 and ptr[line_i] != '='; line_i += 1) {}357 while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {}
358 const key = ptr[0...line_i];358 const key = ptr[0...line_i];
359359
360 var end_i: usize = line_i;360 var end_i: usize = line_i;
361 while (ptr[end_i] != 0; end_i += 1) {}361 while (ptr[end_i] != 0) : (end_i += 1) {}
362 const value = ptr[line_i + 1...end_i];362 const value = ptr[line_i + 1...end_i];
363363
364 %return result.set(key, value);364 %return result.set(key, value);
...@@ -369,13 +369,13 @@ pub fn getEnvMap(allocator: &Allocator) -> %BufMap {...@@ -369,13 +369,13 @@ pub fn getEnvMap(allocator: &Allocator) -> %BufMap {
369pub fn getEnv(key: []const u8) -> ?[]const u8 {369pub fn getEnv(key: []const u8) -> ?[]const u8 {
370 for (environ_raw) |ptr| {370 for (environ_raw) |ptr| {
371 var line_i: usize = 0;371 var line_i: usize = 0;
372 while (ptr[line_i] != 0 and ptr[line_i] != '='; line_i += 1) {}372 while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {}
373 const this_key = ptr[0...line_i];373 const this_key = ptr[0...line_i];
374 if (!mem.eql(u8, key, this_key))374 if (!mem.eql(u8, key, this_key))
375 continue;375 continue;
376376
377 var end_i: usize = line_i;377 var end_i: usize = line_i;
378 while (ptr[end_i] != 0; end_i += 1) {}378 while (ptr[end_i] != 0) : (end_i += 1) {}
379 const this_value = ptr[line_i + 1...end_i];379 const this_value = ptr[line_i + 1...end_i];
380380
381 return this_value;381 return this_value;
std/os/path.zig+2-2
...@@ -25,7 +25,7 @@ pub fn join(allocator: &Allocator, paths: ...) -> %[]u8 {...@@ -25,7 +25,7 @@ pub fn join(allocator: &Allocator, paths: ...) -> %[]u8 {
25 var total_paths_len: usize = paths.len; // 1 slash per path25 var total_paths_len: usize = paths.len; // 1 slash per path
26 {26 {
27 comptime var path_i = 0;27 comptime var path_i = 0;
28 inline while (path_i < paths.len; path_i += 1) {28 inline while (path_i < paths.len) : (path_i += 1) {
29 const arg = ([]const u8)(paths[path_i]);29 const arg = ([]const u8)(paths[path_i]);
30 total_paths_len += arg.len;30 total_paths_len += arg.len;
31 }31 }
...@@ -74,7 +74,7 @@ pub fn isAbsolute(path: []const u8) -> bool {...@@ -74,7 +74,7 @@ pub fn isAbsolute(path: []const u8) -> bool {
74pub fn resolve(allocator: &Allocator, args: ...) -> %[]u8 {74pub fn resolve(allocator: &Allocator, args: ...) -> %[]u8 {
75 var paths: [args.len][]const u8 = undefined;75 var paths: [args.len][]const u8 = undefined;
76 comptime var arg_i = 0;76 comptime var arg_i = 0;
77 inline while (arg_i < args.len; arg_i += 1) {77 inline while (arg_i < args.len) : (arg_i += 1) {
78 paths[arg_i] = args[arg_i];78 paths[arg_i] = args[arg_i];
79 }79 }
80 return resolveSlice(allocator, paths);80 return resolveSlice(allocator, paths);
std/rand.zig+4-4
...@@ -121,7 +121,7 @@ fn MersenneTwister(...@@ -121,7 +121,7 @@ fn MersenneTwister(
121 var prev_value = seed;121 var prev_value = seed;
122 mt.array[0] = prev_value;122 mt.array[0] = prev_value;
123 var i: usize = 1;123 var i: usize = 1;
124 while (i < n; i += 1) {124 while (i < n) : (i += 1) {
125 prev_value = int(i) +% f *% (prev_value ^ (prev_value >> (int.bit_count - 2)));125 prev_value = int(i) +% f *% (prev_value ^ (prev_value >> (int.bit_count - 2)));
126 mt.array[i] = prev_value;126 mt.array[i] = prev_value;
127 }127 }
...@@ -136,12 +136,12 @@ fn MersenneTwister(...@@ -136,12 +136,12 @@ fn MersenneTwister(
136 if (mt.index >= n) {136 if (mt.index >= n) {
137 var i: usize = 0;137 var i: usize = 0;
138138
139 while (i < n - m; i += 1) {139 while (i < n - m) : (i += 1) {
140 const x = (mt.array[i] & UM) | (mt.array[i + 1] & LM);140 const x = (mt.array[i] & UM) | (mt.array[i + 1] & LM);
141 mt.array[i] = mt.array[i + m] ^ (x >> 1) ^ mag01[x & 0x1];141 mt.array[i] = mt.array[i + m] ^ (x >> 1) ^ mag01[x & 0x1];
142 }142 }
143143
144 while (i < n - 1; i += 1) {144 while (i < n - 1) : (i += 1) {
145 const x = (mt.array[i] & UM) | (mt.array[i + 1] & LM);145 const x = (mt.array[i] & UM) | (mt.array[i + 1] & LM);
146 mt.array[i] = mt.array[i + m - n] ^ (x >> 1) ^ mag01[x & 0x1];146 mt.array[i] = mt.array[i + m - n] ^ (x >> 1) ^ mag01[x & 0x1];
147147
...@@ -168,7 +168,7 @@ fn MersenneTwister(...@@ -168,7 +168,7 @@ fn MersenneTwister(
168test "rand float 32" {168test "rand float 32" {
169 var r = Rand.init(42);169 var r = Rand.init(42);
170 var i: usize = 0;170 var i: usize = 0;
171 while (i < 1000; i += 1) {171 while (i < 1000) : (i += 1) {
172 const val = r.float(f32);172 const val = r.float(f32);
173 assert(val >= 0.0);173 assert(val >= 0.0);
174 assert(val < 1.0);174 assert(val < 1.0);
std/special/bootstrap.zig+1-1
...@@ -42,7 +42,7 @@ fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void {...@@ -42,7 +42,7 @@ fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void {
42 std.os.args.raw = argv[0...argc];42 std.os.args.raw = argv[0...argc];
4343
44 var env_count: usize = 0;44 var env_count: usize = 0;
45 while (envp[env_count] != null; env_count += 1) {}45 while (envp[env_count] != null) : (env_count += 1) {}
46 std.os.environ_raw = @ptrCast(&&u8, envp)[0...env_count];46 std.os.environ_raw = @ptrCast(&&u8, envp)[0...env_count];
4747
48 std.debug.user_main_fn = root.main;48 std.debug.user_main_fn = root.main;
std/special/build_runner.zig+1-1
...@@ -55,7 +55,7 @@ pub fn main() -> %void {...@@ -55,7 +55,7 @@ pub fn main() -> %void {
5555
56 var prefix: ?[]const u8 = null;56 var prefix: ?[]const u8 = null;
5757
58 while (arg_i < os.args.count(); arg_i += 1) {58 while (arg_i < os.args.count()) : (arg_i += 1) {
59 const arg = os.args.at(arg_i);59 const arg = os.args.at(arg_i);
60 if (mem.startsWith(u8, arg, "-D")) {60 if (mem.startsWith(u8, arg, "-D")) {
61 const option_contents = arg[2...];61 const option_contents = arg[2...];
std/special/builtin.zig+2-2
...@@ -10,7 +10,7 @@ export fn memset(dest: ?&u8, c: u8, n: usize) {...@@ -10,7 +10,7 @@ export fn memset(dest: ?&u8, c: u8, n: usize) {
10 @setDebugSafety(this, false);10 @setDebugSafety(this, false);
1111
12 var index: usize = 0;12 var index: usize = 0;
13 while (index != n; index += 1)13 while (index != n) : (index += 1)
14 (??dest)[index] = c;14 (??dest)[index] = c;
15}15}
1616
...@@ -18,7 +18,7 @@ export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) {...@@ -18,7 +18,7 @@ export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) {
18 @setDebugSafety(this, false);18 @setDebugSafety(this, false);
1919
20 var index: usize = 0;20 var index: usize = 0;
21 while (index != n; index += 1)21 while (index != n) : (index += 1)
22 (??dest)[index] = (??src)[index];22 (??dest)[index] = (??src)[index];
23}23}
2424
std/special/compiler_rt.zig+1-1
...@@ -296,7 +296,7 @@ export fn __udivsi3(n: su_int, d: su_int) -> su_int {...@@ -296,7 +296,7 @@ export fn __udivsi3(n: su_int, d: su_int) -> su_int {
296 var q: su_int = n << (n_uword_bits - sr);296 var q: su_int = n << (n_uword_bits - sr);
297 var r: su_int = n >> sr;297 var r: su_int = n >> sr;
298 var carry: su_int = 0;298 var carry: su_int = 0;
299 while (sr > 0; sr -= 1) {299 while (sr > 0) : (sr -= 1) {
300 // r:q = ((r:q) << 1) | carry300 // r:q = ((r:q) << 1) | carry
301 r = (r << 1) | (q >> (n_uword_bits - 1));301 r = (r << 1) | (q >> (n_uword_bits - 1));
302 q = (q << 1) | carry;302 q = (q << 1) | carry;
test/cases/const_slice_child.zig+1-1
...@@ -31,7 +31,7 @@ fn bar(argc: usize) {...@@ -31,7 +31,7 @@ fn bar(argc: usize) {
3131
32fn strlen(ptr: &const u8) -> usize {32fn strlen(ptr: &const u8) -> usize {
33 var count: usize = 0;33 var count: usize = 0;
34 while (ptr[count] != 0; count += 1) {}34 while (ptr[count] != 0) : (count += 1) {}
35 return count;35 return count;
36}36}
3737
test/cases/eval.zig+3-3
...@@ -22,7 +22,7 @@ test "testStaticAddOne" {...@@ -22,7 +22,7 @@ test "testStaticAddOne" {
22test "inlinedLoop" {22test "inlinedLoop" {
23 comptime var i = 0;23 comptime var i = 0;
24 comptime var sum = 0;24 comptime var sum = 0;
25 inline while (i <= 5; i += 1)25 inline while (i <= 5) : (i += 1)
26 sum += i;26 sum += i;
27 assert(sum == 15);27 assert(sum == 15);
28}28}
...@@ -157,7 +157,7 @@ test "tryToTrickEvalWithRuntimeIf" {...@@ -157,7 +157,7 @@ test "tryToTrickEvalWithRuntimeIf" {
157157
158fn testTryToTrickEvalWithRuntimeIf(b: bool) -> usize {158fn testTryToTrickEvalWithRuntimeIf(b: bool) -> usize {
159 comptime var i: usize = 0;159 comptime var i: usize = 0;
160 inline while (i < 10; i += 1) {160 inline while (i < 10) : (i += 1) {
161 const result = if (b) false else true;161 const result = if (b) false else true;
162 }162 }
163 comptime {163 comptime {
...@@ -208,7 +208,7 @@ fn three(value: i32) -> i32 { value + 3 }...@@ -208,7 +208,7 @@ fn three(value: i32) -> i32 { value + 3 }
208fn performFn(comptime prefix_char: u8, start_value: i32) -> i32 {208fn performFn(comptime prefix_char: u8, start_value: i32) -> i32 {
209 var result: i32 = start_value;209 var result: i32 = start_value;
210 comptime var i = 0;210 comptime var i = 0;
211 inline while (i < cmd_fns.len; i += 1) {211 inline while (i < cmd_fns.len) : (i += 1) {
212 if (cmd_fns[i].name[0] == prefix_char) {212 if (cmd_fns[i].name[0] == prefix_char) {
213 result = cmd_fns[i].func(result);213 result = cmd_fns[i].func(result);
214 }214 }
test/cases/misc.zig+1-1
...@@ -380,7 +380,7 @@ test "cStringConcatenation" {...@@ -380,7 +380,7 @@ test "cStringConcatenation" {
380380
381 const len = cstr.len(b);381 const len = cstr.len(b);
382 const len_with_null = len + 1;382 const len_with_null = len + 1;
383 {var i: u32 = 0; while (i < len_with_null; i += 1) {383 {var i: u32 = 0; while (i < len_with_null) : (i += 1) {
384 assert(a[i] == b[i]);384 assert(a[i] == b[i]);
385 }}385 }}
386 assert(a[len] == 0);386 assert(a[len] == 0);
test/cases/var_args.zig+1-1
...@@ -2,7 +2,7 @@ const assert = @import("std").debug.assert;...@@ -2,7 +2,7 @@ const assert = @import("std").debug.assert;
22
3fn add(args: ...) -> i32 {3fn add(args: ...) -> i32 {
4 var sum = i32(0);4 var sum = i32(0);
5 {comptime var i: usize = 0; inline while (i < args.len; i += 1) {5 {comptime var i: usize = 0; inline while (i < args.len) : (i += 1) {
6 sum += args[i];6 sum += args[i];
7 }}7 }}
8 return sum;8 return sum;
test/cases/while.zig+1-1
...@@ -58,7 +58,7 @@ fn returnWithImplicitCastFromWhileLoopTest() -> %void {...@@ -58,7 +58,7 @@ fn returnWithImplicitCastFromWhileLoopTest() -> %void {
5858
59test "whileWithContinueExpr" {59test "whileWithContinueExpr" {
60 var sum: i32 = 0;60 var sum: i32 = 0;
61 {var i: i32 = 0; while (i < 10; i += 1) {61 {var i: i32 = 0; while (i < 10) : (i += 1) {
62 if (i == 5) continue;62 if (i == 5) continue;
63 sum += i;63 sum += i;
64 }}64 }}
test/compile_errors.zig+8-8
...@@ -156,18 +156,18 @@ pub fn addCases(cases: &tests.CompileErrorContext) {...@@ -156,18 +156,18 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
156156
157 cases.add("implicit semicolon - while-continue statement",157 cases.add("implicit semicolon - while-continue statement",
158 \\export fn entry() {158 \\export fn entry() {
159 \\ while(true;{}) {}159 \\ while(true):({}) {}
160 \\ var good = {};160 \\ var good = {};
161 \\ while(true;{}) ({})161 \\ while(true):({}) ({})
162 \\ var bad = {};162 \\ var bad = {};
163 \\}163 \\}
164 , ".tmp_source.zig:5:5: error: invalid token: 'var'");164 , ".tmp_source.zig:5:5: error: invalid token: 'var'");
165165
166 cases.add("implicit semicolon - while-continue expression",166 cases.add("implicit semicolon - while-continue expression",
167 \\export fn entry() {167 \\export fn entry() {
168 \\ _ = while(true;{}) {};168 \\ _ = while(true):({}) {};
169 \\ var good = {};169 \\ var good = {};
170 \\ _ = while(true;{}) {}170 \\ _ = while(true):({}) {}
171 \\ var bad = {};171 \\ var bad = {};
172 \\}172 \\}
173 , ".tmp_source.zig:5:5: error: invalid token: 'var'");173 , ".tmp_source.zig:5:5: error: invalid token: 'var'");
...@@ -1231,7 +1231,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {...@@ -1231,7 +1231,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
1231 cases.add("pass integer literal to var args",1231 cases.add("pass integer literal to var args",
1232 \\fn add(args: ...) -> i32 {1232 \\fn add(args: ...) -> i32 {
1233 \\ var sum = i32(0);1233 \\ var sum = i32(0);
1234 \\ {comptime var i: usize = 0; inline while (i < args.len; i += 1) {1234 \\ {comptime var i: usize = 0; inline while (i < args.len) : (i += 1) {
1235 \\ sum += args[i];1235 \\ sum += args[i];
1236 \\ }}1236 \\ }}
1237 \\ return sum;1237 \\ return sum;
...@@ -1315,7 +1315,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {...@@ -1315,7 +1315,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
1315 cases.add("control flow uses comptime var at runtime",1315 cases.add("control flow uses comptime var at runtime",
1316 \\export fn foo() {1316 \\export fn foo() {
1317 \\ comptime var i = 0;1317 \\ comptime var i = 0;
1318 \\ while (i < 5; i += 1) {1318 \\ while (i < 5) : (i += 1) {
1319 \\ bar();1319 \\ bar();
1320 \\ }1320 \\ }
1321 \\}1321 \\}
...@@ -1323,7 +1323,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {...@@ -1323,7 +1323,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
1323 \\fn bar() { }1323 \\fn bar() { }
1324 ,1324 ,
1325 ".tmp_source.zig:3:5: error: control flow attempts to use compile-time variable at runtime",1325 ".tmp_source.zig:3:5: error: control flow attempts to use compile-time variable at runtime",
1326 ".tmp_source.zig:3:21: note: compile-time variable assigned here");1326 ".tmp_source.zig:3:24: note: compile-time variable assigned here");
13271327
1328 cases.add("ignored return value",1328 cases.add("ignored return value",
1329 \\export fn foo() {1329 \\export fn foo() {
...@@ -1373,7 +1373,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {...@@ -1373,7 +1373,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
1373 cases.add("integer literal on a non-comptime var",1373 cases.add("integer literal on a non-comptime var",
1374 \\export fn foo() {1374 \\export fn foo() {
1375 \\ var i = 0;1375 \\ var i = 0;
1376 \\ while (i < 10; i += 1) { }1376 \\ while (i < 10) : (i += 1) { }
1377 \\}1377 \\}
1378 , ".tmp_source.zig:2:5: error: unable to infer variable type");1378 , ".tmp_source.zig:2:5: error: unable to infer variable type");
13791379
test/tests.zig+2-2
...@@ -590,7 +590,7 @@ pub const CompileErrorContext = struct {...@@ -590,7 +590,7 @@ pub const CompileErrorContext = struct {
590 };590 };
591 tc.addSourceFile(".tmp_source.zig", source);591 tc.addSourceFile(".tmp_source.zig", source);
592 comptime var arg_i = 0;592 comptime var arg_i = 0;
593 inline while (arg_i < expected_lines.len; arg_i += 1) {593 inline while (arg_i < expected_lines.len) : (arg_i += 1) {
594 // TODO mem.dupe is because of issue #336594 // TODO mem.dupe is because of issue #336
595 tc.addExpectedError(%%mem.dupe(self.b.allocator, u8, expected_lines[arg_i]));595 tc.addExpectedError(%%mem.dupe(self.b.allocator, u8, expected_lines[arg_i]));
596 }596 }
...@@ -853,7 +853,7 @@ pub const ParseHContext = struct {...@@ -853,7 +853,7 @@ pub const ParseHContext = struct {
853 };853 };
854 tc.addSourceFile("source.h", source);854 tc.addSourceFile("source.h", source);
855 comptime var arg_i = 0;855 comptime var arg_i = 0;
856 inline while (arg_i < expected_lines.len; arg_i += 1) {856 inline while (arg_i < expected_lines.len) : (arg_i += 1) {
857 // TODO mem.dupe is because of issue #336857 // TODO mem.dupe is because of issue #336
858 tc.addExpectedError(%%mem.dupe(self.b.allocator, u8, expected_lines[arg_i]));858 tc.addExpectedError(%%mem.dupe(self.b.allocator, u8, expected_lines[arg_i]));
859 }859 }