Skip to content

Commit

Permalink
fix func postfixer
Browse files Browse the repository at this point in the history
  • Loading branch information
dominhhai committed Jun 20, 2015
1 parent c250a1e commit 4f52e55
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
17 changes: 16 additions & 1 deletion postfixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ exports = module.exports = function toPostfixNotation (exp) {
removeFunction()
continue
} else if (cur === ',') {

removeComma()
continue
}
}

Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions test/2-postfixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 +')
Expand Down
4 changes: 3 additions & 1 deletion test/4-func.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4f52e55

Please sign in to comment.