Skip to content
This repository was archived by the owner on Apr 14, 2020. It is now read-only.

Commit ef316a5

Browse files
committed
Fixed indexing get(i) -> [i], dereferncing, lifetime issues
1 parent 8f36b08 commit ef316a5

16 files changed

+430
-437
lines changed

compiler.rs

Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use interner::*;
22
use core::*;
33
use core::Expr::*;
4-
use types::{int_type, double_type, function_type, function_type_, qualified};
4+
use types::{int_type, double_type, function_type, function_type_, qualified, extract_applied_type};
55
use typecheck::{Types, DataTypes, TypeEnvironment, find_specialized_instances};
66
use scoped_map::ScopedMap;
77
use std::iter::range_step;
88
use std::io::IoResult;
9+
use std::borrow::ToOwned;
910

1011
use core::translate::{translate_module, translate_modules};
1112
use lambda_lift::do_lambda_lift;
@@ -144,7 +145,7 @@ impl Globals for Assembly {
144145
for class in self.classes.iter() {
145146
for decl in class.declarations.iter() {
146147
if decl.name == name {
147-
return Some(Var::Class(&decl.typ.value, decl.typ.constraints, &class.variable));
148+
return Some(Var::Class(&decl.typ.value, &*decl.typ.constraints, &class.variable));
148149
}
149150
}
150151
}
@@ -153,7 +154,7 @@ impl Globals for Assembly {
153154
for sc in self.superCombinators.iter() {
154155
if name == sc.name {
155156
if sc.typ.constraints.len() > 0 {
156-
return Some(Var::Constraint(self.offset + index, &sc.typ.value, sc.typ.constraints));
157+
return Some(Var::Constraint(self.offset + index, &sc.typ.value, &*sc.typ.constraints));
157158
}
158159
else {
159160
return Some(Var::Global(self.offset + index));
@@ -180,7 +181,7 @@ fn find_global<'a>(module: &'a Module<Id>, offset: uint, name: Name) -> Option<V
180181
for class in module.classes.iter() {
181182
for decl in class.declarations.iter() {
182183
if decl.name == name {
183-
return Some(Var::Class(&decl.typ.value, decl.typ.constraints, &class.variable));
184+
return Some(Var::Class(&decl.typ.value, &*decl.typ.constraints, &class.variable));
184185
}
185186
}
186187
}
@@ -195,7 +196,7 @@ fn find_global<'a>(module: &'a Module<Id>, offset: uint, name: Name) -> Option<V
195196
let typ = bind.expression.get_type();
196197
let constraints = &bind.name.typ.constraints;
197198
if constraints.len() > 0 {
198-
Var::Constraint(offset + global_index, typ, *constraints)
199+
Var::Constraint(offset + global_index, typ, &**constraints)
199200
}
200201
else {
201202
Var::Global(offset + global_index)
@@ -275,13 +276,6 @@ impl Types for Module<Id> {
275276
}
276277
}
277278

278-
fn extract_applied_type<'a>(typ: &'a Type) -> &'a Type {
279-
match typ {
280-
&Type::Application(ref lhs, _) => extract_applied_type(*lhs),
281-
_ => typ
282-
}
283-
}
284-
285279
impl Types for Assembly {
286280
///Lookup a type
287281
fn find_type<'a>(&'a self, name: &Name) -> Option<&'a Qualified<Type, Name>> {
@@ -318,11 +312,11 @@ impl Types for Assembly {
318312
for &(ref constraints, ref op) in self.instances.iter() {
319313
match op {
320314
&Type::Application(ref op, ref t) => {
321-
let x = match extract_applied_type(*op) {
315+
let x = match extract_applied_type(&**op) {
322316
&Type::Constructor(ref x) => x,
323317
_ => panic!()
324318
};
325-
let y = match extract_applied_type(*t) {
319+
let y = match extract_applied_type(&**t) {
326320
&Type::Constructor(ref x) => x,
327321
_ => panic!()
328322
};
@@ -331,8 +325,7 @@ impl Types for Assembly {
331325
_ => panic!()
332326
};
333327
if classname.name == x.name && y.name == z.name {
334-
let o : &Type = *t;
335-
return Some((constraints.as_slice(), o));
328+
return Some((constraints.as_slice(), &**t));
336329
}
337330
}
338331
_ => ()
@@ -443,7 +436,7 @@ impl <'a> Compiler<'a> {
443436
Assembly {
444437
superCombinators: superCombinators,
445438
instance_dictionaries: instance_dictionaries,
446-
offset: self.assemblies.iter().flat_map(|assembly| assembly.superCombinators.iter()).len(),
439+
offset: self.assemblies.iter().fold(0, |sum, assembly| sum + assembly.superCombinators.len()),
447440
classes: module.classes.clone(),
448441
instances: module.instances.iter()
449442
.map(|x| (x.constraints.clone(), Type::new_op(x.classname.name, vec![x.typ.clone()])))
@@ -485,7 +478,7 @@ impl <'a> Compiler<'a> {
485478
match expr {
486479
&Lambda(ref ident, ref body) => {
487480
self.new_stack_var(ident.name.clone());
488-
1 + self.compile_lambda_binding(*body, instructions)
481+
1 + self.compile_lambda_binding(&**body, instructions)
489482
}
490483
_ => {
491484
self.compile(expr, instructions, true);
@@ -502,7 +495,7 @@ impl <'a> Compiler<'a> {
502495
Some(ref module) => {
503496
let n = self.assemblies.len();
504497
let offset = if n > 0 {
505-
let assembly = self.assemblies.get(n-1);
498+
let assembly = self.assemblies[n - 1];
506499
assembly.offset + assembly.superCombinators.len()
507500
}
508501
else {
@@ -608,7 +601,7 @@ impl <'a> Compiler<'a> {
608601
name: Name { name: intern("fromInteger"), uid: 0 },
609602
typ: qualified(vec![], function_type_(int_type(), literal.typ.clone())),
610603
});
611-
let number = Literal(Literal { typ: int_type(), value: Integral(i) });
604+
let number = Literal(LiteralData { typ: int_type(), value: Integral(i) });
612605
let apply = Apply(box fromInteger, box number);
613606
self.compile(&apply, instructions, strict);
614607
}
@@ -622,7 +615,7 @@ impl <'a> Compiler<'a> {
622615
name: Name { name: intern("fromRational"), uid: 0 },
623616
typ: qualified(vec![], function_type_(double_type(), literal.typ.clone())),
624617
});
625-
let number = Literal(Literal {
618+
let number = Literal(LiteralData {
626619
typ: double_type(),
627620
value: Fractional(f)
628621
});
@@ -653,12 +646,12 @@ impl <'a> Compiler<'a> {
653646
this.compile(&bind.expression, instructions, false);
654647
this.stackSize += 1;
655648
}
656-
this.compile(*body, instructions, strict);
649+
this.compile(&**body, instructions, strict);
657650
instructions.push(Slide(bindings.len()));
658651
});
659652
}
660653
&Case(ref body, ref alternatives) => {
661-
self.compile(*body, instructions, true);
654+
self.compile(&**body, instructions, true);
662655
self.stackSize += 1;
663656
//Dummy variable for the case expression
664657
//Storage for all the jumps that should go to the end of the case expression
@@ -680,19 +673,19 @@ impl <'a> Compiler<'a> {
680673
//We need to set all the jump instructions to their actual location
681674
//and append Slide instructions to bring the stack back to normal if the match fails
682675
for j in range_step(pattern_end, pattern_start, -1) {
683-
match *instructions.get(j as uint) {
676+
match instructions[j as uint] {
684677
Jump(_) => {
685-
*instructions.get_mut(j as uint) = Jump(instructions.len());
678+
instructions[j as uint] = Jump(instructions.len());
686679
}
687-
JumpFalse(_) => *instructions.get_mut(j as uint) = JumpFalse(instructions.len()),
680+
JumpFalse(_) => instructions[j as uint] = JumpFalse(instructions.len()),
688681
Split(size) => instructions.push(Pop(size)),
689682
_ => ()
690683
}
691684
}
692685
});
693686
}
694687
for branch in end_branches.iter() {
695-
*instructions.get_mut(*branch) = Jump(instructions.len());
688+
instructions[*branch] = Jump(instructions.len());
696689
}
697690
//Remove the matched expr
698691
instructions.push(Slide(1));
@@ -707,7 +700,7 @@ impl <'a> Compiler<'a> {
707700
//Unroll the applications until the function is found
708701
match *expr {
709702
Apply(ref func, ref arg) => {
710-
return self.compile_apply(*func, ArgList::Cons(*arg, &args), instructions, strict)
703+
return self.compile_apply(&**func, ArgList::Cons(&**arg, &args), instructions, strict)
711704
}
712705
_ => ()
713706
}
@@ -839,15 +832,15 @@ impl <'a> Compiler<'a> {
839832
Some(index) => instructions.push(PushDictionaryMember(index)),
840833
None => {
841834
let dictionary_key = find_specialized_instances(function_type, actual_type, constraints);
842-
self.push_dictionary(constraints, dictionary_key, instructions);
835+
self.push_dictionary(constraints, &*dictionary_key, instructions);
843836
}
844837
}
845838
}
846839
_ => {
847840
//get dictionary index
848841
//push dictionary
849842
let dictionary_key = find_specialized_instances(function_type, actual_type, constraints);
850-
self.push_dictionary(constraints, dictionary_key, instructions);
843+
self.push_dictionary(constraints, &*dictionary_key, instructions);
851844
}
852845
}
853846
}
@@ -873,8 +866,8 @@ impl <'a> Compiler<'a> {
873866
debug!("App for ({:?} {:?})", lhs, rhs);
874867
//For function in functions
875868
// Mkap function fold_dictionary(rhs)
876-
self.fold_dictionary(class, *lhs, instructions);
877-
self.fold_dictionary(class, *rhs, instructions);
869+
self.fold_dictionary(class, &**lhs, instructions);
870+
self.fold_dictionary(class, &**rhs, instructions);
878871
instructions.push(MkapDictionary);
879872
}
880873
Type::Variable(ref var) => {
@@ -912,7 +905,7 @@ impl <'a> Compiler<'a> {
912905
}
913906
let mut ii = 0;
914907
for c in constraints.iter() {
915-
let result = self.walk_classes(c.class, &mut |declarations| {
908+
let result = self.walk_classes(c.class, &mut |declarations| -> Option<uint> {
916909
for decl in declarations.iter() {
917910
if decl.name == name {
918911
return Some(ii)
@@ -946,7 +939,7 @@ impl <'a> Compiler<'a> {
946939
//Check if the dictionary already exist
947940
let dict_len = self.instance_dictionaries.len();
948941
for ii in range(0, dict_len) {
949-
if self.instance_dictionaries.get(ii).ref0().equiv(&constraints) {
942+
if self.instance_dictionaries[ii].0 == constraints {
950943
return ii;
951944
}
952945
}
@@ -962,13 +955,6 @@ impl <'a> Compiler<'a> {
962955

963956
fn add_class(&self, constraints: &[(Name, Type)], function_indexes: &mut Vec<uint>) {
964957

965-
fn extract_applied_type<'a>(typ: &'a Type) -> &'a Type {
966-
match typ {
967-
&Type::Application(ref lhs, _) => extract_applied_type(*lhs),
968-
_ => typ
969-
}
970-
}
971-
972958
for &(ref class_name, ref typ) in constraints.iter() {
973959
self.walk_classes(*class_name, &mut |declarations| -> Option<()> {
974960
for decl in declarations.iter() {
@@ -1053,8 +1039,8 @@ fn try_find_instance_type<'a>(class_var: &TypeVariable, class_type: &Type, actua
10531039
None
10541040
}
10551041
(&Type::Application(ref lhs1, ref rhs1), &Type::Application(ref lhs2, ref rhs2)) => {
1056-
try_find_instance_type(class_var, *lhs1, *lhs2)
1057-
.or_else(|| try_find_instance_type(class_var, *rhs1, *rhs2))
1042+
try_find_instance_type(class_var, &**lhs1, &**lhs2)
1043+
.or_else(|| try_find_instance_type(class_var, &**rhs1, &**rhs2))
10581044
}
10591045
_ => None
10601046
}
@@ -1063,10 +1049,10 @@ fn try_find_instance_type<'a>(class_var: &TypeVariable, class_type: &Type, actua
10631049
#[allow(dead_code)]
10641050
pub fn compile(contents: &str) -> Assembly {
10651051
let mut type_env = TypeEnvironment::new();
1066-
compile_with_type_env(&mut type_env, [], contents)
1052+
compile_with_type_env(&mut type_env, &[], contents)
10671053
}
10681054
#[allow(dead_code)]
1069-
pub fn compile_with_type_env(type_env: &mut TypeEnvironment, assemblies: &[&Assembly], contents: &str) -> Assembly {
1055+
pub fn compile_with_type_env<'a>(type_env: &'a mut TypeEnvironment<'a>, assemblies: &[&'a Assembly], contents: &str) -> Assembly {
10701056
use parser::Parser;
10711057

10721058
let mut parser = Parser::new(contents.as_slice().chars());

0 commit comments

Comments
 (0)