authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-10 13:49:52+02:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-10 13:49:52+02:00
log706e0d739e5bb4a6e7a0e4f6e168eb71069e4df2
tree53a5a88ab3bcc4cacc85a9efaf213b0e79cf2111
parent1b81e406f0ca285738404120178b7b3824cf84bc

std.zig.parser readded all tests

* Ops!

1 files changed, 760 insertions(+), 765 deletions(-)

std/zig/parser.zig+760-765
...@@ -1119,12 +1119,6 @@ pub const Parser = struct {...@@ -1119,12 +1119,6 @@ pub const Parser = struct {
1119 },1119 },
11201120
1121 State.TypeExprBegin => |dest_ptr| {1121 State.TypeExprBegin => |dest_ptr| {
1122 const token = self.getNextToken();
1123 if (token.id == Token.Id.Keyword_var) {
1124 @panic("TODO param with type var");
1125 }
1126 self.putBackToken(token);
1127
1128 stack.append(State { .TypeExprEnd = dest_ptr }) catch unreachable;1122 stack.append(State { .TypeExprEnd = dest_ptr }) catch unreachable;
1129 try stack.append(State { .PrefixOpExpression = dest_ptr });1123 try stack.append(State { .PrefixOpExpression = dest_ptr });
1130 continue;1124 continue;
...@@ -1160,9 +1154,6 @@ pub const Parser = struct {...@@ -1160,9 +1154,6 @@ pub const Parser = struct {
1160 try stack.append(State { .AddrOfModifiers = &node.op.AddrOf });1154 try stack.append(State { .AddrOfModifiers = &node.op.AddrOf });
1161 }1155 }
1162 continue;1156 continue;
1163 //Token.Id.Keyword_await => {
1164 // @panic("TODO: await");
1165 //},
1166 } else {1157 } else {
1167 self.putBackToken(token);1158 self.putBackToken(token);
1168 stack.append(State { .SuffixOpExpressionBegin = dest_ptr }) catch unreachable;1159 stack.append(State { .SuffixOpExpressionBegin = dest_ptr }) catch unreachable;
...@@ -1408,7 +1399,6 @@ pub const Parser = struct {...@@ -1408,7 +1399,6 @@ pub const Parser = struct {
1408 continue;1399 continue;
1409 },1400 },
1410 Token.Id.LBracket => {1401 Token.Id.LBracket => {
1411 // TODO: option("align" "(" Expression option(":" Integer ":" Integer) ")")) option("const") option("volatile")
1412 const rbracket_token = self.getNextToken();1402 const rbracket_token = self.getNextToken();
1413 if (rbracket_token.id == Token.Id.RBracket) {1403 if (rbracket_token.id == Token.Id.RBracket) {
1414 const node = try self.createPrefixOp(arena, token, ast.NodePrefixOp.PrefixOp{1404 const node = try self.createPrefixOp(arena, token, ast.NodePrefixOp.PrefixOp{
...@@ -4222,760 +4212,765 @@ fn testCanonical(source: []const u8) !void {...@@ -4222,760 +4212,765 @@ fn testCanonical(source: []const u8) !void {
4222 }4212 }
4223}4213}
42244214
4225//test "zig fmt: get stdout or fail" {4215test "zig fmt: get stdout or fail" {
4226// try testCanonical(4216 try testCanonical(
4227// \\const std = @import("std");4217 \\const std = @import("std");
4228// \\4218 \\
4229// \\pub fn main() !void {4219 \\pub fn main() !void {
4230// \\ // If this program is run without stdout attached, exit with an error.4220 \\ // If this program is run without stdout attached, exit with an error.
4231// \\ // another comment4221 \\ // another comment
4232// \\ var stdout_file = try std.io.getStdOut;4222 \\ var stdout_file = try std.io.getStdOut;
4233// \\}4223 \\}
4234// \\4224 \\
4235// );4225 );
4236//}4226}
4237//4227
4238//test "zig fmt: preserve spacing" {4228test "zig fmt: preserve spacing" {
4239// try testCanonical(4229 try testCanonical(
4240// \\const std = @import("std");4230 \\const std = @import("std");
4241// \\4231 \\
4242// \\pub fn main() !void {4232 \\pub fn main() !void {
4243// \\ var stdout_file = try std.io.getStdOut;4233 \\ var stdout_file = try std.io.getStdOut;
4244// \\ var stdout_file = try std.io.getStdOut;4234 \\ var stdout_file = try std.io.getStdOut;
4245// \\4235 \\
4246// \\ var stdout_file = try std.io.getStdOut;4236 \\ var stdout_file = try std.io.getStdOut;
4247// \\ var stdout_file = try std.io.getStdOut;4237 \\ var stdout_file = try std.io.getStdOut;
4248// \\}4238 \\}
4249// \\4239 \\
4250// );4240 );
4251//}4241}
4252//4242
4253//test "zig fmt: return types" {4243test "zig fmt: return types" {
4254// try testCanonical(4244 try testCanonical(
4255// \\pub fn main() !void {}4245 \\pub fn main() !void {}
4256// \\pub fn main() var {}4246 \\pub fn main() var {}
4257// \\pub fn main() i32 {}4247 \\pub fn main() i32 {}
4258// \\4248 \\
4259// );4249 );
4260//}4250}
4261//4251
4262//test "zig fmt: imports" {4252test "zig fmt: imports" {
4263// try testCanonical(4253 try testCanonical(
4264// \\const std = @import("std");4254 \\const std = @import("std");
4265// \\const std = @import();4255 \\const std = @import();
4266// \\4256 \\
4267// );4257 );
4268//}4258}
4269//4259
4270//test "zig fmt: global declarations" {4260test "zig fmt: global declarations" {
4271// try testCanonical(4261 try testCanonical(
4272// \\const a = b;4262 \\const a = b;
4273// \\pub const a = b;4263 \\pub const a = b;
4274// \\var a = b;4264 \\var a = b;
4275// \\pub var a = b;4265 \\pub var a = b;
4276// \\const a: i32 = b;4266 \\const a: i32 = b;
4277// \\pub const a: i32 = b;4267 \\pub const a: i32 = b;
4278// \\var a: i32 = b;4268 \\var a: i32 = b;
4279// \\pub var a: i32 = b;4269 \\pub var a: i32 = b;
4280// \\extern const a: i32 = b;4270 \\extern const a: i32 = b;
4281// \\pub extern const a: i32 = b;4271 \\pub extern const a: i32 = b;
4282// \\extern var a: i32 = b;4272 \\extern var a: i32 = b;
4283// \\pub extern var a: i32 = b;4273 \\pub extern var a: i32 = b;
4284// \\extern "a" const a: i32 = b;4274 \\extern "a" const a: i32 = b;
4285// \\pub extern "a" const a: i32 = b;4275 \\pub extern "a" const a: i32 = b;
4286// \\extern "a" var a: i32 = b;4276 \\extern "a" var a: i32 = b;
4287// \\pub extern "a" var a: i32 = b;4277 \\pub extern "a" var a: i32 = b;
4288// \\4278 \\
4289// );4279 );
4290//}4280}
4291//4281
4292//test "zig fmt: extern declaration" {4282test "zig fmt: extern declaration" {
4293// try testCanonical(4283 try testCanonical(
4294// \\extern var foo: c_int;4284 \\extern var foo: c_int;
4295// \\4285 \\
4296// );4286 );
4297//}4287}
4298//4288
4299//test "zig fmt: alignment" {4289test "zig fmt: alignment" {
4300// try testCanonical(4290 try testCanonical(
4301// \\var foo: c_int align(1);4291 \\var foo: c_int align(1);
4302// \\4292 \\
4303// );4293 );
4304//}4294}
4305//4295
4306//test "zig fmt: C main" {4296test "zig fmt: C main" {
4307// try testCanonical(4297 try testCanonical(
4308// \\fn main(argc: c_int, argv: &&u8) c_int {4298 \\fn main(argc: c_int, argv: &&u8) c_int {
4309// \\ const a = b;4299 \\ const a = b;
4310// \\}4300 \\}
4311// \\4301 \\
4312// );4302 );
4313//}4303}
4314//4304
4315//test "zig fmt: return" {4305test "zig fmt: return" {
4316// try testCanonical(4306 try testCanonical(
4317// \\fn foo(argc: c_int, argv: &&u8) c_int {4307 \\fn foo(argc: c_int, argv: &&u8) c_int {
4318// \\ return 0;4308 \\ return 0;
4319// \\}4309 \\}
4320// \\4310 \\
4321// \\fn bar() void {4311 \\fn bar() void {
4322// \\ return;4312 \\ return;
4323// \\}4313 \\}
4324// \\4314 \\
4325// );4315 );
4326//}4316}
4327//
4328//test "zig fmt: pointer attributes" {
4329// try testCanonical(
4330// \\extern fn f1(s: &align(&u8) u8) c_int;
4331// \\extern fn f2(s: &&align(1) &const &volatile u8) c_int;
4332// \\extern fn f3(s: &align(1) const &align(1) volatile &const volatile u8) c_int;
4333// \\extern fn f4(s: &align(1) const volatile u8) c_int;
4334// \\
4335// );
4336//}
4337//
4338//test "zig fmt: slice attributes" {
4339// try testCanonical(
4340// \\extern fn f1(s: &align(&u8) u8) c_int;
4341// \\extern fn f2(s: &&align(1) &const &volatile u8) c_int;
4342// \\extern fn f3(s: &align(1) const &align(1) volatile &const volatile u8) c_int;
4343// \\extern fn f4(s: &align(1) const volatile u8) c_int;
4344// \\
4345// );
4346//}
4347//
4348//test "zig fmt: test declaration" {
4349// try testCanonical(
4350// \\test "test name" {
4351// \\ const a = 1;
4352// \\ var b = 1;
4353// \\}
4354// \\
4355// );
4356//}
4357//
4358//test "zig fmt: infix operators" {
4359// try testCanonical(
4360// \\test "infix operators" {
4361// \\ var i = undefined;
4362// \\ i = 2;
4363// \\ i *= 2;
4364// \\ i |= 2;
4365// \\ i ^= 2;
4366// \\ i <<= 2;
4367// \\ i >>= 2;
4368// \\ i &= 2;
4369// \\ i *= 2;
4370// \\ i *%= 2;
4371// \\ i -= 2;
4372// \\ i -%= 2;
4373// \\ i += 2;
4374// \\ i +%= 2;
4375// \\ i /= 2;
4376// \\ i %= 2;
4377// \\ _ = i == i;
4378// \\ _ = i != i;
4379// \\ _ = i != i;
4380// \\ _ = i.i;
4381// \\ _ = i || i;
4382// \\ _ = i!i;
4383// \\ _ = i ** i;
4384// \\ _ = i ++ i;
4385// \\ _ = i ?? i;
4386// \\ _ = i % i;
4387// \\ _ = i / i;
4388// \\ _ = i *% i;
4389// \\ _ = i * i;
4390// \\ _ = i -% i;
4391// \\ _ = i - i;
4392// \\ _ = i +% i;
4393// \\ _ = i + i;
4394// \\ _ = i << i;
4395// \\ _ = i >> i;
4396// \\ _ = i & i;
4397// \\ _ = i ^ i;
4398// \\ _ = i | i;
4399// \\ _ = i >= i;
4400// \\ _ = i <= i;
4401// \\ _ = i > i;
4402// \\ _ = i < i;
4403// \\ _ = i and i;
4404// \\ _ = i or i;
4405// \\}
4406// \\
4407// );
4408//}
4409//
4410//test "zig fmt: precedence" {
4411// try testCanonical(
4412// \\test "precedence" {
4413// \\ a!b();
4414// \\ (a!b)();
4415// \\ !a!b;
4416// \\ !(a!b);
4417// \\ !a{ };
4418// \\ !(a{ });
4419// \\ a + b{ };
4420// \\ (a + b){ };
4421// \\ a << b + c;
4422// \\ (a << b) + c;
4423// \\ a & b << c;
4424// \\ (a & b) << c;
4425// \\ a ^ b & c;
4426// \\ (a ^ b) & c;
4427// \\ a | b ^ c;
4428// \\ (a | b) ^ c;
4429// \\ a == b | c;
4430// \\ (a == b) | c;
4431// \\ a and b == c;
4432// \\ (a and b) == c;
4433// \\ a or b and c;
4434// \\ (a or b) and c;
4435// \\ (a or b) and c;
4436// \\}
4437// \\
4438// );
4439//}
4440//
4441//test "zig fmt: prefix operators" {
4442// try testCanonical(
4443// \\test "prefix operators" {
4444// \\ try return --%~??!*&0;
4445// \\}
4446// \\
4447// );
4448//}
4449//
4450//test "zig fmt: call expression" {
4451// try testCanonical(
4452// \\test "test calls" {
4453// \\ a();
4454// \\ a(1);
4455// \\ a(1, 2);
4456// \\ a(1, 2) + a(1, 2);
4457// \\}
4458// \\
4459// );
4460//}
4461//
4462//test "zig fmt: var args" {
4463// try testCanonical(
4464// \\fn print(args: ...) void {}
4465// \\
4466// );
4467//}
4468//
4469//test "zig fmt: extern function" {
4470// try testCanonical(
4471// \\extern fn puts(s: &const u8) c_int;
4472// \\extern "c" fn puts(s: &const u8) c_int;
4473// \\
4474// );
4475//}
4476//
4477//test "zig fmt: multiline string" {
4478// try testCanonical(
4479// \\const s =
4480// \\ \\ something
4481// \\ \\ something else
4482// \\ ;
4483// \\
4484// );
4485//}
4486//
4487//test "zig fmt: values" {
4488// try testCanonical(
4489// \\test "values" {
4490// \\ 1;
4491// \\ 1.0;
4492// \\ "string";
4493// \\ c"cstring";
4494// \\ 'c';
4495// \\ true;
4496// \\ false;
4497// \\ null;
4498// \\ undefined;
4499// \\ error;
4500// \\ this;
4501// \\ unreachable;
4502// \\}
4503// \\
4504// );
4505//}
4506//
4507//test "zig fmt: indexing" {
4508// try testCanonical(
4509// \\test "test index" {
4510// \\ a[0];
4511// \\ a[0 + 5];
4512// \\ a[0..];
4513// \\ a[0..5];
4514// \\ a[a[0]];
4515// \\ a[a[0..]];
4516// \\ a[a[0..5]];
4517// \\ a[a[0]..];
4518// \\ a[a[0..5]..];
4519// \\ a[a[0]..a[0]];
4520// \\ a[a[0..5]..a[0]];
4521// \\ a[a[0..5]..a[0..5]];
4522// \\}
4523// \\
4524// );
4525//}
4526//
4527//test "zig fmt: struct declaration" {
4528// try testCanonical(
4529// \\const S = struct {
4530// \\ const Self = this;
4531// \\ f1: u8,
4532// \\
4533// \\ fn method(self: &Self) Self {
4534// \\ return *self;
4535// \\ }
4536// \\
4537// \\ f2: u8
4538// \\};
4539// \\
4540// \\const Ps = packed struct {
4541// \\ a: u8,
4542// \\ b: u8,
4543// \\
4544// \\ c: u8
4545// \\};
4546// \\
4547// \\const Es = extern struct {
4548// \\ a: u8,
4549// \\ b: u8,
4550// \\
4551// \\ c: u8
4552// \\};
4553// \\
4554// );
4555//}
4556//
4557//test "zig fmt: enum declaration" {
4558// try testCanonical(
4559// \\const E = enum {
4560// \\ Ok,
4561// \\ SomethingElse = 0
4562// \\};
4563// \\
4564// \\const E2 = enum(u8) {
4565// \\ Ok,
4566// \\ SomethingElse = 255,
4567// \\ SomethingThird
4568// \\};
4569// \\
4570// \\const Ee = extern enum {
4571// \\ Ok,
4572// \\ SomethingElse,
4573// \\ SomethingThird
4574// \\};
4575// \\
4576// \\const Ep = packed enum {
4577// \\ Ok,
4578// \\ SomethingElse,
4579// \\ SomethingThird
4580// \\};
4581// \\
4582// );
4583//}
4584//
4585//test "zig fmt: union declaration" {
4586// try testCanonical(
4587// \\const U = union {
4588// \\ Int: u8,
4589// \\ Float: f32,
4590// \\ None,
4591// \\ Bool: bool
4592// \\};
4593// \\
4594// \\const Ue = union(enum) {
4595// \\ Int: u8,
4596// \\ Float: f32,
4597// \\ None,
4598// \\ Bool: bool
4599// \\};
4600// \\
4601// \\const E = enum {
4602// \\ Int,
4603// \\ Float,
4604// \\ None,
4605// \\ Bool
4606// \\};
4607// \\
4608// \\const Ue2 = union(E) {
4609// \\ Int: u8,
4610// \\ Float: f32,
4611// \\ None,
4612// \\ Bool: bool
4613// \\};
4614// \\
4615// \\const Eu = extern union {
4616// \\ Int: u8,
4617// \\ Float: f32,
4618// \\ None,
4619// \\ Bool: bool
4620// \\};
4621// \\
4622// );
4623//}
4624//
4625//test "zig fmt: error set declaration" {
4626// try testCanonical(
4627// \\const E = error {
4628// \\ A,
4629// \\ B,
4630// \\
4631// \\ C
4632// \\};
4633// \\
4634// );
4635//}
4636//
4637//test "zig fmt: arrays" {
4638// try testCanonical(
4639// \\test "test array" {
4640// \\ const a: [2]u8 = [2]u8{ 1, 2 };
4641// \\ const a: [2]u8 = []u8{ 1, 2 };
4642// \\ const a: [0]u8 = []u8{ };
4643// \\}
4644// \\
4645// );
4646//}
4647//
4648//test "zig fmt: container initializers" {
4649// try testCanonical(
4650// \\const a1 = []u8{ };
4651// \\const a2 = []u8{ 1, 2, 3, 4 };
4652// \\const s1 = S{ };
4653// \\const s2 = S{ .a = 1, .b = 2 };
4654// \\
4655// );
4656//}
4657//
4658//test "zig fmt: catch" {
4659// try testCanonical(
4660// \\test "catch" {
4661// \\ const a: error!u8 = 0;
4662// \\ _ = a catch return;
4663// \\ _ = a catch |err| return;
4664// \\}
4665// \\
4666// );
4667//}
4668//
4669//test "zig fmt: blocks" {
4670// try testCanonical(
4671// \\test "blocks" {
4672// \\ {
4673// \\ const a = 0;
4674// \\ const b = 0;
4675// \\ }
4676// \\
4677// \\ blk: {
4678// \\ const a = 0;
4679// \\ const b = 0;
4680// \\ }
4681// \\
4682// \\ const r = blk: {
4683// \\ const a = 0;
4684// \\ const b = 0;
4685// \\ };
4686// \\}
4687// \\
4688// );
4689//}
4690//
4691//test "zig fmt: switch" {
4692// try testCanonical(
4693// \\test "switch" {
4694// \\ switch (0) {
4695// \\ 0 => {},
4696// \\ 1 => unreachable,
4697// \\ 2, 3 => {},
4698// \\ 4 ... 7 => {},
4699// \\ 1 + 4 * 3 + 22 => {},
4700// \\ else => {
4701// \\ const a = 1;
4702// \\ const b = a;
4703// \\ }
4704// \\ }
4705// \\
4706// \\ const res = switch (0) {
4707// \\ 0 => 0,
4708// \\ 1 => 2,
4709// \\ else => 4
4710// \\ };
4711// \\
4712// \\ const Union = union(enum) {
4713// \\ Int: i64,
4714// \\ Float: f64
4715// \\ };
4716// \\
4717// \\ const u = Union{ .Int = 0 };
4718// \\ switch (u) {
4719// \\ Union.Int => |int| {},
4720// \\ Union.Float => |*float| unreachable
4721// \\ }
4722// \\}
4723// \\
4724// );
4725//}
4726//
4727//test "zig fmt: while" {
4728// try testCanonical(
4729// \\test "while" {
4730// \\ while (10 < 1) {
4731// \\ unreachable;
4732// \\ }
4733// \\
4734// \\ while (10 < 1)
4735// \\ unreachable;
4736// \\
4737// \\ var i: usize = 0;
4738// \\ while (i < 10) : (i += 1) {
4739// \\ continue;
4740// \\ }
4741// \\
4742// \\ i = 0;
4743// \\ while (i < 10) : (i += 1)
4744// \\ continue;
4745// \\
4746// \\ i = 0;
4747// \\ var j: usize = 0;
4748// \\ while (i < 10) : ({
4749// \\ i += 1;
4750// \\ j += 1;
4751// \\ }) {
4752// \\ continue;
4753// \\ }
4754// \\
4755// \\ var a: ?u8 = 2;
4756// \\ while (a) |v| : (a = null) {
4757// \\ continue;
4758// \\ }
4759// \\
4760// \\ while (a) |v| : (a = null)
4761// \\ unreachable;
4762// \\
4763// \\ label: while (10 < 0) {
4764// \\ unreachable;
4765// \\ }
4766// \\
4767// \\ const res = while (0 < 10) {
4768// \\ break 7;
4769// \\ } else {
4770// \\ unreachable;
4771// \\ };
4772// \\
4773// \\ var a: error!u8 = 0;
4774// \\ while (a) |v| {
4775// \\ a = error.Err;
4776// \\ } else |err| {
4777// \\ i = 1;
4778// \\ }
4779// \\
4780// \\ comptime var k: usize = 0;
4781// \\ inline while (i < 10) : (i += 1)
4782// \\ j += 2;
4783// \\}
4784// \\
4785// );
4786//}
4787//
4788//test "zig fmt: for" {
4789// try testCanonical(
4790// \\test "for" {
4791// \\ const a = []u8{ 1, 2, 3 };
4792// \\ for (a) |v| {
4793// \\ continue;
4794// \\ }
4795// \\
4796// \\ for (a) |v|
4797// \\ continue;
4798// \\
4799// \\ for (a) |*v|
4800// \\ continue;
4801// \\
4802// \\ for (a) |v, i| {
4803// \\ continue;
4804// \\ }
4805// \\
4806// \\ for (a) |v, i|
4807// \\ continue;
4808// \\
4809// \\ const res = for (a) |v, i| {
4810// \\ break v;
4811// \\ } else {
4812// \\ unreachable;
4813// \\ };
4814// \\
4815// \\ var num: usize = 0;
4816// \\ inline for (a) |v, i| {
4817// \\ num += v;
4818// \\ num += i;
4819// \\ }
4820// \\}
4821// \\
4822// );
4823//}
4824//
4825//test "zig fmt: if" {
4826// try testCanonical(
4827// \\test "if" {
4828// \\ if (10 < 0) {
4829// \\ unreachable;
4830// \\ }
4831// \\
4832// \\ if (10 < 0) unreachable;
4833// \\
4834// \\ if (10 < 0) {
4835// \\ unreachable;
4836// \\ } else {
4837// \\ const a = 20;
4838// \\ }
4839// \\
4840// \\ if (10 < 0) {
4841// \\ unreachable;
4842// \\ } else if (5 < 0) {
4843// \\ unreachable;
4844// \\ } else {
4845// \\ const a = 20;
4846// \\ }
4847// \\
4848// \\ const is_world_broken = if (10 < 0) true else false;
4849// \\
4850// \\ const a: ?u8 = 10;
4851// \\ const b: ?u8 = null;
4852// \\ if (a) |v| {
4853// \\ const some = v;
4854// \\ } else if (b) |*v| {
4855// \\ unreachable;
4856// \\ } else {
4857// \\ const some = 10;
4858// \\ }
4859// \\
4860// \\ const non_null_a = if (a) |v| v else 0;
4861// \\
4862// \\ const a_err: error!u8 = 0;
4863// \\ if (a_err) |v| {
4864// \\ const p = v;
4865// \\ } else |err| {
4866// \\ unreachable;
4867// \\ }
4868// \\}
4869// \\
4870// );
4871//}
4872//
4873//test "zig fmt: defer" {
4874// try testCanonical(
4875// \\test "defer" {
4876// \\ var i: usize = 0;
4877// \\ defer i = 1;
4878// \\ defer {
4879// \\ i += 2;
4880// \\ i *= i;
4881// \\ }
4882// \\
4883// \\ errdefer i += 3;
4884// \\ errdefer {
4885// \\ i += 2;
4886// \\ i /= i;
4887// \\ }
4888// \\}
4889// \\
4890// );
4891//}
4892//
4893//test "zig fmt: comptime" {
4894// try testCanonical(
4895// \\fn a() u8 {
4896// \\ return 5;
4897// \\}
4898// \\
4899// \\fn b(comptime i: u8) u8 {
4900// \\ return i;
4901// \\}
4902// \\
4903// \\const av = comptime a();
4904// \\const av2 = comptime blk: {
4905// \\ var res = a();
4906// \\ res *= b(2);
4907// \\ break :blk res;
4908// \\};
4909// \\
4910// \\comptime {
4911// \\ _ = a();
4912// \\}
4913// \\
4914// \\test "comptime" {
4915// \\ const av3 = comptime a();
4916// \\ const av4 = comptime blk: {
4917// \\ var res = a();
4918// \\ res *= a();
4919// \\ break :blk res;
4920// \\ };
4921// \\
4922// \\ comptime var i = 0;
4923// \\ comptime {
4924// \\ i = a();
4925// \\ i += b(i);
4926// \\ }
4927// \\}
4928// \\
4929// );
4930//}
4931//
4932//test "zig fmt: fn type" {
4933// try testCanonical(
4934// \\fn a(i: u8) u8 {
4935// \\ return i + 1;
4936// \\}
4937// \\
4938// \\const a: fn(u8) u8 = undefined;
4939// \\const b: extern fn(u8) u8 = undefined;
4940// \\const c: nakedcc fn(u8) u8 = undefined;
4941// \\const ap: fn(u8) u8 = a;
4942// \\
4943// );
4944//}
4945//
4946//test "zig fmt: inline asm" {
4947// try testCanonical(
4948// \\pub fn syscall1(number: usize, arg1: usize) usize {
4949// \\ return asm volatile ("syscall"
4950// \\ : [ret] "={rax}" (-> usize)
4951// \\ : [number] "{rax}" (number),
4952// \\ [arg1] "{rdi}" (arg1)
4953// \\ : "rcx", "r11");
4954// \\}
4955// \\
4956// );
4957//}
4958//
4959//test "zig fmt: coroutines" {
4960// try testCanonical(
4961// \\async fn simpleAsyncFn() void {
4962// \\ x += 1;
4963// \\ suspend;
4964// \\ x += 1;
4965// \\ suspend |p| {}
4966// \\ const p = async simpleAsyncFn() catch unreachable;
4967// \\ await p;
4968// \\}
4969// \\
4970// \\test "coroutine suspend, resume, cancel" {
4971// \\ const p = try async<std.debug.global_allocator> testAsyncSeq();
4972// \\ resume p;
4973// \\ cancel p;
4974// \\}
4975// \\
4976// );
4977//}
49784317
4979test "1" {4318test "zig fmt: pointer attributes" {
4980 try testCanonical(@embedFile("../array_list.zig"));4319 try testCanonical(
4320 \\extern fn f1(s: &align(&u8) u8) c_int;
4321 \\extern fn f2(s: &&align(1) &const &volatile u8) c_int;
4322 \\extern fn f3(s: &align(1) const &align(1) volatile &const volatile u8) c_int;
4323 \\extern fn f4(s: &align(1) const volatile u8) c_int;
4324 \\
4325 );
4981}4326}
4327
4328test "zig fmt: slice attributes" {
4329 try testCanonical(
4330 \\extern fn f1(s: &align(&u8) u8) c_int;
4331 \\extern fn f2(s: &&align(1) &const &volatile u8) c_int;
4332 \\extern fn f3(s: &align(1) const &align(1) volatile &const volatile u8) c_int;
4333 \\extern fn f4(s: &align(1) const volatile u8) c_int;
4334 \\
4335 );
4336}
4337
4338test "zig fmt: test declaration" {
4339 try testCanonical(
4340 \\test "test name" {
4341 \\ const a = 1;
4342 \\ var b = 1;
4343 \\}
4344 \\
4345 );
4346}
4347
4348test "zig fmt: infix operators" {
4349 try testCanonical(
4350 \\test "infix operators" {
4351 \\ var i = undefined;
4352 \\ i = 2;
4353 \\ i *= 2;
4354 \\ i |= 2;
4355 \\ i ^= 2;
4356 \\ i <<= 2;
4357 \\ i >>= 2;
4358 \\ i &= 2;
4359 \\ i *= 2;
4360 \\ i *%= 2;
4361 \\ i -= 2;
4362 \\ i -%= 2;
4363 \\ i += 2;
4364 \\ i +%= 2;
4365 \\ i /= 2;
4366 \\ i %= 2;
4367 \\ _ = i == i;
4368 \\ _ = i != i;
4369 \\ _ = i != i;
4370 \\ _ = i.i;
4371 \\ _ = i || i;
4372 \\ _ = i!i;
4373 \\ _ = i ** i;
4374 \\ _ = i ++ i;
4375 \\ _ = i ?? i;
4376 \\ _ = i % i;
4377 \\ _ = i / i;
4378 \\ _ = i *% i;
4379 \\ _ = i * i;
4380 \\ _ = i -% i;
4381 \\ _ = i - i;
4382 \\ _ = i +% i;
4383 \\ _ = i + i;
4384 \\ _ = i << i;
4385 \\ _ = i >> i;
4386 \\ _ = i & i;
4387 \\ _ = i ^ i;
4388 \\ _ = i | i;
4389 \\ _ = i >= i;
4390 \\ _ = i <= i;
4391 \\ _ = i > i;
4392 \\ _ = i < i;
4393 \\ _ = i and i;
4394 \\ _ = i or i;
4395 \\}
4396 \\
4397 );
4398}
4399
4400test "zig fmt: precedence" {
4401 try testCanonical(
4402 \\test "precedence" {
4403 \\ a!b();
4404 \\ (a!b)();
4405 \\ !a!b;
4406 \\ !(a!b);
4407 \\ !a{ };
4408 \\ !(a{ });
4409 \\ a + b{ };
4410 \\ (a + b){ };
4411 \\ a << b + c;
4412 \\ (a << b) + c;
4413 \\ a & b << c;
4414 \\ (a & b) << c;
4415 \\ a ^ b & c;
4416 \\ (a ^ b) & c;
4417 \\ a | b ^ c;
4418 \\ (a | b) ^ c;
4419 \\ a == b | c;
4420 \\ (a == b) | c;
4421 \\ a and b == c;
4422 \\ (a and b) == c;
4423 \\ a or b and c;
4424 \\ (a or b) and c;
4425 \\ (a or b) and c;
4426 \\}
4427 \\
4428 );
4429}
4430
4431test "zig fmt: prefix operators" {
4432 try testCanonical(
4433 \\test "prefix operators" {
4434 \\ try return --%~??!*&0;
4435 \\}
4436 \\
4437 );
4438}
4439
4440test "zig fmt: call expression" {
4441 try testCanonical(
4442 \\test "test calls" {
4443 \\ a();
4444 \\ a(1);
4445 \\ a(1, 2);
4446 \\ a(1, 2) + a(1, 2);
4447 \\}
4448 \\
4449 );
4450}
4451
4452test "zig fmt: var args" {
4453 try testCanonical(
4454 \\fn print(args: ...) void {}
4455 \\
4456 );
4457}
4458
4459test "zig fmt: extern function" {
4460 try testCanonical(
4461 \\extern fn puts(s: &const u8) c_int;
4462 \\extern "c" fn puts(s: &const u8) c_int;
4463 \\
4464 );
4465}
4466
4467test "zig fmt: multiline string" {
4468 try testCanonical(
4469 \\const s =
4470 \\ \\ something
4471 \\ \\ something else
4472 \\ ;
4473 \\
4474 );
4475}
4476
4477test "zig fmt: values" {
4478 try testCanonical(
4479 \\test "values" {
4480 \\ 1;
4481 \\ 1.0;
4482 \\ "string";
4483 \\ c"cstring";
4484 \\ 'c';
4485 \\ true;
4486 \\ false;
4487 \\ null;
4488 \\ undefined;
4489 \\ error;
4490 \\ this;
4491 \\ unreachable;
4492 \\}
4493 \\
4494 );
4495}
4496
4497test "zig fmt: indexing" {
4498 try testCanonical(
4499 \\test "test index" {
4500 \\ a[0];
4501 \\ a[0 + 5];
4502 \\ a[0..];
4503 \\ a[0..5];
4504 \\ a[a[0]];
4505 \\ a[a[0..]];
4506 \\ a[a[0..5]];
4507 \\ a[a[0]..];
4508 \\ a[a[0..5]..];
4509 \\ a[a[0]..a[0]];
4510 \\ a[a[0..5]..a[0]];
4511 \\ a[a[0..5]..a[0..5]];
4512 \\}
4513 \\
4514 );
4515}
4516
4517test "zig fmt: struct declaration" {
4518 try testCanonical(
4519 \\const S = struct {
4520 \\ const Self = this;
4521 \\ f1: u8,
4522 \\
4523 \\ fn method(self: &Self) Self {
4524 \\ return *self;
4525 \\ }
4526 \\
4527 \\ f2: u8
4528 \\};
4529 \\
4530 \\const Ps = packed struct {
4531 \\ a: u8,
4532 \\ b: u8,
4533 \\
4534 \\ c: u8
4535 \\};
4536 \\
4537 \\const Es = extern struct {
4538 \\ a: u8,
4539 \\ b: u8,
4540 \\
4541 \\ c: u8
4542 \\};
4543 \\
4544 );
4545}
4546
4547test "zig fmt: enum declaration" {
4548 try testCanonical(
4549 \\const E = enum {
4550 \\ Ok,
4551 \\ SomethingElse = 0
4552 \\};
4553 \\
4554 \\const E2 = enum(u8) {
4555 \\ Ok,
4556 \\ SomethingElse = 255,
4557 \\ SomethingThird
4558 \\};
4559 \\
4560 \\const Ee = extern enum {
4561 \\ Ok,
4562 \\ SomethingElse,
4563 \\ SomethingThird
4564 \\};
4565 \\
4566 \\const Ep = packed enum {
4567 \\ Ok,
4568 \\ SomethingElse,
4569 \\ SomethingThird
4570 \\};
4571 \\
4572 );
4573}
4574
4575test "zig fmt: union declaration" {
4576 try testCanonical(
4577 \\const U = union {
4578 \\ Int: u8,
4579 \\ Float: f32,
4580 \\ None,
4581 \\ Bool: bool
4582 \\};
4583 \\
4584 \\const Ue = union(enum) {
4585 \\ Int: u8,
4586 \\ Float: f32,
4587 \\ None,
4588 \\ Bool: bool
4589 \\};
4590 \\
4591 \\const E = enum {
4592 \\ Int,
4593 \\ Float,
4594 \\ None,
4595 \\ Bool
4596 \\};
4597 \\
4598 \\const Ue2 = union(E) {
4599 \\ Int: u8,
4600 \\ Float: f32,
4601 \\ None,
4602 \\ Bool: bool
4603 \\};
4604 \\
4605 \\const Eu = extern union {
4606 \\ Int: u8,
4607 \\ Float: f32,
4608 \\ None,
4609 \\ Bool: bool
4610 \\};
4611 \\
4612 );
4613}
4614
4615test "zig fmt: error set declaration" {
4616 try testCanonical(
4617 \\const E = error {
4618 \\ A,
4619 \\ B,
4620 \\
4621 \\ C
4622 \\};
4623 \\
4624 );
4625}
4626
4627test "zig fmt: arrays" {
4628 try testCanonical(
4629 \\test "test array" {
4630 \\ const a: [2]u8 = [2]u8{ 1, 2 };
4631 \\ const a: [2]u8 = []u8{ 1, 2 };
4632 \\ const a: [0]u8 = []u8{ };
4633 \\}
4634 \\
4635 );
4636}
4637
4638test "zig fmt: container initializers" {
4639 try testCanonical(
4640 \\const a1 = []u8{ };
4641 \\const a2 = []u8{ 1, 2, 3, 4 };
4642 \\const s1 = S{ };
4643 \\const s2 = S{ .a = 1, .b = 2 };
4644 \\
4645 );
4646}
4647
4648test "zig fmt: catch" {
4649 try testCanonical(
4650 \\test "catch" {
4651 \\ const a: error!u8 = 0;
4652 \\ _ = a catch return;
4653 \\ _ = a catch |err| return;
4654 \\}
4655 \\
4656 );
4657}
4658
4659test "zig fmt: blocks" {
4660 try testCanonical(
4661 \\test "blocks" {
4662 \\ {
4663 \\ const a = 0;
4664 \\ const b = 0;
4665 \\ }
4666 \\
4667 \\ blk: {
4668 \\ const a = 0;
4669 \\ const b = 0;
4670 \\ }
4671 \\
4672 \\ const r = blk: {
4673 \\ const a = 0;
4674 \\ const b = 0;
4675 \\ };
4676 \\}
4677 \\
4678 );
4679}
4680
4681test "zig fmt: switch" {
4682 try testCanonical(
4683 \\test "switch" {
4684 \\ switch (0) {
4685 \\ 0 => {},
4686 \\ 1 => unreachable,
4687 \\ 2, 3 => {},
4688 \\ 4 ... 7 => {},
4689 \\ 1 + 4 * 3 + 22 => {},
4690 \\ else => {
4691 \\ const a = 1;
4692 \\ const b = a;
4693 \\ }
4694 \\ }
4695 \\
4696 \\ const res = switch (0) {
4697 \\ 0 => 0,
4698 \\ 1 => 2,
4699 \\ else => 4
4700 \\ };
4701 \\
4702 \\ const Union = union(enum) {
4703 \\ Int: i64,
4704 \\ Float: f64
4705 \\ };
4706 \\
4707 \\ const u = Union{ .Int = 0 };
4708 \\ switch (u) {
4709 \\ Union.Int => |int| {},
4710 \\ Union.Float => |*float| unreachable
4711 \\ }
4712 \\}
4713 \\
4714 );
4715}
4716
4717test "zig fmt: while" {
4718 try testCanonical(
4719 \\test "while" {
4720 \\ while (10 < 1) {
4721 \\ unreachable;
4722 \\ }
4723 \\
4724 \\ while (10 < 1)
4725 \\ unreachable;
4726 \\
4727 \\ var i: usize = 0;
4728 \\ while (i < 10) : (i += 1) {
4729 \\ continue;
4730 \\ }
4731 \\
4732 \\ i = 0;
4733 \\ while (i < 10) : (i += 1)
4734 \\ continue;
4735 \\
4736 \\ i = 0;
4737 \\ var j: usize = 0;
4738 \\ while (i < 10) : ({
4739 \\ i += 1;
4740 \\ j += 1;
4741 \\ }) {
4742 \\ continue;
4743 \\ }
4744 \\
4745 \\ var a: ?u8 = 2;
4746 \\ while (a) |v| : (a = null) {
4747 \\ continue;
4748 \\ }
4749 \\
4750 \\ while (a) |v| : (a = null)
4751 \\ unreachable;
4752 \\
4753 \\ label: while (10 < 0) {
4754 \\ unreachable;
4755 \\ }
4756 \\
4757 \\ const res = while (0 < 10) {
4758 \\ break 7;
4759 \\ } else {
4760 \\ unreachable;
4761 \\ };
4762 \\
4763 \\ var a: error!u8 = 0;
4764 \\ while (a) |v| {
4765 \\ a = error.Err;
4766 \\ } else |err| {
4767 \\ i = 1;
4768 \\ }
4769 \\
4770 \\ comptime var k: usize = 0;
4771 \\ inline while (i < 10) : (i += 1)
4772 \\ j += 2;
4773 \\}
4774 \\
4775 );
4776}
4777
4778test "zig fmt: for" {
4779 try testCanonical(
4780 \\test "for" {
4781 \\ const a = []u8{ 1, 2, 3 };
4782 \\ for (a) |v| {
4783 \\ continue;
4784 \\ }
4785 \\
4786 \\ for (a) |v|
4787 \\ continue;
4788 \\
4789 \\ for (a) |*v|
4790 \\ continue;
4791 \\
4792 \\ for (a) |v, i| {
4793 \\ continue;
4794 \\ }
4795 \\
4796 \\ for (a) |v, i|
4797 \\ continue;
4798 \\
4799 \\ const res = for (a) |v, i| {
4800 \\ break v;
4801 \\ } else {
4802 \\ unreachable;
4803 \\ };
4804 \\
4805 \\ var num: usize = 0;
4806 \\ inline for (a) |v, i| {
4807 \\ num += v;
4808 \\ num += i;
4809 \\ }
4810 \\}
4811 \\
4812 );
4813}
4814
4815test "zig fmt: if" {
4816 try testCanonical(
4817 \\test "if" {
4818 \\ if (10 < 0) {
4819 \\ unreachable;
4820 \\ }
4821 \\
4822 \\ if (10 < 0) unreachable;
4823 \\
4824 \\ if (10 < 0) {
4825 \\ unreachable;
4826 \\ } else {
4827 \\ const a = 20;
4828 \\ }
4829 \\
4830 \\ if (10 < 0) {
4831 \\ unreachable;
4832 \\ } else if (5 < 0) {
4833 \\ unreachable;
4834 \\ } else {
4835 \\ const a = 20;
4836 \\ }
4837 \\
4838 \\ const is_world_broken = if (10 < 0) true else false;
4839 \\
4840 \\ const a: ?u8 = 10;
4841 \\ const b: ?u8 = null;
4842 \\ if (a) |v| {
4843 \\ const some = v;
4844 \\ } else if (b) |*v| {
4845 \\ unreachable;
4846 \\ } else {
4847 \\ const some = 10;
4848 \\ }
4849 \\
4850 \\ const non_null_a = if (a) |v| v else 0;
4851 \\
4852 \\ const a_err: error!u8 = 0;
4853 \\ if (a_err) |v| {
4854 \\ const p = v;
4855 \\ } else |err| {
4856 \\ unreachable;
4857 \\ }
4858 \\}
4859 \\
4860 );
4861}
4862
4863test "zig fmt: defer" {
4864 try testCanonical(
4865 \\test "defer" {
4866 \\ var i: usize = 0;
4867 \\ defer i = 1;
4868 \\ defer {
4869 \\ i += 2;
4870 \\ i *= i;
4871 \\ }
4872 \\
4873 \\ errdefer i += 3;
4874 \\ errdefer {
4875 \\ i += 2;
4876 \\ i /= i;
4877 \\ }
4878 \\}
4879 \\
4880 );
4881}
4882
4883test "zig fmt: comptime" {
4884 try testCanonical(
4885 \\fn a() u8 {
4886 \\ return 5;
4887 \\}
4888 \\
4889 \\fn b(comptime i: u8) u8 {
4890 \\ return i;
4891 \\}
4892 \\
4893 \\const av = comptime a();
4894 \\const av2 = comptime blk: {
4895 \\ var res = a();
4896 \\ res *= b(2);
4897 \\ break :blk res;
4898 \\};
4899 \\
4900 \\comptime {
4901 \\ _ = a();
4902 \\}
4903 \\
4904 \\test "comptime" {
4905 \\ const av3 = comptime a();
4906 \\ const av4 = comptime blk: {
4907 \\ var res = a();
4908 \\ res *= a();
4909 \\ break :blk res;
4910 \\ };
4911 \\
4912 \\ comptime var i = 0;
4913 \\ comptime {
4914 \\ i = a();
4915 \\ i += b(i);
4916 \\ }
4917 \\}
4918 \\
4919 );
4920}
4921
4922test "zig fmt: fn type" {
4923 try testCanonical(
4924 \\fn a(i: u8) u8 {
4925 \\ return i + 1;
4926 \\}
4927 \\
4928 \\const a: fn(u8) u8 = undefined;
4929 \\const b: extern fn(u8) u8 = undefined;
4930 \\const c: nakedcc fn(u8) u8 = undefined;
4931 \\const ap: fn(u8) u8 = a;
4932 \\
4933 );
4934}
4935
4936test "zig fmt: inline asm" {
4937 try testCanonical(
4938 \\pub fn syscall1(number: usize, arg1: usize) usize {
4939 \\ return asm volatile ("syscall"
4940 \\ : [ret] "={rax}" (-> usize)
4941 \\ : [number] "{rax}" (number),
4942 \\ [arg1] "{rdi}" (arg1)
4943 \\ : "rcx", "r11");
4944 \\}
4945 \\
4946 );
4947}
4948
4949test "zig fmt: coroutines" {
4950 try testCanonical(
4951 \\async fn simpleAsyncFn() void {
4952 \\ x += 1;
4953 \\ suspend;
4954 \\ x += 1;
4955 \\ suspend |p| {}
4956 \\ const p = async simpleAsyncFn() catch unreachable;
4957 \\ await p;
4958 \\}
4959 \\
4960 \\test "coroutine suspend, resume, cancel" {
4961 \\ const p = try async<std.debug.global_allocator> testAsyncSeq();
4962 \\ resume p;
4963 \\ cancel p;
4964 \\}
4965 \\
4966 );
4967}
4968
4969//{
4970// var it = self.link_libs.iterator();
4971// while (true) {
4972// const entry = it.next() ?? break;
4973// zig_args.append("--library") catch unreachable;
4974// zig_args.append(entry.key) catch unreachable;
4975// }
4976//}