Lambda calculus-like library written in TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
jlc/src/debug.ts

30 lines
638 B

let indent: string = '';
const logs: string[] = [];
let logging: boolean = false;
export function log(): void {
logging = true;
}
export function withLog<T>(before: string, f: () => T, after: (v: T) => string): T {
if (!logging) {
return f();
}
logs.push(`${indent}${before}`);
const l = logs.length;
indent += ' ';
const result = f();
indent = indent.slice(2);
if (logs.length === l) {
logs[logs.length - 1] += ` ${after(result)}`;
} else {
logs.push(`${indent}${after(result)}`);
}
return result;
}
export function dump() {
console.log('\n\nExecution log:');
console.log(logs.join('\n'));
}