From 586d55df858ff0ca4da0e8dda43eb869ffdee255 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Mon, 6 Apr 2026 17:29:40 -0600 Subject: [PATCH] Letting me save things to _ or _myVar to throw it away. fixing printing newlines in strings --- src/ast.ts | 7 ++++--- src/compiler.ts | 8 ++++++++ src/parser.ts | 14 ++++++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/ast.ts b/src/ast.ts index 0b8bf41..92a03db 100644 --- a/src/ast.ts +++ b/src/ast.ts @@ -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}` diff --git a/src/compiler.ts b/src/compiler.ts index e82434a..38be08c 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -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); diff --git a/src/parser.ts b/src/parser.ts index 1a7be08..ba2b9be 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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') {