Skip to content

Commit

Permalink
chore: zig 0.12.0-dev.878+7abf9b3a8
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Oct 11, 2023
1 parent 17324bc commit 1708cf7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 78 deletions.
8 changes: 4 additions & 4 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn get_buzz_prefix(b: *Build) []const u8 {
pub fn build(b: *Build) !void {
// Check minimum zig version
const current_zig = builtin.zig_version;
const min_zig = std.SemanticVersion.parse("0.12.0-dev.790+ad6f8e3a5") catch return;
const min_zig = std.SemanticVersion.parse("0.12.0-dev.878+7abf9b3a8") catch return;
if (current_zig.order(min_zig).compare(.lt)) {
@panic(b.fmt("Your Zig version v{} does not meet the minimum build requirement of v{}", .{ current_zig, min_zig }));
}
Expand Down Expand Up @@ -322,7 +322,7 @@ pub fn build(b: *Build) !void {
if (build_options.needLibC()) {
exe.linkLibC();
}
exe.main_pkg_path = .{ .path = "." };
exe.main_mod_path = .{ .path = "." };

exe.addOptions("build_options", build_options.step(b));

Expand All @@ -345,7 +345,7 @@ pub fn build(b: *Build) !void {
if (build_options.needLibC()) {
lib.linkLibC();
}
lib.main_pkg_path = .{ .path = "src" };
lib.main_mod_path = .{ .path = "src" };

lib.addOptions("build_options", build_options.step(b));

Expand Down Expand Up @@ -427,7 +427,7 @@ pub fn build(b: *Build) !void {
if (build_options.needLibC()) {
std_lib.linkLibC();
}
std_lib.main_pkg_path = .{ .path = "src" };
std_lib.main_mod_path = .{ .path = "src" };
std_lib.linkLibrary(lib);
std_lib.addOptions("build_options", build_options.step(b));

Expand Down
30 changes: 11 additions & 19 deletions src/mirjit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4849,30 +4849,22 @@ fn generateVarDeclaration(self: *Self, var_declaration_node: *n.VarDeclarationNo
// We should only declare locals
assert(var_declaration_node.slot_type == .Local);

// An inline if expression is the only expression that might add a new local while evaluated so we need
// Some expression (inline if with null unwrap) can add a new local while evaluated so we need
// to reserve our space on the stack right away otherwise the local count will be off
// We have to do this because we use registers in the JIT context, in the VM this ends up being ok even though the local count is off
if (var_declaration_node.value != null and var_declaration_node.value.?.node_type == .If) {
try self.buildPush(
m.MIR_new_uint_op(
self.ctx,
v.Value.Null.val,
),
);
// However this might slow things a bit. Even though we know it's only for the inline if expression, we don't know how deep it is in the value node

try self.buildPush(
m.MIR_new_uint_op(
self.ctx,
v.Value.Null.val,
),
);

if (var_declaration_node.value) |value| {
try self.buildSetLocal(
var_declaration_node.slot,
(try self.generateNode(var_declaration_node.value.?)).?,
);
} else {
try self.buildPush(
if (var_declaration_node.value) |value|
(try self.generateNode(value)).?
else
m.MIR_new_uint_op(
self.ctx,
v.Value.Null.val,
),
(try self.generateNode(value)).?,
);
}

Expand Down
118 changes: 63 additions & 55 deletions tests/052-inline-if.buzz
Original file line number Diff line number Diff line change
@@ -1,84 +1,92 @@
import "std";
import "debug";

test "ternary" {
int value = if (true) 12 else 0;
| test "ternary" {
| int value = if (true) 12 else 0;

assert(value == 12, message: "could use constant inline if");
| assert(value == 12, message: "could use constant inline if");

value = if ("hello".len() == 2) 0 else 12;
| value = if ("hello".len() == 2) 0 else 12;

assert(value == 12, message: "could use inline if");
}
| assert(value == 12, message: "could use inline if");
| }

test "multiple branches" {
const value = 12;
| test "multiple branches" {
| const value = 12;

const expr = if (value == 14)
"hello"
else if (value == 12)
"yolo"
else
null;
| const expr = if (value == 14)
| "hello"
| else if (value == 12)
| "yolo"
| else
| null;

assert(expr == "yolo", message: "Could use multiple branches with inline if");
}
| assert(expr == "yolo", message: "Could use multiple branches with inline if");
| }

test "unwrap" {
str? opt = "hello";
| test "unwrap" {
| str? opt = "hello";

const another = 12;
| const _ = 12;

const expr = if (opt -> hello)
hello
else
"bye";
| const expr = if (opt -> hello)
| hello
| else
| "bye";

assert(expr == "hello", message: "Could unwrap optional in inline if");
}
| assert(expr == "hello", message: "Could unwrap optional in inline if");
| }

test "cast" {
const any unknown = "hello";
| test "cast" {
| const any unknown = "hello";

const expr = if (unknown as str hello)
hello
else
"bye";
| const expr = if (unknown as str hello)
| hello
| else
| "bye";

assert(expr == "hello", message: "Could cast in inline if");
}
| assert(expr == "hello", message: "Could cast in inline if");
| }

test "cast to unwrap null" {
const str? unknown = null;
| test "cast to unwrap null" {
| const str? unknown = null;

const expr = if (unknown as str hello)
hello
else
"hello";
| const expr = if (unknown as str hello)
| hello
| else
| "hello";

assert(expr == "hello", message: "Could unwrap optional using cas in inline if");
}
| assert(expr == "hello", message: "Could unwrap optional using cas in inline if");
| }

test "cast to unwrap null" {
const str? unknown = "hello";
| test "cast to unwrap null" {
| const str? unknown = "hello";

const expr = if (unknown as str hello)
hello
else
"bye";
| const expr = if (unknown as str hello)
| hello
| else
| "bye";

assert(expr == "hello", message: "Could unwrap optional using cast in inline if");
}
| assert(expr == "hello", message: "Could unwrap optional using cast in inline if");
| }

test "not in a var decl" {
str? opt = "hello";

const another = 12;
const _ = 12;

| assert(
| (if (opt -> hello)
| hello
| else
| "bye") == "hello",
| message: "Could unwrap optional in inline if in expression"
| );

assert(
(if (opt -> hello)
const condition = if (opt -> hello)
hello
else
"bye") == "hello",
message: "Could unwrap optional in inline if in expression"
);
"bye";

dump(condition);
}

0 comments on commit 1708cf7

Please sign in to comment.