1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-11 00:52:44 +03:00
mal/ts/stepA_mal.ts

325 lines
12 KiB
TypeScript
Raw Normal View History

2017-02-24 22:31:15 +03:00
import { readline } from "./node_readline";
2017-02-25 08:16:51 +03:00
import { Node, MalType, MalString, MalNil, MalList, MalVector, MalHashMap, MalSymbol, MalFunction, isAST, isSeq } from "./types";
2017-02-24 22:31:15 +03:00
import { Env } from "./env";
import * as core from "./core";
import { readStr } from "./reader";
import { prStr } from "./printer";
2017-02-25 05:32:06 +03:00
// READ
2017-02-24 22:31:15 +03:00
function read(str: string): MalType {
return readStr(str);
}
function quasiquote(ast: MalType): MalType {
if (!isPair(ast)) {
return new MalList([MalSymbol.get("quote"), ast]);
}
2017-02-25 07:28:38 +03:00
if (!isSeq(ast)) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected token type: ${ast.type}, expected: list or vector`);
}
const [arg1, arg2] = ast.list;
2017-02-25 08:12:15 +03:00
if (arg1.type === Node.Symbol && arg1.v === "unquote") {
2017-02-24 22:31:15 +03:00
return arg2;
}
if (isPair(arg1)) {
2017-02-25 07:28:38 +03:00
if (!isSeq(arg1)) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected token type: ${arg1.type}, expected: list or vector`);
}
const [arg11, arg12] = arg1.list;
2017-02-25 08:12:15 +03:00
if (arg11.type === Node.Symbol && arg11.v === "splice-unquote") {
2017-02-24 22:31:15 +03:00
return new MalList([
MalSymbol.get("concat"),
arg12,
quasiquote(new MalList(ast.list.slice(1))),
]);
}
}
return new MalList([
MalSymbol.get("cons"),
quasiquote(arg1),
quasiquote(new MalList(ast.list.slice(1))),
]);
function isPair(ast: MalType) {
2017-02-25 07:28:38 +03:00
if (!isSeq(ast)) {
2017-02-24 22:31:15 +03:00
return false;
}
return 0 < ast.list.length;
}
}
2017-02-25 05:32:06 +03:00
function isMacro(ast: MalType, env: Env): boolean {
2017-02-25 07:28:38 +03:00
if (!isSeq(ast)) {
2017-02-24 22:31:15 +03:00
return false;
}
const s = ast.list[0];
2017-02-25 08:12:15 +03:00
if (s.type !== Node.Symbol) {
2017-02-24 22:31:15 +03:00
return false;
}
const foundEnv = env.find(s);
if (!foundEnv) {
return false;
}
const f = foundEnv.get(s);
2017-02-25 08:12:15 +03:00
if (f.type !== Node.Function) {
2017-02-24 22:31:15 +03:00
return false;
}
return f.isMacro;
}
function macroexpand(ast: MalType, env: Env): MalType {
2017-02-25 05:32:06 +03:00
while (isMacro(ast, env)) {
2017-02-25 07:28:38 +03:00
if (!isSeq(ast)) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected token type: ${ast.type}, expected: list or vector`);
}
const s = ast.list[0];
2017-02-25 08:12:15 +03:00
if (s.type !== Node.Symbol) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected token type: ${s.type}, expected: symbol`);
}
const f = env.get(s);
2017-02-25 08:12:15 +03:00
if (f.type !== Node.Function) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected token type: ${f.type}, expected: function`);
}
ast = f.func(...ast.list.slice(1));
}
return ast;
}
function evalAST(ast: MalType, env: Env): MalType {
switch (ast.type) {
2017-02-25 08:12:15 +03:00
case Node.Symbol:
2017-02-24 22:31:15 +03:00
const f = env.get(ast);
if (!f) {
throw new Error(`unknown symbol: ${ast.v}`);
}
return f;
2017-02-25 08:12:15 +03:00
case Node.List:
2017-02-25 05:32:06 +03:00
return new MalList(ast.list.map(ast => evalMal(ast, env)));
2017-02-25 08:12:15 +03:00
case Node.Vector:
2017-02-25 05:32:06 +03:00
return new MalVector(ast.list.map(ast => evalMal(ast, env)));
2017-02-25 08:12:15 +03:00
case Node.HashMap:
2017-02-24 22:31:15 +03:00
const list: MalType[] = [];
for (const [key, value] of ast.entries()) {
list.push(key);
2017-02-25 05:32:06 +03:00
list.push(evalMal(value, env));
2017-02-24 22:31:15 +03:00
}
return new MalHashMap(list);
default:
return ast;
}
}
2017-02-25 05:32:06 +03:00
// EVAL
function evalMal(ast: MalType, env: Env): MalType {
2017-02-24 22:31:15 +03:00
loop: while (true) {
2017-02-25 08:12:15 +03:00
if (ast.type !== Node.List) {
2017-02-24 22:31:15 +03:00
return evalAST(ast, env);
}
ast = macroexpand(ast, env);
2017-02-25 08:12:15 +03:00
if (!isSeq(ast)) {
2017-02-24 22:31:15 +03:00
return evalAST(ast, env);
}
if (ast.list.length === 0) {
return ast;
}
const first = ast.list[0];
switch (first.type) {
2017-02-25 08:12:15 +03:00
case Node.Symbol:
2017-02-24 22:31:15 +03:00
switch (first.v) {
case "def!": {
const [, key, value] = ast.list;
2017-02-25 08:12:15 +03:00
if (key.type !== Node.Symbol) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
}
if (!value) {
throw new Error(`unexpected syntax`);
}
2017-02-25 05:32:06 +03:00
return env.set(key, evalMal(value, env))
2017-02-24 22:31:15 +03:00
}
case "let*": {
env = new Env(env);
const pairs = ast.list[1];
2017-02-25 07:28:38 +03:00
if (!isSeq(pairs)) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected token type: ${pairs.type}, expected: list or vector`);
}
for (let i = 0; i < pairs.list.length; i += 2) {
const key = pairs.list[i];
const value = pairs.list[i + 1];
2017-02-25 08:12:15 +03:00
if (key.type !== Node.Symbol) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
}
if (!key || !value) {
throw new Error(`unexpected syntax`);
}
2017-02-25 05:32:06 +03:00
env.set(key, evalMal(value, env));
2017-02-24 22:31:15 +03:00
}
ast = ast.list[2];
continue loop;
}
case "quote": {
return ast.list[1];
}
case "quasiquote": {
ast = quasiquote(ast.list[1]);
continue loop;
}
case "defmacro!": {
const [, key, value] = ast.list;
2017-02-25 08:12:15 +03:00
if (key.type !== Node.Symbol) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
}
if (!value) {
throw new Error(`unexpected syntax`);
}
2017-02-25 05:32:06 +03:00
const f = evalMal(value, env);
2017-02-25 08:12:15 +03:00
if (f.type !== Node.Function) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected token type: ${f.type}, expected: function`);
}
f.isMacro = true;
return env.set(key, f);
}
case "macroexpand": {
return macroexpand(ast.list[1], env);
}
case "try*": {
try {
2017-02-25 05:32:06 +03:00
return evalMal(ast.list[1], env);
2017-02-24 22:31:15 +03:00
} catch (e) {
const catchBody = ast.list[2];
2017-02-25 07:28:38 +03:00
if (!isSeq(catchBody)) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected return type: ${catchBody.type}, expected: list or vector`);
}
const catchSymbol = catchBody.list[0];
2017-02-25 08:12:15 +03:00
if (catchSymbol.type === Node.Symbol && catchSymbol.v === "catch*") {
2017-02-24 22:31:15 +03:00
const errorSymbol = catchBody.list[1];
2017-02-25 08:12:15 +03:00
if (errorSymbol.type !== Node.Symbol) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected return type: ${errorSymbol.type}, expected: symbol`);
}
if (!isAST(e)) {
e = new MalString((e as Error).message);
}
2017-02-25 05:32:06 +03:00
return evalMal(catchBody.list[2], new Env(env, [errorSymbol], [e]));
2017-02-24 22:31:15 +03:00
}
throw e;
}
}
case "do": {
const list = ast.list.slice(1, -1);
evalAST(new MalList(list), env);
ast = ast.list[ast.list.length - 1];
continue loop;
}
case "if": {
const [, cond, thenExpr, elseExrp] = ast.list;
2017-02-25 05:32:06 +03:00
const ret = evalMal(cond, env);
2017-02-24 22:31:15 +03:00
let b = true;
2017-02-25 08:12:15 +03:00
if (ret.type === Node.Boolean && !ret.v) {
2017-02-24 22:31:15 +03:00
b = false;
2017-02-25 08:16:51 +03:00
} else if (ret.type === Node.Nil) {
2017-02-24 22:31:15 +03:00
b = false;
}
if (b) {
ast = thenExpr;
} else if (elseExrp) {
ast = elseExrp;
} else {
2017-02-25 08:16:51 +03:00
ast = MalNil.instance;
2017-02-24 22:31:15 +03:00
}
continue loop;
}
case "fn*": {
const [, params, bodyAst] = ast.list;
2017-02-25 07:28:38 +03:00
if (!isSeq(params)) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected return type: ${params.type}, expected: list or vector`);
}
const symbols = params.list.map(param => {
2017-02-25 08:12:15 +03:00
if (param.type !== Node.Symbol) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected return type: ${param.type}, expected: symbol`);
}
return param;
});
2017-02-25 05:32:06 +03:00
return MalFunction.fromLisp(evalMal, env, symbols, bodyAst);
2017-02-24 22:31:15 +03:00
}
}
}
const result = evalAST(ast, env);
2017-02-25 07:28:38 +03:00
if (!isSeq(result)) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected return type: ${result.type}, expected: list or vector`);
}
const [f, ...args] = result.list;
2017-02-25 08:12:15 +03:00
if (f.type !== Node.Function) {
2017-02-24 22:31:15 +03:00
throw new Error(`unexpected token: ${f.type}, expected: function`);
}
if (f.ast) {
ast = f.ast;
env = f.newEnv(args);
continue loop;
}
return f.func(...args);
}
}
2017-02-25 05:32:06 +03:00
// PRINT
2017-02-24 22:31:15 +03:00
function print(exp: MalType): string {
return prStr(exp);
}
const replEnv = new Env();
2017-02-25 05:32:06 +03:00
function rep(str: string): string {
return print(evalMal(read(str), replEnv));
}
// core.EXT: defined using Racket
2017-02-25 04:34:37 +03:00
core.ns.forEach((value, key) => {
2017-02-24 22:31:15 +03:00
replEnv.set(key, value);
2017-02-25 04:34:37 +03:00
});
2017-02-24 22:31:15 +03:00
replEnv.set(MalSymbol.get("eval"), MalFunction.fromBootstrap(ast => {
if (!ast) {
throw new Error(`undefined argument`);
}
2017-02-25 05:32:06 +03:00
return evalMal(ast, replEnv);
2017-02-24 22:31:15 +03:00
}));
replEnv.set(MalSymbol.get("*ARGV*"), new MalList([]));
// core.mal: defined using the language itself
rep(`(def! *host-language* "TypeScript")`);
rep("(def! not (fn* (a) (if a false true)))");
rep(`(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))`);
rep(`(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw "odd number of forms to cond")) (cons 'cond (rest (rest xs)))))))`);
rep("(def! *gensym-counter* (atom 0))");
rep("(def! gensym (fn* [] (symbol (str \"G__\" (swap! *gensym-counter* (fn* [x] (+ 1 x)))))))");
rep("(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) (let* (condvar (gensym)) `(let* (~condvar ~(first xs)) (if ~condvar ~condvar (or ~@(rest xs)))))))))");
if (typeof process !== "undefined" && 2 < process.argv.length) {
replEnv.set(MalSymbol.get("*ARGV*"), new MalList(process.argv.slice(3).map(s => new MalString(s))));
rep(`(load-file "${process.argv[2]}")`);
process.exit(0);
}
rep(`(println (str "Mal [" *host-language* "]"))`);
while (true) {
const line = readline("user> ");
if (line == null) {
break;
}
if (line === "") {
continue;
}
try {
console.log(rep(line));
} catch (e) {
const err: Error = e;
console.error(err.message);
}
}