-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandleConditional.ts
57 lines (37 loc) · 2.1 KB
/
handleConditional.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
55
56
57
import { Expression } from './customClasses/Expression.js';
import { customBoolean, customTypes, customVar, isCustomVar, parserType } from './customClasses/classes.js';
import { customClass } from './customClasses/obj.js';
export const compSymbs = ['==', '<', '>', '>=', '<=', '!=', '!'];
export const incSymbs = ['++', '--', '+=', '-=', '*=', '/='];
function checkCondSingle(leftSide: Expression, op: string, rightSide?: Expression): boolean {
switch (op) {
case "!": return !leftSide.val;
case "==": return leftSide.val === rightSide?.val;
case "<": return leftSide.val < rightSide?.val;
case ">": return leftSide.val > rightSide?.val;
case ">=": return leftSide.val >= rightSide?.val;
case "<=": return leftSide.val <= rightSide?.val;
case "!=": return leftSide.val != rightSide?.val;
default: throw `UNKNOWN OPERATION "${op}" FOR "${leftSide} ${op} ${rightSide}"!`;
}
}
export async function handleConditional(line: string, context: customTypes[], parser: parserType): Promise<customBoolean> {
// get symbol index
const sInd = Array.from(line).findIndex(s => compSymbs.includes(s));
const [leftSide, op, rightside] = [line.slice(0, sInd), line[sInd], line.slice(sInd + 1)];
let v = (await parser(leftSide, context))[0];
const leftVal = (isCustomVar(v)) ? v.val : (v as Expression);
v = (await parser(rightside, context))[0];
const rightVal = (isCustomVar(v)) ? v.val : (v as Expression);
if (leftVal instanceof customClass || rightVal instanceof customClass) {
if (!(leftVal instanceof customClass && rightVal instanceof customClass)) return new customBoolean(false);
for (const v in leftVal) {
if (!(v in rightVal && rightVal[v as keyof customClass] === leftVal[v as keyof customClass])) return new customBoolean(false);
}
return new customBoolean(true);
}
// handle (!thing)
if (!leftVal) throw "NO VALUE FOUND!";
if (!rightside) return new customBoolean(checkCondSingle(leftVal, op));
return new customBoolean(checkCondSingle(leftVal, op, rightVal));
}