From 4f52e5530084f5589abe02871ff81627a5ea1a6a Mon Sep 17 00:00:00 2001 From: Hai Do Minh Date: Sat, 20 Jun 2015 14:11:38 +0900 Subject: [PATCH] fix func postfixer --- package.json | 2 +- postfixer.js | 17 ++++++++++++++++- test/2-postfixer.js | 1 + test/4-func.js | 4 +++- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index f6aacfc..bcbc5df 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ }, "scripts": { "start": "node cli.js", - "stop": "tape test/*.js | faucet", + "stop": "tape test/2-postfixer.js | faucet", "test": "standard && tape test/*.js | faucet" }, "repository": { diff --git a/postfixer.js b/postfixer.js index b01c2b4..19cae95 100644 --- a/postfixer.js +++ b/postfixer.js @@ -63,7 +63,8 @@ exports = module.exports = function toPostfixNotation (exp) { removeFunction() continue } else if (cur === ',') { - + removeComma() + continue } } @@ -150,6 +151,20 @@ exports = module.exports = function toPostfixNotation (exp) { skip = null } + function removeComma () { + out.push(token) + while (stack.length > 0) { + var o = stack.pop() + if (o.substr(o.length - MATH_EP.length) === MATH_EP) { + stack.push(o) + break + } else { + out.push(o) + } + } + token = '' + } + function enableNega () { setNega(true) } diff --git a/test/2-postfixer.js b/test/2-postfixer.js index 03ddb78..47e7b4b 100644 --- a/test/2-postfixer.js +++ b/test/2-postfixer.js @@ -32,6 +32,7 @@ test('infix to postfix test', function (t) { test('func: infix to postfix test', function (t) { t.equal(postfixer('abs(1 - 2)'), '. 1 2 - abs.') t.equal(postfixer('min(10, 20, 30)'), '. 10 20 30 min.') + t.equal(postfixer('max(10, 20 + 5, 30 - 40)'), '. 10 20 5 + 30 40 - max.') // t.equal(postfixer('1 + 2 * 3'), '1 2 3 * +') // t.equal(postfixer('(1 + 2) * 3'), '1 2 + 3 *') // t.equal(postfixer('(abs[1] + 2) * 3 + E'), 'abs[1] 2 + 3 * E +') diff --git a/test/4-func.js b/test/4-func.js index d09e71d..3ad24a7 100644 --- a/test/4-func.js +++ b/test/4-func.js @@ -4,10 +4,12 @@ var app = require('../app') test('func - postfix test', function (t) { t.equal(app('1 3 +'), 4) - t.equal(app('. 3 0 -2 19 min.'), -2) + t.equal(app('. 3 0 -2 10 - 19 min.'), -12) t.equal(app('. 3 0 -2 19 max.'), 19) + t.equal(app('. 3 20 + 0 -2 19 max.'), 23) + t.equal(app('. -1 abs. 10 +'), 11) t.equal(app('. 2 . 2 3 pow. - max.'), -6)