runtime-macro.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 '../language/expression';
  7. // interface Macro {
  8. // readonly argNames: ReadonlyArray<string>,
  9. // readonly argIndex: { [name: string]: number },
  10. // readonly expression: Expression
  11. // }
  12. // namespace Macro {
  13. // export type Table = Map<string, Macro>
  14. // function subst(table: Table, expr: Expression, argIndex: { [name: string]: number }, args: ArrayLike<Expression>): Expression {
  15. // if (Expression.isLiteral(expr)) return expr;
  16. // if (Expression.isSymbol(expr)) {
  17. // const idx = argIndex[expr.name];
  18. // if (typeof idx !== 'undefined') return args[idx];
  19. // if (table.has(expr.name)) {
  20. // const macro = table.get(expr.name)!;
  21. // if (macro.argNames.length === 0) return macro.expression;
  22. // }
  23. // return expr;
  24. // }
  25. // const head = subst(table, expr.head, argIndex, args);
  26. // const headChanged = head !== expr.head;
  27. // if (!expr.args) {
  28. // return headChanged ? Expression.Apply(head) : expr;
  29. // }
  30. // let argsChanged = false;
  31. // if (Expression.isArgumentsArray(expr.args)) {
  32. // let newArgs: Expression[] = [];
  33. // for (let i = 0, _i = expr.args.length; i < _i; i++) {
  34. // const oldArg = expr.args[i];
  35. // const newArg = subst(table, oldArg, argIndex, args);
  36. // if (oldArg !== newArg) argsChanged = true;
  37. // newArgs[newArgs.length] = newArg;
  38. // }
  39. // if (!argsChanged) newArgs = expr.args;
  40. // if (Expression.isSymbol(head) && table.has(head.name)) {
  41. // const macro = table.get(head.name)!;
  42. // if (macro.argNames.length === newArgs.length) {
  43. // return subst(table, macro.expression, macro.argIndex, newArgs);
  44. // }
  45. // }
  46. // if (!headChanged && !argsChanged) return expr;
  47. // return Expression.Apply(head, newArgs);
  48. // } else {
  49. // let newArgs: any = {}
  50. // for (const key of Object.keys(expr.args)) {
  51. // const oldArg = expr.args[key];
  52. // const newArg = subst(table, oldArg, argIndex, args);
  53. // if (oldArg !== newArg) argsChanged = true;
  54. // newArgs[key] = newArg;
  55. // }
  56. // if (!headChanged && !argsChanged) return expr;
  57. // if (!argsChanged) newArgs = expr.args;
  58. // return Expression.Apply(head, newArgs);
  59. // }
  60. // }
  61. // export function substitute(table: Table, macro: Macro, args: ArrayLike<Expression>) {
  62. // if (args.length === 0) return macro.expression;
  63. // return subst(table, macro.expression, macro.argIndex, args);
  64. // }
  65. // }
  66. // export { Macro }