-
Notifications
You must be signed in to change notification settings - Fork 83
/
driver.rs
218 lines (205 loc) · 5.85 KB
/
driver.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
//< ch-0 ch-1 ch-2 ch-3
use std::io;
use std::io::Write;
//> ch-0 ch-1
use iron_llvm::core::value::Value;
//> ch-2
use iron_llvm::target;
//< ch-2
use builder;
use builder::{IRBuilder, ModuleProvider};
//> ch-2
use jitter;
use jitter::JITter;
//< ch-0 ch-1 ch-2
use lexer::*;
//> ch-0
use parser::*;
//> ch-1 ch-2 ch-3
use filer;
//< ch-1 ch-2 ch-3
//< ch-0
pub use self::Stage::{
//> ch-0 ch-1 ch-2
Exec,
//< ch-2
IR,
//< ch-1
AST,
//< ch-0
Tokens
};
//< parser-stage
#[derive(PartialEq, Clone, Debug)]
pub enum Stage {
//> ch-0 ch-1 ch-2 parser-stage
Exec,
//< ch-2
IR,
//< ch-1 parser-stage
AST,
//< ch-0
Tokens
}
//> parser-stage
//< parser-driver
pub fn main_loop(stage: Stage) {
let stdin = io::stdin();
let mut stdout = io::stdout();
let mut input = String::new();
//> ch-0
let mut parser_settings = default_parser_settings();
//> ch-1 ch-2 ch-3 parser-driver
/*
//< ch-2
let mut ir_container = builder::SimpleModuleProvider::new("main");
//> ch-2
*/
//< ch-3
let mut ir_container : Box<JITter> = if stage == Exec {
target::initilalize_native_target();
target::initilalize_native_asm_printer();
jitter::init();
Box::new(
jitter::MCJITter::new("main")
)
} else {
Box::new(
builder::SimpleModuleProvider::new("main")
)
};
//< ch-2
let mut builder_context = builder::Context::new();
//> ch-2 ch-3
match filer::load_stdlib(&mut parser_settings, &mut builder_context, ir_container.get_module_provider()) {
Ok((value, runnable)) => if runnable && stage == Exec {
ir_container.run_function(value);
},
Err(err) => print!("Error occured during stdlib loading: {}\n", err)
};
//< ch-2 ch-3
//< ch-0 ch-1 parser-driver
'main: loop {
print!("> ");
stdout.flush().unwrap();
input.clear();
stdin.read_line(&mut input).ok().expect("Failed to read line");
if input.as_str() == ".quit\n" {
break;
//> ch-0 ch-1 ch-2 ch-3 parser-driver
} else if &input[0..5] == ".load" {
let mut path = input[6..].to_string();
match path.pop() {
Some(_) => (),
None => {
print!("Error occured during loading: empty path\n");
continue;
}
};
match filer::load_ks(path, &mut parser_settings, &mut builder_context, ir_container.get_module_provider()) {
Ok((value, runnable)) => if runnable && stage == Exec {
ir_container.run_function(value);
},
Err(err) => print!("Error occured during loading: {}\n", err)
};
continue;
} else if &input[0..5] == ".dump" {
let mut path = input[6..].to_string();
match path.pop() {
Some(_) => (),
None => {
print!("Error occured during dumping: empty path\n");
continue;
}
};
match filer::dump_bitcode(&path, ir_container.get_module_provider()) {
Ok(_) => (),
Err(_) => print!("Error occured during dumping\n")
};
continue;
} else if input.as_str() == ".help\n" {
print!("Enter Kaleidoscope expressions or special commands.\n");
print!("Special commands are:\n");
print!(".quit -- quit\n");
print!(".load <path> -- load .ks file\n");
print!(".dump <path> -- dump bitcode of currently open module\n");
print!(".help -- show this help message\n");
continue;
//< ch-0 ch-1 ch-2 ch-3 parser-driver
}
//> ch-0
// the constructed AST
let mut ast = Vec::new();
// tokens left from the previous lines
let mut prev = Vec::new();
//< ch-0
loop {
let tokens = tokenize(input.as_str());
if stage == Tokens {
println!("{:?}", tokens);
continue 'main
}
//> ch-0
prev.extend(tokens.into_iter());
let parsing_result = parse(prev.as_slice(), ast.as_slice(), &mut parser_settings);
match parsing_result {
Ok((parsed_ast, rest)) => {
ast.extend(parsed_ast.into_iter());
if rest.is_empty() {
// we have parsed a full expression
break
} else {
prev = rest;
}
},
Err(message) => {
println!("Error occured: {}", message);
continue 'main
}
}
print!(". ");
stdout.flush().unwrap();
input.clear();
stdin.read_line(&mut input).ok().expect("Failed to read line");
//< ch-0
}
//> ch-0
if stage == AST {
println!("{:?}", ast);
continue
}
//> ch-1 parser-driver
match ast.codegen(&mut builder_context,
//> ch-2
ir_container.get_module_provider()
//> ch-3
/*
//< ch-2
&mut ir_container) {
Ok((value, _)) => value.dump(),
//> ch-2
*/
//< ch-3
/*j*/ ) {
Ok((value, runnable)) =>
if runnable && stage == Exec {
println!("=> {}", ir_container.run_function(value));
} else {
value.dump();
},
//< ch-2
Err(message) => println!("Error occured: {}", message)
}
//< ch-0 ch-1 parser-driver
}
//> ch-0 ch-1 parser-driver
if stage == IR
//> ch-2
/*jw*/ || stage == Exec
//< ch-2
/*jw*/ {
ir_container.dump();
}
//< ch-0 ch-1 parser-driver
}
//> ch-0 ch-1 ch-2 ch-3 parser-driver