built-in.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. '<include> types/types.js';
  2. '<include> features/features.js';
  3. '<include> functions/functions.js';
  4. '<include> operators.js';
  5. '<include> constants.js';
  6. let global_scope_data = {}
  7. pour(global_scope_data, built_in_types)
  8. pour(global_scope_data, built_in_functions)
  9. pour(global_scope_data, built_in_constants)
  10. let Global = new Scope(null, global_scope_data, true)
  11. let Eval = new Scope(Global)
  12. let global_helpers = {
  13. /* Core */
  14. [CALL]: call,
  15. [OPERATOR]: get_operator,
  16. /* Features */
  17. [FOR_LOOP_ITER]: for_loop_i,
  18. [FOR_LOOP_ENUM]: for_loop_e,
  19. [FOR_LOOP_VALUE]: for_loop_v,
  20. [FOR_LOOP_ASYNC]: for_loop_a,
  21. [GET]: (o, k, nf, f, r, c) => call(get_data, [o, k, nf], f, r, c),
  22. [SET]: set_data,
  23. [SLICE]: (o, lo, hi, f, r, c) => call(get_slice, [o, lo, hi], f, r, c),
  24. [ITER_COMP]: iterator_comprehension,
  25. [LIST_COMP]: list_comprehension,
  26. /* Object Builders */
  27. [C_SINGLETON]: create_value,
  28. [C_CLASS]: inject_desc(create_class, 'create_class'),
  29. [C_INTERFACE]: inject_desc(create_interface, 'create_interface'),
  30. [C_SCHEMA]: inject_desc(create_schema, 'create_schema'),
  31. [C_STRUCT]: inject_desc(new_struct, 'initialize_structure'),
  32. [C_TYPE]: inject_desc(f => $(f), 'create_simple_type'),
  33. [C_TEMPLATE]: inject_desc(f => new TypeTemplate(f), 'create_type_template'),
  34. [C_FINITE]: inject_desc(one_of, 'create_finite_set_type'),
  35. [C_ENUM]: inject_desc((n, ns) => new Enum(n, ns), 'create_enum'),
  36. [C_FUN_SIG]: inject_desc(create_fun_sig, 'create_function_signature'),
  37. [C_TREE_NODE]: inject_desc(inflate_tree_node, 'inflate_tree_node'),
  38. [C_OBSERVER]: create_observer,
  39. /* Guards */
  40. [REQ_BOOL]: inject_desc(require_bool, 'require_boolean_value'),
  41. [REQ_TYPE]: inject_desc(require_type, 'require_type_object'),
  42. [REQ_PROMISE]: inject_desc(require_promise, 'require_promise'),
  43. [WHEN_FAILED]: inject_desc(when_expr_failed, 'when_expr_failed'),
  44. [MATCH_FAILED]: inject_desc(match_expr_failed, 'match_expr_failed'),
  45. [SWITCH_FAILED]: inject_desc(switch_cmd_failed, 'switch_cmd_failed'),
  46. /* Error Handling */
  47. [INJECT_E_ARGS]: inject_desc(inject_ensure_args, 'inject_ensure_args'),
  48. [ENSURE_FAILED]: ensure_failed,
  49. [TRY_FAILED]: try_failed,
  50. [ENTER_H_HOOK]: enter_handle_hook,
  51. [EXIT_H_HOOK]: exit_handle_hook,
  52. [PANIC]: wrapped_panic,
  53. [ASSERT]: wrapped_assert,
  54. [THROW]: wrapped_throw,
  55. /* Types */
  56. [T_ANY]: Types.Any,
  57. [T_BOOL]: Types.Bool,
  58. [T_VOID]: Types.Void,
  59. [T_TYPE]: Types.Type,
  60. [T_HASH]: Types.Hash,
  61. [T_PROMISE]: Types.Promise,
  62. [T_INSTANCE]: Types.Instance,
  63. [T_ITERATOR]: Types.Iterator,
  64. [T_ASYNC_ITERATOR]: Types.AsyncIterator,
  65. [T_SLICE_INDEX_DEF]: Types.SliceIndexDefault,
  66. [T_PLACEHOLDER]: Types.TypePlaceholder
  67. }
  68. Object.freeze(global_helpers)
  69. function bind_method_call (scope) {
  70. return (obj, name, args, file, row, col) => {
  71. return call_method(scope, obj, name, args, file, row, col)
  72. }
  73. }
  74. function bind_wrap (scope) {
  75. return (proto, replace, desc, raw) => {
  76. return wrap(replace || scope, proto, desc, raw)
  77. }
  78. }
  79. function bind_match_pattern (scope) {
  80. return (is_fixed, pattern, value) => {
  81. return match_pattern(scope, is_fixed, pattern, value)
  82. }
  83. }
  84. let get_helpers = scope => ({
  85. [L_METHOD_CALL]: bind_method_call(scope),
  86. [L_STATIC_SCOPE]: f => get_static(f, scope),
  87. [L_WRAP]: bind_wrap(scope),
  88. [L_VAR_LOOKUP]: inject_desc(scope.lookup.bind(scope), 'lookup_variable'),
  89. [L_VAR_DECL]: inject_desc(scope.declare.bind(scope), 'declare_variable'),
  90. [L_VAR_RESET]: inject_desc(scope.reset.bind(scope), 'reset_variable'),
  91. [L_ADD_FUN]: inject_desc(scope.add_function.bind(scope), 'add_function'),
  92. [L_OP_MOUNT]: inject_desc(scope.mount.bind(scope), 'call_mount_operator'),
  93. [L_OP_PUSH]: inject_desc(scope.push.bind(scope), 'call_push_operator'),
  94. [L_IMPORT_VAR]: inject_desc((m, c) => import_names(scope, m, c), 'import'),
  95. [L_IMPORT_MOD]: inject_desc(c => import_module(scope, c), 'import'),
  96. [L_IMPORT_ALL]: inject_desc(m => import_all(scope, m), 'import'),
  97. [L_MATCH]: inject_desc(bind_match_pattern(scope), 'match_pattern'),
  98. [L_GLOBAL_HELPERS]: global_helpers
  99. })