Skip to content

Commit a42a898

Browse files
committed
add postfix operators
1 parent 376c410 commit a42a898

File tree

7 files changed

+509
-197
lines changed

7 files changed

+509
-197
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Documentation available at [kaoscript](https://github.com/kaoscript/kaoscript).
1212
Changelog
1313
---------
1414

15+
### 0.1.1
16+
17+
- postfix operators
18+
1519
### 0.1.0
1620

1721
- initial release

build/parser.js

Lines changed: 199 additions & 176 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/parser.jison

Lines changed: 82 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,7 +3355,28 @@ ObjectItem // {{{
33553355
// }}}
33563356

33573357
Operand // {{{
3358-
: UnaryExpression
3358+
: PrefixUnaryOperator Operand
3359+
{
3360+
if($1.kind === UnaryOperator.Negative && $2.kind === Kind.NumericExpression) {
3361+
$2.value = -$2.value;
3362+
$$ = location($2, @1, @2);
3363+
}
3364+
else {
3365+
$$ = location({
3366+
kind: Kind.UnaryExpression,
3367+
operator: $1,
3368+
argument: $2
3369+
}, @1, @2);
3370+
}
3371+
}
3372+
| Operand PostfixUnaryOperator
3373+
{
3374+
$$ = location({
3375+
kind: Kind.UnaryExpression,
3376+
operator: $2,
3377+
argument: $1
3378+
}, @1, @2);
3379+
}
33593380
| OperandSX
33603381
;
33613382
// }}}
@@ -3508,7 +3529,28 @@ OperandOrType // {{{
35083529
// }}}
35093530

35103531
OperandNAF // {{{
3511-
: UnaryExpression
3532+
: PrefixUnaryOperator OperandNAF
3533+
{
3534+
if($1.kind === UnaryOperator.Negative && $2.kind === Kind.NumericExpression) {
3535+
$2.value = -$2.value;
3536+
$$ = location($2, @1, @2);
3537+
}
3538+
else {
3539+
$$ = location({
3540+
kind: Kind.UnaryExpression,
3541+
operator: $1,
3542+
argument: $2
3543+
}, @1, @2);
3544+
}
3545+
}
3546+
| OperandNAF PostfixUnaryOperator
3547+
{
3548+
$$ = location({
3549+
kind: Kind.UnaryExpression,
3550+
operator: $2,
3551+
argument: $1
3552+
}, @1, @2);
3553+
}
35123554
| OperandSXNAF
35133555
;
35143556
// }}}
@@ -3661,7 +3703,28 @@ OperandOrTypeNAF // {{{
36613703
// }}}
36623704

36633705
OperandNO // {{{
3664-
: UnaryExpression
3706+
: PrefixUnaryOperator OperandNO
3707+
{
3708+
if($1.kind === UnaryOperator.Negative && $2.kind === Kind.NumericExpression) {
3709+
$2.value = -$2.value;
3710+
$$ = location($2, @1, @2);
3711+
}
3712+
else {
3713+
$$ = location({
3714+
kind: Kind.UnaryExpression,
3715+
operator: $1,
3716+
argument: $2
3717+
}, @1, @2);
3718+
}
3719+
}
3720+
| OperandNO PostfixUnaryOperator
3721+
{
3722+
$$ = location({
3723+
kind: Kind.UnaryExpression,
3724+
operator: $2,
3725+
argument: $1
3726+
}, @1, @2);
3727+
}
36653728
| OperandSXNO
36663729
;
36673730
// }}}
@@ -3896,6 +3959,22 @@ ParenthesisNAF // {{{
38963959
;
38973960
// }}}
38983961

3962+
PostfixUnaryOperator // {{{
3963+
: '--'
3964+
{
3965+
$$ = location({
3966+
kind: UnaryOperator.DecrementPostfix
3967+
}, @1);
3968+
}
3969+
| '++'
3970+
{
3971+
$$ = location({
3972+
kind: UnaryOperator.IncrementPostfix
3973+
}, @1);
3974+
}
3975+
;
3976+
// }}}
3977+
38993978
PrefixUnaryOperator // {{{
39003979
: '--'
39013980
{
@@ -5022,24 +5101,6 @@ TypeVarList // {{{
50225101
;
50235102
// }}}
50245103

5025-
UnaryExpression // {{{
5026-
: PrefixUnaryOperator Operand
5027-
{
5028-
if($1.kind === UnaryOperator.Negative && $2.kind === Kind.NumericExpression) {
5029-
$2.value = -$2.value;
5030-
$$ = location($2, @1, @2);
5031-
}
5032-
else {
5033-
$$ = location({
5034-
kind: Kind.UnaryExpression,
5035-
operator: $1,
5036-
argument: $2
5037-
}, @1, @2);
5038-
}
5039-
}
5040-
;
5041-
// }}}
5042-
50435104
UnlessStatement // {{{
50445105
: 'UNLESS' Expression Block
50455106
{
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"kind": 54,
3+
"attributes": [],
4+
"body": [
5+
{
6+
"kind": 87,
7+
"modifiers": {
8+
"kind": 2,
9+
"start": {
10+
"line": 1,
11+
"column": 1
12+
},
13+
"end": {
14+
"line": 1,
15+
"column": 4
16+
}
17+
},
18+
"declarations": [
19+
{
20+
"kind": 88,
21+
"name": {
22+
"kind": 38,
23+
"name": "i",
24+
"start": {
25+
"line": 1,
26+
"column": 5
27+
},
28+
"end": {
29+
"line": 1,
30+
"column": 6
31+
}
32+
},
33+
"init": {
34+
"kind": 55,
35+
"value": 3.14,
36+
"start": {
37+
"line": 1,
38+
"column": 9
39+
},
40+
"end": {
41+
"line": 1,
42+
"column": 13
43+
}
44+
},
45+
"start": {
46+
"line": 1,
47+
"column": 5
48+
},
49+
"end": {
50+
"line": 1,
51+
"column": 13
52+
}
53+
}
54+
],
55+
"start": {
56+
"line": 1,
57+
"column": 1
58+
},
59+
"end": {
60+
"line": 1,
61+
"column": 13
62+
},
63+
"attributes": []
64+
},
65+
{
66+
"kind": 82,
67+
"operator": {
68+
"kind": 5,
69+
"start": {
70+
"line": 3,
71+
"column": 2
72+
},
73+
"end": {
74+
"line": 3,
75+
"column": 4
76+
}
77+
},
78+
"argument": {
79+
"kind": 38,
80+
"name": "i",
81+
"start": {
82+
"line": 3,
83+
"column": 1
84+
},
85+
"end": {
86+
"line": 3,
87+
"column": 2
88+
}
89+
},
90+
"start": {
91+
"line": 3,
92+
"column": 1
93+
},
94+
"end": {
95+
"line": 3,
96+
"column": 4
97+
},
98+
"attributes": []
99+
}
100+
],
101+
"start": {
102+
"line": 1,
103+
"column": 1
104+
},
105+
"end": {
106+
"line": 3,
107+
"column": 4
108+
}
109+
}

test/fixtures/operator.postfix.inc.ks

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let i = 3.14
2+
3+
i++
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"kind": 54,
3+
"attributes": [],
4+
"body": [
5+
{
6+
"kind": 87,
7+
"modifiers": {
8+
"kind": 2,
9+
"start": {
10+
"line": 1,
11+
"column": 1
12+
},
13+
"end": {
14+
"line": 1,
15+
"column": 4
16+
}
17+
},
18+
"declarations": [
19+
{
20+
"kind": 88,
21+
"name": {
22+
"kind": 38,
23+
"name": "i",
24+
"start": {
25+
"line": 1,
26+
"column": 5
27+
},
28+
"end": {
29+
"line": 1,
30+
"column": 6
31+
}
32+
},
33+
"init": {
34+
"kind": 55,
35+
"value": 3.14,
36+
"start": {
37+
"line": 1,
38+
"column": 9
39+
},
40+
"end": {
41+
"line": 1,
42+
"column": 13
43+
}
44+
},
45+
"start": {
46+
"line": 1,
47+
"column": 5
48+
},
49+
"end": {
50+
"line": 1,
51+
"column": 13
52+
}
53+
}
54+
],
55+
"start": {
56+
"line": 1,
57+
"column": 1
58+
},
59+
"end": {
60+
"line": 1,
61+
"column": 13
62+
},
63+
"attributes": []
64+
},
65+
{
66+
"kind": 82,
67+
"operator": {
68+
"kind": 6,
69+
"start": {
70+
"line": 3,
71+
"column": 1
72+
},
73+
"end": {
74+
"line": 3,
75+
"column": 3
76+
}
77+
},
78+
"argument": {
79+
"kind": 38,
80+
"name": "i",
81+
"start": {
82+
"line": 3,
83+
"column": 3
84+
},
85+
"end": {
86+
"line": 3,
87+
"column": 4
88+
}
89+
},
90+
"start": {
91+
"line": 3,
92+
"column": 1
93+
},
94+
"end": {
95+
"line": 3,
96+
"column": 4
97+
},
98+
"attributes": []
99+
}
100+
],
101+
"start": {
102+
"line": 1,
103+
"column": 1
104+
},
105+
"end": {
106+
"line": 3,
107+
"column": 4
108+
}
109+
}

test/fixtures/operator.prefix.inc.ks

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let i = 3.14
2+
3+
++i

0 commit comments

Comments
 (0)