Skip to content

Commit

Permalink
JSON representation of arithmetic expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Kota Mizushima committed Feb 8, 2024
1 parent d9335cd commit a33ec37
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
12 changes: 6 additions & 6 deletions __tests__/expression_evaluator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
const {evaluate} = require("../expression/expression_evaluator");
const {tAdd, tSub, tMul, tDiv, tInt} = require('../expression/expression_ast');

test('1 + 1 == 2', () => {
test('1 + 1 ==> 2', () => {
const e = tAdd(tInt(1), tInt(1));
expect(evaluate(e)).toBe(2);
});

test('1 + 2 + 3 == 7', () => {
const e = tAdd(tInt(1), tInt(1));
expect(evaluate(e)).toBe(2);
test('1 + 2 + 3 ==> 6', () => {
const e = tAdd(tInt(1), tAdd(tInt(2), tInt(3)));
expect(evaluate(e)).toBe(6);
});

test('1 - 1 == 0', () => {
test('1 - 1 ==> 0', () => {
const e = tSub(tInt(1), tInt(1));
expect(evaluate(e)).toBe(0);
});

test('1 - 2 == 0', () => {
test('1 - 2 ==> -1', () => {
const e = tSub(tInt(1), tInt(2));
expect(evaluate(e)).toBe(-1);
});
Expand Down
62 changes: 62 additions & 0 deletions __tests__/expression_json_evaluator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use strict";
const {evaluateJson} = require("../expression/expression_json_evaluator");

test('1 + 1 ==> 2', () => {
const e = `["+", 1, 1]`;
expect(evaluateJson(e)).toBe(2);
});

test('1 + 2 + 3 ==> 7', () => {
const e = `["+", 1, ["+", 2, 3]]`;
expect(evaluateJson(e)).toBe(6);
});

test('1 - 1 ==> 0', () => {
const e = `["-", 1, 1]`;
expect(evaluateJson(e)).toBe(0);
});

test('1 - 2 ==> -1', () => {
const e = `["-", 1, 2]`;
expect(evaluateJson(e)).toBe(-1);
});

test('1 * 1 ==> 1', () => {
const e = `["*", 1, 1]`;
expect(evaluateJson(e)).toBe(1);
});

test('1 * 0 ==> 0', () => {
const e = `["*", 1, 0]`;
expect(evaluateJson(e)).toBe(0);
});

test('2 * 2 ==> 4', () => {
const e = `["*", 2, 2]`;
expect(evaluateJson(e)).toBe(4);
});

test('0 / 1 ==> 0', () => {
const e = `["/", 0, 1]`;
expect(evaluateJson(e)).toBe(0);
});

test('2 / 1 ==> 2', () => {
const e = `["/", 2, 1]`;
expect(evaluateJson(e)).toBe(2);
});

test('6 / 2 ==> 3', () => {
const e = `["/", 6, 2]`;
expect(evaluateJson(e)).toBe(3);
});

test('(1 + (2 * 3) - 1) / 2 == 3', () => {
const e =
`["/",
["-",
["+", 1, ["*", 2, 3]],
1],
2]`
expect(evaluateJson(e)).toBe(3);
});
20 changes: 20 additions & 0 deletions expression/expression_json_evaluator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const {BinExpr, Int} = require('./expression_ast');
const {evaluate} = require('./expression_evaluator');
function translateToAst(json) {
if(typeof(json) === 'number') {
return new Int(json);
} else if(Array.isArray(json)) {
const op = json[0];
const lhs = translateToAst(json[1]);
const rhs = translateToAst(json[2]);
return new BinExpr(op, lhs, rhs);
} else {
throw new Error("Not implemented for: " + JSON.stringify(json));
}
}
function evaluateJson(jsonString) {
const e = JSON.parse(jsonString);
const ast = translateToAst(e);
return evaluate(ast);
}
module.exports = {evaluateJson};

0 comments on commit a33ec37

Please sign in to comment.