-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops.ts
54 lines (43 loc) · 1.2 KB
/
ops.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Type } from './header.ts';
// Signatures for static type checking
export type Sig = [Type, Type, Type];
const NNN: Sig = ['Num', 'Num', 'Num'];
const NNB: Sig = ['Num', 'Num', 'Bool'];
const BBB: Sig = ['Bool', 'Bool', 'Bool'];
// Weird syntax for dictionary type!
export const OP_SIGNATURES: { [key: string]: Sig } = {
'+': NNN,
'-': NNN,
'*': NNN,
'/': NNN,
'==': NNB, // NOT polymorphic! only for integers.
'!=': NNB,
'<': NNB,
'>': NNB,
'and': BBB,
'or': BBB,
};
// Types and functions for dynamic checking and evaluation
type NNN_func = (x: number, y: number) => number;
type NNB_func = (x: number, y: number) => boolean;
type BBB_func = (x: boolean, y: boolean) => boolean;
// (Num, Num) => Num
export const OPS_NNN: { [key: string]: NNN_func } = {
'+': (x, y) => x + y,
'-': (x, y) => x - y,
'*': (x, y) => x * y,
'/': (x, y) => x / y,
};
// (Num, Num) => Bool
export const OPS_NNB: { [key: string]: NNB_func } = {
// Exact equality
'==': (x, y) => x === y,
'!=': (x, y) => x !== y,
'<': (x, y) => x < y,
'>': (x, y) => x > y,
};
// (Bool, Bool) => Bool
export const OPS_BBB: { [key: string]: BBB_func } = {
'and': (x, y) => x && y,
'or': (x, y) => x || y,
};