authorgravatar for vallahor91@gmail.comVallahor <vallahor91@gmail.com> 2022-05-30 15:45:57-03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:12-07:00
logf07534069deaa500df075ca6424f4bce29dcd7f8
treea32dc7bf80163bbc2e8dc4f21f1a2f2bbaade0d1
parent9be9e4d02c059e55c476fd98e789bbc02c08730a

WIP: switch_block tag


2 files changed, 176 insertions(+), 5 deletions(-)

lib/docs/main.js+30
...@@ -1062,6 +1062,36 @@ var zigAnalysis;...@@ -1062,6 +1062,36 @@ var zigAnalysis;
1062 function exprName(expr, opts) {1062 function exprName(expr, opts) {
1063 switch (Object.keys(expr)[0]) {1063 switch (Object.keys(expr)[0]) {
1064 default: throw "oh no";1064 default: throw "oh no";
1065 case "enumLiteral": {
1066 let literal = expr.enumLiteral;
1067 return literal;
1068 }
1069 case "switchOp":{
1070 let payloadHtml = "switch() {</br>";
1071 for (let i = 0; i < expr.switchOp.cases.length; i++) {
1072 const caseIndex = expr.switchOp.cases[i];
1073 const item = zigAnalysis.exprs[caseIndex];
1074 console.log(caseIndex);
1075 console.log(item);
1076 if (item['enumLiteral']) {
1077 payloadHtml += " " + " ." + exprName(item, opts) + " = " + "</br>";
1078 continue;
1079 }
1080 payloadHtml += " " + exprName(item, opts) + " = " + "</br>";
1081 }
1082 if (expr.switchOp.else_index !== 0) {
1083 const else_index = expr.switchOp.else_index;
1084 const item = zigAnalysis.exprs[else_index];
1085 console.log(item);
1086 payloadHtml += " " + "else" + " = " + "</br>";
1087 }
1088 payloadHtml += "}";
1089 return payloadHtml;
1090 }
1091 case "switchIndex": {
1092 const switchIndex = zigAnalysis.exprs[expr.switchIndex];
1093 return exprName(switchIndex, opts);
1094 }
1065 case "fieldRef" : {1095 case "fieldRef" : {
1066 // const fieldRef = zigAnalysis.decls[expr.fieldRef.index];1096 // const fieldRef = zigAnalysis.decls[expr.fieldRef.index];
1067 // const struct_name = zigAnalysis.decls[expr.struct[0].val.typeRef.refPath[0].declRef].name;1097 // const struct_name = zigAnalysis.decls[expr.struct[0].val.typeRef.refPath[0].declRef].name;
src/Autodoc.zig+146-5
...@@ -659,6 +659,8 @@ const DocData = struct {...@@ -659,6 +659,8 @@ const DocData = struct {
659 enumToInt: usize, // index in `exprs`659 enumToInt: usize, // index in `exprs`
660 compileError: []const u8,660 compileError: []const u8,
661 string: []const u8, // direct value661 string: []const u8, // direct value
662 switchIndex: usize, // index in `exprs`
663 switchOp: SwitchOp,
662 // Index a `type` like struct with expressions664 // Index a `type` like struct with expressions
663 // it's necessary because when a caller ask by a binOp maybe there are665 // it's necessary because when a caller ask by a binOp maybe there are
664 // more binary op inside them, so the caller get's the current `exprs` index666 // more binary op inside them, so the caller get's the current `exprs` index
...@@ -681,6 +683,13 @@ const DocData = struct {...@@ -681,6 +683,13 @@ const DocData = struct {
681 floor: bool = false,683 floor: bool = false,
682 trunc: bool = false,684 trunc: bool = false,
683 };685 };
686 const SwitchOp = struct {
687 cases: []usize,
688 else_index: ?usize,
689 // body_cases: ?[]usize,
690
691 // const Case = struct { lhs: Expr, rhs: Expr };
692 };
684 const As = struct {693 const As = struct {
685 typeRefArg: ?usize, // index in `exprs`694 typeRefArg: ?usize, // index in `exprs`
686 exprArg: usize, // index in `exprs`695 exprArg: usize, // index in `exprs`
...@@ -765,6 +774,16 @@ const DocData = struct {...@@ -765,6 +774,16 @@ const DocData = struct {
765 try w.print("{s}", .{comma});774 try w.print("{s}", .{comma});
766 }775 }
767 },776 },
777 .switchOp => |v| try std.json.stringify(
778 struct { switchOp: SwitchOp }{ .switchOp = v },
779 options,
780 w,
781 ),
782 .switchIndex => |v| try std.json.stringify(
783 struct { switchIndex: usize }{ .switchIndex = v },
784 options,
785 w,
786 ),
768 .binOp => |v| try std.json.stringify(787 .binOp => |v| try std.json.stringify(
769 struct { binOp: BinOp }{ .binOp = v },788 struct { binOp: BinOp }{ .binOp = v },
770 options,789 options,
...@@ -2170,12 +2189,13 @@ fn walkInstruction(...@@ -2170,12 +2189,13 @@ fn walkInstruction(
2170 file,2189 file,
2171 parent_scope,2190 parent_scope,
2172 un_node.operand,2191 un_node.operand,
2173 false,2192 need_type,
2174 );2193 );
2175 const operand_index = self.exprs.items.len;2194 const operand_index = self.exprs.items.len;
2176 try self.exprs.append(self.arena, operand.expr);2195 try self.exprs.append(self.arena, operand.expr);
2196
2177 return DocData.WalkResult{2197 return DocData.WalkResult{
2178 .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },2198 .typeRef = operand.typeRef,
2179 .expr = .{ .bitSizeOf = operand_index },2199 .expr = .{ .bitSizeOf = operand_index },
2180 };2200 };
2181 },2201 },
...@@ -2191,14 +2211,135 @@ fn walkInstruction(...@@ -2191,14 +2211,135 @@ fn walkInstruction(
2191 const operand_index = self.exprs.items.len;2211 const operand_index = self.exprs.items.len;
2192 try self.exprs.append(self.arena, operand.expr);2212 try self.exprs.append(self.arena, operand.expr);
21932213
2194 std.debug.print("un_node = {any}\n", .{un_node});
2195 std.debug.print("operand = {any}\n", .{operand});
2196 std.debug.print("operand_expr = {any}\n", .{operand.expr});
2197 return DocData.WalkResult{2214 return DocData.WalkResult{
2198 .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },2215 .typeRef = .{ .type = @enumToInt(Ref.comptime_int_type) },
2199 .expr = .{ .enumToInt = operand_index },2216 .expr = .{ .enumToInt = operand_index },
2200 };2217 };
2201 },2218 },
2219 .switch_block => {
2220 const pl_node = data[inst_index].pl_node;
2221 const extra = file.zir.extraData(Zir.Inst.SwitchBlock, pl_node.payload_index);
2222 const array_data = try self.arena.alloc(usize, extra.data.bits.scalar_cases_len);
2223 var extra_index = extra.end;
2224
2225 const sep = "=" ** 200;
2226 std.debug.print("{s}\n", .{sep});
2227 std.debug.print("pl_node = {any}\n", .{pl_node});
2228 std.debug.print("extra = {any}\n", .{extra});
2229
2230 const multi_cases_len = if (extra.data.bits.has_multi_cases) blk: {
2231 const multi_cases_len = file.zir.extra[extra_index];
2232 extra_index += 1;
2233 break :blk multi_cases_len;
2234 } else 0;
2235
2236 var else_index: ?usize = null;
2237 const special_prong = extra.data.bits.specialProng();
2238 if (special_prong != .none) {
2239 const body_len = file.zir.extra[extra_index];
2240 extra_index += 1;
2241 const body = file.zir.extra[extra_index..][0..body_len];
2242 extra_index += body.len;
2243 for (body) |body_member| {
2244 const item_ref = @intToEnum(Ref, file.zir.extra[extra_index]);
2245 const item = try self.walkRef(file, parent_scope, item_ref, false);
2246 std.debug.print("prong item_ref = {any}\n", .{item_ref});
2247 std.debug.print("prong item = {any}\n", .{item});
2248 std.debug.print("body member = {any}\n", .{body_member});
2249 const item_index = self.exprs.items.len;
2250 try self.exprs.append(self.arena, item.expr);
2251 else_index = item_index;
2252 }
2253 }
2254
2255 // var array_type: ?DocData.Expr = null;
2256 {
2257 const scalar_cases_len = extra.data.bits.scalar_cases_len;
2258 var scalar_i: usize = 0;
2259 while (scalar_i < scalar_cases_len) : (scalar_i += 1) {
2260 const item_ref = @intToEnum(Ref, file.zir.extra[extra_index]);
2261 const item = try self.walkRef(file, parent_scope, item_ref, false);
2262 extra_index += 1;
2263 const body_len = file.zir.extra[extra_index];
2264 extra_index += 1;
2265 const body = file.zir.extra[extra_index..][0..body_len];
2266 extra_index += body_len;
2267 _ = body;
2268 array_data[scalar_i] = item.expr.as.exprArg;
2269
2270 const body_ref = @intToEnum(Ref, file.zir.extra[extra_index]);
2271 const body_item = try self.walkRef(file, parent_scope, item_ref, false);
2272
2273 array_data[scalar_i] = item.expr.as.exprArg;
2274 std.debug.print("{s}\n", .{sep});
2275 std.debug.print("body item_ref = {any}\n", .{item_ref});
2276 std.debug.print("body item = {any}\n", .{item});
2277 std.debug.print("body_len scalar cases = {any}\n", .{body_ref});
2278 std.debug.print("body scalar cases = {any}\n", .{body_item});
2279 std.debug.print("{s}\n", .{sep});
2280 }
2281 }
2282 {
2283 var multi_i: usize = 0;
2284 while (multi_i < multi_cases_len) : (multi_i += 1) {
2285 const items_len = file.zir.extra[extra_index];
2286 extra_index += 1;
2287 const ranges_len = file.zir.extra[extra_index];
2288 extra_index += 1;
2289 const body_len = file.zir.extra[extra_index];
2290 extra_index += 1;
2291 const items = file.zir.refSlice(extra_index, items_len);
2292 extra_index += items_len;
2293 _ = items;
2294
2295 var range_i: usize = 0;
2296 while (range_i < ranges_len) : (range_i += 1) {
2297 extra_index += 1;
2298 extra_index += 1;
2299 }
2300
2301 const body = file.zir.extra[extra_index..][0..body_len];
2302 extra_index += body_len;
2303
2304 std.debug.print("body multi_i = {any}\n", .{body});
2305 std.debug.print("items = {any}\n", .{items});
2306 }
2307 }
2308
2309 // std.debug.print("multi_cases_len = {}\n", .{multi_cases_len});
2310 std.debug.print("{s}\n", .{sep});
2311
2312 const switch_index = self.exprs.items.len;
2313 try self.exprs.append(self.arena, .{ .switchOp = .{ .cases = array_data, .else_index = else_index } });
2314
2315 return DocData.WalkResult{
2316 .typeRef = .{ .type = @enumToInt(Ref.type_type) },
2317 .expr = .{ .switchIndex = switch_index },
2318 };
2319 },
2320 .switch_cond => {
2321 const un_node = data[inst_index].un_node;
2322 const operand = try self.walkRef(
2323 file,
2324 parent_scope,
2325 un_node.operand,
2326 need_type,
2327 );
2328 const operand_index = self.exprs.items.len;
2329 try self.exprs.append(self.arena, operand.expr);
2330
2331 // const sep = "=" ** 200;
2332 // std.debug.print("{s}\n", .{sep});
2333 // std.debug.print("SWITCH COND\n", .{});
2334 // std.debug.print("un_node {any} \n", .{un_node});
2335 // std.debug.print("operand {any} \n", .{operand});
2336 // std.debug.print("{s}\n", .{sep});
2337
2338 return DocData.WalkResult{
2339 .typeRef = operand.typeRef,
2340 .expr = .{ .typeOf = operand_index },
2341 };
2342 },
22022343
2203 .typeof => {2344 .typeof => {
2204 const un_node = data[inst_index].un_node;2345 const un_node = data[inst_index].un_node;