Letting us redefine functions, reactively

This commit is contained in:
Dustin Swan 2026-02-09 12:54:09 -07:00
parent 33f3b2cfc2
commit 60c8f74d50
No known key found for this signature in database
GPG key ID: 30D46587E2100467
4 changed files with 169 additions and 3 deletions

View file

@ -1,3 +1,7 @@
import { tokenize } from './lexer'
import { Parser } from './parser'
import { recompile } from './compiler'
export const store: Record<string, any> = {};
export const _rt = {
@ -42,7 +46,11 @@ export const _rt = {
},
rebind: (name: string, pathOrValue: any, maybeValue?: any) => {
if (maybeValue === undefined) {
store[name] = pathOrValue;
if (pathOrValue && pathOrValue.ast) {
recompile(name, pathOrValue._ast);
} else {
store[name] = pathOrValue;
}
} else {
const path = pathOrValue as string[];
let obj = store[name];
@ -109,5 +117,12 @@ export const _rt = {
default:
return { width: 0, height: 0 };
}
},
redefine: (name: string) => (code: string) => {
const tokens = tokenize(`_tmp = ${code};`);
const parser = new Parser(tokens, "");
const defs = parser.parse();
recompile(name, defs[0]. body);
return { _tag: 'Ok' };
}
}