escape underscores

This commit is contained in:
nora 2023-07-29 23:49:23 +02:00
parent 7cd50ab554
commit 7c88a3513e
2 changed files with 10 additions and 7 deletions

View file

@ -12,7 +12,7 @@ import { exec } from "child_process";
const input = ` const input = `
function main() = uwu(10); function main() = uwu(10);
function a() = ; function ___print() = ;
function uwu(a: Int) = if a != 0 then ( function uwu(a: Int) = if a != 0 then (
print("uwu\n"); print("uwu\n");

View file

@ -51,6 +51,12 @@ export type Context = {
relocations: Relocation[]; 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 { function internFuncType(cx: Context, type: wasm.FuncType): wasm.TypeIdx {
const existing = getMap(cx.funcTypes, type); const existing = getMap(cx.funcTypes, type);
if (existing !== undefined) { if (existing !== undefined) {
@ -180,7 +186,7 @@ function lowerFunc(cx: Context, item: Item, func: FunctionDef) {
const type = internFuncType(cx, wasmType); const type = internFuncType(cx, wasmType);
const wasmFunc: wasm.Func = { const wasmFunc: wasm.Func = {
_name: func.name, _name: escapeIdentName(func.name),
type, type,
locals: [], locals: [],
body: [], body: [],
@ -682,9 +688,6 @@ function addRt(cx: Context, ast: Ast) {
const startIdx = mod.funcs.length; const startIdx = mod.funcs.length;
mod.funcs.push(start); mod.funcs.push(start);
console.log(mod.funcs.map(({ _name }) => _name));
console.log(startIdx);
const reserveMemory = (amount: number) => { const reserveMemory = (amount: number) => {
const start = cx.reservedHeapMemoryStart; const start = cx.reservedHeapMemoryStart;
@ -707,7 +710,7 @@ function addRt(cx: Context, ast: Ast) {
const iovecArray = reserveMemory(8); const iovecArray = reserveMemory(8);
const print: wasm.Func = { const print: wasm.Func = {
_name: "____print", _name: "___print",
locals: [], locals: [],
type: internFuncType(cx, { params: [POINTER, USIZE], returns: [] }), type: internFuncType(cx, { params: [POINTER, USIZE], returns: [] }),
body: [ body: [