Skip to content
This repository was archived by the owner on Mar 29, 2023. It is now read-only.

Commit abe8aa7

Browse files
authored
Merge pull request glayzzle#215 from glayzzle/issue-181
fix: use unary instead negative number
2 parents d93b707 + e9e62a6 commit abe8aa7

File tree

8 files changed

+756
-33
lines changed

8 files changed

+756
-33
lines changed

src/parser/expr.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,26 +106,13 @@ module.exports = {
106106
if (this.token === "@") return this.node("silent")(this.next().read_expr());
107107
if (this.token === "+")
108108
return this.node("unary")("+", this.next().read_expr());
109+
if (this.token === "-")
110+
return this.node("unary")("-", this.next().read_expr());
109111
if (this.token === "!")
110112
return this.node("unary")("!", this.next().read_expr());
111113
if (this.token === "~")
112114
return this.node("unary")("~", this.next().read_expr());
113115

114-
if (this.token === "-") {
115-
result = this.node();
116-
this.next();
117-
if (
118-
this.token === this.tok.T_LNUMBER ||
119-
this.token === this.tok.T_DNUMBER
120-
) {
121-
// negative number
122-
result = result("number", "-" + this.text(), null);
123-
this.next();
124-
return result;
125-
}
126-
return result("unary", "-", this.read_expr());
127-
}
128-
129116
if (this.token === "(") {
130117
expr = this.next().read_expr();
131118
expr.parenthesizedExpression = true;

test/snapshot/__snapshots__/acid.test.js.snap

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,22 +2735,39 @@ Program {
27352735
},
27362736
},
27372737
"operator": "=",
2738-
"right": Number {
2739-
"kind": "number",
2738+
"right": Unary {
2739+
"kind": "unary",
27402740
"loc": Location {
27412741
"end": Position {
2742-
"column": 14,
2742+
"column": 15,
27432743
"line": 82,
2744-
"offset": 1657,
2744+
"offset": 1658,
27452745
},
2746-
"source": "-",
2746+
"source": "-1",
27472747
"start": Position {
27482748
"column": 13,
27492749
"line": 82,
27502750
"offset": 1656,
27512751
},
27522752
},
2753-
"value": "-1",
2753+
"type": "-",
2754+
"what": Number {
2755+
"kind": "number",
2756+
"loc": Location {
2757+
"end": Position {
2758+
"column": 15,
2759+
"line": 82,
2760+
"offset": 1658,
2761+
},
2762+
"source": "1",
2763+
"start": Position {
2764+
"column": 14,
2765+
"line": 82,
2766+
"offset": 1657,
2767+
},
2768+
},
2769+
"value": "1",
2770+
},
27542771
},
27552772
},
27562773
"kind": "expressionstatement",

test/snapshot/__snapshots__/ast.test.js.snap

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,13 @@ Program {
232232
ExpressionStatement {
233233
"expression": Exit {
234234
"kind": "exit",
235-
"status": Number {
236-
"kind": "number",
237-
"value": "-1",
235+
"status": Unary {
236+
"kind": "unary",
237+
"type": "-",
238+
"what": Number {
239+
"kind": "number",
240+
"value": "1",
241+
},
238242
},
239243
"useDie": false,
240244
},

test/snapshot/__snapshots__/location.test.js.snap

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6171,8 +6171,8 @@ exports[`Test locations negative number 1`] = `
61716171
Program {
61726172
"children": Array [
61736173
ExpressionStatement {
6174-
"expression": Number {
6175-
"kind": "number",
6174+
"expression": Unary {
6175+
"kind": "unary",
61766176
"loc": Location {
61776177
"end": Position {
61786178
"column": 6,
@@ -6186,7 +6186,24 @@ Program {
61866186
"offset": 0,
61876187
},
61886188
},
6189-
"value": "-2112",
6189+
"type": "-",
6190+
"what": Number {
6191+
"kind": "number",
6192+
"loc": Location {
6193+
"end": Position {
6194+
"column": 5,
6195+
"line": 1,
6196+
"offset": 5,
6197+
},
6198+
"source": "2112",
6199+
"start": Position {
6200+
"column": 1,
6201+
"line": 1,
6202+
"offset": 1,
6203+
},
6204+
},
6205+
"value": "2112",
6206+
},
61906207
},
61916208
"kind": "expressionstatement",
61926209
"loc": Location {

test/snapshot/__snapshots__/number.test.js.snap

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ Program {
1313
"name": "a",
1414
},
1515
"operator": "=",
16-
"right": Number {
17-
"kind": "number",
18-
"value": "-1.5",
16+
"right": Unary {
17+
"kind": "unary",
18+
"type": "-",
19+
"what": Number {
20+
"kind": "number",
21+
"value": "1.5",
22+
},
1923
},
2024
},
2125
"kind": "expressionstatement",

test/snapshot/__snapshots__/scalar.test.js.snap

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ Program {
1515
"operator": "=",
1616
"right": OffsetLookup {
1717
"kind": "offsetlookup",
18-
"offset": Number {
19-
"kind": "number",
20-
"value": "-5",
18+
"offset": Unary {
19+
"kind": "unary",
20+
"type": "-",
21+
"what": Number {
22+
"kind": "number",
23+
"value": "5",
24+
},
2125
},
2226
"what": StaticLookup {
2327
"kind": "staticlookup",

0 commit comments

Comments
 (0)