Skip to content

Commit ffb1298

Browse files
committed
Fix basic operators
1 parent 0bcf35d commit ffb1298

5 files changed

Lines changed: 61 additions & 16 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
#### Used for:
66

7-
* expressions evaluators, calculators
7+
* expressions evaluators, calculators
88
* subsets of languages (eg. [justin](#justin)<!-- [jz](https://github.com/dy/jz) -->)
99
* sandboxes, playgrounds, safe eval (eg. [glsl-transpiler](https://github.com/stackgl/glsl-transpiler))
1010
* custom DSL (eg. [piezo](https://github.com/dy/piezo)) <!-- uneural -->
@@ -38,7 +38,7 @@ _Subscript_ supports [common syntax](https://en.wikipedia.org/wiki/Comparison_of
3838
* `a < b`, `a <= b`, `a > b`, `a >= b`, `a == b`, `a != b`
3939
* `~a`, `a & b`, `a ^ b`, `a | b`, `a << b`, `a >> b`
4040
* `!a`, `a && b`, `a || b`
41-
* `a = b`, `a += b`, `a -= b`, `a *= b`, `a /= b`, `a %= b`, , `a <<= b`, `a >>= b`
41+
* `a = b`, `a += b`, `a -= b`, `a *= b`, `a /= b`, `a %= b`, `a <<= b`, `a >>= b`
4242
* `(a, (b))`, `a; b;`
4343
* `"abc"`, `'abc'`
4444
* `0.1`, `1.2e+3`
@@ -115,10 +115,10 @@ null|empty; // placeholder
115115

116116
// eg.
117117
['()', 'a'] // (a)
118-
['()', 'a',,] // a()
118+
['()', 'a', null] // a()
119119
['()', 'a', 'b'] // a(b)
120-
['+', 'a'] // +a
121-
['+','a',,] // a+
120+
['++', 'a'] // ++a
121+
['++','a', null] // a++
122122
```
123123

124124
### Stringify

feature/increment.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ import { token, expr } from "../src/parse.js"
22
import { operator, compile, prop } from "../src/compile.js"
33
import { PREC_POSTFIX } from "../src/const.js"
44

5-
let inc, dec
6-
token('++', PREC_POSTFIX, a => a ? ['++-', a] : ['++', expr(PREC_POSTFIX - 1)])
5+
token('++', PREC_POSTFIX, a => a ? ['++', a, null,] : ['++', expr(PREC_POSTFIX - 1)])
76
// ++a, ++((a)), ++a.b, ++a[b]
8-
operator('++', inc = (a) => prop(a, (obj, path) => ++obj[path]))
9-
operator('++-', inc = (a) => prop(a, (obj, path) => obj[path]++))
7+
operator('++', (a,b) => prop(a, b === null ? (obj, path) => obj[path]++ : (obj, path) => ++obj[path]))
108

11-
token('--', PREC_POSTFIX, a => a ? ['--+', a] : ['--', expr(PREC_POSTFIX - 1)])
9+
token('--', PREC_POSTFIX, a => a ? ['--', a, null,] : ['--', expr(PREC_POSTFIX - 1)])
1210
// --a, --a.b, --a[b]
13-
operator('--', dec = (a) => (prop(a, (obj, path) => --obj[path])))
14-
operator('--+', dec = (a) => (prop(a, (obj, path) => obj[path]--)))
11+
operator('--', (a, b) => prop(a, b === null ? (obj, path) => obj[path]-- : (obj, path) => --obj[path]))

justin.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

subscript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import './feature/mult.js'
1111
import './feature/add.js'
1212
import './feature/increment.js'
1313
import './feature/bitwise.js'
14+
import './feature/logic.js'
1415
import './feature/compare.js'
1516
import './feature/shift.js'
16-
import './feature/logic.js'
1717
import compile from './src/compile.js'
1818
import parse from './src/parse.js'
1919

test/parse.js

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { PREC_MULT } from '../src/const.js'
99

1010
test('parse: basic', t => {
1111
parse("a==2")
12+
parse("a!=2")
1213
is(parse('a >> b'), ['>>', 'a', 'b'])
1314
is(parse('a || b'), ['||', 'a', 'b'])
1415
is(parse('a && b || c'), ['||', ['&&', 'a', 'b'], 'c'])
@@ -73,6 +74,53 @@ test('parse: basic', t => {
7374
)
7475
})
7576

77+
test('parse: default operators', t => {
78+
is(parse(`a.b`), ['.','a', 'b'])
79+
is(parse(`a[b]`), ['[]','a', 'b'])
80+
is(parse(`a(b)`), ['()','a', 'b'])
81+
is(parse(`a++`), ['++','a',null,])
82+
is(parse(`a--`), ['--','a',null,])
83+
is(parse(`++a`), ['++','a'])
84+
is(parse(`--a`), ['--','a'])
85+
is(parse(`a * b`), ['*','a', 'b'])
86+
is(parse(`a / b`), ['/','a', 'b'])
87+
is(parse(`a % b`), ['%','a', 'b'])
88+
is(parse(`+a`), ['+','a'])
89+
is(parse(`-a`), ['-','a'])
90+
is(parse(`a + b`), ['+','a', 'b'])
91+
is(parse(`a - b`), ['-','a', 'b'])
92+
is(parse(`a < b`), ['<','a', 'b'])
93+
is(parse(`a <= b`), ['<=','a', 'b'])
94+
is(parse(`a > b`), ['>','a', 'b'])
95+
is(parse(`a >= b`), ['>=','a', 'b'])
96+
is(parse(`a == b`), ['==','a', 'b'])
97+
is(parse(`a != b`), ['!=','a', 'b'])
98+
is(parse(`~a`), ['~','a'])
99+
is(parse(`a & b`), ['&','a', 'b'])
100+
is(parse(`a ^ b`), ['^','a', 'b'])
101+
is(parse(`a | b`), ['|','a', 'b'])
102+
is(parse(`a << b`), ['<<','a', 'b'])
103+
is(parse(`a >> b`), ['>>','a', 'b'])
104+
is(parse(`!a`), ['!','a'])
105+
is(parse(`a && b`), ['&&','a', 'b'])
106+
is(parse(`a || b`), ['||','a', 'b'])
107+
is(parse(`a = b`), ['=','a', 'b'])
108+
is(parse(`a += b`), ['+=','a', 'b'])
109+
is(parse(`a -= b`), ['-=','a', 'b'])
110+
is(parse(`a *= b`), ['*=','a', 'b'])
111+
is(parse(`a /= b`), ['/=','a', 'b'])
112+
is(parse(`a %= b`), ['%=','a', 'b'])
113+
is(parse(`a <<= b`), ['<<=','a', 'b'])
114+
is(parse(`a >>= b`), ['>>=','a', 'b'])
115+
is(parse(`(a(b))`), ['()',['()', 'a', 'b']])
116+
is(parse(`a; b;`), [';','a', 'b', null])
117+
is(parse(`"abc"`), [,'abc'])
118+
is(parse(`'abc'`), [,'abc'])
119+
is(parse(`0.1`), [,0.1])
120+
is(parse(`1.2e+3`), [,1.2e+3])
121+
122+
})
123+
76124
test('parse: strings', t => {
77125
throws(() => parse('"a'), 'bad string')
78126
is(parse('a + b'), ['+', 'a', 'b'])
@@ -132,8 +180,8 @@ test('parse: unaries', t => {
132180
is(parse('a-+!b'), ['-', 'a', ['+', ['!', 'b']]])
133181
is(parse('a * -a'), ['*', 'a', ['-', 'a']])
134182

135-
is(parse('a--'), ['--+', 'a'])
136-
is(parse('a++'), ['++-', 'a'])
183+
is(parse('a--'), ['--', 'a', null])
184+
is(parse('a++'), ['++', 'a', null])
137185
})
138186

139187
test('parse: prop access', t => {

0 commit comments

Comments
 (0)