Implemented step 2.
This commit is contained in:
1 parent
97f047e10a
commit
df9e91ebd5
4 files changed
+149
-51
No files matched your search
@@ -0,0 +1,230 @@
|
||||
import {assertEquals} from "https://deno.land/std@0.224.0/assert/assert_equals.ts";
|
||||
|
||||
export type StringToken = { type: "string"; value: string };
|
||||
export type NumberToken = { type: "number"; value: number };
|
||||
export type BooleanToken = { type: "boolean"; value: boolean };
|
||||
export type SymToken = { type: "sym"; value: string };
|
||||
export type NilToken = { type: "nil"; value: null };
|
||||
export type ListToken = {
|
||||
type: "list";
|
||||
value: (
|
||||
| StringToken
|
||||
| NumberToken
|
||||
| BooleanToken
|
||||
| SymToken
|
||||
| NilToken
|
||||
| ListToken
|
||||
| FunctionToken
|
||||
)[];
|
||||
};
|
||||
|
||||
export type FunctionToken = {type: 'function', value: (...args: any[]) => any};
|
||||
|
||||
export type AST =
|
||||
| NilToken
|
||||
| StringToken
|
||||
| NumberToken
|
||||
| BooleanToken
|
||||
| SymToken
|
||||
| FunctionToken
|
||||
| ListToken;
|
||||
|
||||
function findStringEnd(s: string): number {
|
||||
let idx = 1;
|
||||
let previousChar = "";
|
||||
for (const c of Array.from(s.substring(1))) {
|
||||
if ('"' === c && previousChar !== "\\") {
|
||||
return idx;
|
||||
}
|
||||
previousChar = c;
|
||||
idx++;
|
||||
}
|
||||
throw new Error(`Syntax error. The string ${s} never ends.`);
|
||||
}
|
||||
|
||||
function findNextString(source: string): [string, string] {
|
||||
const stringEnd = findStringEnd(source);
|
||||
const ret = JSON.parse(source.substring(0, stringEnd + 1));
|
||||
return [ret, source.substring(stringEnd + 1).trim()];
|
||||
}
|
||||
|
||||
function findTokenEnd(source: string): number {
|
||||
// const match = source.match(/(\s|\))/gi);
|
||||
const re = /(\s|\))/gi;
|
||||
const ret = source.search(re);
|
||||
return ret !== -1 ? ret : source.length;
|
||||
}
|
||||
|
||||
function isNumber(token: string): boolean {
|
||||
return /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/.test(token);
|
||||
}
|
||||
|
||||
function findNextNoneStringToken(source: string): AST {
|
||||
const tokenEnd = findTokenEnd(source);
|
||||
const token = source.substring(0, tokenEnd);
|
||||
|
||||
if (token === "nil") {
|
||||
return { type: "nil", value: null };
|
||||
}
|
||||
|
||||
if (token === "true" || token === "false") {
|
||||
return { type: "boolean", value: token === "true" };
|
||||
}
|
||||
if (isNumber(token)) {
|
||||
return { type: "number", value: JSON.parse(token) };
|
||||
}
|
||||
return { type: "sym", value: token };
|
||||
}
|
||||
|
||||
function findNextToken(source: string): [AST, string] {
|
||||
if (source.startsWith("(")) {
|
||||
let x = source.substring(1).trim();
|
||||
let token;
|
||||
const lst: AST[] = [];
|
||||
while (!x.trim().startsWith(")")) {
|
||||
[token, x] = findNextToken(x);
|
||||
lst.push(token);
|
||||
x = x.trim();
|
||||
}
|
||||
return [{ type: "list", value: lst }, x.trim().substring(1)];
|
||||
}
|
||||
|
||||
if (source.startsWith('"')) {
|
||||
const [ret, rest] = findNextString(source);
|
||||
return [{ type: "string", value: ret }, rest];
|
||||
}
|
||||
|
||||
const tokenEnd = findTokenEnd(source);
|
||||
const token = source.substring(0, tokenEnd);
|
||||
return [findNextNoneStringToken(token), source.substring(tokenEnd).trim()];
|
||||
}
|
||||
|
||||
export function readAST(source: string): AST {
|
||||
source = source.trim();
|
||||
const [ret, _] = findNextToken(source);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
export function printAST(form: AST): string {
|
||||
if (form.type === "list") {
|
||||
const ret = form.value.map(printAST).join(" ");
|
||||
return `(${ret})`;
|
||||
}
|
||||
if (form.type === "sym") {
|
||||
return form.value;
|
||||
}
|
||||
if (form.type === "nil") {
|
||||
return "nil";
|
||||
}
|
||||
return JSON.stringify(form.value);
|
||||
}
|
||||
|
||||
Deno.test("basic strings", function (): void {
|
||||
const testCases: [string, string][] = [
|
||||
['"foo"', "foo"],
|
||||
[' "foo" ', "foo"],
|
||||
['"bonjour \\"poline\\""', 'bonjour "poline"'],
|
||||
];
|
||||
for (const [input, exptValue] of testCases) {
|
||||
const expt: StringToken = { type: "string", value: exptValue };
|
||||
assertEquals(expt, readAST(input));
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("basic symbols", function (): void {
|
||||
const testCases: [string, string][] = [
|
||||
["foo", "foo"],
|
||||
["trueFalse", "trueFalse"],
|
||||
["nil?", "nil?"],
|
||||
];
|
||||
for (const [input, exptValue] of testCases) {
|
||||
const expt: SymToken = { type: "sym", value: exptValue };
|
||||
assertEquals(expt, readAST(input));
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("basic numbers", function (): void {
|
||||
const testCases: [string, number][] = [
|
||||
["567", 567],
|
||||
["-34", -34],
|
||||
["-34.5", -34.5],
|
||||
];
|
||||
for (const [input, exptValue] of testCases) {
|
||||
const expt: NumberToken = { type: "number", value: exptValue };
|
||||
assertEquals(expt, readAST(input));
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("basic booleans", function (): void {
|
||||
const testCases: [string, boolean][] = [
|
||||
["true", true],
|
||||
["false", false],
|
||||
];
|
||||
for (const [input, exptValue] of testCases) {
|
||||
const expt: BooleanToken = { type: "boolean", value: exptValue };
|
||||
assertEquals(expt, readAST(input));
|
||||
}
|
||||
});
|
||||
|
||||
function numberToken(value: number): NumberToken {
|
||||
return { type: "number", value };
|
||||
}
|
||||
|
||||
function symToken(value: string): SymToken {
|
||||
return { type: "sym", value };
|
||||
}
|
||||
|
||||
function listToken(value: (NumberToken | SymToken | ListToken)[]): ListToken {
|
||||
return { type: "list", value: value };
|
||||
}
|
||||
|
||||
Deno.test("basic list", function (): void {
|
||||
const input = "(add 3 4)";
|
||||
const expt = listToken([
|
||||
symToken("add"),
|
||||
numberToken(3),
|
||||
numberToken(4),
|
||||
]);
|
||||
assertEquals(expt, readAST(input));
|
||||
});
|
||||
|
||||
Deno.test("list of numbers", function (): void {
|
||||
const input = "( 1 2 3)";
|
||||
const expt = listToken([
|
||||
numberToken(1),
|
||||
numberToken(2),
|
||||
numberToken(3),
|
||||
]);
|
||||
assertEquals(expt, readAST(input));
|
||||
});
|
||||
|
||||
Deno.test("composed list", function (): void {
|
||||
const input = "(add (mul 4 9) (inc -5))";
|
||||
const expt = listToken([
|
||||
symToken("add"),
|
||||
listToken([symToken("mul"), numberToken(4), numberToken(9)]),
|
||||
listToken([symToken("inc"), numberToken(-5)]),
|
||||
]);
|
||||
assertEquals(expt, readAST(input));
|
||||
});
|
||||
|
||||
Deno.test("basic nil", function () {
|
||||
const expt: NilToken = { type: "nil", value: null };
|
||||
assertEquals(expt, readAST("nil"));
|
||||
});
|
||||
|
||||
Deno.test("step 1 tests", function (): void {
|
||||
const cases: [string, string][] = [
|
||||
["123", "123"],
|
||||
[" 123 ", "123"],
|
||||
["abc", "abc"],
|
||||
[" abc ", "abc"],
|
||||
["(123 456)", "(123 456)"],
|
||||
["( 123 456 789 )", "(123 456 789)"],
|
||||
["( + 2 (* 3 4) ) ", "(+ 2 (* 3 4))"],
|
||||
];
|
||||
for (const [input, expt] of cases) {
|
||||
assertEquals(expt, printAST(readAST(input)));
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
import {readUserInputs} from "./shared.ts";
|
||||
|
||||
function read(source: string): string {
|
||||
return source;
|
||||
}
|
||||
|
||||
function eval_(ast: string): string {
|
||||
return ast;
|
||||
}
|
||||
|
||||
function print(form: string): string {
|
||||
return form
|
||||
}
|
||||
|
||||
function rep(source: string): string{
|
||||
return print(eval_(read( source)))
|
||||
}
|
||||
|
||||
|
||||
|
||||
export function main(): void {
|
||||
for(const userInput of readUserInputs()){
|
||||
rep(userInput);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import {readUserInputs} from "./shared.ts";
|
||||
import {AST, readAST, printAST} from './reader-printer.ts';
|
||||
|
||||
function read(source: string): AST {
|
||||
return readAST(source);
|
||||
}
|
||||
|
||||
function eval_(ast: AST): AST {
|
||||
return ast;
|
||||
}
|
||||
|
||||
function print(form: AST): string {
|
||||
return printAST(form);
|
||||
}
|
||||
|
||||
function rep(source: string): string {
|
||||
return print(eval_(read(source)));
|
||||
}
|
||||
|
||||
|
||||
export function main(): void {
|
||||
for (const userInput of readUserInputs()) {
|
||||
rep(userInput);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import {assertEquals} from "https://deno.land/std@0.224.0/assert/assert_equals.ts";
|
||||
import {AST, FunctionToken, printAST, readAST} from "./reader-printer.ts";
|
||||
|
||||
type MalEnv = Map<string, FunctionToken>;
|
||||
|
||||
function functionToken(fn: (...args: any[]) => any): FunctionToken {
|
||||
return { type: "function", value: fn };
|
||||
}
|
||||
|
||||
function isBoolean(v: unknown): v is boolean {
|
||||
return v === true || v === false;
|
||||
}
|
||||
|
||||
function isNumber(v: unknown): v is number {
|
||||
return typeof v === "number" && isFinite(v);
|
||||
}
|
||||
|
||||
function isString(v: unknown): v is string {
|
||||
return typeof v === "string" || v instanceof String;
|
||||
}
|
||||
|
||||
class REPL {
|
||||
private readonly env: MalEnv;
|
||||
|
||||
constructor(env: MalEnv) {
|
||||
this.env = env;
|
||||
}
|
||||
|
||||
static mathRepl(): REPL {
|
||||
const mathFunctions = new Map<string, FunctionToken>();
|
||||
mathFunctions.set("+", functionToken((a, b) => a + b));
|
||||
mathFunctions.set("-", functionToken((a, b) => a - b));
|
||||
mathFunctions.set("*", functionToken((a, b) => a * b));
|
||||
mathFunctions.set("/", functionToken((a, b) => a / b));
|
||||
|
||||
return new REPL(mathFunctions);
|
||||
}
|
||||
|
||||
read(source: string): AST {
|
||||
return readAST(source);
|
||||
}
|
||||
|
||||
cast(value: unknown): AST {
|
||||
if (value === true || value === false) {
|
||||
return { type: "boolean", value: value };
|
||||
}
|
||||
if (isBoolean(value)) {
|
||||
return { type: "boolean", value: value };
|
||||
}
|
||||
if (isString(value)) {
|
||||
return { type: "string", value: value };
|
||||
}
|
||||
if (isNumber(value)) {
|
||||
return { type: "number", value: value };
|
||||
}
|
||||
throw new Error(`Unknown return type from functon: ${value}`);
|
||||
}
|
||||
|
||||
eval_(ast: AST): AST {
|
||||
if (ast.type === "list") {
|
||||
const fn = this.eval_(ast.value[0]) as FunctionToken;
|
||||
const ret = fn.value(
|
||||
...ast.value.slice(1).map((n) => this.eval_(n).value),
|
||||
);
|
||||
return this.cast(ret);
|
||||
}
|
||||
if (ast.type === "sym") {
|
||||
const ret = this.env.get(ast.value);
|
||||
if (ret === undefined) {
|
||||
throw new Error(`Unknown reference ${ast.value}`);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
return ast;
|
||||
}
|
||||
|
||||
print(form: AST): string {
|
||||
return printAST(form);
|
||||
}
|
||||
|
||||
rep(source: string): string {
|
||||
return this.print(this.eval_(this.read(source)));
|
||||
}
|
||||
}
|
||||
|
||||
Deno.test("basic", function (): void {
|
||||
const repl = REPL.mathRepl();
|
||||
assertEquals(repl.rep("(+ 1 1)"), "2");
|
||||
assertEquals(repl.rep("(+ 2 (* 3 4))"), "14");
|
||||
});
|
||||
Reference in new issue
Block a user