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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
#[macro_use]
extern crate lalrpop_util;
lalrpop_mod!(pub script);
pub mod error;
use error::*;
pub mod ast;
use ast::*;
use runtime::bytecode::*;
use runtime::memory::*;
use runtime::standard::get_default_namespace_descriptors;
use runtime::standard::STANDARD_CONTEXT_ID;
use runtime::prelude::*;
use codespan::FileId;
use codespan::Span;
use parking_lot::RwLock;
use std::clone::Clone as RustClone;
use std::collections::HashMap;
use std::i32;
use std::rc::Rc;
pub type Bytecode = Vec<Op>;
pub type CompileResult = std::result::Result<Bytecode, CompileError>;
fn is_exact_float(val: f64) -> bool {
((val as f32) as f64) == val
}
pub struct CompileContext {
file_id: FileId,
context_id: ContextId,
gcd_last: u16,
pub constant_descriptors: HashMap<GlobalConstantDescriptor, ObjectRef>,
dcd_last: DebugSpanDescriptor,
pub debug_symbol_descriptors: HashMap<DebugSpanDescriptor, DebugSymbol>,
}
impl CompileContext {
pub fn new(context_id: ContextId, file_id: FileId) -> CompileContext {
CompileContext {
file_id,
context_id,
gcd_last: 0,
constant_descriptors: HashMap::new(),
dcd_last: 0,
debug_symbol_descriptors: HashMap::new(),
}
}
pub fn gcd_gen(&mut self) -> GlobalConstantDescriptor {
let old = self.gcd_last;
self.gcd_last += 1;
(self.context_id, old)
}
pub fn dsd_gen(&mut self) -> DebugSpanDescriptor {
let old = self.dcd_last;
self.dcd_last += 1;
old
}
}
fn builtin_functions() -> HashMap<String, Op> {
let mut res = HashMap::new();
res.insert("<add>".to_string(), Op::add);
res.insert("<sub>".to_string(), Op::sub);
res.insert("<mul>".to_string(), Op::mul);
res.insert("<div>".to_string(), Op::div);
res.insert("<mod>".to_string(), Op::mod_);
res.insert("<eq>".to_string(), Op::cmp_eq);
res.insert("<neq>".to_string(), Op::cmp_neq);
res.insert("<gt>".to_string(), Op::cmp_gt);
res.insert("<lt>".to_string(), Op::cmp_lt);
res.insert("<geq>".to_string(), Op::cmp_geq);
res.insert("<leq>".to_string(), Op::cmp_leq);
res.insert("<and>".to_string(), Op::and);
res.insert("<or>".to_string(), Op::or);
res.insert("<bitand>".to_string(), Op::bitand);
res.insert("<bitor>".to_string(), Op::bitor);
res.insert("<bitxor>".to_string(), Op::bitxor);
res.insert("<not>".to_string(), Op::not);
res.insert("<neg>".to_string(), Op::neg);
res.insert("<index>".to_string(), Op::index_get);
res
}
pub struct CompileManager {
pub context_stack: Vec<CompileContext>,
local_index_last: LocalName,
pub local_index: HashMap<(ContextId, String), LocalName>,
context_id_last: ContextId,
default_namespace_descriptors: HashMap<String, GlobalConstantDescriptor>,
pub memory_manager: MemoryManager,
}
pub enum NameLookupResult {
MyLocal(LocalName),
ExternLocal(NonLocalUnmappedName),
Global(GlobalConstantDescriptor),
NotFound,
}
impl CompileManager {
pub fn new(file_id: FileId) -> Self {
let mut mem_manager = MemoryManager::new();
let context_id = STANDARD_CONTEXT_ID + 1;
mem_manager.register_context(context_id);
CompileManager {
context_stack: vec![CompileContext::new(STANDARD_CONTEXT_ID + 1, file_id)],
local_index_last: 0,
local_index: HashMap::new(),
context_id_last: STANDARD_CONTEXT_ID + 2,
default_namespace_descriptors: get_default_namespace_descriptors(),
memory_manager: mem_manager,
}
}
pub fn context_id_gen(&mut self) -> ContextId {
let old = self.context_id_last;
self.context_id_last += 1;
old
}
pub fn create_debug_descriptor(&mut self, span: Span) -> DebugSpanDescriptor {
let debug_descr = self.context().dsd_gen();
let file_id = self.context().file_id;
self.context()
.debug_symbol_descriptors
.insert(debug_descr, DebugSymbol::new(file_id, span));
debug_descr
}
pub fn create_constant_descriptor(&mut self, obj: ObjectRef) -> GlobalConstantDescriptor {
let const_descr = self.context().gcd_gen();
self.context().constant_descriptors.insert(const_descr, obj);
const_descr
}
pub fn context(&mut self) -> &mut CompileContext {
self.context_stack.last_mut().unwrap()
}
pub fn local_name_gen(&mut self) -> LocalName {
let old = self.local_index_last;
self.local_index_last += 1;
old
}
pub fn name_lookup(&mut self, name: &String) -> NameLookupResult {
let mut first = true;
for context in self.context_stack.iter().rev() {
if let Some(local_index) = self.local_index.get(&(context.context_id, name.clone())) {
if first {
return NameLookupResult::MyLocal(*local_index);
} else {
self.memory_manager
.do_not_drop(context.context_id, *local_index)
.expect("Internal memory manager error on do_not_drop");
return NameLookupResult::ExternLocal((context.context_id, *local_index));
}
}
if first {
first = false;
}
}
if let Some(global_index) = self.default_namespace_descriptors.get(name) {
NameLookupResult::Global(*global_index)
} else {
NameLookupResult::NotFound
}
}
pub fn compile_literal(&mut self, ast: &Literal) -> CompileResult {
let descr = self.context().gcd_gen();
let constant: ObjectRef = match ast {
Literal::Unit(_) => {
return Ok(vec![Op::push_unit]);
}
Literal::Integer(val, _) => {
if *val < i32::MAX as i64 && *val > i32::MIN as i64 {
return Ok(vec![Op::push_int(*val as i32)]);
}
IntObject::new(*val)
}
Literal::Float(val, _) => {
if is_exact_float(*val) {
return Ok(vec![Op::push_float(*val as f32)]);
}
FloatObject::new(*val)
}
Literal::Bool(val, _) => BoolObject::new(*val),
Literal::Str(val, _) => StringObject::new(RustClone::clone(val)),
Literal::Char(val, _) => CharObject::new(*val),
Literal::FormatString(f) => {
return self.compile_format_string(&f);
}
};
self.context().constant_descriptors.insert(descr, constant);
Ok(vec![Op::push_const_clone(descr)])
}
pub fn compile_list_literal(&mut self, ast: &ListLiteral) -> CompileResult {
let mut res = vec![];
for item in ast.values.iter() {
res.append(&mut self.compile_expr(item)?);
}
res.push(Op::mklist(ast.values.len() as u16));
Ok(res)
}
pub fn compile_tuple_literal(&mut self, ast: &TupleLiteral) -> CompileResult {
let mut res = vec![];
for item in ast.values.iter() {
res.append(&mut self.compile_expr(item)?);
}
res.push(Op::mktuple(ast.values.len() as u16));
Ok(res)
}
pub fn compile_set_literal(&mut self, ast: &SetLiteral) -> CompileResult {
let mut res = vec![];
for item in ast.values.iter() {
res.append(&mut self.compile_expr(item)?);
}
res.push(Op::debug(self.create_debug_descriptor(ast.span)));
res.push(Op::mkset(ast.values.len() as u16));
Ok(res)
}
pub fn compile_dict_literal(&mut self, ast: &DictLiteral) -> CompileResult {
let mut res = vec![];
for (key, val) in ast.values.iter() {
res.append(&mut self.compile_expr(key)?);
res.append(&mut self.compile_expr(val)?);
}
res.push(Op::debug(self.create_debug_descriptor(ast.span)));
res.push(Op::mkdict(ast.values.len() as u16));
Ok(res)
}
pub fn compile_func_call(&mut self, ast: &FuncCall) -> CompileResult {
let mut res = vec![];
let builtins = builtin_functions();
if let Expr::Variable(name) = &*ast.func {
if let Some(op) = builtins.get(&name.name) {
if &name.name == "<and>" {
assert!(ast.arguments.len() == 2);
res.append(&mut self.compile_expr(&ast.arguments[0])?);
let mut short_arg_2 = self.compile_expr(&ast.arguments[1])?;
res.push(Op::push_bool(false));
res.push(Op::swap);
res.push(Op::not);
res.push(Op::cond_jmp(short_arg_2.len() as u16 as i16 + 2));
res.push(Op::pop);
res.append(&mut short_arg_2);
return Ok(res);
} else if &name.name == "<or>" {
assert!(ast.arguments.len() == 2);
res.append(&mut self.compile_expr(&ast.arguments[0])?);
let mut short_arg_2 = self.compile_expr(&ast.arguments[1])?;
res.push(Op::push_bool(true));
res.push(Op::swap);
res.push(Op::cond_jmp(short_arg_2.len() as u16 as i16 + 2));
res.push(Op::pop);
res.append(&mut short_arg_2);
return Ok(res);
}
for arg in ast.arguments.iter() {
res.append(&mut self.compile_expr(arg)?);
}
res.push(Op::debug(self.create_debug_descriptor(ast.span)));
res.push(*op);
return Ok(res);
}
match self.name_lookup(&name.name) {
NameLookupResult::MyLocal(name) => {
res.push(Op::load(name));
}
NameLookupResult::ExternLocal(name) => {
res.push(Op::load_non_local(name));
}
NameLookupResult::Global(name) => {
res.push(Op::push_global_default(name));
}
NameLookupResult::NotFound => {
return Err(CompileError::new(
CompileErrorType::UndefinedVariable(ast.func.span()),
format!("Undefined function: {}", name.name),
));
}
}
} else {
res.append(&mut self.compile_expr(&*ast.func)?);
}
for arg in ast.arguments.iter() {
res.append(&mut self.compile_expr(arg)?);
}
res.push(Op::debug(self.create_debug_descriptor(ast.span)));
res.push(Op::call_function(ast.arguments.len() as u8));
Ok(res)
}
pub fn compile_attr_lookup(&mut self, ast: &AttrLookup) -> CompileResult {
let mut res = vec![];
res.append(&mut self.compile_expr(&*ast.parent)?);
let name_val = StringObject::new(RustClone::clone(&ast.attribute.name));
res.push(Op::push_const(self.create_constant_descriptor(name_val)));
res.push(Op::debug(self.create_debug_descriptor(ast.span)));
res.push(Op::get_attr);
Ok(res)
}
pub fn compile_method_call(&mut self, ast: &MethodCall) -> CompileResult {
let mut res = vec![];
res.append(&mut self.compile_expr(&*ast.parent)?);
let name_val = StringObject::new(RustClone::clone(&ast.fname.name));
res.push(Op::push_const(self.create_constant_descriptor(name_val)));
for arg in ast.arguments.iter() {
res.append(&mut self.compile_expr(arg)?);
}
res.push(Op::debug(self.create_debug_descriptor(ast.span)));
res.push(Op::call_method(ast.arguments.len() as u8));
Ok(res)
}
pub fn compile_expr(&mut self, ast: &Expr) -> CompileResult {
match ast {
Expr::Variable(v) => {
let mut res = vec![];
res.push(Op::debug(self.create_debug_descriptor(v.span)));
res.push(match self.name_lookup(&v.name) {
NameLookupResult::MyLocal(name) => Op::load(name),
NameLookupResult::ExternLocal(name) => Op::load_non_local(name),
NameLookupResult::Global(name) => Op::push_global_default(name),
NameLookupResult::NotFound => {
return Err(CompileError::new(
CompileErrorType::UndefinedVariable(ast.span()),
format!("Undefined variable: {}", v.name),
))
}
});
Ok(res)
}
Expr::Literal(l) => self.compile_literal(l),
Expr::ListLiteral(l) => self.compile_list_literal(l),
Expr::SetLiteral(l) => self.compile_set_literal(l),
Expr::DictLiteral(l) => self.compile_dict_literal(l),
Expr::TupleLiteral(t) => self.compile_tuple_literal(t),
Expr::MethodCall(m) => self.compile_method_call(m),
Expr::FuncCall(f) => self.compile_func_call(f),
Expr::AttrLookup(a) => self.compile_attr_lookup(a),
Expr::IndexedExpr(i) => self.compile_indexed_expr(i),
Expr::SlicedExpr(s) => self.compile_sliced_expr(s),
Expr::PostPreOp(o) => self.compile_post_pre_op(o),
Expr::AnonFuncDefinition(a) => self.compile_anon_func_definition(a),
Expr::Error => unreachable!(),
}
}
pub fn compile_indexed_expr(&mut self, ast: &IndexedExpr) -> CompileResult {
let mut res = vec![];
res.append(&mut self.compile_expr(&*ast.parent)?);
res.append(&mut self.compile_expr(&*ast.index)?);
res.push(Op::debug(self.create_debug_descriptor(ast.span)));
res.push(Op::index_get);
Ok(res)
}
pub fn compile_post_pre_op(&mut self, ast: &PostPreOp) -> CompileResult {
let mut res = vec![];
let debug_descr = self.create_debug_descriptor(ast.span);
res.append(&mut self.compile_expr(&ast.val.as_expr())?);
match ast.variant {
PPOVariant::PostIncrement => {
res.push(Op::dup);
res.push(Op::push_int(1));
res.push(Op::debug(debug_descr));
res.push(Op::add);
}
PPOVariant::PreIncrement => {
res.push(Op::push_int(1));
res.push(Op::debug(debug_descr));
res.push(Op::add);
res.push(Op::dup);
}
PPOVariant::PostDecrement => {
res.push(Op::dup);
res.push(Op::push_int(1));
res.push(Op::debug(debug_descr));
res.push(Op::sub);
}
PPOVariant::PreDecrement => {
res.push(Op::push_int(1));
res.push(Op::debug(debug_descr));
res.push(Op::sub);
res.push(Op::dup);
}
}
res.append(&mut self.compile_assign_to(&ast.val)?);
Ok(res)
}
pub fn compile_sliced_expr(&mut self, ast: &SlicedExpr) -> CompileResult {
let mut res = vec![];
res.append(&mut self.compile_expr(&*ast.parent)?);
match &ast.start {
Some(start_expr) => {
res.append(&mut self.compile_expr(&*start_expr)?);
}
None => {
res.push(Op::push_unit);
}
}
match &ast.end {
Some(end_expr) => {
res.append(&mut self.compile_expr(&*end_expr)?);
}
None => {
res.push(Op::push_unit);
}
}
match &ast.step {
Some(step_expr) => {
res.append(&mut self.compile_expr(&*step_expr)?);
}
None => {
res.push(Op::push_unit);
}
}
res.push(Op::make_slice);
Ok(res)
}
pub fn compile_for_loop(&mut self, ast: &ForLoop) -> CompileResult {
let mut res = vec![];
res.append(&mut self.compile_expr(&ast.iter)?);
let debug_descr = self.create_debug_descriptor(ast.iter.span());
res.push(Op::debug(debug_descr));
res.push(Op::make_iter);
let local_iter_name = self.local_name_gen();
let cid = self.context().context_id;
res.push(Op::store(local_iter_name));
let local_name = self.local_name_gen();
self.local_index
.insert((cid, ast.binding.name.clone()), local_name);
let mut body = self.compile_statement_list(&ast.body)?;
res.push(Op::load(local_iter_name));
let skip_body_offset = body.len() as u16 as i16 + 3;
let back_to_dup_offset = -(body.len() as u16 as i16) - 4;
res.push(Op::debug(debug_descr));
res.push(Op::take_iter(skip_body_offset));
res.push(Op::store(local_name));
res.append(&mut body);
res.push(Op::jmp(back_to_dup_offset));
Ok(res)
}
pub fn compile_while_loop(&mut self, ast: &WhileLoop) -> CompileResult {
let mut res = vec![];
let mut cond = self.compile_expr(&ast.cond)?;
cond.push(Op::not);
let mut body = self.compile_statement_list(&ast.body)?;
let skip_body = Op::cond_jmp(2 + body.len() as i16);
let to_beginning = Op::jmp(-(body.len() as i16 + cond.len() as i16 + 1));
res.append(&mut cond);
res.push(skip_body);
res.append(&mut body);
res.push(to_beginning);
Ok(res)
}
pub fn compile_if_statement(&mut self, ast: &IfStatement) -> CompileResult {
let mut cond = self.compile_expr(&ast.condition)?;
let mut body2 = self.compile_statement_list(&ast.then_body)?;
let mut body1 = match ast.tail {
Some(IfTail::ElseIf(ref ifstmt)) => self.compile_if_statement(ifstmt)?,
Some(IfTail::Else(ref stmtlist)) => self.compile_statement_list(stmtlist)?,
None => vec![],
};
let skip_body1_offset = body1.len() + 2;
let skip_body2_offset = body2.len() + 1;
let mut res = vec![];
res.append(&mut cond);
res.push(Op::cond_jmp(skip_body1_offset as i16));
res.append(&mut body1);
res.push(Op::jmp(skip_body2_offset as i16));
res.append(&mut body2);
Ok(res)
}
pub fn compile_case_of(&mut self, ast: &CaseOf) -> CompileResult {
let my_local = self.local_name_gen();
let mut res = vec![];
let mut exit_jmp_indices: Vec<usize> = vec![];
res.append(&mut self.compile_expr(&ast.condition)?);
res.push(Op::store(my_local));
for (expr, body) in ast.cases.iter() {
let mut body = self.compile_statement_list(&body)?;
res.push(Op::load(my_local));
res.append(&mut self.compile_expr(&expr)?);
res.push(Op::debug(self.create_debug_descriptor(expr.span())));
res.push(Op::cmp_neq);
res.push(Op::cond_jmp(body.len() as i16 + 2));
res.append(&mut body);
exit_jmp_indices.push(res.len());
res.push(Op::jmp(0));
}
if let Some(body) = &ast.default {
res.append(&mut self.compile_statement_list(body)?);
}
for index in exit_jmp_indices {
res[index] = Op::jmp(res.len() as i16 - index as i16);
}
Ok(res)
}
pub fn compile_func_definition(&mut self, ast: &FuncDefinition) -> CompileResult {
let my_local = self.local_name_gen();
let cid = self.context().context_id;
self.local_index
.insert((cid, ast.name.name.clone()), my_local);
let new_cid = self.context_id_gen();
self.memory_manager.register_context(new_cid);
let sub_context = CompileContext::new(new_cid, self.context().file_id);
for arg in ast.args.iter() {
let name = self.local_name_gen();
self.local_index
.insert((sub_context.context_id, arg.name.clone()), name);
}
let sub_context_id = sub_context.context_id;
self.context_stack.push(sub_context);
let mut func_code = vec![];
for arg in ast.args.iter() {
func_code.push(Op::store(
*self
.local_index
.get(&(sub_context_id, arg.name.clone()))
.unwrap(),
));
}
func_code.append(&mut self.compile_statement_list(&ast.body)?);
let finished_context = self.context_stack.pop().unwrap();
let sub_context = GlobalContext {
constant_descriptors: finished_context.constant_descriptors,
debug_descriptors: finished_context.debug_symbol_descriptors,
};
let function_obj = Function {
nargs: ast.args.len(),
name: ast.name.name.clone(),
context: Rc::new(sub_context),
context_id: finished_context.context_id,
least_ancestors: RwLock::new(None),
code: func_code,
};
let my_descr = self.create_constant_descriptor(ObjectRef::new(function_obj));
let mut res = vec![];
res.push(Op::push_const_clone(my_descr));
res.push(Op::attach_ancestors);
res.push(Op::store(my_local));
Ok(res)
}
pub fn compile_anon_func_definition(&mut self, ast: &AnonFuncDefinition) -> CompileResult {
let new_cid = self.context_id_gen();
self.memory_manager.register_context(new_cid);
let sub_context = CompileContext::new(new_cid, self.context().file_id);
for arg in ast.args.iter() {
let name = self.local_name_gen();
self.local_index
.insert((sub_context.context_id, arg.name.clone()), name);
}
let sub_context_id = sub_context.context_id;
self.context_stack.push(sub_context);
let mut func_code = vec![];
for arg in ast.args.iter() {
func_code.push(Op::store(
*self
.local_index
.get(&(sub_context_id, arg.name.clone()))
.unwrap(),
));
}
func_code.append(&mut self.compile_statement_list(&ast.body)?);
let finished_context = self.context_stack.pop().unwrap();
let sub_context = GlobalContext {
constant_descriptors: finished_context.constant_descriptors,
debug_descriptors: finished_context.debug_symbol_descriptors,
};
let function_obj = Function {
nargs: ast.args.len(),
name: "<anonymous>".to_string(),
context: Rc::new(sub_context),
context_id: finished_context.context_id,
least_ancestors: RwLock::new(None),
code: func_code,
};
let my_descr = self.create_constant_descriptor(ObjectRef::new(function_obj));
let mut res = vec![];
res.push(Op::push_const_clone(my_descr));
res.push(Op::attach_ancestors);
Ok(res)
}
pub fn compile_return_statement(&mut self, ast: &ReturnStatement) -> CompileResult {
let mut res = vec![];
res.append(&mut self.compile_expr(&ast.ret)?);
res.push(Op::ret);
Ok(res)
}
pub fn compile_format_string(&mut self, ast: &FormatString) -> CompileResult {
let mut res = vec![];
let debug_descr = self.create_debug_descriptor(ast.span);
res.push(Op::push_const(
self.create_constant_descriptor(StringObject::new(ast.val.clone())),
));
for subs in ast.substitutions.iter().rev() {
res.append(&mut self.compile_expr(subs)?);
}
res.push(Op::debug(debug_descr));
res.push(Op::fmt_string(ast.substitutions.len() as u8));
Ok(res)
}
pub fn compile_sh_statement(&mut self, ast: &ShStatement) -> CompileResult {
let mut res = vec![];
res.append(&mut self.compile_format_string(&ast.inner)?);
res.push(Op::debug(self.create_debug_descriptor(ast.span)));
res.push(Op::sh);
Ok(res)
}
pub fn compile_assign_to(&mut self, ast: &AssignmentLHS) -> CompileResult {
let debug_descr = self.create_debug_descriptor(ast.as_expr().span());
let mut res = vec![];
match &ast {
AssignmentLHS::Identifier(id) => {
res.push(Op::debug(debug_descr));
match self.name_lookup(&id.name) {
NameLookupResult::MyLocal(index) => {
res.push(Op::store(index));
}
NameLookupResult::ExternLocal(name) => {
res.push(Op::store_non_local(name));
}
_ => {
let local_name = self.local_name_gen();
let cid = self.context().context_id;
self.local_index
.insert(RustClone::clone(&(cid, id.name.clone())), local_name);
res.push(Op::store(local_name));
}
}
}
AssignmentLHS::AttrLookup(a_lookup) => {
res.append(&mut self.compile_expr(&a_lookup.parent)?);
res.push(Op::push_const(self.create_constant_descriptor(
StringObject::new(a_lookup.attribute.name.clone()),
)));
res.push(Op::swap);
res.push(Op::debug(debug_descr));
res.push(Op::set_attr);
}
AssignmentLHS::Indexed(indexed) => {
res.append(&mut self.compile_expr(&indexed.parent)?);
res.push(Op::swap);
res.append(&mut self.compile_expr(&indexed.index)?);
res.push(Op::swap);
res.push(Op::debug(debug_descr));
res.push(Op::index_set);
}
}
Ok(res)
}
pub fn compile_assignment(&mut self, ast: &Assignment) -> CompileResult {
let mut res = vec![];
res.append(&mut self.compile_expr(&ast.val)?);
res.append(&mut self.compile_assign_to(&ast.lhs)?);
Ok(res)
}
pub fn compile_statement(&mut self, ast: &Statement) -> CompileResult {
match ast {
Statement::ForLoop(f) => self.compile_for_loop(f),
Statement::WhileLoop(w) => self.compile_while_loop(w),
Statement::IfStatement(i) => self.compile_if_statement(i),
Statement::CaseOf(c) => self.compile_case_of(c),
Statement::ReturnStatement(r) => self.compile_return_statement(r),
Statement::ShStatement(s) => self.compile_sh_statement(s),
Statement::FuncDefinition(f) => self.compile_func_definition(f),
Statement::Assignment(a) => self.compile_assignment(a),
Statement::Expr(e) => {
let mut vals = self.compile_expr(e)?;
vals.push(Op::pop);
Ok(vals)
}
Statement::Error => unreachable!(),
}
}
pub fn compile_statement_list(&mut self, ast: &StatementList) -> CompileResult {
let mut res = vec![];
for statement in ast.statements.iter() {
res.append(&mut self.compile_statement(statement)?);
}
if *(&self.context().context_id) == 0 {
panic!();
}
Ok(res)
}
}