TypeScript: step 5
authorvvakame <vvakame+dev@gmail.com>
Fri, 24 Feb 2017 07:30:25 +0000 (16:30 +0900)
committervvakame <vvakame+dev@gmail.com>
Fri, 24 Feb 2017 07:30:25 +0000 (16:30 +0900)
ts/core.ts
ts/package.json
ts/step2_eval.ts
ts/step3_env.ts
ts/step4_if_fn_do.ts
ts/step5_tco.ts [new file with mode: 0644]
ts/types.ts

index 0357863..73c9aa7 100644 (file)
@@ -126,6 +126,6 @@ export const ns: Map<MalSymbol, MalFunction> = (() => {
     };
 
     const map = new Map<MalSymbol, MalFunction>();
-    Object.keys(ns).forEach(key => map.set(MalSymbol.get(key), new MalFunction(ns[key])));
+    Object.keys(ns).forEach(key => map.set(MalSymbol.get(key), MalFunction.fromBootstrap(ns[key])));
     return map;
 })();
index ddf63c3..40ca029 100644 (file)
@@ -5,12 +5,13 @@
   "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",
+    "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",
     "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:step4": "cd .. && make 'test^ts^step4'",
+    "test:step5": "cd .. && make 'test^ts^step5'"
   },
   "dependencies": {
     "ffi": "^2.2.0"
index a22315e..31c6b69 100644 (file)
@@ -44,11 +44,11 @@ function evalSexp(ast: MalType, env: MalEnvironment): MalType {
         return ast;
     }
     const result = evalAST(ast, env) as MalList;
-    const [f, ...rest] = result.list;
+    const [f, ...args] = result.list;
     if (!MalFunction.is(f)) {
         throw new Error(`unexpected token: ${f.type}, expected: function`);
     }
-    return f.func(...rest);
+    return f.func(...args);
 }
 
 function print(exp: MalType): string {
@@ -56,10 +56,10 @@ function print(exp: MalType): string {
 }
 
 const replEnv: MalEnvironment = {
-    "+": new MalFunction((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v + b!.v)),
-    "-": new MalFunction((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v - b!.v)),
-    "*": new MalFunction((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v * b!.v)),
-    "/": new MalFunction((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v / b!.v)),
+    "+": MalFunction.fromBootstrap((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v + b!.v)),
+    "-": MalFunction.fromBootstrap((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v - b!.v)),
+    "*": MalFunction.fromBootstrap((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v * b!.v)),
+    "/": MalFunction.fromBootstrap((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v / b!.v)),
 };
 function rep(str: string): string {
     return print(evalSexp(read(str), replEnv));
index 16a39ea..b129ecb 100644 (file)
@@ -75,11 +75,11 @@ function evalSexp(ast: MalType, env: Env): MalType {
             }
     }
     const result = evalAST(ast, env) as MalList;
-    const [f, ...rest] = result.list;
+    const [f, ...args] = result.list;
     if (!MalFunction.is(f)) {
         throw new Error(`unexpected token: ${f.type}, expected: function`);
     }
-    return f.func(...rest);
+    return f.func(...args);
 }
 
 function print(exp: MalType): string {
@@ -87,10 +87,10 @@ function print(exp: MalType): string {
 }
 
 const replEnv = new Env();
-replEnv.set(MalSymbol.get("+"), new MalFunction((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v + b!.v)));
-replEnv.set(MalSymbol.get("-"), new MalFunction((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v - b!.v)));
-replEnv.set(MalSymbol.get("*"), new MalFunction((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v * b!.v)));
-replEnv.set(MalSymbol.get("/"), new MalFunction((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v / b!.v)));
+replEnv.set(MalSymbol.get("+"), MalFunction.fromBootstrap((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v + b!.v)));
+replEnv.set(MalSymbol.get("-"), MalFunction.fromBootstrap((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v - b!.v)));
+replEnv.set(MalSymbol.get("*"), MalFunction.fromBootstrap((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v * b!.v)));
+replEnv.set(MalSymbol.get("/"), MalFunction.fromBootstrap((a?: MalNumber, b?: MalNumber) => new MalNumber(a!.v / b!.v)));
 
 function rep(str: string): string {
     return print(evalSexp(read(str), replEnv));
index d07884e..86f2fca 100644 (file)
@@ -111,7 +111,7 @@ function evalSexp(ast: MalType, env: Env): MalType {
                         }
                         return arg;
                     });
-                    return new MalFunction((...fnArgs: MalType[]) => {
+                    return MalFunction.fromBootstrap((...fnArgs: MalType[]) => {
                         return evalSexp(binds, new Env(env, symbols, fnArgs));
                     });
                 }
@@ -121,11 +121,11 @@ function evalSexp(ast: MalType, env: Env): MalType {
     if (!MalList.is(result) && !MalVector.is(result)) {
         throw new Error(`unexpected return type: ${result.type}, expected: list or vector`);
     }
-    const [f, ...rest] = result.list;
+    const [f, ...args] = result.list;
     if (!MalFunction.is(f)) {
         throw new Error(`unexpected token: ${f.type}, expected: function`);
     }
-    return f.func(...rest);
+    return f.func(...args);
 }
 
 function print(exp: MalType): string {
diff --git a/ts/step5_tco.ts b/ts/step5_tco.ts
new file mode 100644 (file)
index 0000000..c240d91
--- /dev/null
@@ -0,0 +1,170 @@
+import { readline } from "./node_readline";
+
+import { MalType, 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 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 "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);
+}
+
+// core.mal: defined using the language itself
+rep("(def! not (fn* (a) (if a false true)))");
+
+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);
+    }
+}
index e707be7..99358a3 100644 (file)
@@ -1,3 +1,5 @@
+import { Env } from "./env";
+
 export type MalType = MalList | MalNumber | MalString | MalNull | MalBoolean | MalSymbol | MalKeyword | MalVector | MalHashMap | MalFunction;
 
 export function equals(a: MalType, b: MalType, strict?: boolean): boolean {
@@ -160,12 +162,51 @@ export class MalHashMap {
     }
 }
 
+type MalF = (...args: (MalType | undefined)[]) => MalType;
+
 export class MalFunction {
     static is(f: MalType): f is MalFunction {
         return f instanceof MalFunction;
     }
 
+    static fromLisp(evalSexpr: (ast: MalType, env: Env) => MalType, env: Env, params: MalSymbol[], bodyAst: MalType): MalFunction {
+        const f = new MalFunction();
+        f.func = (...args) => evalSexpr(bodyAst, new Env(env, params, malTypes2malSymbols(args)));
+        f.env = env;
+        f.params = params;
+        f.ast = bodyAst;
+
+        return f;
+
+        function malTypes2malSymbols(args: (MalType | undefined)[]): MalSymbol[] {
+            return args.map(arg => {
+                if (!arg) {
+                    throw new Error(`undefined argument`);
+                }
+                if (!MalSymbol.is(arg)) {
+                    throw new Error(`unexpected token type: ${arg.type}, expected: symbol`);
+                }
+                return arg;
+            });
+        }
+    }
+
+    static fromBootstrap(func: MalF): MalFunction {
+        const f = new MalFunction();
+        f.func = func;
+        return f;
+    }
+
     type: "function" = "function";
-    constructor(public func: (...args: (MalType | undefined)[]) => MalType) {
+
+    func: MalF;
+    ast: MalType;
+    env: Env;
+    params: MalSymbol[];
+
+    private constructor() { }
+
+    newEnv(args: MalType[]) {
+        return new Env(this.env, this.params, args);
     }
 }