-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Week 14 challenge | ||
|
||
## ButY evaluator | ||
|
||
For this week's challenge you will implement an **evaluator** for **math expressions** in the imaginary programming language **ButY**. | ||
|
||
### Example | ||
|
||
Here's an example of a simple math expression in ButY: | ||
|
||
```text | ||
5 × ⸨ ⸨ 18 ÷ 2 ⸩ − 7 ⸩ | ||
``` | ||
|
||
Which... yeah. Luckily you won't have to write a lexer or parser, because your evaluator will be given an **Abstract Syntax Tree** as input: | ||
|
||
```js | ||
{ | ||
type: 'BinaryExpression', | ||
operator: 'multiply', | ||
left: { | ||
type: 'Literal', | ||
value: 5 | ||
}, | ||
right: { | ||
type: 'BinaryExpression', | ||
operator: 'subtract', | ||
left: { | ||
type: 'BinaryExpression', | ||
operator: 'divide', | ||
left: { | ||
type: 'Literal', | ||
value: 18 | ||
}, | ||
right: { | ||
type: 'Literal', | ||
value: 2 | ||
} | ||
}, | ||
right: { | ||
type: 'Literal', | ||
value: 7 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
In this example the expected return value is `10`. | ||
|
||
#### Examples in code | ||
|
||
``` | ||
evaluate({ type: 'Literal', value: 42 }); | ||
// returns 42 | ||
evaluate({ | ||
type: 'BinaryExpression', | ||
operator: 'add', | ||
left: { type: 'Literal', value: 32 }, | ||
right: { type: 'Literal', value: 10 }, | ||
}); | ||
// returns 42 | ||
``` | ||
|
||
### AST interface | ||
|
||
``` | ||
type Expression = Literal | BinaryExpression | ||
type Literal = { | ||
type: 'Literal', | ||
value: number | ||
} | ||
type BinaryExpression = { | ||
type: 'BinaryExpression', | ||
operator: 'add', 'subtract', 'multiply', 'divide', | ||
left: Expression, | ||
right: Expression | ||
} | ||
``` | ||
|
||
### Other notes | ||
|
||
You do not need to account for dividing by 0. | ||
|
||
### Author | ||
|
||
Peter-Paul | ||
|
||
### Upload link | ||
|
||
### Results |
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,7 @@ | ||
const evaluate = (ast) => { | ||
// your solution here | ||
|
||
return undefined; | ||
}; | ||
|
||
module.exports = evaluate; |
Large diffs are not rendered by default.
Oops, something went wrong.
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,48 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const spec = require("./test-cases/spec.json"); | ||
const solutions = fs.readdirSync(path.resolve(__dirname, "solutions")); | ||
|
||
describe.each(solutions)("%s", (filename) => { | ||
const evaluate = require(path.resolve(__dirname, "solutions", filename)); | ||
|
||
test.each([ | ||
["42", 42, { type: "Literal", value: 42 }], | ||
[ | ||
"42 + 10", | ||
42, | ||
{ | ||
type: "BinaryExpression", | ||
operator: "add", | ||
left: { type: "Literal", value: 32 }, | ||
right: { type: "Literal", value: 10 }, | ||
}, | ||
], | ||
[ | ||
"5 × ⸨ ⸨ 18 ÷ 2 ⸩ − 7 ⸩", | ||
10, | ||
{ | ||
type: "BinaryExpression", | ||
operator: "multiply", | ||
left: { type: "Literal", value: 5 }, | ||
right: { | ||
type: "BinaryExpression", | ||
operator: "subtract", | ||
left: { | ||
type: "BinaryExpression", | ||
operator: "divide", | ||
left: { type: "Literal", value: 18 }, | ||
right: { type: "Literal", value: 2 }, | ||
}, | ||
right: { type: "Literal", value: 7 }, | ||
}, | ||
}, | ||
], | ||
])("%s = %d", (expression, result, input) => { | ||
expect(evaluate(input)).toBe(result); | ||
}); | ||
|
||
test.each(spec)("spec[%#]", ({ inputs, result }) => { | ||
expect(evaluate(...inputs)).toBe(result); | ||
}); | ||
}); |
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