Letting me save things to _ or _myVar to throw it away. fixing printing newlines in strings

This commit is contained in:
Dustin Swan 2026-04-06 17:29:40 -06:00
parent d36f694d80
commit 586d55df85
No known key found for this signature in database
GPG key ID: 30D46587E2100467
3 changed files with 24 additions and 5 deletions

View file

@ -243,7 +243,7 @@ export function prettyPrint(ast: AST, indent = 0): string {
const escaped = val.value
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n')
// .replace(/\n/g, '\\n')
.replace(/\t/g, '\\t');
return `"${escaped}"`;
}
@ -347,13 +347,14 @@ export function prettyPrint(ast: AST, indent = 0): string {
return `...${prettyPrint(ast.spread, indent)}`;
case 'definition':
const displayName = /^_\d+$/.test(ast.name) ? '_' : ast.name;
const ann = ast.annotation
? ` : ${ast.annotation.constraints.length > 0
? ast.annotation.constraints.map(c => `${c.className} ${c.typeVar}`).join(', ') + ' :: '
: ''}${prettyPrintType(ast.annotation.type)}`
: '';
if (!ast.body) return `${ast.name}${ann};`;
return `${ast.name}${ann} = ${prettyPrint(ast.body, indent)};`;
if (!ast.body) return `${displayName}${ann};`;
return `${displayName}${ann} = ${prettyPrint(ast.body, indent)};`;
default:
return `Unknown AST kind: ${i}${(ast as any).kind}`

View file

@ -268,6 +268,10 @@ export function compileAndRun(defs: Definition[], typeDefs: TypeDefinition[] = [
for (const def of defs) {
if (!def.body) continue; // type declaration only
if (def.name.startsWith('_')) {
definitions.set(def.name, def);
continue;
}
definitions.set(def.name, def);
const free = freeVars(def.body);
const deps = new Set([...free].filter(v => topLevel.has(v)));
@ -287,6 +291,10 @@ export function compileAndRun(defs: Definition[], typeDefs: TypeDefinition[] = [
for (const def of defs) {
if (!def.body) continue;
if (def.name.startsWith('_')) {
definitions.set(def.name, def); // keep it..
continue; // but don't compile it
}
const ctx: CompileCtx = { topLevel, bound: new Set() };
const compiled = `store[${JSON.stringify(def.name)}] = ${compile(def.body, ctx)};`;
compiledDefs.push(compiled);

View file

@ -199,9 +199,19 @@ export class Parser {
return { definitions, typeDefinitions, classDefinitions, instanceDeclarations };
}
private underscoreCounter = 0;
private parseDefinition(): Definition {
const nameToken = this.expect('ident');
const name = (nameToken as { value: string }).value;
let nameToken: Token;
let name: string;
if (this.current().kind === 'underscore') {
nameToken = this.advance();
name = `_${this.underscoreCounter++}`;
} else {
nameToken = this.expect('ident');
name = (nameToken as { value: string }).value;
}
let annotation: Annotation | undefined;
if (this.current().kind === 'colon') {