| ... | ... | @@ -33,6 +33,8 @@ pub fn expr(mod: *Module, scope: *Scope, node: *ast.Node) InnerError!*zir.Inst { |
| 33 | 33 | .GreaterOrEqual => return cmp(mod, scope, node.castTag(.GreaterOrEqual).?, .gte), |
| 34 | 34 | .LessThan => return cmp(mod, scope, node.castTag(.LessThan).?, .lt), |
| 35 | 35 | .LessOrEqual => return cmp(mod, scope, node.castTag(.LessOrEqual).?, .lte), |
| 36 | .Period => return field(mod, scope, node.castTag(.Period).?), |
| 37 | .SuffixOp => return suffixOp(mod, scope, node.castTag(.SuffixOp).?), |
| 36 | 38 | .BoolNot => return boolNot(mod, scope, node.castTag(.BoolNot).?), |
| 37 | 39 | else => return mod.failNode(scope, node, "TODO implement astgen.Expr for {}", .{@tagName(node.tag)}), |
| 38 | 40 | } |
| ... | ... | @@ -121,6 +123,43 @@ fn assign(mod: *Module, scope: *Scope, infix_node: *ast.Node.SimpleInfixOp) Inne |
| 121 | 123 | } |
| 122 | 124 | } |
| 123 | 125 | |
| 126 | /// Identifier nodes -> ZIR string instruction |
| 127 | pub fn identifierString(mod: *Module, scope: *Scope, node: *ast.Node.Identifier) InnerError!*zir.Inst { |
| 128 | const tree = scope.tree(); |
| 129 | const src = tree.token_locs[node.token].start; |
| 130 | |
| 131 | var ident_name = tree.tokenSlice(node.token); |
| 132 | if (std.mem.startsWith(u8, ident_name, "@")) |
| 133 | ident_name = ident_name[2 .. ident_name.len - 1]; |
| 134 | |
| 135 | return mod.addZIRInst(scope, src, zir.Inst.Str, .{ .bytes = ident_name }, .{}); |
| 136 | } |
| 137 | |
| 138 | fn field(mod: *Module, scope: *Scope, node: *ast.Node.SimpleInfixOp) InnerError!*zir.Inst { |
| 139 | const tree = scope.tree(); |
| 140 | const src = tree.token_locs[node.op_token].start; |
| 141 | |
| 142 | const lhs = try expr(mod, scope, node.lhs); |
| 143 | const field_name = try identifierString(mod, scope, node.rhs.castTag(.Identifier).?); |
| 144 | |
| 145 | const pointer = try mod.addZIRInst(scope, src, zir.Inst.FieldPtr, .{ .object_ptr = lhs, .field_name = field_name }, .{}); |
| 146 | return mod.addZIRInst(scope, src, zir.Inst.Deref, .{ .ptr = pointer }, .{}); |
| 147 | } |
| 148 | |
| 149 | fn suffixOp(mod: *Module, scope: *Scope, node: *ast.Node.SuffixOp) InnerError!*zir.Inst { |
| 150 | switch (node.op) { |
| 151 | .Deref => { |
| 152 | const tree = scope.tree(); |
| 153 | const src = tree.token_locs[node.rtoken].start; |
| 154 | |
| 155 | const lhs = try expr(mod, scope, node.lhs); |
| 156 | |
| 157 | return mod.addZIRInst(scope, src, zir.Inst.Deref, .{ .ptr = lhs }, .{}); |
| 158 | }, |
| 159 | else => return mod.failNode(scope, &node.base, "TODO implement astGenExpr for suffixOp {}", .{node.op}), |
| 160 | } |
| 161 | } |
| 162 | |
| 124 | 163 | fn add(mod: *Module, scope: *Scope, infix_node: *ast.Node.SimpleInfixOp) InnerError!*zir.Inst { |
| 125 | 164 | const lhs = try expr(mod, scope, infix_node.lhs); |
| 126 | 165 | const rhs = try expr(mod, scope, infix_node.rhs); |