Skip to content

Commit

Permalink
It works!
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-y committed Sep 24, 2024
1 parent 03d6d86 commit ecb7b1d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 119 deletions.
1 change: 0 additions & 1 deletion src/lib/host-runtime/bool-to-int.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/lib/host-runtime/index.ts

This file was deleted.

77 changes: 0 additions & 77 deletions src/lib/host-runtime/strings.ts

This file was deleted.

20 changes: 5 additions & 15 deletions src/parser/reader-macros/string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Identifier, List } from "../../syntax-objects/index.js";
import { Identifier, StringLiteral } from "../../syntax-objects/index.js";
import { ReaderMacro } from "./types.js";

export const stringMacro: ReaderMacro = {
Expand Down Expand Up @@ -31,19 +31,9 @@ export const stringMacro: ReaderMacro = {
});
}

return new List([
"String",
[
"object",
[
":",
"chars",
[
"FixedArray",
...token.value.split("").map((char) => char.charCodeAt(0)),
],
],
],
]);
return new StringLiteral({
value: token.value,
location: token.location,
});
},
};
39 changes: 16 additions & 23 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
import binaryen from "binaryen";
import { StringsTable } from "./lib/host-runtime/strings.js";

export function run(mod: binaryen.Module) {
const binary = mod.emitBinary();
const compiled = new WebAssembly.Module(binary);
const strings = new StringsTable();
const instance = new WebAssembly.Instance(compiled, {
strings: {
"alloc-string": () => strings.allocString(),
"de-alloc-string": (index: number) => strings.deAllocString(index),
"add-char-code-to-string": (code: number, index: number) =>
strings.addCharCodeToString(code, index),
"str-len": (index: number) => strings.strLength(index),
"print-str": (index: number) => strings.printStr(index),
"get-char-code-from-string": (charIndex: number, strIndex: number) =>
strings.getCharCodeFromString(charIndex, strIndex),
"str-equals": (aIndex: number, bIndex: number) =>
strings.strEquals(aIndex, bIndex),
"str-starts-with": (aIndex: number, bIndex: number) =>
strings.strStartsWith(aIndex, bIndex),
"str-ends-with": (aIndex: number, bIndex: number) =>
strings.strEndsWith(aIndex, bIndex),
"str-includes": (aIndex: number, bIndex: number) =>
strings.strIncludes(aIndex, bIndex),
"str-test": (strIndex: number, regexIndex: number, flagsIndex: number) =>
strings.strTest(strIndex, regexIndex, flagsIndex),
},
utils: {
log: (val: number) => console.log(val),
},
});

console.log((instance.exports as any).main());
const fns = instance.exports as any;
const newStringReader = fns.new_string_reader;
const read_next_char = fns.read_next_char;
const result = fns.main();
const reader = newStringReader(result);

let str = "";
while (true) {
const char = read_next_char(reader);
if (char < 0) {
break;
}
str += String.fromCharCode(char);
}

console.log(str);
}
23 changes: 23 additions & 0 deletions src/semantics/init-entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
nop,
UnionType,
IntersectionType,
Identifier,
} from "../syntax-objects/index.js";
import { Match, MatchCase } from "../syntax-objects/match.js";
import { SemanticProcessor } from "./types.js";
Expand All @@ -24,6 +25,28 @@ export const initEntities: SemanticProcessor = (expr) => {
return expr.applyMap(initEntities);
}

if (expr.isStringLiteral()) {
return initEntities(
new List({
...expr.metadata,
value: [
"String",
[
"object",
[
":",
"chars",
[
"FixedArray",
...expr.value.split("").map((c) => c.charCodeAt(0)),
],
],
],
],
})
);
}

if (!expr.isList()) return expr;

if (expr.calls("define_function")) {
Expand Down
4 changes: 2 additions & 2 deletions std/string.voyd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::macros::all
use array::all
use operators::all

obj String {
pub obj String {
chars: FixedArray<i32>
}

Expand All @@ -26,7 +26,7 @@ impl String
pub fn length(self) -> i32
self.chars.length<i32>()

pub fn concatenated(self, other: String) -> String
pub fn '+'(self, other: String) -> String
let new_length = self.chars.length<i32>() + other.chars.length<i32>()
let new_chars = new_fixed_array<i32>(new_length)
new_chars.copy<i32>({
Expand Down

0 comments on commit ecb7b1d

Please sign in to comment.