compiler.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // /**
  2. // * Copyright (c) 2018 Mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. // *
  4. // * @author David Sehnal <david.sehnal@gmail.com>
  5. // */
  6. // import Expression from './expression'
  7. // import Environment from './runtime/environment'
  8. // import RuntimeExpression from './runtime/expression'
  9. // import SymbolRuntime, { RuntimeArguments } from './runtime/symbol'
  10. // export type CompiledExpression<T> = () => T
  11. // type Compiler = <T>(expression: Expression) => CompiledExpression<T>
  12. // function Compiler<C>(env: Environment): Compiler {
  13. // return expression => compile(env, expression).runtime;
  14. // }
  15. // type CompileResult = { isConst: boolean, runtime: RuntimeExpression }
  16. // namespace CompileResult {
  17. // export function Const(value: any): CompileResult { return { isConst: true, runtime: RuntimeExpression.constant(value) } }
  18. // export function Dynamic(runtime: RuntimeExpression): CompileResult { return { isConst: false, runtime } }
  19. // }
  20. // function wrap<T>(envProvider: (ctx?: C) => Environment, runtime: RuntimeExpression<T>) {
  21. // return (ctx: C) => runtime(envProvider(ctx));
  22. // }
  23. // function noRuntimeFor(symbol: string) {
  24. // throw new Error(`Could not find runtime for symbol '${symbol}'.`);
  25. // }
  26. // function applySymbolStatic(runtime: SymbolRuntime, args: RuntimeArguments) {
  27. // return CompileResult.Dynamic(env => runtime(env, args))
  28. // }
  29. // function applySymbolDynamic(head: RuntimeExpression, args: RuntimeArguments) {
  30. // return CompileResult.Dynamic(env => {
  31. // const value = head(env);
  32. // const symbol = env.runtimeTable[value];
  33. // if (!symbol) noRuntimeFor(value);
  34. // return symbol.runtime(env, args);
  35. // })
  36. // }
  37. // function apply(env: Environment, head: CompileResult, args: RuntimeArguments, constArgs: boolean): CompileResult {
  38. // if (head.isConst) {
  39. // const value = head.runtime(env);
  40. // const symbol = env.runtimeTable[value];
  41. // if (!symbol) throw new Error(`Could not find runtime for symbol '${value}'.`);
  42. // if (symbol.attributes.isStatic && constArgs) return CompileResult.Const(symbol.runtime(env, args));
  43. // return applySymbolStatic(symbol.runtime, args);
  44. // }
  45. // return applySymbolDynamic(head.runtime, args);
  46. // }
  47. // function compile(env: Environment, expression: Expression): CompileResult {
  48. // if (Expression.isLiteral(expression)) {
  49. // return CompileResult.Const(expression);
  50. // }
  51. // if (Expression.isSymbol(expression)) {
  52. // // TOTO: this needs to look up in the symbol table.
  53. // return 0 as any;
  54. // }
  55. // const head = compile(env, expression.head);
  56. // if (!expression.args) {
  57. // return apply(env, head, [], true);
  58. // } else if (Expression.isArgumentsArray(expression.args)) {
  59. // const args = [];
  60. // let constArgs = false;
  61. // for (const arg of expression.args) {
  62. // const compiled = compile(env, arg);
  63. // constArgs = constArgs && compiled.isConst;
  64. // args.push(compiled.runtime);
  65. // }
  66. // return apply(env, head, args as any, constArgs);
  67. // } else {
  68. // const args = Object.create(null);
  69. // let constArgs = false;
  70. // for (const key of Object.keys(expression.args)) {
  71. // const compiled = compile(env, expression.args[key]);
  72. // constArgs = constArgs && compiled.isConst;
  73. // args[key] = compiled.runtime;
  74. // }
  75. // return apply(env, head, args, constArgs);
  76. // }
  77. // }
  78. // export default Compiler