authorgravatar for codroid@gmail.comhryx <codroid@gmail.com> 2019-06-27 21:02:48-07:00
committergravatar for codroid@gmail.comhryx <codroid@gmail.com> 2019-06-27 21:02:48-07:00
logb4bd52cc515e698a0bcf64653d829efeb2e0b4cb
tree18ac905ac01e3d88a4e417b7d4ea3cc4e4d885bd
parent84353515817929d694efd22e528e00e8de3d41a1
signaturelock-open Commit is signed but in an unrecognized format.

Create and render big.Int from IntegerLiteral; group BinaryOperator


1 files changed, 31 insertions(+), 9 deletions(-)

src-self-hosted/translate_c.zig+31-9
...@@ -372,22 +372,22 @@ fn transBinaryOperator(...@@ -372,22 +372,22 @@ fn transBinaryOperator(
372 },372 },
373 .Add => {373 .Add => {
374 const node = if (cIsUnsignedInteger(qt))374 const node = if (cIsUnsignedInteger(qt))
375 try transCreateNodeInfixOp(rp, scope, stmt, .AddWrap, .PlusPercent, "+%")375 try transCreateNodeInfixOp(rp, scope, stmt, .AddWrap, .PlusPercent, "+%", true)
376 else376 else
377 try transCreateNodeInfixOp(rp, scope, stmt, .Add, .Plus, "+");377 try transCreateNodeInfixOp(rp, scope, stmt, .Add, .Plus, "+", true);
378 return maybeSuppressResult(rp, scope, result_used, TransResult{378 return maybeSuppressResult(rp, scope, result_used, TransResult{
379 .node = &node.base,379 .node = node,
380 .child_scope = scope,380 .child_scope = scope,
381 .node_scope = scope,381 .node_scope = scope,
382 });382 });
383 },383 },
384 .Sub => {384 .Sub => {
385 const node = if (cIsUnsignedInteger(qt))385 const node = if (cIsUnsignedInteger(qt))
386 try transCreateNodeInfixOp(rp, scope, stmt, .SubWrap, .MinusPercent, "-%")386 try transCreateNodeInfixOp(rp, scope, stmt, .SubWrap, .MinusPercent, "-%", true)
387 else387 else
388 try transCreateNodeInfixOp(rp, scope, stmt, .Sub, .Minus, "-");388 try transCreateNodeInfixOp(rp, scope, stmt, .Sub, .Minus, "-", true);
389 return maybeSuppressResult(rp, scope, result_used, TransResult{389 return maybeSuppressResult(rp, scope, result_used, TransResult{
390 .node = &node.base,390 .node = node,
391 .child_scope = scope,391 .child_scope = scope,
392 .node_scope = scope,392 .node_scope = scope,
393 });393 });
...@@ -1056,7 +1056,9 @@ fn transCreateNodeInfixOp(...@@ -1056,7 +1056,9 @@ fn transCreateNodeInfixOp(
1056 op: ast.Node.InfixOp.Op,1056 op: ast.Node.InfixOp.Op,
1057 op_tok_id: std.zig.Token.Id,1057 op_tok_id: std.zig.Token.Id,
1058 bytes: []const u8,1058 bytes: []const u8,
1059) !*ast.Node.InfixOp {1059 grouped: bool,
1060) !*ast.Node {
1061 const lparen = if (grouped) try appendToken(rp.c, .LParen, "(") else undefined;
1060 const lhs = try transExpr(rp, scope, ZigClangBinaryOperator_getLHS(stmt), .used, .l_value);1062 const lhs = try transExpr(rp, scope, ZigClangBinaryOperator_getLHS(stmt), .used, .l_value);
1061 const op_token = try appendToken(rp.c, op_tok_id, bytes);1063 const op_token = try appendToken(rp.c, op_tok_id, bytes);
1062 const rhs = try transExpr(rp, scope, ZigClangBinaryOperator_getRHS(stmt), .used, .r_value);1064 const rhs = try transExpr(rp, scope, ZigClangBinaryOperator_getRHS(stmt), .used, .r_value);
...@@ -1068,7 +1070,16 @@ fn transCreateNodeInfixOp(...@@ -1068,7 +1070,16 @@ fn transCreateNodeInfixOp(
1068 .op = op,1070 .op = op,
1069 .rhs = rhs.node,1071 .rhs = rhs.node,
1070 };1072 };
1071 return node;1073 if (!grouped) return &node.base;
1074 const rparen = try appendToken(rp.c, .RParen, ")");
1075 const grouped_expr = try rp.c.a().create(ast.Node.GroupedExpression);
1076 grouped_expr.* = ast.Node.GroupedExpression{
1077 .base = ast.Node{ .id = .GroupedExpression },
1078 .lparen = lparen,
1079 .expr = &node.base,
1080 .rparen = rparen,
1081 };
1082 return &grouped_expr.base;
1072}1083}
10731084
1074fn transCreateNodePtrType(1085fn transCreateNodePtrType(
...@@ -1096,10 +1107,21 @@ fn transCreateNodePtrType(...@@ -1096,10 +1107,21 @@ fn transCreateNodePtrType(
1096}1107}
10971108
1098fn transCreateNodeAPInt(c: *Context, int: ?*const ZigClangAPSInt) !*ast.Node {1109fn transCreateNodeAPInt(c: *Context, int: ?*const ZigClangAPSInt) !*ast.Node {
1110 const num_limbs = ZigClangAPSInt_getNumWords(int.?);
1111 var big = try std.math.big.Int.initCapacity(c.a(), num_limbs);
1112 defer big.deinit();
1113 const data = ZigClangAPSInt_getRawData(int.?);
1114 var i: usize = 0;
1115 while (i < num_limbs) : (i += 1) big.limbs[i] = data[i];
1116 const str = big.toString(c.a(), 10) catch |err| switch (err) {
1117 error.OutOfMemory => return error.OutOfMemory,
1118 else => unreachable,
1119 };
1120 const token = try appendToken(c, .IntegerLiteral, str);
1099 const node = try c.a().create(ast.Node.IntegerLiteral);1121 const node = try c.a().create(ast.Node.IntegerLiteral);
1100 node.* = ast.Node.IntegerLiteral{1122 node.* = ast.Node.IntegerLiteral{
1101 .base = ast.Node{ .id = .IntegerLiteral },1123 .base = ast.Node{ .id = .IntegerLiteral },
1102 .token = try appendToken(c, .IntegerLiteral, "3333333"), // TODO1124 .token = token,
1103 };1125 };
1104 return &node.base;1126 return &node.base;
1105}1127}