array syntax is [10]i32 instead of [i32; 10]

This commit is contained in:
Andrew Kelley 2016-01-05 22:47:47 -07:00
parent e21a83dd74
commit 4ef062b9c8
7 changed files with 24 additions and 32 deletions

View File

@ -72,7 +72,7 @@ PointerType : token(Ampersand) option(token(Const)) Type
MaybeType : token(Question) Type
ArrayType : token(LBracket) Type token(Semicolon) Expression token(RBracket)
ArrayType : token(LBracket) option(Expression) token(RBracket) Type
Block : token(LBrace) list(option(Statement), token(Semicolon)) token(RBrace)

View File

@ -3,7 +3,7 @@ export executable "arrays";
use "std.zig";
pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
var array : [u32; 5];
var array : [5]u32;
var i : u32 = 0;
while (i < 5) {

View File

@ -27,14 +27,14 @@ struct Rand {
return y;
}
/// Write `count` bytes of randomness into `buf`.
pub fn get_bytes(r: &Rand, buf: &u8, count: usize) {
var bytes_left = r.get_bytes_aligned(buf, count);
/// Fill `buf` with randomness.
pub fn get_bytes(r: &Rand, buf: []u8) {
var bytes_left = r.get_bytes_aligned(buf);
if (bytes_left > 0) {
var rand_val_array : [u8; #sizeof(u32)];
*(rand_val_array.ptr as &u32) = r.get_u32();
while (bytes_left > 0) {
buf[count - bytes_left] = rand_val_array[#sizeof(u32) - bytes_left];
buf[buf.len - bytes_left] = rand_val_array[#sizeof(u32) - bytes_left];
bytes_left -= 1;
}
}
@ -49,7 +49,7 @@ struct Rand {
var rand_val_array : [u8; #sizeof(u64)];
while (true) {
r.get_bytes_aligned(rand_val_array.ptr, rand_val_array.len);
r.get_bytes_aligned(rand_val_array);
const rand_val = *(rand_val_array.ptr as &u64);
if (rand_val < upper_bound) {
return start + (rand_val % range);
@ -75,14 +75,12 @@ struct Rand {
}
}
// does not populate the remaining (count % 4) bytes
fn get_bytes_aligned(r: &Rand, buf: &u8, count: usize) -> usize {
var bytes_left = count;
var buf_ptr = buf;
// does not populate the remaining (buf.len % 4) bytes
fn get_bytes_aligned(r: &Rand, buf: []u8) -> usize {
var bytes_left = buf.len;
while (bytes_left > 4) {
*(buf_ptr as &u32) = r.get_u32();
*(&buf[buf.len - bytes_left] as &u32) = r.get_u32();
bytes_left -= #sizeof(u32);
buf_ptr += #sizeof(u32);
}
return bytes_left;
}

View File

@ -1062,7 +1062,7 @@ static AstNode *ast_parse_compiler_fn_call(ParseContext *pc, int *token_index, b
/*
Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType | MaybeType | CompilerFnExpr
PointerType : token(Ampersand) option(token(Const)) Type
ArrayType : token(LBracket) Type token(Semicolon) token(Number) token(RBracket)
ArrayType : token(LBracket) option(Expression) token(RBracket) Type
*/
static AstNode *ast_parse_type(ParseContext *pc, int *token_index) {
Token *token = &pc->tokens->at(*token_index);
@ -1103,17 +1103,11 @@ static AstNode *ast_parse_type(ParseContext *pc, int *token_index) {
} else if (token->id == TokenIdLBracket) {
node->data.type.type = AstNodeTypeTypeArray;
node->data.type.array_size = ast_parse_expression(pc, token_index, false);
ast_eat_token(pc, token_index, TokenIdRBracket);
node->data.type.child_type = ast_parse_type(pc, token_index);
Token *semicolon_token = &pc->tokens->at(*token_index);
*token_index += 1;
ast_expect_token(pc, semicolon_token, TokenIdSemicolon);
node->data.type.array_size = ast_parse_expression(pc, token_index, true);
Token *rbracket_token = &pc->tokens->at(*token_index);
*token_index += 1;
ast_expect_token(pc, rbracket_token, TokenIdRBracket);
} else {
ast_invalid_token_error(pc, token);
}

View File

@ -106,7 +106,7 @@ struct AstNodeType {
AstNodeTypeType type;
Buf primitive_name;
AstNode *child_type;
AstNode *array_size;
AstNode *array_size; // can be null
bool is_const;
AstNode *compiler_expr;
};

View File

@ -53,7 +53,7 @@ pub fn fprint_str(fd: isize, str: string) -> isize {
// TODO error handling
pub fn print_u64(x: u64) -> isize {
// TODO use max_u64_base10_digits instead of hardcoding 20
var buf: [u8; 20];
var buf: [20]u8;
const len = buf_print_u64(buf.ptr, x);
return write(stdout_fileno, buf.ptr, len);
}
@ -62,7 +62,7 @@ pub fn print_u64(x: u64) -> isize {
// TODO error handling
pub fn print_i64(x: i64) -> isize {
// TODO use max_u64_base10_digits instead of hardcoding 20
var buf: [u8; 20];
var buf: [20]u8;
const len = buf_print_i64(buf.ptr, x);
return write(stdout_fileno, buf.ptr, len);
}
@ -83,9 +83,9 @@ fn buf_print_i64(out_buf: &u8, x: i64) -> usize {
}
fn buf_print_u64(out_buf: &u8, x: u64) -> usize {
var buf: [u8; max_u64_base10_digits];
var buf: [max_u64_base10_digits]u8;
var a = x;
var index = max_u64_base10_digits;
var index = buf.len;
while (true) {
const digit = a % 10;
@ -96,7 +96,7 @@ fn buf_print_u64(out_buf: &u8, x: u64) -> usize {
break;
}
const len = max_u64_base10_digits - index;
const len = buf.len - index;
// TODO memcpy intrinsic
var i: usize = 0;

View File

@ -349,7 +349,7 @@ done:
use "std.zig";
pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
var array : [u32; 5];
var array : [5]u32;
var i : u32 = 0;
while (i < 5) {
@ -784,7 +784,7 @@ use "std.zig";
const ARRAY_SIZE : u8 = 20;
pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
var array : [u8; ARRAY_SIZE];
var array : [ARRAY_SIZE]u8;
print_u64(#sizeof(#typeof(array)));
print_str("\n");
return 0;