133 lines
3.3 KiB
TypeScript
133 lines
3.3 KiB
TypeScript
// const STR = (s: string): StringValue => ({type: "string", value: s});
|
|
// const FN = (f: FunctionValue["value"]): FunctionValue => ({type: "function", value: f});
|
|
// const SYM = (s: string): SymbolValue => ({type: "symbol", value: s});
|
|
// const UNDEF = (): UndefinedValue => ({type: "undefined", value: null});
|
|
// const CODE = (p: CodeBlockValue["value"]): CodeBlockValue => ({type: "code", value: p});
|
|
// const THUNK = (t: ThunkValue["value"]): ThunkValue => ({type: "thunk", value: t});
|
|
// const BOOL = (b: boolean): BooleanValue => ({type: "boolean", value: b});
|
|
|
|
// console.log("HELLO");
|
|
|
|
// type SymbolValue = {
|
|
// type: "symbol",
|
|
// value: string,
|
|
// }
|
|
|
|
// type FunctionValue = {
|
|
// type: "function",
|
|
// value: (arg: Value, env: Environment, passalong: Passalong) => Result,
|
|
// }
|
|
|
|
// type StringValue = {
|
|
// type: "string",
|
|
// value: string,
|
|
// }
|
|
|
|
// type BooleanValue = {
|
|
// type: "boolean",
|
|
// value: boolean,
|
|
// }
|
|
|
|
// type UndefinedValue = {
|
|
// type: "undefined",
|
|
// value: null,
|
|
// }
|
|
|
|
// type CodeBlockValue = {
|
|
// type: "code",
|
|
// value: Array<Value>,
|
|
// }
|
|
|
|
// type ThunkValue = {
|
|
// type: "thunk",
|
|
// value: Array<Value>,
|
|
// }
|
|
|
|
// type Value =
|
|
// | SymbolValue
|
|
// | FunctionValue
|
|
// | StringValue
|
|
// | UndefinedValue
|
|
// | CodeBlockValue
|
|
// | ThunkValue
|
|
// | BooleanValue
|
|
|
|
// type Passalong = Value;
|
|
|
|
// type Result<V extends Value = Value> = {
|
|
// result: V,
|
|
// passalong: any,
|
|
// }
|
|
|
|
// type Environment = {[key: string]: Value}
|
|
|
|
// const evaluate_codeblock = (codeBlock: CodeBlockValue, env: Environment): Result => {
|
|
// let result: Result = {
|
|
// result: UNDEF(),
|
|
// passalong: UNDEF(),
|
|
// };
|
|
// const terms = [...codeBlock.value];
|
|
// while (terms.length) {
|
|
// console.log('evaluating phrase', terms);
|
|
// result = evaluate_phrase(terms, env, result.passalong);
|
|
// }
|
|
// return result;
|
|
// }
|
|
|
|
// const evaluate_phrase = (terms: Array<Value>, env: Environment, passalong: Passalong): Result => {
|
|
// let head: Result = evaluate_term(terms.shift() as Value, env, passalong);
|
|
// let pa = passalong;
|
|
// while (head.result.type === "function") {
|
|
// if (!terms.length) {
|
|
// throw 'Runtime Error: head function has no argument to be called with';
|
|
// }
|
|
// const tail: Result = evaluate_term(terms.shift() as Value, env, UNDEF());
|
|
// head = head.result.value(tail.result, env, pa);
|
|
// pa = head.passalong;
|
|
// }
|
|
// return head;
|
|
// }
|
|
|
|
// const evaluate_term = (term: Value, env: Environment, passalong: Passalong): Result => {
|
|
// if (term.type === "symbol") {
|
|
// return {
|
|
// result: env[term.value],
|
|
// passalong: UNDEF(), // TODO: this should include term.value as a "name"
|
|
// }
|
|
// } else if (term.type === "string") {
|
|
// return {
|
|
// result: term,
|
|
// passalong: UNDEF(),
|
|
// }
|
|
// } else if (term.type === "function") {
|
|
// return {
|
|
// result: term,
|
|
// passalong: UNDEF(),
|
|
// }
|
|
// } else if (term.type === "boolean") {
|
|
// return {
|
|
// result: term,
|
|
// passalong: UNDEF(),
|
|
// }
|
|
// }
|
|
// return {
|
|
// result: UNDEF(),
|
|
// passalong: UNDEF(),
|
|
// }
|
|
// }
|
|
|
|
// const my_env: Environment = {
|
|
// print: FN((arg, env, passalong) => {
|
|
// console.log(arg);
|
|
// return {
|
|
// result: UNDEF(),
|
|
// passalong: UNDEF(),
|
|
// }
|
|
// })
|
|
// }
|
|
|
|
// const my_prog: CodeBlockValue = CODE([
|
|
// SYM("print"), STR("hi"),
|
|
// ]);
|
|
|
|
// console.log(evaluate_codeblock(my_prog, my_env));
|