Skip to content

Commit

Permalink
_ initializes void value.
Browse files Browse the repository at this point in the history
  • Loading branch information
fubark committed Sep 2, 2024
1 parent cf24d0d commit f61bb82
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub const NodeType = enum(u7) {
unwrap_choice,
unwrap_or,
use_alias,
void_lit,
whileCondStmt,
whileInfStmt,
whileOptStmt,
Expand Down Expand Up @@ -700,6 +701,7 @@ fn NodeData(comptime node_t: NodeType) type {
.unwrap_choice => UnwrapChoice,
.unwrap_or => UnwrapOr,
.use_alias => UseAlias,
.void_lit => Token,
.whileCondStmt => WhileCondStmt,
.whileInfStmt => WhileInfStmt,
.whileOptStmt => WhileOptStmt,
Expand Down Expand Up @@ -838,6 +840,7 @@ pub const Node = struct {
.unwrap_choice => self.cast(.unwrap_choice).left.pos(),
.unwrap_or => self.cast(.unwrap_or).opt.pos(),
.use_alias => self.cast(.use_alias).pos,
.void_lit => self.cast(.void_lit).pos,
.whileInfStmt => self.cast(.whileInfStmt).pos,
.whileCondStmt => self.cast(.whileCondStmt).pos,
.whileOptStmt => self.cast(.whileOptStmt).pos,
Expand Down Expand Up @@ -913,7 +916,7 @@ pub const UnaryOp = enum(u8) {
};

test "ast internals." {
try t.eq(std.enums.values(NodeType).len, 100);
try t.eq(std.enums.values(NodeType).len, 101);
try t.eq(@sizeOf(NodeHeader), 1);
}

Expand Down
13 changes: 13 additions & 0 deletions src/bc_gen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ fn genExpr(c: *Chunk, idx: usize, cstr: Cstr) anyerror!GenValue {
.unwrapChoice => genUnwrapChoice(c, idx, cstr, node),
.unwrap_or => genUnwrapOr(c, idx, cstr, node),
.varSym => genVarSym(c, idx, cstr, node),
.voidv => genVoid(c, cstr, node),
.blockExpr => genBlockExpr(c, idx, cstr, node),
else => {
rt.errZFmt(c.vm, "{}\n", .{code});
Expand Down Expand Up @@ -1237,6 +1238,18 @@ fn genUnwrapChoice(c: *Chunk, loc: usize, cstr: Cstr, node: *ast.Node) !GenValue
return finishDstInst(c, inst, retain);
}

fn genVoid(c: *Chunk, cstr: Cstr, node: *ast.Node) !GenValue {
const inst = try bc.selectForNoErrNoDepInst(c, cstr, bt.Void, false, node);
if (inst.requiresPreRelease) {
try pushRelease(c, inst.dst, node);
}
// Does not generate inst.
if (inst.own_dst) {
try initSlot(c, inst.dst, false, node);
}
return finishNoErrNoDepInst(c, inst, false);
}

fn genTrue(c: *Chunk, cstr: Cstr, node: *ast.Node) !GenValue {
const inst = try bc.selectForNoErrNoDepInst(c, cstr, bt.Boolean, false, node);
if (inst.requiresPreRelease) {
Expand Down
1 change: 1 addition & 0 deletions src/ir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub const ExprCode = enum(u8) {
list,
map,

voidv,
truev,
falsev,
errorv,
Expand Down
4 changes: 4 additions & 0 deletions src/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2926,6 +2926,10 @@ pub const Parser = struct {
return @ptrCast(try self.newSpanNode(.ident, start));
}
},
.underscore => {
self.advance();
return try self.ast.newNodeErase(.void_lit, .{ .pos = self.tokenSrcPos(start) });
},
.true_k => {
self.advance();
return try self.ast.newNodeErase(.trueLit, .{ .pos = self.tokenSrcPos(start) });
Expand Down
8 changes: 8 additions & 0 deletions src/sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5482,6 +5482,9 @@ pub const ChunkExt = struct {
}
return c.reportErrorFmt("Can not infer dot literal.", &.{}, node);
},
.void_lit => {
return c.semaVoid(node);
},
.trueLit => {
return c.semaTrue(node);
},
Expand Down Expand Up @@ -6321,6 +6324,11 @@ pub const ChunkExt = struct {
return ExprResult.initStatic(irIdx, bt.Byte);
}

pub fn semaVoid(c: *cy.Chunk, node: *ast.Node) !ExprResult {
const loc = try c.ir.pushExpr(.voidv, c.alloc, bt.Void, node, {});
return ExprResult.initStatic(loc, bt.Void);
}

pub fn semaTrue(c: *cy.Chunk, node: *ast.Node) !ExprResult {
const loc = try c.ir.pushExpr(.truev, c.alloc, bt.Boolean, node, {});
return ExprResult.initStatic(loc, bt.Boolean);
Expand Down
3 changes: 3 additions & 0 deletions src/std/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ fn eq2(c: cy.Context, type_id: cy.TypeId, act: rt.Any, exp: rt.Any) bool {
}
} else {
switch (type_id) {
bt.Void => {
return true;
},
bt.Byte => {
if (act.asByte() == exp.asByte()) {
return true;
Expand Down
1 change: 1 addition & 0 deletions src/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ pub const SemaExt = struct {

pub fn isUnboxedType(s: *cy.Sema, id: cy.TypeId) bool {
switch (id) {
bt.Void,
bt.Byte,
bt.Integer => return true,
bt.Symbol,
Expand Down
6 changes: 6 additions & 0 deletions src/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,9 @@ fn box(vm: *VM, val: Value, type_id: cy.TypeId) !cy.Value {
@panic("Unsupported.");
}
}
if (type_id == bt.Void) {
return cy.Value.Void;
}
return vm.allocBoxValue(type_id, val.val);
}

Expand All @@ -2067,6 +2070,9 @@ pub fn unbox(vm: *VM, val: Value, type_id: cy.TypeId) cy.Value {
@panic("Unsupported.");
}
}
if (type_id == bt.Void) {
return cy.Value.initRaw(0);
}
return val.asHeapObject().object.firstValue;
}

Expand Down
1 change: 1 addition & 0 deletions test/behavior_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ if (!aot) {
run.case("types/type_alias.cy");
run.case("types/type_spec.cy");
run.case("types/unnamed_object.cy");
run.case("types/void.cy");

if (!cy.isWasm) {
run.case2(Config.initFileModules("./test/modules/type_spec.cy"), "modules/type_spec.cy");
Expand Down
8 changes: 8 additions & 0 deletions test/types/void.cy
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use test

-- Initialize void.
var a = _
test.eq(typeOf(a), void)
test.eq(a, _)

--cytest: pass

0 comments on commit f61bb82

Please sign in to comment.