Parser-interpreter
Simple example of a parser-interpreter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const syntax = {
'+': function(a, b) {return a + b},
'-': function(a, b) {return a - b},
'*': function(a, b) {return a * b},
'/': function(a, b) {return a / b}
};
const calc = function(exp) {
let [a, b, op] = exp;
if (!syntax[op] || [a, b].some(elt => typeof(elt) !== 'number')) {
return 'Error';
} else {
return syntax[op](a, b);
}
};
Tests
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const assert = require('assert');
describe('calc()', function() {
it("should add", function(){
assert.equal(calc([1, 2, "+"]), 3);
});
it("should subtract", function(){
assert.equal(calc([1, 2, '-']), -1);
});
it("should multiply", function(){
assert.equal(calc([1, 2, '*']), 2);
});
it("should divide", function(){
assert.equal(calc([1, 2, '/']), 0.5);
});
it("should handle errors", function(){
assert.equal(calc(['1', 2, '+']), 'Error');
assert.equal(calc([1, 2, '&']), 'Error');
});
});