mirror of
https://github.com/kanaka/mal.git
synced 2024-11-10 12:47:45 +03:00
TypeScript: step 4
This commit is contained in:
parent
86fa880669
commit
dfe70453b4
131
ts/core.ts
131
ts/core.ts
@ -0,0 +1,131 @@
|
||||
import { MalType, MalSymbol, MalFunction, MalNull, MalList, MalVector, MalBoolean, MalNumber, MalString, equals } from "./types";
|
||||
import { prStr } from "./printer";
|
||||
|
||||
export const ns: Map<MalSymbol, MalFunction> = (() => {
|
||||
const ns: { [symbol: string]: typeof MalFunction.prototype.func; } = {
|
||||
"pr-str"(...args: MalType[]): MalString {
|
||||
return new MalString(args.map(v => prStr(v, true)).join(" "));
|
||||
},
|
||||
"str"(...args: MalType[]): MalString {
|
||||
return new MalString(args.map(v => prStr(v, false)).join(""));
|
||||
},
|
||||
prn(...args: MalType[]): MalNull {
|
||||
const str = args.map(v => prStr(v, true)).join(" ");
|
||||
console.log(str);
|
||||
return MalNull.instance;
|
||||
},
|
||||
println(...args: MalType[]): MalNull {
|
||||
const str = args.map(v => prStr(v, false)).join(" ");
|
||||
console.log(str);
|
||||
return MalNull.instance;
|
||||
},
|
||||
list(...args: MalType[]): MalList {
|
||||
return new MalList(args);
|
||||
},
|
||||
"list?"(v: MalType): MalBoolean {
|
||||
return new MalBoolean(v instanceof MalList);
|
||||
},
|
||||
"empty?"(v: MalType): MalBoolean {
|
||||
if (!MalList.is(v) && !MalVector.is(v)) {
|
||||
return new MalBoolean(false);
|
||||
}
|
||||
return new MalBoolean(v.list.length === 0);
|
||||
},
|
||||
count(v: MalType): MalNumber {
|
||||
if (MalList.is(v) || MalVector.is(v)) {
|
||||
return new MalNumber(v.list.length);
|
||||
}
|
||||
if (MalNull.is(v)) {
|
||||
return new MalNumber(0);
|
||||
}
|
||||
throw new Error(`unexpected symbol: ${v.type}`);
|
||||
},
|
||||
"+"(a: MalType, b: MalType): MalNumber {
|
||||
if (!MalNumber.is(a)) {
|
||||
throw new Error(`unexpected symbol: ${a.type}, expected: number`);
|
||||
}
|
||||
if (!MalNumber.is(b)) {
|
||||
throw new Error(`unexpected symbol: ${b.type}, expected: number`);
|
||||
}
|
||||
|
||||
return new MalNumber(a.v + b.v);
|
||||
},
|
||||
"-"(a: MalType, b: MalType): MalNumber {
|
||||
if (!MalNumber.is(a)) {
|
||||
throw new Error(`unexpected symbol: ${a.type}, expected: number`);
|
||||
}
|
||||
if (!MalNumber.is(b)) {
|
||||
throw new Error(`unexpected symbol: ${b.type}, expected: number`);
|
||||
}
|
||||
|
||||
return new MalNumber(a.v - b.v);
|
||||
},
|
||||
"*"(a: MalType, b: MalType): MalNumber {
|
||||
if (!MalNumber.is(a)) {
|
||||
throw new Error(`unexpected symbol: ${a.type}, expected: number`);
|
||||
}
|
||||
if (!MalNumber.is(b)) {
|
||||
throw new Error(`unexpected symbol: ${b.type}, expected: number`);
|
||||
}
|
||||
|
||||
return new MalNumber(a.v * b.v);
|
||||
},
|
||||
"/"(a: MalType, b: MalType): MalNumber {
|
||||
if (!MalNumber.is(a)) {
|
||||
throw new Error(`unexpected symbol: ${a.type}, expected: number`);
|
||||
}
|
||||
if (!MalNumber.is(b)) {
|
||||
throw new Error(`unexpected symbol: ${b.type}, expected: number`);
|
||||
}
|
||||
|
||||
return new MalNumber(a.v / b.v);
|
||||
},
|
||||
"="(a: MalType, b: MalType): MalBoolean {
|
||||
return new MalBoolean(equals(a, b));
|
||||
},
|
||||
"<"(a: MalType, b: MalType): MalBoolean {
|
||||
if (!MalNumber.is(a)) {
|
||||
throw new Error(`unexpected symbol: ${a.type}, expected: number`);
|
||||
}
|
||||
if (!MalNumber.is(b)) {
|
||||
throw new Error(`unexpected symbol: ${b.type}, expected: number`);
|
||||
}
|
||||
|
||||
return new MalBoolean(a.v < b.v);
|
||||
},
|
||||
"<="(a: MalType, b: MalType): MalBoolean {
|
||||
if (!MalNumber.is(a)) {
|
||||
throw new Error(`unexpected symbol: ${a.type}, expected: number`);
|
||||
}
|
||||
if (!MalNumber.is(b)) {
|
||||
throw new Error(`unexpected symbol: ${b.type}, expected: number`);
|
||||
}
|
||||
|
||||
return new MalBoolean(a.v <= b.v);
|
||||
},
|
||||
">"(a: MalType, b: MalType): MalBoolean {
|
||||
if (!MalNumber.is(a)) {
|
||||
throw new Error(`unexpected symbol: ${a.type}, expected: number`);
|
||||
}
|
||||
if (!MalNumber.is(b)) {
|
||||
throw new Error(`unexpected symbol: ${b.type}, expected: number`);
|
||||
}
|
||||
|
||||
return new MalBoolean(a.v > b.v);
|
||||
},
|
||||
">="(a: MalType, b: MalType): MalBoolean {
|
||||
if (!MalNumber.is(a)) {
|
||||
throw new Error(`unexpected symbol: ${a.type}, expected: number`);
|
||||
}
|
||||
if (!MalNumber.is(b)) {
|
||||
throw new Error(`unexpected symbol: ${b.type}, expected: number`);
|
||||
}
|
||||
|
||||
return new MalBoolean(a.v >= b.v);
|
||||
},
|
||||
};
|
||||
|
||||
const map = new Map<MalSymbol, MalFunction>();
|
||||
Object.keys(ns).forEach(key => map.set(MalSymbol.get(key), new MalFunction(ns[key])));
|
||||
return map;
|
||||
})();
|
13
ts/env.ts
13
ts/env.ts
@ -1,10 +1,19 @@
|
||||
import { MalType, MalSymbol } from "./types";
|
||||
import { MalType, MalSymbol, MalList } from "./types";
|
||||
|
||||
export class Env {
|
||||
data: Map<MalSymbol, MalType>;
|
||||
|
||||
constructor(public outer?: Env) {
|
||||
constructor(public outer?: Env, binds: MalSymbol[] = [], exprts: MalType[] = []) {
|
||||
this.data = new Map();
|
||||
|
||||
for (let i = 0; i < binds.length; i++) {
|
||||
const bind = binds[i];
|
||||
if (bind.v === "&") {
|
||||
this.set(binds[i + 1], new MalList(exprts.slice(i)));
|
||||
break;
|
||||
}
|
||||
this.set(bind, exprts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
set(key: MalSymbol, value: MalType): MalType {
|
||||
|
@ -5,11 +5,12 @@
|
||||
"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",
|
||||
"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: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:step3": "cd .. && make 'test^ts^step3'",
|
||||
"test:step4": "cd .. && make 'test^ts^step4'"
|
||||
},
|
||||
"dependencies": {
|
||||
"ffi": "^2.2.0"
|
||||
|
@ -3,16 +3,16 @@ import { MalType } from "./types";
|
||||
export function prStr(v: MalType, printReadably = true): string {
|
||||
switch (v.type) {
|
||||
case "list":
|
||||
return `(${v.list.map(v => prStr(v)).join(" ")})`;
|
||||
return `(${v.list.map(v => prStr(v, printReadably)).join(" ")})`;
|
||||
case "vector":
|
||||
return `[${v.list.map(v => prStr(v)).join(" ")}]`;
|
||||
return `[${v.list.map(v => prStr(v, printReadably)).join(" ")}]`;
|
||||
case "hash-map":
|
||||
let result = "{";
|
||||
for (const [key, value] of v.map) {
|
||||
if (result !== "{") {
|
||||
result += " ";
|
||||
}
|
||||
result += `${prStr(key)} ${prStr(value)}`;
|
||||
result += `${prStr(key, printReadably)} ${prStr(value, printReadably)}`;
|
||||
}
|
||||
result += "}";
|
||||
return result;
|
||||
@ -35,6 +35,6 @@ export function prStr(v: MalType, printReadably = true): string {
|
||||
case "keyword":
|
||||
return `:${v.v.substr(1)}`;
|
||||
case "function":
|
||||
throw new Error(`invalid state`);
|
||||
return "#<function>";
|
||||
}
|
||||
}
|
||||
|
@ -134,7 +134,7 @@ function readAtom(reader: Reader): MalType {
|
||||
}
|
||||
switch (token) {
|
||||
case "nil":
|
||||
return new MalNull();
|
||||
return MalNull.instance;
|
||||
case "true":
|
||||
return new MalBoolean(true);
|
||||
case "false":
|
||||
|
@ -45,7 +45,7 @@ function evalSexp(ast: MalType, env: MalEnvironment): MalType {
|
||||
}
|
||||
const result = evalAST(ast, env) as MalList;
|
||||
const [f, ...rest] = result.list;
|
||||
if (!MalFunction.instanceOf(f)) {
|
||||
if (!MalFunction.is(f)) {
|
||||
throw new Error(`unexpected token: ${f.type}, expected: function`);
|
||||
}
|
||||
return f.func(...rest);
|
||||
|
@ -76,7 +76,7 @@ function evalSexp(ast: MalType, env: Env): MalType {
|
||||
}
|
||||
const result = evalAST(ast, env) as MalList;
|
||||
const [f, ...rest] = result.list;
|
||||
if (!MalFunction.instanceOf(f)) {
|
||||
if (!MalFunction.is(f)) {
|
||||
throw new Error(`unexpected token: ${f.type}, expected: function`);
|
||||
}
|
||||
return f.func(...rest);
|
||||
|
161
ts/step4_if_fn_do.ts
Normal file
161
ts/step4_if_fn_do.ts
Normal file
@ -0,0 +1,161 @@
|
||||
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 {
|
||||
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*": {
|
||||
let letEnv = 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`);
|
||||
}
|
||||
|
||||
letEnv.set(key, evalSexp(value, letEnv));
|
||||
}
|
||||
return evalSexp(ast.list[2], letEnv);
|
||||
}
|
||||
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`);
|
||||
}
|
||||
return ret.list[ret.list.length - 1];
|
||||
}
|
||||
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) {
|
||||
return evalSexp(thenExpr, env);
|
||||
} else if (elseExrp) {
|
||||
return evalSexp(elseExrp, env);
|
||||
} else {
|
||||
return MalNull.instance;
|
||||
}
|
||||
}
|
||||
case "fn*": {
|
||||
const [, args, binds] = ast.list;
|
||||
if (!MalList.is(args) && !MalVector.is(args)) {
|
||||
throw new Error(`unexpected return type: ${args.type}, expected: list or vector`);
|
||||
}
|
||||
const symbols = args.list.map(arg => {
|
||||
if (!MalSymbol.is(arg)) {
|
||||
throw new Error(`unexpected return type: ${arg.type}, expected: symbol`);
|
||||
}
|
||||
return arg;
|
||||
});
|
||||
return new MalFunction((...fnArgs: MalType[]) => {
|
||||
return evalSexp(binds, new Env(env, symbols, fnArgs));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
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, ...rest] = result.list;
|
||||
if (!MalFunction.is(f)) {
|
||||
throw new Error(`unexpected token: ${f.type}, expected: function`);
|
||||
}
|
||||
return f.func(...rest);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
85
ts/types.ts
85
ts/types.ts
@ -1,6 +1,54 @@
|
||||
export type MalType = MalList | MalNumber | MalString | MalNull | MalBoolean | MalSymbol | MalKeyword | MalVector | MalHashMap | MalFunction;
|
||||
|
||||
export function equals(a: MalType, b: MalType, strict?: boolean): boolean {
|
||||
if (strict && a.constructor !== b.constructor) {
|
||||
return false;
|
||||
} else if (
|
||||
(MalList.is(a) || MalVector.is(a))
|
||||
&& (MalList.is(b) || MalVector.is(b))
|
||||
) {
|
||||
return listEquals(a.list, b.list);
|
||||
}
|
||||
|
||||
if (MalNull.is(a) && MalNull.is(b)) {
|
||||
return true;
|
||||
}
|
||||
if (
|
||||
(MalList.is(a) && MalList.is(b))
|
||||
|| (MalVector.is(a) && MalVector.is(b))
|
||||
) {
|
||||
return listEquals(a.list, b.list);
|
||||
}
|
||||
if (
|
||||
(MalNumber.is(a) && MalNumber.is(b))
|
||||
|| (MalString.is(a) && MalString.is(b))
|
||||
|| (MalBoolean.is(a) && MalBoolean.is(b))
|
||||
|| (MalSymbol.is(a) && MalSymbol.is(b))
|
||||
|| (MalKeyword.is(a) && MalKeyword.is(b))
|
||||
) {
|
||||
return a.v === b.v;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
function listEquals(a: MalType[], b: MalType[]): boolean {
|
||||
if (a.length !== b.length) {
|
||||
return false;
|
||||
}
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
if (!equals(a[i], b[i], strict)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export class MalList {
|
||||
static is(f: MalType): f is MalList {
|
||||
return f instanceof MalList;
|
||||
}
|
||||
|
||||
type: "list" = "list";
|
||||
|
||||
constructor(public list: MalType[]) {
|
||||
@ -8,28 +56,51 @@ export class MalList {
|
||||
}
|
||||
|
||||
export class MalNumber {
|
||||
static is(f: MalType): f is MalNumber {
|
||||
return f instanceof MalNumber;
|
||||
}
|
||||
|
||||
type: "number" = "number";
|
||||
constructor(public v: number) {
|
||||
}
|
||||
}
|
||||
|
||||
export class MalString {
|
||||
static is(f: MalType): f is MalString {
|
||||
return f instanceof MalString;
|
||||
}
|
||||
|
||||
type: "string" = "string";
|
||||
constructor(public v: string) {
|
||||
}
|
||||
}
|
||||
|
||||
export class MalNull {
|
||||
static is(f: MalType): f is MalNull {
|
||||
return f instanceof MalNull;
|
||||
}
|
||||
|
||||
static instance = new MalNull();
|
||||
type: "null" = "null";
|
||||
|
||||
private constructor() { }
|
||||
}
|
||||
|
||||
export class MalBoolean {
|
||||
static is(f: MalType): f is MalBoolean {
|
||||
return f instanceof MalBoolean;
|
||||
}
|
||||
|
||||
type: "boolean" = "boolean";
|
||||
constructor(public v: boolean) {
|
||||
}
|
||||
}
|
||||
|
||||
export class MalSymbol {
|
||||
static is(f: MalType): f is MalSymbol {
|
||||
return f instanceof MalSymbol;
|
||||
}
|
||||
|
||||
static map = new Map<symbol, MalSymbol>();
|
||||
|
||||
static get(name: string): MalSymbol {
|
||||
@ -50,6 +121,10 @@ export class MalSymbol {
|
||||
}
|
||||
|
||||
export class MalKeyword {
|
||||
static is(f: MalType): f is MalKeyword {
|
||||
return f instanceof MalKeyword;
|
||||
}
|
||||
|
||||
type: "keyword" = "keyword";
|
||||
constructor(public v: string) {
|
||||
this.v = String.fromCodePoint(0x29E) + this.v;
|
||||
@ -57,12 +132,20 @@ export class MalKeyword {
|
||||
}
|
||||
|
||||
export class MalVector {
|
||||
static is(f: MalType): f is MalVector {
|
||||
return f instanceof MalVector;
|
||||
}
|
||||
|
||||
type: "vector" = "vector";
|
||||
constructor(public list: MalType[]) {
|
||||
}
|
||||
}
|
||||
|
||||
export class MalHashMap {
|
||||
static is(f: MalType): f is MalHashMap {
|
||||
return f instanceof MalHashMap;
|
||||
}
|
||||
|
||||
type: "hash-map" = "hash-map";
|
||||
map = new Map<MalType, MalType>();
|
||||
constructor(list: MalType[]) {
|
||||
@ -78,7 +161,7 @@ export class MalHashMap {
|
||||
}
|
||||
|
||||
export class MalFunction {
|
||||
static instanceOf(f: MalType): f is MalFunction {
|
||||
static is(f: MalType): f is MalFunction {
|
||||
return f instanceof MalFunction;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user