Repository navigation
Expand file tree
/
Copy pathqualityOfLife.ts
More file actions
182 lines (182 loc) · 5.86 KB
/
Copy pathqualityOfLife.ts
File metadata and controls
182 lines (182 loc) · 5.86 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//{//quality of life, macro-like functions
function loga(...a){console.log(...a);return a[0]}
function logData(...a){
console.log(...a.map(v=>v.toLog?.()??v))
}//log data emmits surtain information
/**
* loga : \# £console.log(..#..);
* logData : \{£console.log[...a.map <| v->traitof v >= trait[toLog=\(void)]? v.toLog?.():v};
* debugMode : true;
* assert : [condision msg ??= "" errorFunc ??= a->Error[e]] -> void:(
* void
* )
* */
const debugMode = true;
function assert(condision,msg = "",errorFunc = e=>Error(e)):true|Error{
if(debugMode){
msg ??= "";//msg:String|()->String
if(!condision)throw errorFunc("ASSERTION FAILLED:" + (typeof msg == "function"?msg():msg));
}
return true
}
function assume(condision,msg = "",errorFunc = e=>Error(e)):()=>any{
if(debugMode){
msg ??= "";
if(!condision)throw errorFunc("ASSUMPTION FAILLED:" + msg);
}
return (fooUsingAssumption:Fn|any)=>typeof fooUsingAssumption == "function" ? fooUsingAssumption(condision) : fooUsingAssumption;
}
assert.fail = function(msg = undefined,errorFunc = e=>Error(e)){
if(debugMode){
msg ??= "impossible case found";
assert(false,msg,errorFunc);
}
}
assert.impossibleCase = function(msg = "",errorFunc = e=>Error(e)){
if(debugMode){
assert(false,"impossible case: " + msg,errorFunc);
}
}
assert.expect = function(condision,msg = "",errorFunc = e=>Error(e)){
if(debugMode){
assert(condision,"expected: " + msg,errorFunc);
}
}
function unimplemented(msg = "",errorFunc = e=>Error(e)){
if(debugMode){
throw errorFunc("UNIMPLEMENTED:" + msg);
}
}
unimplemented.flaggedErrors = {};
unimplemented.silent = function(name?:String,returnValue,state?:Any,errorFunc = e=>Error(e)){
unimplemented.flaggedErrors[name] ??= {state,error:errorFunc};
return returnValue;
}
function todo(msg = "",errorFunc:(e)=>Error<e>){
if(debugMode){
throw (errorFunc??Error)("TODO:" + msg);
}
}
todo.flaggedErrors = {};
todo.silent = function(name?:String,returnValue,state?:Any,errorFunc = e=>Error(e)){
todo.flaggedErrors[name] ??= {state,error:errorFunc};
return returnValue;
}
function silentError(name?:String,state?:Any,errorFunc = e=>Error(e)){
silentError.flaggedErrors[name] ??= {state,error:errorFunc};
}
silentError.flaggedErrors = {};
function pass(v?:Any){return v}//marks a block as not meant to contain any code
function forBailOld(length,onError_default=undefined){
//example: let n=forBailOld(array.length);while(true){n();}
let i_bail = 0;
return function next(onError=onError_default){
if(debugMode)if(i_bail++>length){
if(onError)onError(i_bail);
throw Error("BAILED");
}
}
}
function* forBailGenerator(length,onError_default=undefined){
//example: for(let _ of forBailGenerator(array.length)){...}
for(let i = 0; i < length;i++)yield i;
if(debugMode){
if(onError_default)onError_default(i_bail);
throw Error("BAILED");
}
}
function match<V,B,T>(value:V,setOfCases:MatchCase[],defaultCase:(v)=>T):T{
"use strict";
//type MatchCase=[(V[]|V->B|bool), B|V->T]
let i = -1;
let tryNext = forBailOld(setOfCases.length);
while(++i < setOfCases.length){
let _case:MatchCase;
tryNext();
_case = setOfCases[i];
if(!(_case instanceof Array))throw Error(`missing case at index ${i}, got '${_case}'. May have missed a comma between cases.`)
let condition = _case[0];
let then = _case[1];
let input:V|B = value;
if(typeof then != "function")throw Error("compiler syntax error: case "+i+" is missing `V->T`");
if(
typeof condition == "function"?input=condition(value):
condition instanceof Array?input=condition.includes(value):
value == condition
//(()=>{
//console.error(_case[0])
//throw Error("compiler syntax error: case " + i + " is missing `V[]|V->bool`");
//})()
)return then(input,value);
}
if(defaultCase)return defaultCase(value,setOfCases);
else throw Error("compiler error: unhandled case: '"+value?.toString()+"'");
}
function matchFlags<F,T>(flags:F,setOfCases:MatchCase[],defaultCase:(v)=>T):T{//UNFINISHED
"use strict";
//where Flags:bool[]|(Number&uint)|{[Symbol]:any}
type F = Flags;
//type MatchCase=[(F|F->bool), F->T]
let i = -1;
let _case:MatchCase;
let unhandledFlags = [];
let tryNext = forBailOld(setOfCases.length);
unimplemented("need to convert the code to match flags instead of cases");
unimplemented("while loop should run all of the valid cases, unlike")
//while(++i < setOfCases.length){
// tryNext();
// _case = setOfCases[i];
// if(typeof _case[1] != "function")throw Error("compiler syntax error: case "+i+" is missing `V->T`");
// if(
// typeof _case[0] == "function"?_case[0](flags):
// _case[0] instanceof Array?_case[0].includes(flags):
// (()=>{
// console.error(_case[0])
// throw Error("compiler syntax error: case " + i + " is missing `V[]|V->bool`");
// })()
// )return _case[1](flags);
//}
if(defaultCase)return defaultCase(flags,setOfCases);
else throw Error("compiler error: unhandled flag: '"+unhandledFlags[0]?.toString()+"'");
}
function EnumSymbols(...list:String[]):{[Item<list>]:Symbol}{
return Object.freeze(list.reduce((s,v)=>[s,s[v]=Symbol(v)][0],{}));
}
type Option<T> = T|null;
function getFile(fileName):Option<String>{
let file;
try {
file = Deno.readTextFileSync(fileName);
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
//Error("file does not exist");
return null;
}
return file;
}
function getFile_expect(fileName,throwError):String{
let result = getFile(fileName);
if(result instanceof Error)throwError();
return result;
}
const closingBracketMap = {"{": "}", "[": "]", "(": ")"};
const methods = {
todo,
unimplemented,
loga,
logData,
match,
pass,
assert,
assume,
forBailOld,
forBailGenerator,
matchFlags,
EnumSymbols,
getFile_expect,
silentError,
};
export {methods};
//}//----