Fixing pretty print ast. adding a few more builtins :( getting further with inspector
This commit is contained in:
parent
30234875fe
commit
8bc05efa1e
9 changed files with 272 additions and 82 deletions
|
|
@ -12,7 +12,7 @@ export function compile(ast: AST): string {
|
|||
return JSON.stringify(ast.value.value);
|
||||
if (ast.value.kind === 'int' || ast.value.kind === 'float')
|
||||
return JSON.stringify(ast.value.value);
|
||||
throw new Error(`Cannot compile literal of kind ${ast.value.kind}`);
|
||||
throw new Error(`Cannot compile literal`); // of kind ${ast.value.kind}`);
|
||||
|
||||
case 'variable':
|
||||
return sanitize(ast.name);
|
||||
|
|
@ -35,9 +35,12 @@ export function compile(ast: AST): string {
|
|||
return `${compile(ast.func)}(${args})`;
|
||||
|
||||
case 'record': {
|
||||
const fields = Object.entries(ast.fields)
|
||||
.map(([k, v]) => `${sanitizeName(k)}: ${compile(v)}`);
|
||||
return `({${fields.join(', ')}})`;
|
||||
const parts = ast.entries.map(entry =>
|
||||
entry.kind === 'spread'
|
||||
? `...${compile(entry.expr)}`
|
||||
: `${sanitizeName(entry.key)}: ${compile(entry.value)}`
|
||||
)
|
||||
return `({${parts.join(', ')}})`;
|
||||
}
|
||||
|
||||
case 'list': {
|
||||
|
|
@ -98,7 +101,7 @@ function sanitize(name: string): string {
|
|||
|
||||
if (ops[name]) return ops[name];
|
||||
|
||||
const natives = ['measure', 'measureText', 'storeSearch', 'getSource', 'debug', 'len', 'slice', 'str', 'redefine', 'stateful', 'batch', 'noOp', 'rerender', 'focus', 'ui'];
|
||||
const natives = ['measure', 'measureText', 'storeSearch', 'getSource', 'debug', 'nth', 'len', 'slice', 'join', 'chars', 'split', 'str', 'redefine', 'stateful', 'batch', 'noOp', 'rerender', 'focus', 'ui'];
|
||||
if (natives.includes(name)) return `_rt.${name}`;
|
||||
|
||||
return sanitizeName(name);
|
||||
|
|
@ -284,7 +287,11 @@ function freeVars(ast: AST, bound: Set<string> = new Set()): Set<string> {
|
|||
}
|
||||
|
||||
case 'record': {
|
||||
const allVars = Object.values(ast.fields).flatMap(v => [...freeVars(v, bound)]);
|
||||
const allVars = ast.entries.flatMap(entry =>
|
||||
entry.kind === 'spread'
|
||||
? [...freeVars(entry.expr, bound)]
|
||||
: [...freeVars(entry.value, bound)]
|
||||
);
|
||||
return new Set(allVars);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue