-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathspecial_forms.js
More file actions
150 lines (124 loc) · 2.87 KB
/
Copy pathspecial_forms.js
File metadata and controls
150 lines (124 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import Core from '../core';
async function _case(condition, clauses) {
return Core.Patterns.defmatch(...clauses)(condition);
}
async function cond(clauses) {
for (const clause of clauses) {
if (clause[0]) {
return clause[1]();
}
}
throw new Error();
}
function _for(expression, generators, collectable_protocol, into = []) {
let [result, fun] = collectable_protocol.into(into);
const generatedValues = run_list_generators(generators.pop()(), generators);
for (const value of generatedValues) {
if (expression.guard.apply(this, value)) {
result = fun(
result,
new Core.Tuple(Symbol.for('cont'), expression.fn.apply(this, value)),
);
}
}
return fun(result, Symbol.for('done'));
}
async function run_list_generators(generator, generators) {
if (generators.length == 0) {
return generator.map(x => {
if (Array.isArray(x)) {
return x;
}
return [x];
});
}
const list = generators.pop();
const next_gen = [];
for (const j of list()) {
for (const i of generator) {
next_gen.push([j].concat(i));
}
}
return run_list_generators(next_gen, generators);
}
async function _try(
do_fun,
rescue_function,
catch_fun,
else_function,
after_function,
) {
let result = null;
try {
result = do_fun();
} catch (e) {
let ex_result = null;
if (rescue_function) {
try {
ex_result = rescue_function(e);
return ex_result;
} catch (ex) {
if (ex instanceof Core.Patterns.MatchError) {
throw ex;
}
}
}
if (catch_fun) {
try {
ex_result = catch_fun(e);
return ex_result;
} catch (ex) {
if (ex instanceof Core.Patterns.MatchError) {
throw ex;
}
}
}
throw e;
} finally {
if (after_function) {
after_function();
}
}
if (else_function) {
try {
return else_function(result);
} catch (ex) {
if (ex instanceof Core.Patterns.MatchError) {
throw new Error('No Match Found in Else');
}
throw ex;
}
} else {
return result;
}
}
async function _with(...args) {
let argsToPass = [];
let successFunction = null;
let elseFunction = null;
if (typeof args[args.length - 2] === 'function') {
[successFunction, elseFunction] = args.splice(-2);
} else {
successFunction = args.pop();
}
for (let i = 0; i < args.length; i++) {
const [pattern, func] = args[i];
const result = func(...argsToPass);
const patternResult = Core.Patterns.match_or_default(pattern, result);
if (patternResult == null) {
if (elseFunction) {
return elseFunction.call(null, result);
}
return result;
}
argsToPass = argsToPass.concat(patternResult);
}
return successFunction(...argsToPass);
}
export default {
_case,
cond,
_for,
_try,
_with,
};