compiling. interpreting was too slow
This commit is contained in:
parent
6edf592637
commit
2cd5a609bb
8 changed files with 650 additions and 21 deletions
56
src/runtime-js.ts
Normal file
56
src/runtime-js.ts
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
export const store: Record<string, any> = {};
|
||||
|
||||
export const _rt = {
|
||||
add: (a: number) => (b: number) => a + b,
|
||||
sub: (a: number) => (b: number) => a - b,
|
||||
mul: (a: number) => (b: number) => a * b,
|
||||
div: (a: number) => (b: number) => a / b,
|
||||
mod: (a: number) => (b: number) => a % b,
|
||||
cat: (a: string) => (b: string) => a + b,
|
||||
max: (a: number) => (b: number) => Math.max(a, b),
|
||||
min: (a: number) => (b: number) => Math.min(a, b),
|
||||
|
||||
eq: (a: any) => (b: any) => ({ _tag: a === b ? 'True' : 'False' }),
|
||||
neq: (a: any) => (b: any) => ({ _tag: a !== b ? 'True' : 'False' }),
|
||||
gt: (a: any) => (b: any) => ({ _tag: a > b ? 'True' : 'False' }),
|
||||
lt: (a: any) => (b: any) => ({ _tag: a < b ? 'True' : 'False' }),
|
||||
gte: (a: any) => (b: any) => ({ _tag: a >= b ? 'True' : 'False' }),
|
||||
lte: (a: any) => (b: any) => ({ _tag: a <= b ? 'True' : 'False' }),
|
||||
|
||||
len: (list: any[]) => list.length,
|
||||
str: (x: any) => String(x),
|
||||
slice: (s: string | any[]) => (start: number) => (end: number) => s.slice(start, end),
|
||||
debug: (label: string) => (value: any) => { console.log(label, value); return value; },
|
||||
measureText: (text: string) => {
|
||||
const canvas = document.createElement('canvas');
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (ctx) {
|
||||
ctx.font = '16px "SF Mono", "Monaco", "Menlo", monospace';
|
||||
return Math.floor(ctx.measureText(text).width);
|
||||
}
|
||||
return text.length * 10; // fallback
|
||||
},
|
||||
storeSearch: (query: string) => {
|
||||
const results: string[] = [];
|
||||
const searchTerm = query.toLowerCase();
|
||||
console.log("in storeSearch. query: ", searchTerm);
|
||||
for (const name of Object.keys(store)) {
|
||||
if (searchTerm === '' || name.toLowerCase().includes(searchTerm)) {
|
||||
results.push(name);
|
||||
}
|
||||
}
|
||||
return results;
|
||||
},
|
||||
rebind: (name: string, pathOrValue: any, maybeValue?: any) => {
|
||||
if (maybeValue === undefined) {
|
||||
store[name] = pathOrValue;
|
||||
} else {
|
||||
const path = pathOrValue as string[];
|
||||
let obj = store[name];
|
||||
for (let i = 0; i < path.length - 1; i++) {
|
||||
obj = obj[path[i]];
|
||||
}
|
||||
obj[path[path.length - 1]] = maybeValue;
|
||||
}
|
||||
},
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue