feature. See also
. The project being documented here (as the example) is the Zig library itself.
AstGen.simpleBinOp
fn simpleBinOp(
gz: *GenZir,
scope: *Scope,
ri: ResultInfo,
node: Ast.Node.Index,
op_inst_tag: Zir.Inst.Tag,
) InnerError!Zir.Inst.Ref
File
Code
fn simpleBinOp(
gz: *GenZir,
scope: *Scope,
ri: ResultInfo,
node: Ast.Node.Index,
op_inst_tag: Zir.Inst.Tag,
) InnerError!Zir.Inst.Ref {
const astgen = gz.astgen;
const tree = astgen.tree;
const lhs_node, const rhs_node = tree.nodeData(node).node_and_node;
if (op_inst_tag == .cmp_neq or op_inst_tag == .cmp_eq) {
const str = if (op_inst_tag == .cmp_eq) "==" else "!=";
if (tree.nodeTag(lhs_node) == .string_literal or
tree.nodeTag(rhs_node) == .string_literal)
return astgen.failNode(node, "cannot compare strings with {s}", .{str});
}
const lhs = try reachableExpr(gz, scope, .{ .rl = .none }, lhs_node, node);
const cursor = switch (op_inst_tag) {
.add, .sub, .mul, .div, .mod_rem => maybeAdvanceSourceCursorToMainToken(gz, node),
else => undefined,
};
const rhs = try reachableExpr(gz, scope, .{ .rl = .none }, rhs_node, node);
switch (op_inst_tag) {
.add, .sub, .mul, .div, .mod_rem => {
try emitDbgStmt(gz, cursor);
},
else => {},
}
const result = try gz.addPlNode(op_inst_tag, node, Zir.Inst.Bin{ .lhs = lhs, .rhs = rhs });
return rvalue(gz, ri, result, node);
}