From 7c88a3513e339e1aa7ce65d348b32a1819a96070 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 29 Jul 2023 23:49:23 +0200 Subject: [PATCH] escape underscores --- src/index.ts | 4 ++-- src/lower.ts | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 886a7d3..656bf8e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,10 +12,10 @@ import { exec } from "child_process"; const input = ` function main() = uwu(10); -function a() = ; +function ___print() = ; function uwu(a: Int) = if a != 0 then ( - print("uwu\n"); + print("uwu\n"); uwu(a - 1); ); `; diff --git a/src/lower.ts b/src/lower.ts index 228f4d2..7fd9e90 100644 --- a/src/lower.ts +++ b/src/lower.ts @@ -51,6 +51,12 @@ export type Context = { relocations: Relocation[]; }; +function escapeIdentName(name: string): string { + // this allows the implementation to use an odd number of leading + // underscores + return name.replace(/_/g, "__"); +} + function internFuncType(cx: Context, type: wasm.FuncType): wasm.TypeIdx { const existing = getMap(cx.funcTypes, type); if (existing !== undefined) { @@ -180,7 +186,7 @@ function lowerFunc(cx: Context, item: Item, func: FunctionDef) { const type = internFuncType(cx, wasmType); const wasmFunc: wasm.Func = { - _name: func.name, + _name: escapeIdentName(func.name), type, locals: [], body: [], @@ -682,9 +688,6 @@ function addRt(cx: Context, ast: Ast) { const startIdx = mod.funcs.length; mod.funcs.push(start); - console.log(mod.funcs.map(({ _name }) => _name)); - - console.log(startIdx); const reserveMemory = (amount: number) => { const start = cx.reservedHeapMemoryStart; @@ -707,7 +710,7 @@ function addRt(cx: Context, ast: Ast) { const iovecArray = reserveMemory(8); const print: wasm.Func = { - _name: "____print", + _name: "___print", locals: [], type: internFuncType(cx, { params: [POINTER, USIZE], returns: [] }), body: [