| ... | ... | @@ -218,24 +218,54 @@ pub const Tree = struct { |
| 218 | 218 | } |
| 219 | 219 | const Positionals = @TypeOf(inst.positionals); |
| 220 | 220 | try stream.writeAll("= " ++ @tagName(inst_tag) ++ "("); |
| 221 | | inline for (@typeInfo(Positionals).Struct.fields) |arg_field, i| { |
| 221 | const pos_fields = @typeInfo(Positionals).Struct.fields; |
| 222 | inline for (pos_fields) |arg_field, i| { |
| 222 | 223 | if (i != 0) { |
| 223 | 224 | try stream.writeAll(", "); |
| 224 | 225 | } |
| 225 | 226 | try self.writeParamToStream(stream, @field(inst.positionals, arg_field.name), inst_table); |
| 226 | 227 | } |
| 228 | |
| 229 | comptime var need_comma = pos_fields.len != 0; |
| 230 | const KW_Args = @TypeOf(inst.kw_args); |
| 231 | inline for (@typeInfo(KW_Args).Struct.fields) |arg_field, i| { |
| 232 | if (need_comma) { |
| 233 | try stream.writeAll(",\n "); |
| 234 | } |
| 235 | if (@typeInfo(arg_field.field_type) == .Optional) { |
| 236 | if (@field(inst.kw_args, arg_field.name)) |non_optional| { |
| 237 | try stream.print("{}=", .{arg_field.name}); |
| 238 | try self.writeParamToStream(stream, non_optional, inst_table); |
| 239 | need_comma = true; |
| 240 | } |
| 241 | } else { |
| 242 | try stream.print("{}=", .{arg_field.name}); |
| 243 | try self.writeParamToStream(stream, @field(inst.kw_args, arg_field.name), inst_table); |
| 244 | need_comma = true; |
| 245 | } |
| 246 | } |
| 247 | |
| 227 | 248 | try stream.writeByte(')'); |
| 228 | 249 | } |
| 229 | 250 | |
| 230 | | pub fn writeParamToStream(self: Tree, stream: var, param: var, inst_table: *const InstPtrTable) !void { |
| 251 | fn writeParamToStream(self: Tree, stream: var, param: var, inst_table: *const InstPtrTable) !void { |
| 252 | if (@typeInfo(@TypeOf(param)) == .Enum) { |
| 253 | return stream.writeAll(@tagName(param)); |
| 254 | } |
| 231 | 255 | switch (@TypeOf(param)) { |
| 232 | 256 | Value => { |
| 233 | 257 | try stream.print("{}", .{param}); |
| 234 | 258 | }, |
| 235 | | *Inst => { |
| 236 | | const info = inst_table.getValue(param).?; |
| 237 | | const prefix = if (info.fn_body == null) "@" else "%"; |
| 238 | | try stream.print("{}{}", .{ prefix, info.index }); |
| 259 | *Inst => return self.writeInstParamToStream(stream, param, inst_table), |
| 260 | []*Inst => { |
| 261 | try stream.writeByte('['); |
| 262 | for (param) |inst, i| { |
| 263 | if (i != 0) { |
| 264 | try stream.writeAll(", "); |
| 265 | } |
| 266 | try self.writeInstParamToStream(stream, inst, inst_table); |
| 267 | } |
| 268 | try stream.writeByte(']'); |
| 239 | 269 | }, |
| 240 | 270 | Inst.Fn.Body => { |
| 241 | 271 | try stream.writeAll("{\n"); |
| ... | ... | @@ -246,9 +276,16 @@ pub const Tree = struct { |
| 246 | 276 | } |
| 247 | 277 | try stream.writeByte('}'); |
| 248 | 278 | }, |
| 279 | bool => return stream.writeByte("01"[@boolToInt(param)]), |
| 249 | 280 | else => |T| @compileError("unimplemented: rendering parameter of type " ++ @typeName(T)), |
| 250 | 281 | } |
| 251 | 282 | } |
| 283 | |
| 284 | fn writeInstParamToStream(self: Tree, stream: var, inst: *Inst, inst_table: *const InstPtrTable) !void { |
| 285 | const info = inst_table.getValue(inst).?; |
| 286 | const prefix = if (info.fn_body == null) "@" else "%"; |
| 287 | try stream.print("{}{}", .{ prefix, info.index }); |
| 288 | } |
| 252 | 289 | }; |
| 253 | 290 | |
| 254 | 291 | const ParseContext = struct { |