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

Commit 0b79e1a

Browse files
committed
Changed most use of InternedStr to Name where name lookup is concerned
Tests are currently failing since there was much reliance on uid == 0 for global bindings, types etc.
1 parent 4bbf5f6 commit 0b79e1a

File tree

7 files changed

+259
-241
lines changed

7 files changed

+259
-241
lines changed

compiler.rs

Lines changed: 71 additions & 101 deletions
Large diffs are not rendered by default.

core.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@ impl Module<Id> {
3030

3131
#[deriving(Clone, PartialEq)]
3232
pub struct Class<Ident> {
33-
pub constraints: ~[Constraint],
34-
pub name : InternedStr,
33+
pub constraints: ~[Constraint<Name>],
34+
pub name : Name,
3535
pub variable : TypeVariable,
36-
pub declarations : ~[module::TypeDeclaration],
36+
pub declarations : ~[module::TypeDeclaration<Name>],
3737
pub bindings: ~[Binding<Ident>]
3838
}
3939

4040
#[deriving(Clone)]
4141
pub struct Instance<Ident = InternedStr> {
4242
pub bindings : ~[Binding<Ident>],
43-
pub constraints : ~[Constraint],
43+
pub constraints : ~[Constraint<Name>],
4444
pub typ : Type,
45-
pub classname : InternedStr
45+
pub classname : Name
4646
}
4747

4848
#[deriving(Clone, PartialEq)]
@@ -165,7 +165,7 @@ impl <'a> Equiv<&'a str> for Name {
165165
#[deriving(PartialEq, Eq, Hash, Clone)]
166166
pub struct Id<T = Name> {
167167
pub name: T,
168-
pub typ: Qualified<Type>
168+
pub typ: Qualified<Type, Name>
169169
}
170170

171171
impl fmt::Show for Id {
@@ -175,7 +175,7 @@ impl fmt::Show for Id {
175175
}
176176

177177
impl <T> Id<T> {
178-
pub fn new(name: T, typ: Type, constraints: ~[Constraint]) -> Id<T> {
178+
pub fn new(name: T, typ: Type, constraints: ~[Constraint<Name>]) -> Id<T> {
179179
Id { name: name, typ: module::qualified(constraints, typ) }
180180
}
181181
}
@@ -392,7 +392,7 @@ pub mod translate {
392392

393393
struct Translator<'a> {
394394
name_supply: NameSupply,
395-
functions_in_class: |InternedStr|:'a -> (&'a TypeVariable, &'a [TypeDeclaration])
395+
functions_in_class: |Name|:'a -> (&'a TypeVariable, &'a [TypeDeclaration<Name>])
396396
}
397397

398398
#[deriving(Show)]
@@ -490,7 +490,7 @@ pub mod translate {
490490
}
491491

492492
///Creates stub functions for each undeclared function in the instance
493-
fn create_default_stubs(class_var: &TypeVariable, class_decls: &[TypeDeclaration], instance: &Instance<Id<Name>>) -> Vec<Binding<Id<Name>>> {
493+
fn create_default_stubs(class_var: &TypeVariable, class_decls: &[TypeDeclaration<Name>], instance: &Instance<Id<Name>>) -> Vec<Binding<Id<Name>>> {
494494
class_decls.iter()
495495
.filter(|decl| instance.bindings.iter().find(|bind| bind.name.as_slice().ends_with(decl.name.as_slice())).is_none())
496496
.map(|decl| {
@@ -500,18 +500,17 @@ pub mod translate {
500500
let mut typ = decl.typ.clone();
501501
::typecheck::replace_var(&mut typ.value, class_var, &instance.typ);
502502
{
503-
let mut context = ~[];
504-
::std::mem::swap(&mut context, &mut typ.constraints);
503+
let context = ::std::mem::replace(&mut typ.constraints, box []);
505504
//Remove all constraints which refer to the class's variable
506-
let vec_context: Vec<Constraint> = context.move_iter()
505+
let vec_context: Vec<Constraint<Name>> = context.move_iter()
507506
.filter(|c| c.variables[0] != *class_var)
508507
.collect();
509508
typ.constraints = FromVec::from_vec(vec_context);
510509
}
511510
let Qualified { value: typ, constraints: constraints } = typ;
512-
let default_name = module::encode_binding_identifier(instance.classname, decl.name);
511+
let default_name = module::encode_binding_identifier(instance.classname.name, decl.name.name);
513512
let typ_name = module::extract_applied_type(&instance.typ).ctor().name;
514-
let instance_fn_name = module::encode_binding_identifier(typ_name, decl.name);
513+
let instance_fn_name = module::encode_binding_identifier(typ_name, decl.name.name);
515514

516515
//Example stub for undeclared (/=)
517516
//(/=) = #Eq/=
@@ -623,7 +622,7 @@ impl <'a> Translator<'a> {
623622
fn do_bind2_id(&mut self, m_a: Type, m_b: Type) -> Expr<Id<Name>> {
624623
debug!("m_a {}", m_a);
625624
let c = match *m_a.appl() {
626-
TypeVariable(ref var) => ~[Constraint { class: intern("Monad"), variables: ~[var.clone()] }],
625+
TypeVariable(ref var) => ~[Constraint { class: Name { name: intern("Monad"), uid: 0 }, variables: ~[var.clone()] }],
627626
_ => ~[]
628627
};
629628
let typ = function_type_(m_a, function_type_(m_b.clone(), m_b));
@@ -641,7 +640,7 @@ impl <'a> Translator<'a> {
641640
let m_b = result.get_type().clone();
642641
debug!("m_a {}", m_a);
643642
let c = match *m_a.appl() {
644-
TypeVariable(ref var) => ~[Constraint { class: intern("Monad"), variables: ~[var.clone()] }],
643+
TypeVariable(ref var) => ~[Constraint { class: Name { name: intern("Monad"), uid: 0 }, variables: ~[var.clone()] }],
645644
_ => ~[]
646645
};
647646
let arg2_type = function_type_(a.clone(), m_b.clone());

deriving.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub fn generate_deriving(instances: &mut Vec<Instance<Id<Name>>>, data: &DataDef
1616
instances.push(Instance {
1717
constraints: box [],
1818
typ: data.typ.value.clone(),
19-
classname: intern("Eq"),
19+
classname: Name { name: intern("Eq"), uid: 0 },
2020
bindings: FromVec::from_vec(bindings)
2121
});
2222
}
@@ -28,7 +28,7 @@ pub fn generate_deriving(instances: &mut Vec<Instance<Id<Name>>>, data: &DataDef
2828
instances.push(Instance {
2929
constraints: box [],
3030
typ: data.typ.value.clone(),
31-
classname: intern("Ord"),
31+
classname: Name { name: intern("Ord"), uid: 0 },
3232
bindings: FromVec::from_vec(bindings)
3333
});
3434
}
@@ -103,10 +103,10 @@ impl DerivingGen {
103103
let data_name = extract_applied_type(&data.typ.value).ctor().name;
104104
let name = encode_binding_identifier(data_name, intern(funcname));
105105
//Create a constraint for each type parameter
106-
fn make_constraints(mut result: Vec<Constraint>, class: InternedStr, typ: &Type) -> ~[Constraint] {
106+
fn make_constraints(mut result: Vec<Constraint<Name>>, class: InternedStr, typ: &Type) -> ~[Constraint<Name>] {
107107
match typ {
108108
&TypeApplication(ref f, ref param) => {
109-
result.push(Constraint { class: class, variables: box [param.var().clone()] });
109+
result.push(Constraint { class: Name { name: class, uid: 0 }, variables: box [param.var().clone()] });
110110
make_constraints(result, class, *f)
111111
}
112112
_ => FromVec::from_vec(result)

module.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub struct Module<Ident = InternedStr> {
1010
pub name : Ident,
1111
pub imports: ~[Import<Ident>],
1212
pub bindings : ~[Binding<Ident>],
13-
pub typeDeclarations : ~[TypeDeclaration],
13+
pub typeDeclarations : ~[TypeDeclaration<Ident>],
1414
pub classes : ~[Class<Ident>],
1515
pub instances : ~[Instance<Ident>],
1616
pub dataDefinitions : ~[DataDefinition<Ident>],
@@ -26,19 +26,19 @@ pub struct Import<Ident> {
2626

2727
#[deriving(Clone)]
2828
pub struct Class<Ident = InternedStr> {
29-
pub constraints: ~[Constraint],
30-
pub name : InternedStr,
29+
pub constraints: ~[Constraint<Ident>],
30+
pub name : Ident,
3131
pub variable : TypeVariable,
32-
pub declarations : ~[TypeDeclaration],
32+
pub declarations : ~[TypeDeclaration<Ident>],
3333
pub bindings: ~[Binding<Ident>]
3434
}
3535

3636
#[deriving(Clone)]
3737
pub struct Instance<Ident = InternedStr> {
3838
pub bindings : ~[Binding<Ident>],
39-
pub constraints : ~[Constraint],
39+
pub constraints : ~[Constraint<Ident>],
4040
pub typ : Type,
41-
pub classname : InternedStr
41+
pub classname : Ident
4242
}
4343

4444
#[deriving(Clone, PartialEq)]
@@ -47,21 +47,21 @@ pub struct Binding<Ident = InternedStr> {
4747
pub arguments: ~[Pattern<Ident>],
4848
pub matches: Match<Ident>,
4949
pub where : Option<~[Binding<Ident>]>,
50-
pub typ: Qualified<Type>
50+
pub typ: Qualified<Type, Ident>
5151
}
5252

5353
#[deriving(PartialEq, Eq, Clone, Show)]
5454
pub struct Constructor<Ident = InternedStr> {
5555
pub name : Ident,
56-
pub typ : Qualified<Type>,
56+
pub typ : Qualified<Type, Ident>,
5757
pub tag : int,
5858
pub arity : int
5959
}
6060

6161
#[deriving(PartialEq, Clone)]
6262
pub struct DataDefinition<Ident = InternedStr> {
6363
pub constructors : ~[Constructor<Ident>],
64-
pub typ : Qualified<Type>,
64+
pub typ : Qualified<Type, Ident>,
6565
pub parameters : HashMap<InternedStr, int>,
6666
pub deriving: ~[Ident]
6767
}
@@ -70,7 +70,7 @@ pub struct DataDefinition<Ident = InternedStr> {
7070
pub struct Newtype<Ident = InternedStr> {
7171
pub typ: Qualified<Type>,
7272
pub constructor_name: Ident,
73-
pub constructor_type: Qualified<Type>,
73+
pub constructor_type: Qualified<Type, Ident>,
7474
pub deriving: ~[Ident]
7575
}
7676

@@ -89,11 +89,11 @@ pub struct FixityDeclaration<Ident = InternedStr> {
8989
}
9090

9191
#[deriving(Clone, PartialEq, Eq, Default)]
92-
pub struct TypeDeclaration {
93-
pub typ : Qualified<Type>,
94-
pub name : InternedStr
92+
pub struct TypeDeclaration<Ident = InternedStr> {
93+
pub typ : Qualified<Type, Ident>,
94+
pub name : Ident
9595
}
96-
impl fmt::Show for TypeDeclaration {
96+
impl <T : fmt::Show> fmt::Show for TypeDeclaration<T> {
9797
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9898
write!(f, "{} :: {}", self.name, self.typ)
9999
}
@@ -188,7 +188,7 @@ pub enum Expr<Ident = InternedStr> {
188188
Case(Box<TypedExpr<Ident>>, ~[Alternative<Ident>]),
189189
IfElse(Box<TypedExpr<Ident>>, Box<TypedExpr<Ident>>, Box<TypedExpr<Ident>>),
190190
Do(~[DoBinding<Ident>], Box<TypedExpr<Ident>>),
191-
TypeSig(Box<TypedExpr<Ident>>, Qualified<Type>),
191+
TypeSig(Box<TypedExpr<Ident>>, Qualified<Type, Ident>),
192192
Paren(Box<TypedExpr<Ident>>)
193193
}
194194
impl <T: fmt::Show> fmt::Show for Binding<T> {

0 commit comments

Comments
 (0)