mirror of
https://github.com/ziglang/zig.git
synced 2024-11-15 08:33:06 +00:00
update for loop syntax
it matches more closely the %% binary operator syntax See #51
This commit is contained in:
parent
4208435f66
commit
4339d55562
@ -85,7 +85,7 @@ SwitchItem = Expression | (Expression "..." Expression)
|
||||
|
||||
WhileExpression = "while" "(" Expression ")" Expression
|
||||
|
||||
ForExpression = "for" "(" "Symbol" "," Expression option("," "Symbol") ")" Expression
|
||||
ForExpression = "for" "(" Expression ")" option("|" "Symbol" option("," "Symbol") "|") Expression
|
||||
|
||||
BoolOrExpression = BoolAndExpression "||" BoolOrExpression | BoolAndExpression
|
||||
|
||||
|
@ -1797,7 +1797,7 @@ static AstNode *ast_parse_symbol(ParseContext *pc, int *token_index) {
|
||||
}
|
||||
|
||||
/*
|
||||
ForExpression : token(For) token(LParen) Symbol token(Comma) Expression option(token(Comma) token(Symbol)) token(RParen) Expression
|
||||
ForExpression = "for" "(" Expression ")" option("|" "Symbol" option("," "Symbol") "|") Expression
|
||||
*/
|
||||
static AstNode *ast_parse_for_expr(ParseContext *pc, int *token_index, bool mandatory) {
|
||||
Token *token = &pc->tokens->at(*token_index);
|
||||
@ -1814,18 +1814,24 @@ static AstNode *ast_parse_for_expr(ParseContext *pc, int *token_index, bool mand
|
||||
AstNode *node = ast_create_node(pc, NodeTypeForExpr, token);
|
||||
|
||||
ast_eat_token(pc, token_index, TokenIdLParen);
|
||||
node->data.for_expr.elem_node = ast_parse_symbol(pc, token_index);
|
||||
ast_eat_token(pc, token_index, TokenIdComma);
|
||||
node->data.for_expr.array_expr = ast_parse_expression(pc, token_index, true);
|
||||
|
||||
Token *comma = &pc->tokens->at(*token_index);
|
||||
if (comma->id == TokenIdComma) {
|
||||
*token_index += 1;
|
||||
node->data.for_expr.index_node = ast_parse_symbol(pc, token_index);
|
||||
}
|
||||
|
||||
ast_eat_token(pc, token_index, TokenIdRParen);
|
||||
|
||||
Token *maybe_bar = &pc->tokens->at(*token_index);
|
||||
if (maybe_bar->id == TokenIdBinOr) {
|
||||
*token_index += 1;
|
||||
node->data.for_expr.elem_node = ast_parse_symbol(pc, token_index);
|
||||
|
||||
Token *maybe_comma = &pc->tokens->at(*token_index);
|
||||
if (maybe_comma->id == TokenIdComma) {
|
||||
*token_index += 1;
|
||||
|
||||
node->data.for_expr.index_node = ast_parse_symbol(pc, token_index);
|
||||
}
|
||||
|
||||
ast_eat_token(pc, token_index, TokenIdBinOr);
|
||||
}
|
||||
|
||||
node->data.for_expr.body = ast_parse_expression(pc, token_index, true);
|
||||
|
||||
normalize_parent_ptrs(node);
|
||||
|
@ -25,7 +25,7 @@ fn strlen(ptr: &const u8) -> isize {
|
||||
|
||||
fn call_main() -> unreachable {
|
||||
var args: [argc][]u8 = undefined;
|
||||
for (arg, args, i) {
|
||||
for (args) |arg, i| {
|
||||
const ptr = argv[i];
|
||||
args[i] = ptr[0...strlen(ptr)];
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ pub struct Rand {
|
||||
}
|
||||
|
||||
fn generate_numbers(r: &Rand) {
|
||||
for (item, r.array, i) {
|
||||
for (r.array) |item, i| {
|
||||
const y : u32 = (item & 0x80000000) + (r.array[(i + 1) % ARRAY_SIZE] & 0x7fffffff);
|
||||
const untempered : u32 = r.array[(i + 397) % ARRAY_SIZE] ^ (y >> 1);
|
||||
r.array[i] = if ((y % 2) == 0) {
|
||||
|
@ -173,7 +173,7 @@ pub error Overflow;
|
||||
pub fn parse_u64(buf: []u8, radix: u8) -> %u64 {
|
||||
var x : u64 = 0;
|
||||
|
||||
for (c, buf) {
|
||||
for (buf) |c| {
|
||||
const digit = char_to_digit(c);
|
||||
|
||||
if (digit > radix) {
|
||||
|
@ -8,7 +8,7 @@ struct TestFn {
|
||||
extern var zig_test_fn_list: []TestFn;
|
||||
|
||||
pub fn run_tests() -> %void {
|
||||
for (test_fn, zig_test_fn_list, i) {
|
||||
for (zig_test_fn_list) |test_fn, i| {
|
||||
%%stderr.print_str("Test ");
|
||||
%%stderr.print_i64(i + 1);
|
||||
%%stderr.print_str("/");
|
||||
|
@ -1120,20 +1120,20 @@ import "std.zig";
|
||||
|
||||
pub fn main(args: [][]u8) -> %void {
|
||||
const array = []u8 {9, 8, 7, 6};
|
||||
for (item, array) {
|
||||
for (array) |item| {
|
||||
%%stdout.print_u64(item);
|
||||
%%stdout.printf("\n");
|
||||
}
|
||||
for (item, array, index) {
|
||||
for (array) |item, index| {
|
||||
%%stdout.print_i64(index);
|
||||
%%stdout.printf("\n");
|
||||
}
|
||||
const unknown_size: []u8 = array;
|
||||
for (item, unknown_size) {
|
||||
for (unknown_size) |item| {
|
||||
%%stdout.print_u64(item);
|
||||
%%stdout.printf("\n");
|
||||
}
|
||||
for (item, unknown_size, index) {
|
||||
for (unknown_size) |item, index| {
|
||||
%%stdout.print_i64(index);
|
||||
%%stdout.printf("\n");
|
||||
}
|
||||
@ -1145,7 +1145,7 @@ import "std.zig";
|
||||
|
||||
pub fn main(args: [][]u8) -> %void {
|
||||
const fns = []@typeof(fn1) { fn1, fn2, fn3, fn4, };
|
||||
for (f, fns) {
|
||||
for (fns) |f| {
|
||||
%%stdout.print_u64(f());
|
||||
%%stdout.printf("\n");
|
||||
}
|
||||
@ -1434,7 +1434,7 @@ export fn main(args: c_int, argv: &&u8) -> c_int {
|
||||
|
||||
qsort((&c_void)(array.ptr), c_ulong(array.len), @sizeof(i32), compare_fn);
|
||||
|
||||
for (item, array, i) {
|
||||
for (array) |item, i| {
|
||||
if (item != i) {
|
||||
abort();
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ enum AnEnumWithPayload {
|
||||
fn continue_in_for_loop() {
|
||||
const array = []i32 {1, 2, 3, 4, 5};
|
||||
var sum : i32 = 0;
|
||||
for (x, array) {
|
||||
for (array) |x| {
|
||||
sum += x;
|
||||
if (x < 3) {
|
||||
continue;
|
||||
|
Loading…
Reference in New Issue
Block a user