TypeScript: step 7
authorvvakame <vvakame+dev@gmail.com>
Fri, 24 Feb 2017 11:16:23 +0000 (20:16 +0900)
committervvakame <vvakame+dev@gmail.com>
Fri, 24 Feb 2017 11:16:23 +0000 (20:16 +0900)
ts/core.ts
ts/package.json
ts/step7_quote.ts [new file with mode: 0644]

index b55a162..8ecda3f 100644 (file)
@@ -35,6 +35,25 @@ export const ns: Map<MalSymbol, MalFunction> = (() => {
             const content = fs.readFileSync(v.v, "UTF-8");
             return new MalString(content);
         },
+        cons(a: MalType, b: MalType) {
+            if (!MalList.is(b) && !MalVector.is(b)) {
+                throw new Error(`unexpected symbol: ${b.type}, expected: list or vector`);
+            }
+
+            return new MalList([a].concat(b.list));
+        },
+        concat(...args: MalType[]) {
+            const list = args
+                .map(arg => {
+                    if (!MalList.is(arg) && !MalVector.is(arg)) {
+                        throw new Error(`unexpected symbol: ${arg.type}, expected: list or vector`);
+                    }
+                    return arg;
+                })
+                .reduce((p, c) => p.concat(c.list), [] as MalType[]);
+
+            return new MalList(list);
+        },
         list(...args: MalType[]): MalList {
             return new MalList(args);
         },
index be4e775..50c9fe7 100644 (file)
@@ -5,14 +5,15 @@
   "description": "Make a Lisp (mal) language implemented in TypeScript",
   "scripts": {
     "build": "tsfmt -r && tsc -p ./",
-    "test": "npm run build && npm run test:step0 && npm run test:step1 && npm run test:step2 && npm run test:step3 && npm run test:step4 && npm run test:step5 && npm run test:step6",
+    "test": "npm run build && npm run test:step0 && npm run test:step1 && npm run test:step2 && npm run test:step3 && npm run test:step4 && npm run test:step5 && npm run test:step6 && npm run test:step7",
     "test:step0": "cd .. && make 'test^ts^step0'",
     "test:step1": "cd .. && make 'test^ts^step1'",
     "test:step2": "cd .. && make 'test^ts^step2'",
     "test:step3": "cd .. && make 'test^ts^step3'",
     "test:step4": "cd .. && make 'test^ts^step4'",
     "test:step5": "cd .. && make 'test^ts^step5'",
-    "test:step6": "cd .. && make 'test^ts^step6'"
+    "test:step6": "cd .. && make 'test^ts^step6'",
+    "test:step7": "cd .. && make 'test^ts^step7'"
   },
   "dependencies": {
     "ffi": "^2.2.0"
diff --git a/ts/step7_quote.ts b/ts/step7_quote.ts
new file mode 100644 (file)
index 0000000..2f9749e
--- /dev/null
@@ -0,0 +1,232 @@
+import { readline } from "./node_readline";
+
+import { MalType, MalString, MalBoolean, MalNull, MalList, MalVector, MalHashMap, MalSymbol, MalFunction } from "./types";
+import { Env } from "./env";
+import * as core from "./core";
+import { readStr } from "./reader";
+import { prStr } from "./printer";
+
+function read(str: string): MalType {
+    return readStr(str);
+}
+
+function quasiquote(ast: MalType): MalType {
+    if (!isPair(ast)) {
+        return new MalList([MalSymbol.get("quote"), ast]);
+    }
+    if (!MalList.is(ast) && !MalVector.is(ast)) {
+        throw new Error(`unexpected token type: ${ast.type}, expected: list or vector`);
+    }
+    const [arg1, arg2] = ast.list;
+    if (MalSymbol.is(arg1) && arg1.v === "unquote") {
+        return arg2;
+    }
+    if (isPair(arg1)) {
+        if (!MalList.is(arg1) && !MalVector.is(arg1)) {
+            throw new Error(`unexpected token type: ${arg1.type}, expected: list or vector`);
+        }
+        const [arg11, arg12] = arg1.list;
+        if (MalSymbol.is(arg11) && arg11.v === "splice-unquote") {
+            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) {
+        if (!MalList.is(ast) && !MalVector.is(ast)) {
+            return false;
+        }
+
+        return 0 < ast.list.length;
+    }
+}
+
+function evalAST(ast: MalType, env: Env): MalType {
+    switch (ast.type) {
+        case "symbol":
+            const f = env.get(ast);
+            if (!f) {
+                throw new Error(`unknown symbol: ${ast.v}`);
+            }
+            return f;
+        case "list":
+            return new MalList(ast.list.map(ast => evalSexp(ast, env)));
+        case "vector":
+            return new MalVector(ast.list.map(ast => evalSexp(ast, env)));
+        case "hash-map":
+            const list: MalType[] = [];
+            for (const [key, value] of ast.map) {
+                list.push(key);
+                list.push(evalSexp(value, env));
+            }
+            return new MalHashMap(list);
+        default:
+            return ast;
+    }
+}
+
+function evalSexp(ast: MalType, env: Env): MalType {
+    loop: while (true) {
+        if (ast.type !== "list") {
+            return evalAST(ast, env);
+        }
+        if (ast.list.length === 0) {
+            return ast;
+        }
+        const first = ast.list[0];
+        switch (first.type) {
+            case "symbol":
+                switch (first.v) {
+                    case "def!": {
+                        const [, key, value] = ast.list;
+                        if (!MalSymbol.is(key)) {
+                            throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
+                        }
+                        if (!value) {
+                            throw new Error(`unexpected syntax`);
+                        }
+                        return env.set(key, evalSexp(value, env))
+                    }
+                    case "let*": {
+                        env = new Env(env);
+                        const pairs = ast.list[1];
+                        if (!MalList.is(pairs) && !MalVector.is(pairs)) {
+                            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];
+                            if (!MalSymbol.is(key)) {
+                                throw new Error(`unexpected token type: ${key.type}, expected: symbol`);
+                            }
+                            if (!key || !value) {
+                                throw new Error(`unexpected syntax`);
+                            }
+
+                            env.set(key, evalSexp(value, env));
+                        }
+                        ast = ast.list[2];
+                        continue loop;
+                    }
+                    case "quote": {
+                        return ast.list[1];
+                    }
+                    case "quasiquote": {
+                        ast = quasiquote(ast.list[1]);
+                        continue loop;
+                    }
+                    case "do": {
+                        const [, ...list] = ast.list;
+                        const ret = evalAST(new MalList(list), env);
+                        if (!MalList.is(ret) && !MalVector.is(ret)) {
+                            throw new Error(`unexpected return type: ${ret.type}, expected: list or vector`);
+                        }
+                        ast = ret.list[ret.list.length - 1];
+                        continue loop;
+                    }
+                    case "if": {
+                        const [, cond, thenExpr, elseExrp] = ast.list;
+                        const ret = evalSexp(cond, env);
+                        let b = true;
+                        if (MalBoolean.is(ret) && !ret.v) {
+                            b = false;
+                        } else if (MalNull.is(ret)) {
+                            b = false;
+                        }
+                        if (b) {
+                            ast = thenExpr;
+                        } else if (elseExrp) {
+                            ast = elseExrp;
+                        } else {
+                            ast = MalNull.instance;
+                        }
+                        continue loop;
+                    }
+                    case "fn*": {
+                        const [, params, bodyAst] = ast.list;
+                        if (!MalList.is(params) && !MalVector.is(params)) {
+                            throw new Error(`unexpected return type: ${params.type}, expected: list or vector`);
+                        }
+                        const symbols = params.list.map(param => {
+                            if (!MalSymbol.is(param)) {
+                                throw new Error(`unexpected return type: ${param.type}, expected: symbol`);
+                            }
+                            return param;
+                        });
+                        return MalFunction.fromLisp(evalSexp, env, symbols, bodyAst);
+                    }
+                }
+        }
+        const result = evalAST(ast, env);
+        if (!MalList.is(result) && !MalVector.is(result)) {
+            throw new Error(`unexpected return type: ${result.type}, expected: list or vector`);
+        }
+        const [f, ...args] = result.list;
+        if (!MalFunction.is(f)) {
+            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);
+    }
+}
+
+function print(exp: MalType): string {
+    return prStr(exp);
+}
+
+const replEnv = new Env();
+for (const [key, value] of core.ns) {
+    replEnv.set(key, value);
+}
+replEnv.set(MalSymbol.get("eval"), MalFunction.fromBootstrap(ast => {
+    if (!ast) {
+        throw new Error(`undefined argument`);
+    }
+    return evalSexp(ast, replEnv);
+}));
+
+replEnv.set(MalSymbol.get("*ARGV*"), new MalList([]));
+
+// core.mal: defined using the language itself
+rep("(def! not (fn* (a) (if a false true)))");
+rep(`(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))`);
+
+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);
+}
+
+function rep(str: string): string {
+    return print(evalSexp(read(str), replEnv));
+}
+
+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);
+    }
+}