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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use memory::Unit;
use value::Value;
use ::{read, hash_map, hash_set, vector, nil};
use ::{wasm, right_into};
use super::get_statics;
#[derive(Debug)]
pub struct Func {
pub argc: u32,
pub local_slots: Vec<u8>,
pub captured: Value,
pub current_depth: u32,
pub code_bytes: Vec<u8>,
pub code_log: Vec<String>,
pub loop_point: u32,
pub recur_targets: Vec<u32>,
pub loop_terminals: Vec<u32>,
pub try_point: u32,
pub try_pinned: Value,
}
#[derive(Debug)]
pub struct Context {
pub signatures: Value,
pub import_v: Value,
pub imports: Value,
pub global_v: Value,
pub globals: Value,
pub constant_data: Vec<u8>,
pub vtable: Value,
pub funcs: Vec<Func>,
pub curr_func: usize,
}
pub fn show_context(ctx: &Context) {
}
pub fn compile_top_level(form: &Value) -> Box<Context> {
let mut ctx = Box::new(Context {
signatures: Default::default(),
import_v: vector(),
imports: hash_map(),
global_v: vector().conj(nil()).conj(nil()),
globals: hash_map(),
constant_data: vec![],
vtable: vector(),
funcs: vec![Func {
argc: 0, captured: nil(), local_slots: vec![], code_bytes: vec![], code_log: vec![],
current_depth: 0,
loop_point: 0, recur_targets: vec![], loop_terminals: vec![],
try_point: 0, try_pinned: nil(),
}, Func {
argc: 0, captured: nil(), local_slots: vec![], code_bytes: vec![], code_log: vec![],
current_depth: 0,
loop_point: 0, recur_targets: vec![], loop_terminals: vec![],
try_point: 0, try_pinned: nil(),
}],
curr_func: 1,
});
let res = {
let m = hash_map();
let s = hash_set();
let v = vector();
comp(&mut ctx, form, &m, &s, &s, &v, 0)
};
let value_sym = read("value ");
if res != value_sym {
unimplemented!();
}
let init_f: &mut Func = ctx.funcs.get_mut(0).unwrap();
init_f.code_bytes.push(wasm::Op::I32_CONST);
init_f.code_bytes.push(0u8);
init_f.code_bytes.push(wasm::Op::END);
let top_f: &mut Func = ctx.funcs.get_mut(1).unwrap();
top_f.code_bytes.push(wasm::Op::END);
ctx
}
pub fn register_import(ctx: &mut Context, description: &Value) -> u32 {
let name_k = read(":name ");
let name = description.get(&name_k);
let idx = ctx.imports.get(name).split_out();
if idx.is_integral() {
idx.as_i64() as u32
} else {
let ct = ctx.import_v.count();
use std::mem::replace;
let iv = replace(&mut ctx.import_v, nil());
ctx.import_v = iv.conj(description.split_out());
let i = replace(&mut ctx.imports, nil());
ctx.imports = i.assoc(name.split_out(), ct.into());
ct
}
}
pub fn comp(ctx: &mut Context, form: &Value, named: &Value, terminal: &Value,
dormant: &Value, vacant: &Value, next_local: u32) -> Value {
if form.is_symbol() {
if form.has_namespace() {
unimplemented!()
} else {
if named.contains(form) {
if terminal.contains(form) {
let f: &mut Func = ctx.funcs.get_mut(ctx.curr_func).unwrap();
f.code_bytes.push(wasm::Op::LOCAL);
wasm::uleb128(&mut f.code_bytes, named.get(form).as_i64() as u64);
} else {
}
} else {
let f: &mut Func = ctx.funcs.get_mut(ctx.curr_func).unwrap();
let capture_idx = f.captured.get(form).as_i64();
}
unimplemented!()
}
}
if form.is_list() {
let first = form.peek();
if first.is_symbol() {
let s = format!("{}", first);
if &s == "def" { return comp_def(ctx, form, named, terminal, dormant, vacant, next_local) }
if &s == "fn" { return comp_fn(ctx, form, named, terminal, dormant, vacant, next_local) }
if &s == "if" { return comp_if(ctx, form, named, terminal, dormant, vacant, next_local) }
if &s == "do" { return comp_do(ctx, form, named, terminal, dormant, vacant, next_local) }
if &s == "let" { return comp_let(ctx, form, named, terminal, dormant, vacant, next_local) }
if &s == "loop" { return comp_loop(ctx, form, named, terminal, dormant, vacant, next_local) }
if &s == "recur" { return comp_recur(ctx, form, named, terminal, dormant, vacant, next_local) }
if &s == "quote" { return comp_recur(ctx, form, named, terminal, dormant, vacant, next_local) }
}
return comp_call(ctx, form, named, terminal, dormant, vacant, next_local)
}
if form.is_vector() { return comp_vector(ctx, form, named, terminal, dormant, vacant, next_local) }
if form.is_set() { unimplemented!() }
if form.is_map() { unimplemented!() }
if form.is_integral() { return comp_integral(ctx, form, named, terminal, dormant, vacant, next_local) }
unimplemented!("Compile expression {}", form)
}
pub fn comp_def(ctx: &mut Context, form: &Value,
named: &Value, terminal: &Value, dormant: &Value, vacant: &Value, next_local: u32) -> Value {
let x = comp(ctx, form.nth(2), named, terminal, dormant, vacant, next_local);
unimplemented!()
}
pub fn sym_to_idx(v: &Value) -> Value {
let mut to_idx = hash_map();
let ct = v.count();
for i in 0..ct {
to_idx = to_idx.assoc(v.nth(i).split_out(), i.into());
}
to_idx
}
pub fn comp_fn(ctx: &mut Context, form: &Value,
named: &Value, terminal: &Value, dormant: &Value, vacant: &Value, next_local: u32) -> Value {
let captures_vector = form.meta().get(&get_statics().captures).split_out();
let capture_map = {
let capture_map = sym_to_idx(&captures_vector);
if form.nth(1).is_symbol() {
capture_map.assoc(form.nth(1).split_out(), Value::from(-1))
} else {
capture_map
}
};
let b = "{:name [\"fress\" \"new_func\"]\
:args [i32 i32 i32]\
:ret i32}";
let c = "{:name [\"fress\" \"add_capture\"]\
:args [i32 i32]\
:ret nil}";
let new_int = read(b);
let idx = register_import(ctx, &new_int);
unimplemented!()
}
pub fn comp_if(ctx: &mut Context, form: &Value,
named: &Value, terminal: &Value, dormant: &Value, vacant: &Value, next_local: u32) -> Value {
let locals_used = form.meta().get(&get_statics().local_use).split_out();
unimplemented!()
}
pub fn count_down(terminals_used: &mut Value, subform: &Value) -> Value {
unimplemented!()
}
pub fn comp_body(ctx: &mut Context, form: &Value, named: &Value, terminals_used: &Value,
dormant: &Value, vacant: &Value, next_local: u32) -> Value {
let mut terminals_used = terminals_used.split_out();
let mut vac = vacant.split_out();
let mut nam = named.split_out();
let ct = form.count();
for i in 0..ct {
let subform_terms = count_down(&mut terminals_used, form.nth(i));
let on_stack = comp(ctx, form.nth(i), &nam,
&subform_terms, dormant, &vac, next_local);
}
unimplemented!()
}
pub fn comp_do(ctx: &mut Context, form: &Value,
named: &Value, terminal: &Value, dormant: &Value, vacant: &Value, next_local: u32) -> Value {
unimplemented!()
}
pub fn comp_let(ctx: &mut Context, form: &Value,
named: &Value, terminal: &Value, dormant: &Value, vacant: &Value, next_local: u32) -> Value {
unimplemented!()
}
pub fn comp_loop(ctx: &mut Context, form: &Value,
named: &Value, terminal: &Value, dormant: &Value, vacant: &Value, next_local: u32) -> Value {
unimplemented!()
}
pub fn comp_recur(ctx: &mut Context, form: &Value,
named: &Value, terminal: &Value, dormant: &Value, vacant: &Value, next_local: u32) -> Value {
unimplemented!()
}
pub fn comp_quote(ctx: &mut Context, form: &Value,
named: &Value, terminal: &Value, dormant: &Value, vacant: &Value, next_local: u32) -> Value {
unimplemented!()
}
pub fn comp_call(ctx: &mut Context, form: &Value,
named: &Value, terminal: &Value, dormant: &Value, vacant: &Value, next_local: u32) -> Value {
let b = "{fress/conj {:name [\"fress\" \"conj\"]\
:args [value value]\
:ret value}}";
let base = read(b);
let first = form.peek();
if first.is_symbol() && first.has_namespace() && base.contains(&first) {
let base_fn = base.get(&first).split_out();
let idx = register_import(ctx, &base_fn);
let ct = form.count();
for i in 1..ct {
let e = form.nth(i);
let r = comp(ctx, e, named, terminal, dormant, vacant, 0);
}
let f: &mut Func = ctx.funcs.get_mut(ctx.curr_func).unwrap();
f.code_bytes.push(wasm::Op::CALL);
wasm::uleb128(&mut f.code_bytes, idx as u64);
return read("value");
} else {
unimplemented!()
}
}
pub fn comp_vector(ctx: &mut Context, form: &Value,
named: &Value, terminal: &Value, dormant: &Value, vacant: &Value, next_local: u32) -> Value {
let b = "{:name [\"fress\" \"new_vector\"]\
:args []\
:ret value}";
if form.is_empty() {
let new_v = read(b);
let idx = register_import(ctx, &new_v);
let f: &mut Func = ctx.funcs.get_mut(ctx.curr_func).unwrap();
f.code_bytes.push(wasm::Op::CALL);
wasm::uleb128(&mut f.code_bytes, idx as u64);
return read("value");
}
unimplemented!()
}
pub fn comp_integral(ctx: &mut Context, form: &Value,
named: &Value, terminal: &Value, dormant: &Value, vacant: &Value, next_local: u32) -> Value {
let b = "{:name [\"fress\" \"from_signed_i64\"]\
:args [i64]\
:ret value}";
let new_int = read(b);
let idx = register_import(ctx, &new_int);
let f: &mut Func = ctx.funcs.get_mut(ctx.curr_func).unwrap();
f.code_bytes.push(wasm::Op::I64_CONST);
wasm::sleb128(&mut f.code_bytes, form.as_i64());
f.code_bytes.push(wasm::Op::CALL);
wasm::uleb128(&mut f.code_bytes, idx as u64);
return read("value");
}
use std::cell::Cell;
thread_local! {
pub static PRIMITIVES: Cell<Unit> = Cell::new(Unit { word: 0});
}
pub fn init_primitives() {
let x = read(PRIMITIVE_INIT)._consume().unit();
PRIMITIVES.with(|c| c.set(x));
}
pub fn get_primitives() -> &'static Value {
let x = PRIMITIVES.with(|c| c.as_ptr());
unsafe { &*(x as usize as *const Value) }
}
static PRIMITIVE_INIT: &'static str = "\
{fress/conj {:name [\"fress\" \"conj\"]\
:args [value value]\
:ret value},
fress/nil? {:name [\"fress\" \"nil?\"]\
:args [&value]\
:ret value}}
";
#[no_mangle]
pub extern fn new_vector() -> u32 {
let x = vector();
x._consume().unit().u32()
}
#[no_mangle]
pub extern fn post_output(v: u32) {
let val = Unit::from(v).handle();
let p = val.to_string();
unsafe {
super::post_output(p.as_ptr() as u32, p.len() as u32);
}
}
#[no_mangle]
pub extern fn drop(v: u32) {
let _ = Unit::from(v).handle().value();
}
#[no_mangle]
pub extern fn console_log(v: u32) {
let val = Unit::from(v).handle();
log!("{}", val);
}
#[no_mangle]
pub extern fn conj(c: u32, v: u32) -> u32 {
let coll = Unit::from(c).handle().value();
let val = Unit::from(v).handle().value();
let res = coll.conj(val);
res._consume().unit().u32()
}
#[no_mangle]
pub extern fn from_signed_i64(x: u64) -> u32 {
let v = Value::from(x as i64);
v._consume().unit().u32()
}