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
//! Tokay compiler
use super::*;
use crate::error::Error;
use crate::reader::*;
use crate::value;
use crate::value::RefValue;
use crate::vm::*;
use indexmap::{IndexMap, IndexSet, indexset};
use log;
use std::cell::RefCell;
/** Tokay compiler instance
A tokay compiler initializes a Tokay parser for later re-use when called multiple times.
The compiler works in a mode so that statics, variables and constants once built
won't be removed and can be accessed on later calls.
*/
pub struct Compiler {
parser: Option<parser::Parser>, // Internal Tokay parser
pub debug: u8, // Compiler debug mode
pub(super) restrict: bool, // Restrict assignment of reserved identifiers (required by prelude bootstrap)
pub(super) statics: RefCell<IndexSet<RefValue>>, // Static values collected during compilation
// TODO: As workaround to emulate old behavior of the Compiler struct
main: ImlParseletModel, // keep global parselet
constants: IndexMap<String, ImlValue>, // keep global constants
}
impl Compiler {
/** Initialize a new compiler.
The compiler serves functions to compile Tokay source code into programs executable by
the Tokay VM. It uses an intermediate language representation to implement derives of
generics, statics, etc.
*/
pub fn new() -> Self {
// Always create standard statics; These are referenced during AST traversal.
let statics = indexset![
value!(void),
value!(null),
value!(true),
value!(false),
value!(0),
value!(1),
];
// Create compiler instance
let mut compiler = Self {
parser: None,
debug: 0,
restrict: false,
statics: RefCell::new(statics),
// TODO: workaround...
main: ImlParseletModel::new(None),
constants: IndexMap::new(),
};
// Compile with the default prelude
compiler.load_prelude();
compiler.restrict = true;
// Set compiler debug level afterwards
compiler.debug = if let Ok(level) = std::env::var("TOKAY_DEBUG") {
level.parse::<u8>().unwrap_or_default()
} else {
0
};
compiler
}
/** Compile a Tokay program from an existing AST into the compiler. */
pub(super) fn compile_from_ast(
&mut self,
ast: &RefValue,
name: Option<String>,
) -> Result<Option<Program>, Vec<Error>> {
log::trace!("compile_from_ast");
// Create main parselet from current main model
let main_parselet = ImlRefParselet::new(ImlParselet::new(
// TODO: Keep backward compatible: copy Compiler's main model into the main_parselet
Some(self.main.clone()),
None,
None,
None,
Some(name.unwrap_or("__main__".to_string())),
5,
false,
));
// println!("=> self.constants {:?}", self.constants.keys());
self.constants = {
// Create new global scope
let global_scope = Scope::new(self, ScopeLevel::Parselet(main_parselet.clone()), None);
// Extend compiler's constants into global_scope
global_scope
.constants
.borrow_mut()
.extend(self.constants.clone());
// Traverse the parsed AST
ast::traverse(&global_scope, &ast);
// try to resolve any open usages
global_scope.resolve_usages();
// println!("constants {:#?}, {} usages", global_scope.constants, global_scope.usages.borrow().len());
// Report unresolved names
// println!("usages = {:?}", global_scope.usages);
for usage in global_scope.usages.borrow_mut().drain(..) {
global_scope
.push_error(usage.offset(), format!("Use of undefined name '{}'", usage));
}
// Break on error
if !global_scope.errors.borrow().is_empty() {
return Err(global_scope.errors.borrow_mut().drain(..).collect());
}
// Otherwise, write new contants back into compiler
global_scope.constants.take()
};
// println!("<= self.constants {:?}", self.constants.keys());
// TODO: Keep backward compatible: copy main parselet and constants into compiler
self.main = main_parselet.borrow().model.borrow().clone();
self.main.body = ImlOp::Nop;
self.main.begin = ImlOp::Nop;
self.main.end = ImlOp::Nop;
/*
if self.debug > 1 {
println!("--- Global scope ---\n{:#?}", scope)
}
*/
if self.debug > 1 {
println!("--- Intermediate main ---\n{:#?}", main_parselet);
}
let program = ImlProgram::new(main_parselet);
match program.compile() {
Ok(program) => {
if self.debug > 1 {
println!("--- Finalized program ---");
program.dump();
}
Ok(Some(program))
}
Err(errors) => Err(errors),
}
}
/** Compile a Tokay program from a Reader source into the compiler. */
pub fn compile(&mut self, reader: Reader) -> Result<Option<Program>, Vec<Error>> {
log::trace!("compile");
// Create the Tokay parser when not already done
if self.parser.is_none() {
self.parser = Some(Parser::new());
}
let parser = self.parser.as_ref().unwrap();
let ast = match parser.parse(reader) {
Ok(ast) => ast,
Err(error) => {
return Err(vec![error]);
}
};
if self.debug > 0 {
ast::print(&ast);
//println!("###\n{:#?}\n###", ast);
}
self.compile_from_ast(&ast, None)
}
/// Shortcut to compile a Tokay program from a &str into the compiler.
pub fn compile_from_str(&mut self, src: &str) -> Result<Option<Program>, Vec<Error>> {
self.compile(Reader::new(
None,
Box::new(std::io::Cursor::new(src.to_owned())),
))
}
/** Register a static value within a compiler instance.
This avoids that the compiler produces multiple results pointing to effectively the same values
(althought they are different objects, but the same value)
*/
pub(super) fn register_static(&self, value: RefValue) -> ImlValue {
log::trace!("register_static value = {:?}", value);
let mut statics = self.statics.borrow_mut();
if let Some(value) = statics.get(&value) {
log::trace!("value already known");
ImlValue::Value(value.clone())
} else {
statics.insert(value.clone());
log::trace!("value added to registry");
ImlValue::Value(value)
}
}
/** Register a named constant within a compiler instance. */
pub fn constant(&mut self, name: &str, value: RefValue) {
log::trace!("constant name = {:?} value = {:?}", name, value);
self.constants
.insert(name.to_string(), ImlValue::from(value));
}
/** Register a global variable with a given value. */
pub fn global(&mut self, name: &str, value: RefValue) {
log::trace!("global name = {:?} value = {:?}", name, value);
self.main
.signature
.insert(name.to_string(), Some(ImlValue::from(value)));
self.main.var(name);
}
}