authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-05 22:47:47-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-05 22:47:47-07:00
log4ef062b9c819a2d7bfa9dd3394713ac9e8051660
treecae97813bd1925ea7e18f96dc33fde913d161066
parente21a83dd74822d9d8b272079a2e0e0b01aff60d4

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


7 files changed, 23 insertions(+), 31 deletions(-)

doc/langref.md+1-1
...@@ -72,7 +72,7 @@ PointerType : token(Ampersand) option(token(Const)) Type...@@ -72,7 +72,7 @@ PointerType : token(Ampersand) option(token(Const)) Type
7272
73MaybeType : token(Question) Type73MaybeType : token(Question) Type
7474
75ArrayType : token(LBracket) Type token(Semicolon) Expression token(RBracket)75ArrayType : token(LBracket) option(Expression) token(RBracket) Type
7676
77Block : token(LBrace) list(option(Statement), token(Semicolon)) token(RBrace)77Block : token(LBrace) list(option(Statement), token(Semicolon)) token(RBrace)
7878
example/arrays/arrays.zig+1-1
...@@ -3,7 +3,7 @@ export executable "arrays";...@@ -3,7 +3,7 @@ export executable "arrays";
3use "std.zig";3use "std.zig";
44
5pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {5pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
6 var array : [u32; 5];6 var array : [5]u32;
77
8 var i : u32 = 0;8 var i : u32 = 0;
9 while (i < 5) {9 while (i < 5) {
example/rand/main.zig+9-11
...@@ -27,14 +27,14 @@ struct Rand {...@@ -27,14 +27,14 @@ struct Rand {
27 return y;27 return y;
28 }28 }
2929
30 /// Write `count` bytes of randomness into `buf`.30 /// Fill `buf` with randomness.
31 pub fn get_bytes(r: &Rand, buf: &u8, count: usize) {31 pub fn get_bytes(r: &Rand, buf: []u8) {
32 var bytes_left = r.get_bytes_aligned(buf, count);32 var bytes_left = r.get_bytes_aligned(buf);
33 if (bytes_left > 0) {33 if (bytes_left > 0) {
34 var rand_val_array : [u8; #sizeof(u32)];34 var rand_val_array : [u8; #sizeof(u32)];
35 *(rand_val_array.ptr as &u32) = r.get_u32();35 *(rand_val_array.ptr as &u32) = r.get_u32();
36 while (bytes_left > 0) {36 while (bytes_left > 0) {
37 buf[count - bytes_left] = rand_val_array[#sizeof(u32) - bytes_left];37 buf[buf.len - bytes_left] = rand_val_array[#sizeof(u32) - bytes_left];
38 bytes_left -= 1;38 bytes_left -= 1;
39 }39 }
40 }40 }
...@@ -49,7 +49,7 @@ struct Rand {...@@ -49,7 +49,7 @@ struct Rand {
49 var rand_val_array : [u8; #sizeof(u64)];49 var rand_val_array : [u8; #sizeof(u64)];
5050
51 while (true) {51 while (true) {
52 r.get_bytes_aligned(rand_val_array.ptr, rand_val_array.len);52 r.get_bytes_aligned(rand_val_array);
53 const rand_val = *(rand_val_array.ptr as &u64);53 const rand_val = *(rand_val_array.ptr as &u64);
54 if (rand_val < upper_bound) {54 if (rand_val < upper_bound) {
55 return start + (rand_val % range);55 return start + (rand_val % range);
...@@ -75,14 +75,12 @@ struct Rand {...@@ -75,14 +75,12 @@ struct Rand {
75 }75 }
76 }76 }
7777
78 // does not populate the remaining (count % 4) bytes78 // does not populate the remaining (buf.len % 4) bytes
79 fn get_bytes_aligned(r: &Rand, buf: &u8, count: usize) -> usize {79 fn get_bytes_aligned(r: &Rand, buf: []u8) -> usize {
80 var bytes_left = count;80 var bytes_left = buf.len;
81 var buf_ptr = buf;
82 while (bytes_left > 4) {81 while (bytes_left > 4) {
83 *(buf_ptr as &u32) = r.get_u32();82 *(&buf[buf.len - bytes_left] as &u32) = r.get_u32();
84 bytes_left -= #sizeof(u32);83 bytes_left -= #sizeof(u32);
85 buf_ptr += #sizeof(u32);
86 }84 }
87 return bytes_left;85 return bytes_left;
88 }86 }
src/parser.cpp+4-10
...@@ -1062,7 +1062,7 @@ static AstNode *ast_parse_compiler_fn_call(ParseContext *pc, int *token_index, b...@@ -1062,7 +1062,7 @@ static AstNode *ast_parse_compiler_fn_call(ParseContext *pc, int *token_index, b
1062/*1062/*
1063Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType | MaybeType | CompilerFnExpr1063Type : token(Symbol) | token(Unreachable) | token(Void) | PointerType | ArrayType | MaybeType | CompilerFnExpr
1064PointerType : token(Ampersand) option(token(Const)) Type1064PointerType : token(Ampersand) option(token(Const)) Type
1065ArrayType : token(LBracket) Type token(Semicolon) token(Number) token(RBracket)1065ArrayType : token(LBracket) option(Expression) token(RBracket) Type
1066*/1066*/
1067static AstNode *ast_parse_type(ParseContext *pc, int *token_index) {1067static AstNode *ast_parse_type(ParseContext *pc, int *token_index) {
1068 Token *token = &pc->tokens->at(*token_index);1068 Token *token = &pc->tokens->at(*token_index);
...@@ -1103,17 +1103,11 @@ static AstNode *ast_parse_type(ParseContext *pc, int *token_index) {...@@ -1103,17 +1103,11 @@ static AstNode *ast_parse_type(ParseContext *pc, int *token_index) {
1103 } else if (token->id == TokenIdLBracket) {1103 } else if (token->id == TokenIdLBracket) {
1104 node->data.type.type = AstNodeTypeTypeArray;1104 node->data.type.type = AstNodeTypeTypeArray;
11051105
1106 node->data.type.child_type = ast_parse_type(pc, token_index);1106 node->data.type.array_size = ast_parse_expression(pc, token_index, false);
1107
1108 Token *semicolon_token = &pc->tokens->at(*token_index);
1109 *token_index += 1;
1110 ast_expect_token(pc, semicolon_token, TokenIdSemicolon);
11111107
1112 node->data.type.array_size = ast_parse_expression(pc, token_index, true);1108 ast_eat_token(pc, token_index, TokenIdRBracket);
11131109
1114 Token *rbracket_token = &pc->tokens->at(*token_index);1110 node->data.type.child_type = ast_parse_type(pc, token_index);
1115 *token_index += 1;
1116 ast_expect_token(pc, rbracket_token, TokenIdRBracket);
1117 } else {1111 } else {
1118 ast_invalid_token_error(pc, token);1112 ast_invalid_token_error(pc, token);
1119 }1113 }
src/parser.hpp+1-1
...@@ -106,7 +106,7 @@ struct AstNodeType {...@@ -106,7 +106,7 @@ struct AstNodeType {
106 AstNodeTypeType type;106 AstNodeTypeType type;
107 Buf primitive_name;107 Buf primitive_name;
108 AstNode *child_type;108 AstNode *child_type;
109 AstNode *array_size;109 AstNode *array_size; // can be null
110 bool is_const;110 bool is_const;
111 AstNode *compiler_expr;111 AstNode *compiler_expr;
112};112};
std/std.zig+5-5
...@@ -53,7 +53,7 @@ pub fn fprint_str(fd: isize, str: string) -> isize {...@@ -53,7 +53,7 @@ pub fn fprint_str(fd: isize, str: string) -> isize {
53// TODO error handling53// TODO error handling
54pub fn print_u64(x: u64) -> isize {54pub fn print_u64(x: u64) -> isize {
55 // TODO use max_u64_base10_digits instead of hardcoding 2055 // TODO use max_u64_base10_digits instead of hardcoding 20
56 var buf: [u8; 20];56 var buf: [20]u8;
57 const len = buf_print_u64(buf.ptr, x);57 const len = buf_print_u64(buf.ptr, x);
58 return write(stdout_fileno, buf.ptr, len);58 return write(stdout_fileno, buf.ptr, len);
59}59}
...@@ -62,7 +62,7 @@ pub fn print_u64(x: u64) -> isize {...@@ -62,7 +62,7 @@ pub fn print_u64(x: u64) -> isize {
62// TODO error handling62// TODO error handling
63pub fn print_i64(x: i64) -> isize {63pub fn print_i64(x: i64) -> isize {
64 // TODO use max_u64_base10_digits instead of hardcoding 2064 // TODO use max_u64_base10_digits instead of hardcoding 20
65 var buf: [u8; 20];65 var buf: [20]u8;
66 const len = buf_print_i64(buf.ptr, x);66 const len = buf_print_i64(buf.ptr, x);
67 return write(stdout_fileno, buf.ptr, len);67 return write(stdout_fileno, buf.ptr, len);
68}68}
...@@ -83,9 +83,9 @@ fn buf_print_i64(out_buf: &u8, x: i64) -> usize {...@@ -83,9 +83,9 @@ fn buf_print_i64(out_buf: &u8, x: i64) -> usize {
83}83}
8484
85fn buf_print_u64(out_buf: &u8, x: u64) -> usize {85fn buf_print_u64(out_buf: &u8, x: u64) -> usize {
86 var buf: [u8; max_u64_base10_digits];86 var buf: [max_u64_base10_digits]u8;
87 var a = x;87 var a = x;
88 var index = max_u64_base10_digits;88 var index = buf.len;
8989
90 while (true) {90 while (true) {
91 const digit = a % 10;91 const digit = a % 10;
...@@ -96,7 +96,7 @@ fn buf_print_u64(out_buf: &u8, x: u64) -> usize {...@@ -96,7 +96,7 @@ fn buf_print_u64(out_buf: &u8, x: u64) -> usize {
96 break;96 break;
97 }97 }
9898
99 const len = max_u64_base10_digits - index;99 const len = buf.len - index;
100100
101 // TODO memcpy intrinsic101 // TODO memcpy intrinsic
102 var i: usize = 0;102 var i: usize = 0;
test/run_tests.cpp+2-2
...@@ -349,7 +349,7 @@ done:...@@ -349,7 +349,7 @@ done:
349use "std.zig";349use "std.zig";
350350
351pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {351pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
352 var array : [u32; 5];352 var array : [5]u32;
353353
354 var i : u32 = 0;354 var i : u32 = 0;
355 while (i < 5) {355 while (i < 5) {
...@@ -784,7 +784,7 @@ use "std.zig";...@@ -784,7 +784,7 @@ use "std.zig";
784const ARRAY_SIZE : u8 = 20;784const ARRAY_SIZE : u8 = 20;
785785
786pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {786pub fn main(argc: isize, argv: &&u8, env: &&u8) -> i32 {
787 var array : [u8; ARRAY_SIZE];787 var array : [ARRAY_SIZE]u8;
788 print_u64(#sizeof(#typeof(array)));788 print_u64(#sizeof(#typeof(array)));
789 print_str("\n");789 print_str("\n");
790 return 0;790 return 0;