gofront 1.3.2 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gofront",
3
- "version": "1.3.2",
3
+ "version": "1.3.3",
4
4
  "author": "Luuk van Venrooij",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -58,6 +58,43 @@ const NS_CONSTANTS = {
58
58
  utf8: { RuneError: "0xFFFD", MaxRune: "0x10FFFF", UTFMax: "4" },
59
59
  };
60
60
 
61
+ const BINARY_OP_PREC = {
62
+ "||": 1,
63
+ "&&": 2,
64
+ "==": 3,
65
+ "!=": 3,
66
+ "<": 3,
67
+ ">": 3,
68
+ "<=": 3,
69
+ ">=": 3,
70
+ "+": 4,
71
+ "-": 4,
72
+ "|": 4,
73
+ "^": 4,
74
+ "*": 5,
75
+ "/": 5,
76
+ "%": 5,
77
+ "<<": 5,
78
+ ">>": 5,
79
+ "&": 5,
80
+ "&^": 5,
81
+ };
82
+
83
+ function formatBinaryOperand(genExpr, child, parentOp, isRight) {
84
+ const code = genExpr(child);
85
+ if (child.kind !== "BinaryExpr") return code;
86
+ const parentPrec = BINARY_OP_PREC[parentOp] || 0;
87
+ const childPrec = BINARY_OP_PREC[child.op] || 0;
88
+ return (isRight ? childPrec <= parentPrec : childPrec < parentPrec)
89
+ ? `(${code})`
90
+ : code;
91
+ }
92
+
93
+ function formatUnaryOperand(genExpr, child) {
94
+ const code = genExpr(child);
95
+ return child.kind === "BinaryExpr" ? `(${code})` : code;
96
+ }
97
+
61
98
  const INT_TYPE_NAMES = new Set([
62
99
  "int",
63
100
  "uint",
@@ -250,7 +287,7 @@ export const expressionGenMethods = {
250
287
  if (op === "-") return `{ re: -${inner}.re, im: -${inner}.im }`;
251
288
  return inner;
252
289
  }
253
- return `${op}${this.genExpr(expr.operand)}`;
290
+ return `${op}${formatUnaryOperand((e) => this.genExpr(e), expr.operand)}`;
254
291
  },
255
292
 
256
293
  _genSelectorExpr(expr) {
@@ -910,8 +947,20 @@ export const expressionGenMethods = {
910
947
  isComplex(expr.right._type)
911
948
  )
912
949
  return this._genComplexBinary(expr);
913
- const l = this.genExpr(expr.left);
914
- const r = this.genExpr(expr.right);
950
+
951
+ const l = formatBinaryOperand(
952
+ (e) => this.genExpr(e),
953
+ expr.left,
954
+ expr.op,
955
+ false,
956
+ );
957
+ const r = formatBinaryOperand(
958
+ (e) => this.genExpr(e),
959
+ expr.right,
960
+ expr.op,
961
+ true,
962
+ );
963
+
915
964
  if (
916
965
  expr.op === "/" &&
917
966
  this.isIntType(expr.left._type) &&