|
1 | | -import Core from '../core'; |
| 1 | +import Core from "../core"; |
2 | 2 |
|
3 | | -function _case(condition, clauses){ |
| 3 | +function _case(condition, clauses) { |
4 | 4 | return Core.Patterns.defmatch(...clauses)(condition); |
5 | 5 | } |
6 | 6 |
|
7 | | -function cond(clauses){ |
8 | | - for(let clause of clauses){ |
9 | | - if(clause[0]){ |
| 7 | +function cond(clauses) { |
| 8 | + for (let clause of clauses) { |
| 9 | + if (clause[0]) { |
10 | 10 | return clause[1](); |
11 | 11 | } |
12 | 12 | } |
13 | 13 |
|
14 | 14 | throw new Error(); |
15 | 15 | } |
16 | 16 |
|
17 | | -function map_update(map, values){ |
| 17 | +function map_update(map, values) { |
18 | 18 | return Object.freeze( |
19 | | - Object.assign( |
20 | | - Object.create(map.constructor.prototype), map, values |
21 | | - ) |
| 19 | + Object.assign(Object.create(map.constructor.prototype), map, values) |
22 | 20 | ); |
23 | 21 | } |
24 | 22 |
|
25 | | -function _for(collections, fun, filter = () => true, into = [], previousValues = []){ |
26 | | - let pattern = collections[0][0]; |
27 | | - let collection = collections[0][1]; |
| 23 | +function _for(expression, generators, into = []) { |
| 24 | + const generatedValues = run_list_generators(generators.pop()(), generators); |
28 | 25 |
|
29 | | - if(collections.length === 1){ |
30 | | - if(collection instanceof Core.BitString){ |
31 | | - let bsSlice = collection.slice(0, pattern.byte_size()); |
32 | | - let i = 1; |
| 26 | + let result = into; |
33 | 27 |
|
34 | | - while(bsSlice.byte_size == pattern.byte_size()){ |
35 | | - let r = Core.Patterns.match_or_default(pattern, bsSlice); |
36 | | - let args = previousValues.concat(r); |
37 | | - |
38 | | - if(r && filter.apply(this, args)){ |
39 | | - into = into.concat([fun.apply(this, args)]); |
40 | | - } |
41 | | - |
42 | | - bsSlice = collection.slice(pattern.byte_size() * i, pattern.byte_size() * (i + 1)); |
43 | | - i++; |
44 | | - } |
45 | | - |
46 | | - return into; |
47 | | - }else{ |
48 | | - for(let elem of collection){ |
49 | | - let r = Core.Patterns.match_or_default(pattern, elem); |
50 | | - let args = previousValues.concat(r); |
51 | | - |
52 | | - if(r && filter.apply(this, args)){ |
53 | | - into = into.concat([fun.apply(this, args)]); |
54 | | - } |
55 | | - } |
56 | | - |
57 | | - return into; |
| 28 | + for (let value of generatedValues) { |
| 29 | + if (expression.guard.apply(this, value)) { |
| 30 | + result = result.concat([expression.fn.apply(this, value)]); |
58 | 31 | } |
59 | | - }else{ |
60 | | - let _into = []; |
61 | | - |
62 | | - if(collection instanceof Core.BitString){ |
63 | | - let bsSlice = collection.slice(0, pattern.byte_size()); |
64 | | - let i = 1; |
| 32 | + } |
65 | 33 |
|
66 | | - while(bsSlice.byte_size == pattern.byte_size()){ |
67 | | - let r = Core.Patterns.match_or_default(pattern, bsSlice); |
68 | | - if(r){ |
69 | | - _into = into.concat(this._for(collections.slice(1), fun, filter, _into, previousValues.concat(r))); |
70 | | - } |
| 34 | + return result; |
| 35 | +} |
71 | 36 |
|
72 | | - bsSlice = collection.slice(pattern.byte_size() * i, pattern.byte_size() * (i + 1)); |
73 | | - i++; |
| 37 | +function run_list_generators(generator, generators) { |
| 38 | + if (generators.length == 0) { |
| 39 | + return generator.map(x => { |
| 40 | + if (Array.isArray(x)) { |
| 41 | + return x; |
| 42 | + } else { |
| 43 | + return [x]; |
74 | 44 | } |
75 | | - }else{ |
76 | | - for(let elem of collection){ |
77 | | - let r = Core.Patterns.match_or_default(pattern, elem); |
78 | | - if(r){ |
79 | | - _into = into.concat(this._for(collections.slice(1), fun, filter, _into, previousValues.concat(r))); |
80 | | - } |
| 45 | + }); |
| 46 | + } else { |
| 47 | + const list = generators.pop(); |
| 48 | + |
| 49 | + let next_gen = []; |
| 50 | + for (let j of list()) { |
| 51 | + for (let i of generator) { |
| 52 | + next_gen.push([j].concat(i)); |
81 | 53 | } |
82 | 54 | } |
83 | 55 |
|
84 | | - return _into; |
| 56 | + return run_list_generators(next_gen, generators); |
85 | 57 | } |
86 | 58 | } |
87 | 59 |
|
88 | | -function _try(do_fun, rescue_function, catch_fun, else_function, after_function){ |
| 60 | +function _try( |
| 61 | + do_fun, |
| 62 | + rescue_function, |
| 63 | + catch_fun, |
| 64 | + else_function, |
| 65 | + after_function |
| 66 | +) { |
89 | 67 | let result = null; |
90 | 68 |
|
91 | | - try{ |
| 69 | + try { |
92 | 70 | result = do_fun(); |
93 | | - }catch(e){ |
| 71 | + } catch (e) { |
94 | 72 | let ex_result = null; |
95 | 73 |
|
96 | | - if(rescue_function){ |
97 | | - try{ |
| 74 | + if (rescue_function) { |
| 75 | + try { |
98 | 76 | ex_result = rescue_function(e); |
99 | 77 | return ex_result; |
100 | | - }catch(ex){ |
101 | | - if(ex instanceof Core.Patterns.MatchError){ |
| 78 | + } catch (ex) { |
| 79 | + if (ex instanceof Core.Patterns.MatchError) { |
102 | 80 | throw ex; |
103 | 81 | } |
104 | 82 | } |
105 | 83 | } |
106 | 84 |
|
107 | | - if(catch_fun){ |
108 | | - try{ |
| 85 | + if (catch_fun) { |
| 86 | + try { |
109 | 87 | ex_result = catch_fun(e); |
110 | 88 | return ex_result; |
111 | | - }catch(ex){ |
112 | | - if(ex instanceof Core.Patterns.MatchError){ |
| 89 | + } catch (ex) { |
| 90 | + if (ex instanceof Core.Patterns.MatchError) { |
113 | 91 | throw ex; |
114 | 92 | } |
115 | 93 | } |
116 | 94 | } |
117 | 95 |
|
118 | 96 | throw e; |
119 | | - |
120 | | - }finally{ |
121 | | - if(after_function){ |
| 97 | + } finally { |
| 98 | + if (after_function) { |
122 | 99 | after_function(); |
123 | 100 | } |
124 | 101 | } |
125 | 102 |
|
126 | | - if(else_function){ |
127 | | - try{ |
| 103 | + if (else_function) { |
| 104 | + try { |
128 | 105 | return else_function(result); |
129 | | - }catch(ex){ |
130 | | - if(ex instanceof Core.Patterns.MatchError){ |
131 | | - throw new Error("No Match Found in Else"); |
132 | | - } |
| 106 | + } catch (ex) { |
| 107 | + if (ex instanceof Core.Patterns.MatchError) { |
| 108 | + throw new Error("No Match Found in Else"); |
| 109 | + } |
133 | 110 |
|
134 | 111 | throw ex; |
135 | 112 | } |
136 | | - }else{ |
| 113 | + } else { |
137 | 114 | return result; |
138 | 115 | } |
139 | 116 | } |
140 | 117 |
|
141 | | -function _with(...args){ |
| 118 | +function _with(...args) { |
142 | 119 | let argsToPass = []; |
143 | 120 | let successFunction = null; |
144 | 121 | let elseFunction = null; |
145 | 122 |
|
146 | | - if(typeof(args[args.length - 2]) === 'function'){ |
| 123 | + if (typeof args[args.length - 2] === "function") { |
147 | 124 | [successFunction, elseFunction] = args.splice(-2); |
148 | | - }else{ |
| 125 | + } else { |
149 | 126 | successFunction = args.pop(); |
150 | 127 | } |
151 | 128 |
|
152 | | - for(let i = 0; i < args.length; i++){ |
| 129 | + for (let i = 0; i < args.length; i++) { |
153 | 130 | let [pattern, func] = args[i]; |
154 | 131 |
|
155 | 132 | let result = func.apply(null, argsToPass); |
156 | 133 |
|
157 | 134 | let patternResult = Core.Patterns.match_or_default(pattern, result); |
158 | 135 |
|
159 | | - if(patternResult == null){ |
160 | | - if(elseFunction){ |
| 136 | + if (patternResult == null) { |
| 137 | + if (elseFunction) { |
161 | 138 | return elseFunction.call(null, result); |
162 | | - }else{ |
| 139 | + } else { |
163 | 140 | return result; |
164 | 141 | } |
165 | | - }else{ |
| 142 | + } else { |
166 | 143 | argsToPass = argsToPass.concat(patternResult); |
167 | 144 | } |
168 | 145 | } |
|
0 commit comments