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
//#[cfg(not(target_arch = "wasm32"))]
//pub fn run_io(heap: &Heap, prog: &Program, tids: &[usize], host: u64) {
//fn read_input() -> String {
//let mut input = String::new();
//stdin().read_line(&mut input).expect("string");
//if let Some('\n') = input.chars().next_back() { input.pop(); }
//if let Some('\r') = input.chars().next_back() { input.pop(); }
//return input;
//}
//use std::io::{stdin,stdout,Write};
//loop {
//let term = reduce(heap, prog, tids, host); // FIXME: add parallelism
//match get_tag(term) {
//CTR => {
//let fid = get_ext(term);
//// IO.done a : (IO a)
//if fid == IO_DONE {
//let done = load_arg(heap, term, 0);
//free(heap, 0, get_loc(term, 0), 1);
//link(heap, host, done);
//println!("");
//println!("");
//break;
//}
//// IO.do_input (String -> IO a) : (IO a)
//if fid == IO_DO_INPUT {
//let cont = load_arg(heap, term, 0);
//let text = make_string(heap, tids[0], &read_input());
//let app0 = alloc(heap, tids[0], 2);
//link(heap, app0 + 0, cont);
//link(heap, app0 + 1, text);
//free(heap, 0, get_loc(term, 0), 1);
//let done = App(app0);
//link(heap, host, done);
//}
//// IO.do_output String (Wrd -> IO a) : (IO a)
//if fid == IO_DO_OUTPUT {
//if let Some(show) = readback_string(heap, prog, tids, get_loc(term, 0)) {
//print!("{}", show);
//stdout().flush().ok();
//let cont = load_arg(heap, term, 1);
//let app0 = alloc(heap, tids[0], 2);
//link(heap, app0 + 0, cont);
//link(heap, app0 + 1, Wrd(0));
//free(heap, 0, get_loc(term, 0), 2);
//let text = load_arg(heap, term, 0);
//collect(heap, prog, 0, text);
//let done = App(app0);
//link(heap, host, done);
//} else {
//println!("Runtime type error: attempted to print a non-string.");
//println!("{}", crate::language::readback::as_code(heap, prog, get_loc(term, 0)));
//std::process::exit(0);
//}
//}
//// IO.do_fetch String (String -> IO a) : (IO a)
//if fid == IO_DO_FETCH {
//if let Some(url) = readback_string(heap, prog, tids, get_loc(term, 0)) {
//let body = reqwest::blocking::get(url).unwrap().text().unwrap(); // FIXME: treat
//let cont = load_arg(heap, term, 2);
//let app0 = alloc(heap, tids[0], 2);
//let text = make_string(heap, tids[0], &body);
//link(heap, app0 + 0, cont);
//link(heap, app0 + 1, text);
//free(heap, 0, get_loc(term, 0), 3);
//let opts = load_arg(heap, term, 1); // FIXME: use options
//collect(heap, prog, 0, opts);
//let done = App(app0);
//link(heap, host, done);
//} else {
//println!("Runtime type error: attempted to print a non-string.");
//println!("{}", crate::language::readback::as_code(heap, prog, get_loc(term, 0)));
//std::process::exit(0);
//}
//}
//// IO.do_store String String (Wrd -> IO a) : (IO a)
//if fid == IO_DO_STORE {
//if let Some(key) = readback_string(heap, prog, tids, get_loc(term, 0)) {
//if let Some(val) = readback_string(heap, prog, tids, get_loc(term, 1)) {
//std::fs::write(key, val).ok(); // TODO: Handle errors
//let cont = load_arg(heap, term, 2);
//let app0 = alloc(heap, tids[0], 2);
//link(heap, app0 + 0, cont);
//link(heap, app0 + 1, Wrd(0));
//free(heap, 0, get_loc(term, 0), 2);
//let key = load_arg(heap, term, 0);
//collect(heap, prog, 0, key);
//free(heap, 0, get_loc(term, 1), 2);
//let val = load_arg(heap, term, 1);
//collect(heap, prog, 0, val);
//let done = App(app0);
//link(heap, host, done);
//} else {
//println!("Runtime type error: attempted to store a non-string.");
//println!("{}", crate::language::readback::as_code(heap, prog, get_loc(term, 1)));
//std::process::exit(0);
//}
//} else {
//println!("Runtime type error: attempted to store to a non-string key.");
//println!("{}", crate::language::readback::as_code(heap, prog, get_loc(term, 0)));
//std::process::exit(0);
//}
//}
//// IO.do_load String (String -> IO a) : (IO a)
//if fid == IO_DO_LOAD {
//if let Some(key) = readback_string(heap, prog, tids, get_loc(term, 0)) {
//let file = std::fs::read(key).unwrap(); // TODO: Handle errors
//let file = std::str::from_utf8(&file).unwrap();
//let cont = load_arg(heap, term, 1);
//let text = make_string(heap, tids[0], file);
//let app0 = alloc(heap, tids[0], 2);
//link(heap, app0 + 0, cont);
//link(heap, app0 + 1, text);
//free(heap, 0, get_loc(term, 0), 2);
//let done = App(app0);
//link(heap, host, done);
//} else {
//println!("Runtime type error: attempted to read from a non-string key.");
//println!("{}", crate::language::readback::as_code(heap, prog, get_loc(term, 0)));
//std::process::exit(0);
//}
//}
//break;
//}
//_ => {
//break;
//}
//}
//}
//}
//pub fn make_string(heap: &Heap, tid: usize, text: &str) -> Ptr {
//let mut term = Ctr(STRING_NIL, 0);
//for chr in text.chars().rev() { // TODO: reverse
//let ctr0 = alloc(heap, tid, 2);
//link(heap, ctr0 + 0, Wrd(chr as u64));
//link(heap, ctr0 + 1, term);
//term = Ctr(STRING_CONS, ctr0);
//}
//return term;
//}