forked from gluon-lang/gluon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.rs
236 lines (213 loc) · 7.34 KB
/
io.rs
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
use std::string::String as StdString;
use std::io::{Read, stdin};
use std::fmt;
use std::fs::File;
use std::sync::Mutex;
use vm::{Result, Variants};
use vm::gc::{Gc, Traverseable};
use vm::types::*;
use vm::thread::ThreadInternal;
use vm::thread::{Thread, Status, RootStr};
use vm::api::{Array, Generic, VmType, Getable, Pushable, IO, WithVM, Userdata, primitive};
use vm::api::generic::{A, B};
use vm::internal::Value;
use super::Compiler;
fn print_int(i: VmInt) -> IO<()> {
print!("{}", i);
IO::Value(())
}
fn print(s: RootStr) -> IO<()> {
println!("{}", &*s);
IO::Value(())
}
struct GluonFile(Mutex<File>);
impl Userdata for GluonFile {}
impl fmt::Debug for GluonFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "File")
}
}
impl VmType for GluonFile {
type Type = GluonFile;
}
impl Traverseable for GluonFile {
fn traverse(&self, _: &mut Gc) {}
}
fn open_file(s: &str) -> IO<GluonFile> {
match File::open(s) {
Ok(f) => IO::Value(GluonFile(Mutex::new(f))),
Err(err) => IO::Exception(format!("{}", err)),
}
}
fn read_file<'vm>(file: WithVM<'vm, &GluonFile>, count: usize) -> IO<Array<'vm, u8>> {
let WithVM { vm, value: file } = file;
let mut file = file.0.lock().unwrap();
let mut buffer = Vec::with_capacity(count);
unsafe {
buffer.set_len(count);
match file.read(&mut *buffer) {
Ok(bytes_read) => {
let value = {
let stack = vm.get_stack();
match vm.alloc(&stack, &buffer[..bytes_read]) {
Ok(value) => value,
Err(err) => return IO::Exception(format!("{}", err)),
}
};
IO::Value(Getable::from_value(vm, Variants::new(&Value::Array(value)))
.expect("Array"))
}
Err(err) => IO::Exception(format!("{}", err)),
}
}
}
fn read_file_to_string(s: &str) -> IO<String> {
let mut buffer = String::new();
match File::open(s).and_then(|mut file| file.read_to_string(&mut buffer)) {
Ok(_) => IO::Value(buffer),
Err(err) => {
use std::fmt::Write;
buffer.clear();
let _ = write!(&mut buffer, "{}", err);
IO::Exception(buffer)
}
}
}
fn read_char() -> IO<char> {
match stdin().bytes().next() {
Some(result) => {
match result {
Ok(b) => {
::std::char::from_u32(b as u32)
.map(IO::Value)
.unwrap_or_else(|| IO::Exception("Not a valid char".into()))
}
Err(err) => IO::Exception(format!("{}", err)),
}
}
None => IO::Exception("No read".into()),
}
}
fn read_line() -> IO<String> {
let mut buffer = String::new();
match stdin().read_line(&mut buffer) {
Ok(_) => IO::Value(buffer),
Err(err) => {
use std::fmt::Write;
buffer.clear();
let _ = write!(&mut buffer, "{}", err);
IO::Exception(buffer)
}
}
}
/// IO a -> (String -> IO a) -> IO a
fn catch_io(vm: &Thread) -> Status {
let mut stack = vm.current_frame();
let frame_level = stack.stack.get_frames().len();
let action = stack[0];
stack.push(action);
0.push(vm, &mut stack.stack).unwrap();
match vm.call_function(stack, 1) {
Ok(_) => Status::Ok,
Err(err) => {
stack = vm.current_frame();
while stack.stack.get_frames().len() > frame_level {
match stack.exit_scope() {
Some(new_stack) => stack = new_stack,
None => return Status::Error,
}
}
let callback = stack[1];
stack.push(callback);
let fmt = format!("{}", err);
let _ = fmt.push(vm, &mut stack.stack);
0.push(vm, &mut stack.stack).unwrap();
match vm.call_function(stack, 2) {
Ok(_) => Status::Ok,
Err(err) => {
stack = vm.current_frame();
let fmt = format!("{}", err);
let _ = fmt.push(vm, &mut stack.stack);
Status::Error
}
}
}
}
}
fn run_expr(expr: WithVM<RootStr>) -> IO<String> {
let WithVM { vm, value: expr } = expr;
let mut stack = vm.current_frame();
let frame_level = stack.stack.get_frames().len();
drop(stack);
let run_result = Compiler::new().run_expr::<Generic<A>>(vm, "<top>", &expr);
stack = vm.current_frame();
match run_result {
Ok((value, typ)) => IO::Value(format!("{:?} : {}", value.0, typ)),
Err(err) => {
let trace = stack.stacktrace(frame_level);
let fmt = format!("{}\n{}", err, trace);
while stack.stack.get_frames().len() > frame_level {
match stack.exit_scope() {
Some(new_stack) => stack = new_stack,
None => return IO::Exception(fmt),
}
}
IO::Exception(fmt)
}
}
}
fn load_script(name: WithVM<RootStr>, expr: RootStr) -> IO<String> {
let WithVM { vm, value: name } = name;
let mut stack = vm.current_frame();
let frame_level = stack.stack.get_frames().len();
drop(stack);
let run_result = Compiler::new().load_script(vm, &name[..], &expr);
stack = vm.current_frame();
match run_result {
Ok(()) => IO::Value(format!("Loaded {}", &name[..])),
Err(err) => {
let trace = stack.stacktrace(frame_level);
let fmt = format!("{}\n{}", err, trace);
while stack.stack.get_frames().len() > frame_level {
match stack.exit_scope() {
Some(new_stack) => stack = new_stack,
None => return IO::Exception(fmt),
}
}
IO::Exception(fmt)
}
}
}
pub fn load(vm: &Thread) -> Result<()> {
try!(vm.register_type::<GluonFile>("File", &[]));
// io_flat_map f m : (a -> IO b) -> IO a -> IO b
// = f (m ())
let io_flat_map = vec![
// [f, m, ()] Initial stack
Call(1), // [f, m_ret] Call m ()
PushInt(0), // [f, m_ret, ()] Add a dummy argument ()
TailCall(2), // [f_ret] Call f m_ret ()
];
let io_flat_map_type = <fn (fn (A) -> IO<B>, IO<A>) -> IO<B> as VmType>::make_type(vm);
try!(vm.add_bytecode("io_flat_map", io_flat_map_type, 3, io_flat_map));
try!(vm.add_bytecode("io_pure",
<fn(A) -> IO<A> as VmType>::make_type(vm),
2,
vec![Pop(1)]));
// IO functions
try!(vm.define_global("io",
record!(
print_int => primitive!(1 print_int),
open_file => primitive!(1 open_file),
read_file => primitive!(2 read_file),
read_file_to_string => primitive!(1 read_file_to_string),
read_char => primitive!(0 read_char),
read_line => primitive!(0 read_line),
print => primitive!(1 print),
catch =>
primitive::<fn (IO<A>, fn (StdString) -> IO<A>) -> IO<A>>("io.catch", catch_io),
run_expr => primitive!(1 run_expr),
load_script => primitive!(2 load_script)
)));
Ok(())
}