| ... | ... | @@ -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 | } |
| ... | ... | @@ -90,7 +92,7 @@ fn varDecl(mod: *Module, scope: *Scope, node: *ast.Node.VarDecl) InnerError!Scop |
| 90 | 92 | return mod.failNode(scope, init_node, "TODO implement result locations", .{}); |
| 91 | 93 | } |
| 92 | 94 | const init_inst = try expr(mod, scope, init_node); |
| 93 | | const ident_name = tree.tokenSlice(node.name_token); // TODO support @"aoeu" identifiers |
| 95 | const ident_name = try identifierTokenString(mod, scope, node.name_token); |
| 94 | 96 | return Scope.LocalVar{ |
| 95 | 97 | .parent = scope, |
| 96 | 98 | .gen_zir = scope.getGenZIR(), |
| ... | ... | @@ -110,7 +112,7 @@ fn assign(mod: *Module, scope: *Scope, infix_node: *ast.Node.SimpleInfixOp) Inne |
| 110 | 112 | if (infix_node.lhs.tag == .Identifier) { |
| 111 | 113 | const ident = @fieldParentPtr(ast.Node.Identifier, "base", infix_node.lhs); |
| 112 | 114 | const tree = scope.tree(); |
| 113 | | const ident_name = tree.tokenSlice(ident.token); |
| 115 | const ident_name = try identifierTokenString(mod, scope, ident.token); |
| 114 | 116 | if (std.mem.eql(u8, ident_name, "_")) { |
| 115 | 117 | return expr(mod, scope, infix_node.rhs); |
| 116 | 118 | } else { |
| ... | ... | @@ -121,6 +123,60 @@ fn assign(mod: *Module, scope: *Scope, infix_node: *ast.Node.SimpleInfixOp) Inne |
| 121 | 123 | } |
| 122 | 124 | } |
| 123 | 125 | |
| 126 | /// Identifier token -> String (allocated in scope.arena()) |
| 127 | pub fn identifierTokenString(mod: *Module, scope: *Scope, token: ast.TokenIndex) InnerError![]const u8 { |
| 128 | const tree = scope.tree(); |
| 129 | |
| 130 | const ident_name = tree.tokenSlice(token); |
| 131 | if (std.mem.startsWith(u8, ident_name, "@")) { |
| 132 | const raw_string = ident_name[1..]; |
| 133 | var bad_index: usize = undefined; |
| 134 | return std.zig.parseStringLiteral(scope.arena(), raw_string, &bad_index) catch |err| switch (err) { |
| 135 | error.InvalidCharacter => { |
| 136 | const bad_byte = raw_string[bad_index]; |
| 137 | const src = tree.token_locs[token].start; |
| 138 | return mod.fail(scope, src + 1 + bad_index, "invalid string literal character: '{c}'\n", .{bad_byte}); |
| 139 | }, |
| 140 | else => |e| return e, |
| 141 | }; |
| 142 | } |
| 143 | return ident_name; |
| 144 | } |
| 145 | |
| 146 | pub fn identifierStringInst(mod: *Module, scope: *Scope, node: *ast.Node.Identifier) InnerError!*zir.Inst { |
| 147 | const tree = scope.tree(); |
| 148 | const src = tree.token_locs[node.token].start; |
| 149 | |
| 150 | const ident_name = try identifierTokenString(mod, scope, node.token); |
| 151 | |
| 152 | return mod.addZIRInst(scope, src, zir.Inst.Str, .{ .bytes = ident_name }, .{}); |
| 153 | } |
| 154 | |
| 155 | fn field(mod: *Module, scope: *Scope, node: *ast.Node.SimpleInfixOp) InnerError!*zir.Inst { |
| 156 | const tree = scope.tree(); |
| 157 | const src = tree.token_locs[node.op_token].start; |
| 158 | |
| 159 | const lhs = try expr(mod, scope, node.lhs); |
| 160 | const field_name = try identifierStringInst(mod, scope, node.rhs.castTag(.Identifier).?); |
| 161 | |
| 162 | const pointer = try mod.addZIRInst(scope, src, zir.Inst.FieldPtr, .{ .object_ptr = lhs, .field_name = field_name }, .{}); |
| 163 | return mod.addZIRInst(scope, src, zir.Inst.Deref, .{ .ptr = pointer }, .{}); |
| 164 | } |
| 165 | |
| 166 | fn suffixOp(mod: *Module, scope: *Scope, node: *ast.Node.SuffixOp) InnerError!*zir.Inst { |
| 167 | switch (node.op) { |
| 168 | .Deref => { |
| 169 | const tree = scope.tree(); |
| 170 | const src = tree.token_locs[node.rtoken].start; |
| 171 | |
| 172 | const lhs = try expr(mod, scope, node.lhs); |
| 173 | |
| 174 | return mod.addZIRInst(scope, src, zir.Inst.Deref, .{ .ptr = lhs }, .{}); |
| 175 | }, |
| 176 | else => return mod.failNode(scope, &node.base, "TODO implement astGenExpr for suffixOp {}", .{node.op}), |
| 177 | } |
| 178 | } |
| 179 | |
| 124 | 180 | fn add(mod: *Module, scope: *Scope, infix_node: *ast.Node.SimpleInfixOp) InnerError!*zir.Inst { |
| 125 | 181 | const lhs = try expr(mod, scope, infix_node.lhs); |
| 126 | 182 | const rhs = try expr(mod, scope, infix_node.rhs); |
| ... | ... | @@ -257,8 +313,7 @@ fn identifier(mod: *Module, scope: *Scope, ident: *ast.Node.Identifier) InnerErr |
| 257 | 313 | defer tracy.end(); |
| 258 | 314 | |
| 259 | 315 | const tree = scope.tree(); |
| 260 | | // TODO implement @"aoeu" identifiers |
| 261 | | const ident_name = tree.tokenSlice(ident.token); |
| 316 | const ident_name = try identifierTokenString(mod, scope, ident.token); |
| 262 | 317 | const src = tree.token_locs[ident.token].start; |
| 263 | 318 | if (mem.eql(u8, ident_name, "_")) { |
| 264 | 319 | return mod.failNode(scope, &ident.base, "TODO implement '_' identifier", .{}); |