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
This commit is contained in:
Andrew Kelley 2017-05-03 18:12:07 -04:00
parent 644ea2dde9
commit 698829b772
25 changed files with 100 additions and 68 deletions

View File

@ -79,8 +79,6 @@ SwitchProng = (list(SwitchItem, ",") | "else") "=>" option("|" option("*") Symbo
SwitchItem = Expression | (Expression "..." Expression)
WhileExpression(body) = "while" "(" Expression option(";" Expression) ")" body
ForExpression(body) = "for" "(" Expression ")" option("|" option("*") Symbol option("," Symbol) "|") body
BoolOrExpression = BoolAndExpression "or" BoolOrExpression | BoolAndExpression
@ -95,6 +93,8 @@ TryExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|")
TestExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" BlockExpression(body))
WhileExpression(body) = "while" "(" Expression ")" option("|" option("*") Symbol "|") option(":" "(" Expression ")") body option("else" option("|" Symbol "|") BlockExpression(body))
BoolAndExpression = ComparisonExpression "and" BoolAndExpression | ComparisonExpression
ComparisonExpression = BinaryOrExpression ComparisonOperator BinaryOrExpression | BinaryOrExpression

View File

@ -7,7 +7,7 @@ pub fn main() -> %void {
const exe = os.args.at(0);
var catted_anything = false;
var arg_i: usize = 1;
while (arg_i < os.args.count(); arg_i += 1) {
while (arg_i < os.args.count()) : (arg_i += 1) {
const arg = os.args.at(arg_i);
if (mem.eql(u8, arg, "-")) {
catted_anything = true;

View File

@ -609,8 +609,12 @@ struct AstNodeTestExpr {
struct AstNodeWhileExpr {
AstNode *condition;
Buf *var_symbol;
bool var_is_ptr;
AstNode *continue_expr;
AstNode *body;
AstNode *else_node;
Buf *err_symbol;
bool is_inline;
};

View File

@ -1580,7 +1580,7 @@ static AstNode *ast_parse_bool_or_expr(ParseContext *pc, size_t *token_index, bo
}
/*
WhileExpression(body) = option("inline") "while" "(" Expression option(";" Expression) ")" body
WhileExpression(body) = option("inline") "while" "(" Expression ")" option("|" option("*") Symbol "|") option(":" "(" Expression ")") body option("else" option("|" Symbol "|") BlockExpression(body))
*/
static AstNode *ast_parse_while_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
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
ast_eat_token(pc, token_index, TokenIdLParen);
node->data.while_expr.condition = ast_parse_expression(pc, token_index, true);
ast_eat_token(pc, token_index, TokenIdRParen);
Token *semi_or_rparen = &pc->tokens->at(*token_index);
Token *open_bar_tok = &pc->tokens->at(*token_index);
if (open_bar_tok->id == TokenIdBinOr) {
*token_index += 1;
if (semi_or_rparen->id == TokenIdRParen) {
*token_index += 1;
node->data.while_expr.body = ast_parse_block_or_expression(pc, token_index, true);
} else if (semi_or_rparen->id == TokenIdSemicolon) {
*token_index += 1;
node->data.while_expr.continue_expr = ast_parse_expression(pc, token_index, true);
ast_eat_token(pc, token_index, TokenIdRParen);
node->data.while_expr.body = ast_parse_block_or_expression(pc, token_index, true);
} else {
ast_invalid_token_error(pc, semi_or_rparen);
Token *star_tok = &pc->tokens->at(*token_index);
if (star_tok->id == TokenIdStar) {
*token_index += 1;
node->data.while_expr.var_is_ptr = true;
}
Token *var_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
node->data.while_expr.var_symbol = token_buf(var_name_tok);
ast_eat_token(pc, token_index, TokenIdBinOr);
}
Token *colon_tok = &pc->tokens->at(*token_index);
if (colon_tok->id == TokenIdColon) {
*token_index += 1;
ast_eat_token(pc, token_index, TokenIdLParen);
node->data.while_expr.continue_expr = ast_parse_expression(pc, token_index, true);
ast_eat_token(pc, token_index, TokenIdRParen);
}
node->data.while_expr.body = ast_parse_block_or_expression(pc, token_index, true);
Token *else_tok = &pc->tokens->at(*token_index);
if (else_tok->id == TokenIdKeywordElse) {
*token_index += 1;
Token *else_bar_tok = &pc->tokens->at(*token_index);
if (else_bar_tok->id == TokenIdBinOr) {
*token_index += 1;
Token *err_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
node->data.while_expr.err_symbol = token_buf(err_name_tok);
ast_eat_token(pc, token_index, TokenIdBinOr);
}
node->data.while_expr.body = ast_parse_block_or_expression(pc, token_index, true);
}
return node;
}

View File

@ -17,7 +17,7 @@ pub fn encodeWithAlphabet(dest: []u8, source: []const u8, alphabet: []const u8)
var i: usize = 0;
var out_index: usize = 0;
while (i + 2 < source.len; i += 3) {
while (i + 2 < source.len) : (i += 3) {
dest[out_index] = alphabet[(source[i] >> 2) & 0x3f];
out_index += 1;

View File

@ -3,13 +3,13 @@ const assert = debug.assert;
pub fn len(ptr: &const u8) -> usize {
var count: usize = 0;
while (ptr[count] != 0; count += 1) {}
while (ptr[count] != 0) : (count += 1) {}
return count;
}
pub fn cmp(a: &const u8, b: &const u8) -> i8 {
var index: usize = 0;
while (a[index] == b[index] and a[index] != 0; index += 1) {}
while (a[index] == b[index] and a[index] != 0) : (index += 1) {}
if (a[index] > b[index]) {
return 1;
} else if (a[index] < b[index]) {

View File

@ -79,7 +79,7 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty
var ignored_count: usize = 0;
var fp = usize(@frameAddress());
while (fp != 0; fp = *@intToPtr(&const usize, fp)) {
while (fp != 0) : (fp = *@intToPtr(&const usize, fp)) {
if (ignored_count < ignore_frame_count) {
ignored_count += 1;
continue;
@ -108,7 +108,7 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty
if (line_info.column == 0) {
%return out_stream.write("\n");
} else {
{var col_i: usize = 1; while (col_i < line_info.column; col_i += 1) {
{var col_i: usize = 1; while (col_i < line_info.column) : (col_i += 1) {
%return out_stream.writeByte(' ');
}}
%return out_stream.write(GREEN ++ "^" ++ RESET ++ "\n");
@ -594,7 +594,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
var this_offset = st.debug_line.offset;
var this_index: usize = 0;
while (this_offset < debug_line_end; this_index += 1) {
while (this_offset < debug_line_end) : (this_index += 1) {
%return in_stream.seekTo(this_offset);
var is_64: bool = undefined;
@ -628,7 +628,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
const standard_opcode_lengths = %return st.allocator().alloc(u8, opcode_base - 1);
{var i: usize = 0; while (i < opcode_base - 1; i += 1) {
{var i: usize = 0; while (i < opcode_base - 1) : (i += 1) {
standard_opcode_lengths[i] = %return in_stream.readByte();
}}

View File

@ -200,7 +200,7 @@ pub fn formatBuf(buf: []const u8, width: usize,
var leftover_padding = if (width > buf.len) (width - buf.len) else return true;
const pad_byte: u8 = ' ';
while (leftover_padding > 0; leftover_padding -= 1) {
while (leftover_padding > 0) : (leftover_padding -= 1) {
if (!output(context, (&pad_byte)[0...1]))
return false;
}

View File

@ -43,7 +43,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
assert(it.initial_modification_count == it.hm.modification_count); // concurrent modification
}
if (it.count >= it.hm.size) return null;
while (it.index < it.hm.entries.len; it.index += 1) {
while (it.index < it.hm.entries.len) : (it.index += 1) {
const entry = &it.hm.entries[it.index];
if (entry.used) {
it.index += 1;
@ -112,7 +112,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
pub fn remove(hm: &Self, key: K) -> ?&Entry {
hm.incrementModificationCount();
const start_index = hm.keyToIndex(key);
{var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index; roll_over += 1) {
{var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
const index = (start_index + roll_over) % hm.entries.len;
var entry = &hm.entries[index];
@ -121,7 +121,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
if (!eql(entry.key, key)) continue;
while (roll_over < hm.entries.len; roll_over += 1) {
while (roll_over < hm.entries.len) : (roll_over += 1) {
const next_index = (start_index + roll_over + 1) % hm.entries.len;
const next_entry = &hm.entries[next_index];
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,
const start_index = hm.keyToIndex(key);
var roll_over: usize = 0;
var distance_from_start_index: usize = 0;
while (roll_over < hm.entries.len; {roll_over += 1; distance_from_start_index += 1}) {
while (roll_over < hm.entries.len) : ({roll_over += 1; distance_from_start_index += 1}) {
const index = (start_index + roll_over) % hm.entries.len;
const entry = &hm.entries[index];
@ -215,7 +215,7 @@ pub fn HashMap(comptime K: type, comptime V: type,
fn internalGet(hm: &Self, key: K) -> ?&Entry {
const start_index = hm.keyToIndex(key);
{var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index; roll_over += 1) {
{var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) {
const index = (start_index + roll_over) % hm.entries.len;
const entry = &hm.entries[index];

View File

@ -78,11 +78,11 @@ test "basic list test" {
var list = List(i32).init(&debug.global_allocator);
defer list.deinit();
{var i: usize = 0; while (i < 10; i += 1) {
{var i: usize = 0; while (i < 10) : (i += 1) {
%%list.append(i32(i + 1));
}}
{var i: usize = 0; while (i < 10; i += 1) {
{var i: usize = 0; while (i < 10) : (i += 1) {
assert(list.items[i] == i32(i + 1));
}}

View File

@ -123,7 +123,7 @@ pub fn set(comptime T: type, dest: []T, value: T) {
pub fn cmp(comptime T: type, a: []const T, b: []const T) -> Cmp {
const n = math.min(a.len, b.len);
var i: usize = 0;
while (i < n; i += 1) {
while (i < n) : (i += 1) {
if (a[i] == b[i]) continue;
return if (a[i] > b[i]) Cmp.Greater else if (a[i] < b[i]) Cmp.Less else Cmp.Equal;
}
@ -164,7 +164,7 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) -> ?usi
var i: usize = 0;
const end = haystack.len - needle.len;
while (i <= end; i += 1) {
while (i <= end) : (i += 1) {
if (eql(T, haystack[i...i + needle.len], needle))
return i;
}
@ -263,14 +263,14 @@ const SplitIterator = struct {
pub fn next(self: &SplitIterator) -> ?[]const u8 {
// move to beginning of token
while (self.index < self.s.len and self.s[self.index] == self.c; self.index += 1) {}
while (self.index < self.s.len and self.s[self.index] == self.c) : (self.index += 1) {}
const start = self.index;
if (start == self.s.len) {
return null;
}
// move to end of token
while (self.index < self.s.len and self.s[self.index] != self.c; self.index += 1) {}
while (self.index < self.s.len and self.s[self.index] != self.c) : (self.index += 1) {}
const end = self.index;
return self.s[start...end];
@ -280,7 +280,7 @@ const SplitIterator = struct {
pub fn rest(self: &const SplitIterator) -> []const u8 {
// move to beginning of token
var index: usize = self.index;
while (index < self.s.len and self.s[index] == self.c; index += 1) {}
while (index < self.s.len and self.s[index] == self.c) : (index += 1) {}
return self.s[index...];
}
};

View File

@ -276,7 +276,7 @@ pub fn posixExecve(exe_path: []const u8, argv: []const []const u8, env_map: &con
{
var it = env_map.iterator();
var i: usize = 0;
while (true; i += 1) {
while (true) : (i += 1) {
const pair = it.next() ?? break;
const env_buf = %return allocator.alloc(u8, pair.key.len + pair.value.len + 2);
@ -354,11 +354,11 @@ pub fn getEnvMap(allocator: &Allocator) -> %BufMap {
for (environ_raw) |ptr| {
var line_i: usize = 0;
while (ptr[line_i] != 0 and ptr[line_i] != '='; line_i += 1) {}
while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {}
const key = ptr[0...line_i];
var end_i: usize = line_i;
while (ptr[end_i] != 0; end_i += 1) {}
while (ptr[end_i] != 0) : (end_i += 1) {}
const value = ptr[line_i + 1...end_i];
%return result.set(key, value);
@ -369,13 +369,13 @@ pub fn getEnvMap(allocator: &Allocator) -> %BufMap {
pub fn getEnv(key: []const u8) -> ?[]const u8 {
for (environ_raw) |ptr| {
var line_i: usize = 0;
while (ptr[line_i] != 0 and ptr[line_i] != '='; line_i += 1) {}
while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {}
const this_key = ptr[0...line_i];
if (!mem.eql(u8, key, this_key))
continue;
var end_i: usize = line_i;
while (ptr[end_i] != 0; end_i += 1) {}
while (ptr[end_i] != 0) : (end_i += 1) {}
const this_value = ptr[line_i + 1...end_i];
return this_value;

View File

@ -25,7 +25,7 @@ pub fn join(allocator: &Allocator, paths: ...) -> %[]u8 {
var total_paths_len: usize = paths.len; // 1 slash per path
{
comptime var path_i = 0;
inline while (path_i < paths.len; path_i += 1) {
inline while (path_i < paths.len) : (path_i += 1) {
const arg = ([]const u8)(paths[path_i]);
total_paths_len += arg.len;
}
@ -74,7 +74,7 @@ pub fn isAbsolute(path: []const u8) -> bool {
pub fn resolve(allocator: &Allocator, args: ...) -> %[]u8 {
var paths: [args.len][]const u8 = undefined;
comptime var arg_i = 0;
inline while (arg_i < args.len; arg_i += 1) {
inline while (arg_i < args.len) : (arg_i += 1) {
paths[arg_i] = args[arg_i];
}
return resolveSlice(allocator, paths);

View File

@ -121,7 +121,7 @@ fn MersenneTwister(
var prev_value = seed;
mt.array[0] = prev_value;
var i: usize = 1;
while (i < n; i += 1) {
while (i < n) : (i += 1) {
prev_value = int(i) +% f *% (prev_value ^ (prev_value >> (int.bit_count - 2)));
mt.array[i] = prev_value;
}
@ -136,12 +136,12 @@ fn MersenneTwister(
if (mt.index >= n) {
var i: usize = 0;
while (i < n - m; i += 1) {
while (i < n - m) : (i += 1) {
const x = (mt.array[i] & UM) | (mt.array[i + 1] & LM);
mt.array[i] = mt.array[i + m] ^ (x >> 1) ^ mag01[x & 0x1];
}
while (i < n - 1; i += 1) {
while (i < n - 1) : (i += 1) {
const x = (mt.array[i] & UM) | (mt.array[i + 1] & LM);
mt.array[i] = mt.array[i + m - n] ^ (x >> 1) ^ mag01[x & 0x1];
@ -168,7 +168,7 @@ fn MersenneTwister(
test "rand float 32" {
var r = Rand.init(42);
var i: usize = 0;
while (i < 1000; i += 1) {
while (i < 1000) : (i += 1) {
const val = r.float(f32);
assert(val >= 0.0);
assert(val < 1.0);

View File

@ -42,7 +42,7 @@ fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void {
std.os.args.raw = argv[0...argc];
var env_count: usize = 0;
while (envp[env_count] != null; env_count += 1) {}
while (envp[env_count] != null) : (env_count += 1) {}
std.os.environ_raw = @ptrCast(&&u8, envp)[0...env_count];
std.debug.user_main_fn = root.main;

View File

@ -55,7 +55,7 @@ pub fn main() -> %void {
var prefix: ?[]const u8 = null;
while (arg_i < os.args.count(); arg_i += 1) {
while (arg_i < os.args.count()) : (arg_i += 1) {
const arg = os.args.at(arg_i);
if (mem.startsWith(u8, arg, "-D")) {
const option_contents = arg[2...];

View File

@ -10,7 +10,7 @@ export fn memset(dest: ?&u8, c: u8, n: usize) {
@setDebugSafety(this, false);
var index: usize = 0;
while (index != n; index += 1)
while (index != n) : (index += 1)
(??dest)[index] = c;
}
@ -18,7 +18,7 @@ export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) {
@setDebugSafety(this, false);
var index: usize = 0;
while (index != n; index += 1)
while (index != n) : (index += 1)
(??dest)[index] = (??src)[index];
}

View File

@ -296,7 +296,7 @@ export fn __udivsi3(n: su_int, d: su_int) -> su_int {
var q: su_int = n << (n_uword_bits - sr);
var r: su_int = n >> sr;
var carry: su_int = 0;
while (sr > 0; sr -= 1) {
while (sr > 0) : (sr -= 1) {
// r:q = ((r:q) << 1) | carry
r = (r << 1) | (q >> (n_uword_bits - 1));
q = (q << 1) | carry;

View File

@ -31,7 +31,7 @@ fn bar(argc: usize) {
fn strlen(ptr: &const u8) -> usize {
var count: usize = 0;
while (ptr[count] != 0; count += 1) {}
while (ptr[count] != 0) : (count += 1) {}
return count;
}

View File

@ -22,7 +22,7 @@ test "testStaticAddOne" {
test "inlinedLoop" {
comptime var i = 0;
comptime var sum = 0;
inline while (i <= 5; i += 1)
inline while (i <= 5) : (i += 1)
sum += i;
assert(sum == 15);
}
@ -157,7 +157,7 @@ test "tryToTrickEvalWithRuntimeIf" {
fn testTryToTrickEvalWithRuntimeIf(b: bool) -> usize {
comptime var i: usize = 0;
inline while (i < 10; i += 1) {
inline while (i < 10) : (i += 1) {
const result = if (b) false else true;
}
comptime {
@ -208,7 +208,7 @@ fn three(value: i32) -> i32 { value + 3 }
fn performFn(comptime prefix_char: u8, start_value: i32) -> i32 {
var result: i32 = start_value;
comptime var i = 0;
inline while (i < cmd_fns.len; i += 1) {
inline while (i < cmd_fns.len) : (i += 1) {
if (cmd_fns[i].name[0] == prefix_char) {
result = cmd_fns[i].func(result);
}

View File

@ -380,7 +380,7 @@ test "cStringConcatenation" {
const len = cstr.len(b);
const len_with_null = len + 1;
{var i: u32 = 0; while (i < len_with_null; i += 1) {
{var i: u32 = 0; while (i < len_with_null) : (i += 1) {
assert(a[i] == b[i]);
}}
assert(a[len] == 0);

View File

@ -2,7 +2,7 @@ const assert = @import("std").debug.assert;
fn add(args: ...) -> i32 {
var sum = i32(0);
{comptime var i: usize = 0; inline while (i < args.len; i += 1) {
{comptime var i: usize = 0; inline while (i < args.len) : (i += 1) {
sum += args[i];
}}
return sum;

View File

@ -58,7 +58,7 @@ fn returnWithImplicitCastFromWhileLoopTest() -> %void {
test "whileWithContinueExpr" {
var sum: i32 = 0;
{var i: i32 = 0; while (i < 10; i += 1) {
{var i: i32 = 0; while (i < 10) : (i += 1) {
if (i == 5) continue;
sum += i;
}}

View File

@ -156,18 +156,18 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
cases.add("implicit semicolon - while-continue statement",
\\export fn entry() {
\\ while(true;{}) {}
\\ while(true):({}) {}
\\ var good = {};
\\ while(true;{}) ({})
\\ while(true):({}) ({})
\\ var bad = {};
\\}
, ".tmp_source.zig:5:5: error: invalid token: 'var'");
cases.add("implicit semicolon - while-continue expression",
\\export fn entry() {
\\ _ = while(true;{}) {};
\\ _ = while(true):({}) {};
\\ var good = {};
\\ _ = while(true;{}) {}
\\ _ = while(true):({}) {}
\\ var bad = {};
\\}
, ".tmp_source.zig:5:5: error: invalid token: 'var'");
@ -1231,7 +1231,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
cases.add("pass integer literal to var args",
\\fn add(args: ...) -> i32 {
\\ var sum = i32(0);
\\ {comptime var i: usize = 0; inline while (i < args.len; i += 1) {
\\ {comptime var i: usize = 0; inline while (i < args.len) : (i += 1) {
\\ sum += args[i];
\\ }}
\\ return sum;
@ -1315,7 +1315,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
cases.add("control flow uses comptime var at runtime",
\\export fn foo() {
\\ comptime var i = 0;
\\ while (i < 5; i += 1) {
\\ while (i < 5) : (i += 1) {
\\ bar();
\\ }
\\}
@ -1323,7 +1323,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
\\fn bar() { }
,
".tmp_source.zig:3:5: error: control flow attempts to use compile-time variable at runtime",
".tmp_source.zig:3:21: note: compile-time variable assigned here");
".tmp_source.zig:3:24: note: compile-time variable assigned here");
cases.add("ignored return value",
\\export fn foo() {
@ -1373,7 +1373,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
cases.add("integer literal on a non-comptime var",
\\export fn foo() {
\\ var i = 0;
\\ while (i < 10; i += 1) { }
\\ while (i < 10) : (i += 1) { }
\\}
, ".tmp_source.zig:2:5: error: unable to infer variable type");

View File

@ -590,7 +590,7 @@ pub const CompileErrorContext = struct {
};
tc.addSourceFile(".tmp_source.zig", source);
comptime var arg_i = 0;
inline while (arg_i < expected_lines.len; arg_i += 1) {
inline while (arg_i < expected_lines.len) : (arg_i += 1) {
// TODO mem.dupe is because of issue #336
tc.addExpectedError(%%mem.dupe(self.b.allocator, u8, expected_lines[arg_i]));
}
@ -853,7 +853,7 @@ pub const ParseHContext = struct {
};
tc.addSourceFile("source.h", source);
comptime var arg_i = 0;
inline while (arg_i < expected_lines.len; arg_i += 1) {
inline while (arg_i < expected_lines.len) : (arg_i += 1) {
// TODO mem.dupe is because of issue #336
tc.addExpectedError(%%mem.dupe(self.b.allocator, u8, expected_lines[arg_i]));
}