-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary.ts
36 lines (29 loc) · 888 Bytes
/
binary.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { Formula } from "./formula";
export type BinaryOperation = (first: number, second: number) => number;
export class BinaryOperator {
constructor(
public symbol: string,
public precedence: number,
public left: boolean,
public operation: BinaryOperation
) {}
create(left: Formula, right: Formula): Formula {
return new BinaryFormula(this.operation, left, right);
}
}
export class BinaryFunction {
constructor(public name: string, public operation: BinaryOperation) {}
create(left: Formula, right: Formula): Formula {
return new BinaryFormula(this.operation, left, right);
}
}
export class BinaryFormula implements Formula {
constructor(
public operation: BinaryOperation,
public left: Formula,
public right: Formula
) {}
evaluate(): number {
return this.operation(this.left.evaluate(), this.right.evaluate());
}
}