stage2: implement compare operator AST->ZIR

This commit is contained in:
Andrew Kelley 2020-07-08 07:04:43 +00:00
parent 5e60872060
commit be0546d877
2 changed files with 29 additions and 0 deletions

View File

@ -1309,6 +1309,33 @@ fn astGenInfixOp(self: *Module, scope: *Scope, infix_node: *ast.Node.InfixOp) In
return self.addZIRInst(scope, src, zir.Inst.Add, .{ .lhs = lhs, .rhs = rhs }, .{});
},
.BangEqual,
.EqualEqual,
.GreaterThan,
.GreaterOrEqual,
.LessThan,
.LessOrEqual,
=> {
const lhs = try self.astGenExpr(scope, infix_node.lhs);
const rhs = try self.astGenExpr(scope, infix_node.rhs);
const tree = scope.tree();
const src = tree.token_locs[infix_node.op_token].start;
return self.addZIRInst(scope, src, zir.Inst.Cmp, .{
.lhs = lhs,
.op = @as(std.math.CompareOperator, switch (infix_node.op) {
.BangEqual => .neq,
.EqualEqual => .eq,
.GreaterThan => .gt,
.GreaterOrEqual => .gte,
.LessThan => .lt,
.LessOrEqual => .lte,
else => unreachable,
}),
.rhs = rhs,
}, .{});
},
else => |op| {
return self.failNode(scope, &infix_node.base, "TODO implement infix operator {}", .{op});
},

View File

@ -538,6 +538,8 @@ pub const Inst = struct {
kw_args: struct {},
};
/// TODO get rid of the op positional arg and make that data part of
/// the base Inst tag.
pub const Cmp = struct {
pub const base_tag = Tag.cmp;
base: Inst,