forked from akeep/scheme-to-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprelude.js
184 lines (144 loc) · 3.64 KB
/
prelude.js
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
function voidf(k) { return k(undefined); };
function pk() {
console.log.apply(console, arguments);
return arguments[arguments.length - 1];
}
function trampoline(thunk) {
// console.log('trampoline start', thunk)
while (thunk && typeof thunk === "function") {
// console.log('boing');
thunk = thunk();
}
return thunk
}
function prepend(v, a) {
// a.slice().unshift(0); // avoid mutation with copy
a.unshift(v);
return a;
}
function shift(a) {
return a.shift();
}
function apply(func, args) {
// apply FUNC to ARGS
return func.apply(undefined, args);
}
function returnk(k) { return k; };
function wrap(v) { return function(k) { return k(v); } ;};
function unwrap(k) { return k(returnk) };
function apply2(func, args) {
// unwrap ARGS and apply FUNC
args = args.map(unwrap);
return func.apply(undefined, args);
}
/* primitives */
function add(a, b) {
return a + b;
}
function times(a, b) {
return a * b;
}
/* testing javascript-procedure and javascript-callable */
function frob(a, b, c) {
return a + b + c;
}
function frob2(func) {
out = func(42, 42);
return out;
}
/* others */
let EMPTY_LIST = {type: 'empty list'};
let RUSE_EMPTY_LIST = function(k) { return k(EMPTY_LIST); };
function assume(v, message) {
console.assert(v, message);
}
/* symbols */
let SYMBOLS = {};
function ruse_symbol_get_or_create(string) {
let out = SYMBOLS[string];
if (out === undefined) {
out = {type: "symbol", value: string};
SYMBOLS[string] = out;
}
return out;
}
/* list */
ruse_cons_record = {
type: "record",
subtype: "<cons>",
fields: {car: 0, cdr: 1},
}
function ruse_cons(a, b) {
console.assert(b !== undefined);
let instance = {type: ruse_cons_record, fields: [a, b]};
return instance;
}
function ruse_arguments_to_list(args) {
args = Array.prototype.slice.call(args);
let k = shift(args)
args.reverse();
let out = EMPTY_LIST;
for(i in args) {
let value = unwrap(args[i]);
out = ruse_cons(value, out);
}
return wrap(out);
}
function ruse_cons_star() {
let args = Array.prototype.slice.call(arguments);
// args.shift();
args.reverse()
let out = args.shift();
for (k in args) {
out = ruse_cons(args[k], out)
}
return out;
}
/* define-record-type helpers */
function ruse_make_record_type(name) {
if (name.value == '<cons>') {
return ruse_cons_record;
}
let tags = Array.prototype.slice.call(arguments);
shift(tags);
let fields = {};
for(let i in tags){
fields[tags[i].value] = i;
}
return {type: "record", subtype: name.value, fields: fields};
}
function ruse_record_constructor(type) {
return function (k) {
let args = Array.prototype.slice.call(arguments);
shift(args)
// TODO: check correct number of args
args = args.map(unwrap);
let instance = {type: type, fields: args};
return k(instance);
}
}
function ruse_record_predicate(type) {
return function(k, obj) {
obj = unwrap(obj).type
return k(obj === type);
}
}
function ruse_record_modifier(type, name) {
let index = type.fields[name.value];
return function(k, instance, value) {
instance = unwrap(instance);
value = unwrap(value);
instance.fields[index] = value;
return k(voidf);
}
}
function ruse_record_accessor(type, name) {
let index = type.fields[name.value];
return function(k, instance) {
instance = unwrap(instance);
let v = instance.fields[index];
return k(v);
}
}
/* program */
let program =