Skip to content

Commit

Permalink
Use templates from separate module.
Browse files Browse the repository at this point in the history
  • Loading branch information
fubark committed Feb 11, 2024
1 parent 2818639 commit de8f451
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
16 changes: 4 additions & 12 deletions src/builtins/builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -533,21 +533,13 @@ const ParseCyberState = struct {
node: cy.Node,
};

fn genTypeSpecString(vm: *cy.VM, ast: cy.ast.AstView, headId: cy.NodeId) !cy.Value {
if (headId != cy.NullNode) {
fn genTypeSpecString(vm: *cy.VM, ast: cy.ast.AstView, exprId: cy.NodeId) !cy.Value {
if (exprId != cy.NullNode) {
var sb: std.ArrayListUnmanaged(u8) = .{};
defer sb.deinit(vm.alloc);

var name = ast.nodeStringById(headId);
try sb.appendSlice(vm.alloc, name);

var curId = ast.node(headId).next();
while (curId != cy.NullNode) {
try sb.append(vm.alloc, '.');
name = ast.nodeStringById(curId);
try sb.appendSlice(vm.alloc, name);
curId = ast.node(curId).next();
}
var enc = cy.ast.Encoder{ .ast = ast };
try enc.writeNode(sb.writer(vm.alloc), exprId);

return try vm.retainOrAllocAstring(sb.items);
} else {
Expand Down
11 changes: 6 additions & 5 deletions src/cte.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,25 @@ fn execTemplateCtNodes(c: *cy.Chunk, template: *cy.sym.TypeTemplate, params: []c
try paramMap.put(c.alloc, param.name, params[i]);
}

const tchunk = template.chunk();
const res = try c.alloc.alloc(cy.NodeId, template.ctNodes.len);
for (template.ctNodes, 0..) |ctNodeId, i| {
const node = c.ast.node(ctNodeId);
const node = tchunk.ast.node(ctNodeId);

// Check for simple template param replacement.
const child = c.ast.node(node.data.comptimeExpr.child);
const child = tchunk.ast.node(node.data.comptimeExpr.child);
if (child.type() == .ident) {
const name = c.ast.nodeString(child);
const name = tchunk.ast.nodeString(child);
if (paramMap.get(name)) |param| {
res[i] = try cte.genNodeFromValue(c, param, node.srcPos);
res[i] = try cte.genNodeFromValue(tchunk, param, node.srcPos);
continue;
}
}
// General CTE.
return error.TODO;
}

c.updateAstView(c.parser.ast.view());
tchunk.updateAstView(tchunk.parser.ast.view());
return res;
}

Expand Down
14 changes: 7 additions & 7 deletions src/sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -968,33 +968,33 @@ pub fn declareHostObject(c: *cy.Chunk, nodeId: cy.NodeId) !*cy.sym.HostObjectTyp
}

pub fn declareTemplateVariant(c: *cy.Chunk, template: *cy.sym.TypeTemplate, variantId: u32) !*cy.sym.Sym {
const tchunk = template.chunk();
const template_n = tchunk.ast.node(template.declId);
switch (template.kind) {
.object_t => {
const sym = try c.declareObjectVariantType(template, variantId);

// Set variant in context chunk so sema ops know to swap nodes.
const variant = template.variants.items[variantId];
c.patchTemplateNodes = variant.patchNodes;
tchunk.patchTemplateNodes = variant.patchNodes;

const template_n = c.ast.node(template.declId);
try declareObjectMembers(c, @ptrCast(sym), template_n.data.typeTemplate.typeDecl);
try declareObjectMembers(tchunk, @ptrCast(sym), template_n.data.typeTemplate.typeDecl);

// Defer method sema.
const mod = sym.getMod();
try mod.chunk.variantFuncSyms.appendSlice(c.alloc, mod.funcs.items);
return @ptrCast(sym);
},
.enum_t => {
const template_n = c.ast.node(template.declId);
const enum_n = c.ast.node(template_n.data.typeTemplate.typeDecl);
const enum_n = tchunk.ast.node(template_n.data.typeTemplate.typeDecl);
const isChoiceType = enum_n.data.enumDecl.isChoiceType;
const sym = try c.declareEnumVariantType(template, isChoiceType, variantId);

// Set variant in context chunk so sema ops know to swap nodes.
const variant = template.variants.items[variantId];
c.patchTemplateNodes = variant.patchNodes;
tchunk.patchTemplateNodes = variant.patchNodes;

try declareEnumMembers(c, sym, template_n.data.typeTemplate.typeDecl);
try declareEnumMembers(tchunk, sym, template_n.data.typeTemplate.typeDecl);
return @ptrCast(sym);
},
}
Expand Down
4 changes: 4 additions & 0 deletions src/sym.zig
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,10 @@ pub const TypeTemplate = struct {
/// This array allows each variant to build their replacement AST
/// without scanning the entire template again for compile-time expressions.
ctNodes: []const cy.NodeId,

pub fn chunk(self: *const TypeTemplate) *cy.Chunk {
return self.head.parent.?.getMod().?.chunk;
}
};

pub const TemplateParam = struct {
Expand Down
4 changes: 4 additions & 0 deletions test/modules/import.cy
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ my v = a.Vec2.new(3, 4)
t.eq(v.x, 3.0)
t.eq(v.y, 4.0)

-- Templates from another module.
var b a.TFoo#int = [a: 123]
t.eq(b.a, 123)

--cytest: pass
6 changes: 5 additions & 1 deletion test/modules/test_mods/a.cy
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ func Vec2.new(x float, y float):
type Vec2Alias Vec2

type Bar:
var a float
var a float

template(T type)
type TFoo:
var a #T

0 comments on commit de8f451

Please sign in to comment.