Skip to content

Commit afffcb4

Browse files
committed
fix: fix Parenthesis closure
Prevent adding automatic parenthesis when expression already contains parenthesis
1 parent c2fba76 commit afffcb4

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

Diff for: dist/browser/math-expression-evaluator.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/lexer.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,12 @@ Mexp.lex = function (inp, tokens) {
442442
if (cType === 0) {
443443
allowed = type0
444444
asterick = empty
445-
inc(ptc, 2).push(2)
445+
inc(ptc, 2)
446446
str.push(obj)
447-
str.push(openingParObj)
447+
if (nodes[i + 1].type !== 4) {
448+
str.push(openingParObj)
449+
ptc.push(2)
450+
}
448451
// bracToClose++
449452
} else if (cType === 1) {
450453
if (pre.type === 1) {
@@ -507,9 +510,13 @@ Mexp.lex = function (inp, tokens) {
507510
if (cType === 8) {
508511
allowed = type0
509512
asterick = empty
510-
inc(ptc, 4).push(4)
513+
inc(ptc, 4)
511514
str.push(obj)
512-
str.push(openingParObj)
515+
// str.push(openingParObj)
516+
if (nodes[i + 1].type !== 4) {
517+
str.push(openingParObj)
518+
ptc.push(4)
519+
}
513520
} else if (cType === 9) {
514521
if (pre.type === 9) {
515522
if (pre.value === Mexp.math.add) {
@@ -556,9 +563,12 @@ Mexp.lex = function (inp, tokens) {
556563
} else if (cType === 12) {
557564
allowed = type0
558565
asterick = empty
559-
inc(ptc, 6).push(6)
566+
inc(ptc, 6)
560567
str.push(obj)
561-
str.push(openingParObj)
568+
if (nodes[i + 1].type !== 4) {
569+
str.push(openingParObj)
570+
ptc.push(6)
571+
}
562572
} else if (cType === 13) {
563573
allowed = type1
564574
asterick = type3Asterick

Diff for: test/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ describe('Ading Token', function () {
320320
// console.log("PAGAL", a.eval("min(4,ceil(0.1*10))"))
321321
assert.equal(a.eval("min(4,ceil(0.011*100))"), 2)
322322
})
323+
it('should also evaluate to correct two functions', function () {
324+
// console.log("PAGAL", a.eval("min(4,ceil(0.1*10))"))
325+
assert.equal(a.eval("ceil(min(4, 0.0801*100))"), 4)
326+
})
323327

324328
it('should tell to compllete expression', function () {
325329
a.addToken([

0 commit comments

Comments
 (0)