-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSON representation of arithmetic expression
- Loading branch information
Kota Mizushima
committed
Feb 8, 2024
1 parent
d9335cd
commit a33ec37
Showing
3 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |