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

Commit 3787836

Browse files
committed
Global names have no longer always uid == 0
uid == 0 is reserved for the Prelude to allow code generation and translation to have a simple construction of True, False etc. For all other modules a single uid is defined for the global bindings in that module. All instances and classes use the same uid as the one defined for the class in question. This is not fully implemented in the Renamer as of now
1 parent 0b79e1a commit 3787836

File tree

4 files changed

+52
-40
lines changed

4 files changed

+52
-40
lines changed

compiler.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ impl <'a> Compiler<'a> {
604604
}
605605
else {
606606
let fromInteger = Identifier(Id {
607-
name: Name { name: intern("fromInteger"), uid: 999999 },
607+
name: Name { name: intern("fromInteger"), uid: 0 },
608608
typ: qualified(~[], function_type_(int_type(), literal.typ.clone())),
609609
});
610610
let number = Literal(Literal { typ: int_type(), value: Integral(i) });
@@ -618,7 +618,7 @@ impl <'a> Compiler<'a> {
618618
}
619619
else {
620620
let fromRational = Identifier(Id {
621-
name: Name { name: intern("fromRational"), uid: 999999 },
621+
name: Name { name: intern("fromRational"), uid: 0 },
622622
typ: qualified(~[], function_type_(double_type(), literal.typ.clone())),
623623
});
624624
let number = Literal(Literal {
@@ -810,7 +810,7 @@ impl <'a> Compiler<'a> {
810810
let mut b = String::from_str("#");
811811
b.push_str(typename);
812812
b.push_str(name.as_slice());
813-
let instance_fn_name = Name { name: intern(b.as_slice()), uid: 0 };
813+
let instance_fn_name = Name { name: intern(b.as_slice()), uid: name.uid };
814814
match self.find(instance_fn_name) {
815815
Some(GlobalVariable(index)) => {
816816
instructions.push(PushGlobal(index));
@@ -979,7 +979,7 @@ impl <'a> Compiler<'a> {
979979
b.push_str(x.name.as_slice());
980980
b.push_str(decl.name.as_slice());
981981
let f = intern(b.as_slice());
982-
let name = Name { name: f, uid: 0 };
982+
let name = Name { name: f, uid: decl.name.uid };
983983
match self.find(name) {
984984
Some(GlobalVariable(index)) => {
985985
function_indexes.push(index as uint);

core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,8 @@ pub mod translate {
515515
//Example stub for undeclared (/=)
516516
//(/=) = #Eq/=
517517
Binding {
518-
name: Id::new(Name { name: instance_fn_name, uid: 0 }, typ.clone(), constraints.clone()),
519-
expression: Identifier(Id::new(Name { name: default_name, uid: 0 }, typ, constraints))
518+
name: Id::new(Name { name: instance_fn_name, uid: decl.name.uid }, typ.clone(), constraints.clone()),
519+
expression: Identifier(Id::new(Name { name: default_name, uid: decl.name.uid }, typ, constraints))
520520
}
521521
})
522522
.collect()

renamer.rs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub struct NameSupply {
5353
impl NameSupply {
5454

5555
pub fn new() -> NameSupply {
56-
NameSupply { unique_id: 0 }
56+
NameSupply { unique_id: 1 }
5757
}
5858
///Create a unique Name which are anonymous
5959
pub fn anonymous(&mut self) -> Name {
@@ -75,6 +75,11 @@ impl NameSupply {
7575

7676
///The renamer has methods which turns the ASTs identifiers from simple strings
7777
///into unique Names
78+
///Currently there is some constraints on what the unique ids should be.
79+
///Each module gets one uid which it uses for a top level declarations (bindings, types, etc)
80+
///All functions which are in a class or an instance gets the same id as the class has,
81+
///this is to allow the compiler to find the specific instance/class functions when it constructs dictionaries
82+
///All uid's of the other names can have any uid (as long as it introduces no name collisions)
7883
struct Renamer {
7984
///Mapping of strings into the unique name
8085
uniques: ScopedMap<InternedStr, Name>,
@@ -88,27 +93,19 @@ impl Renamer {
8893
Renamer { uniques: ScopedMap::new(), name_supply: NameSupply::new(), errors: Errors::new() }
8994
}
9095

91-
fn insert_globals(&mut self, module: &Module<InternedStr>) {
92-
for ctor in module.dataDefinitions.iter().flat_map(|data| data.constructors.iter()) {
93-
self.make_unique(ctor.name.clone());
94-
}
95-
for newtype in module.newtypes.iter() {
96-
self.make_unique(newtype.constructor_name.clone());
97-
}
98-
for bind in module.instances.iter().flat_map(|instance| binding_groups(instance.bindings.as_slice())) {
99-
self.make_unique(bind[0].name.clone());
100-
}
101-
for class in module.classes.iter() {
102-
self.make_unique(class.name);
103-
for decl in class.declarations.iter() {
104-
self.make_unique(decl.name.clone());
105-
}
106-
for bind in binding_groups(class.bindings) {
107-
self.make_unique(bind[0].name.clone());
108-
}
109-
}
110-
for bind in binding_groups(module.bindings.as_slice()) {
111-
self.make_unique(bind[0].name.clone());
96+
fn insert_globals(&mut self, module: &Module<InternedStr>, uid: uint) {
97+
let mut names = module.dataDefinitions.iter()
98+
.flat_map(|data| data.constructors.iter().map(|ctor| ctor.name))
99+
.chain(module.newtypes.iter().map(|newtype| newtype.constructor_name))
100+
.chain(module.instances.iter()
101+
.flat_map(|instance| binding_groups(instance.bindings.as_slice()).map(|binds| binds[0].name)))
102+
.chain(module.classes.iter().flat_map(|class|
103+
Some(class.name).move_iter()
104+
.chain(class.declarations.iter().map(|decl| decl.name))
105+
.chain(binding_groups(class.bindings).map(|binds| binds[0].name))))
106+
.chain(binding_groups(module.bindings.as_slice()).map(|binds| binds[0].name));
107+
for name in names {
108+
self.declare_global(name, uid);
112109
}
113110
}
114111

@@ -269,6 +266,12 @@ impl Renamer {
269266
u
270267
}
271268
}
269+
fn declare_global(&mut self, s: InternedStr, module_id: uint) -> Name {
270+
self.make_unique(s);
271+
let name = self.uniques.find_mut(&s).unwrap();
272+
name.uid = module_id;
273+
*name
274+
}
272275
}
273276

274277
pub fn rename_expr(expr: TypedExpr<InternedStr>) -> TypedExpr<Name> {
@@ -281,9 +284,14 @@ pub fn rename_module(module: Module<InternedStr>) -> Module<Name> {
281284
rename_module_(&mut renamer, module)
282285
}
283286
pub fn rename_module_(renamer: &mut Renamer, module: Module<InternedStr>) -> Module<Name> {
284-
renamer.insert_globals(&module);
287+
let mut name = renamer.make_unique(module.name);
288+
if name.as_slice() == "Prelude" {
289+
renamer.uniques.find_mut(&name.name).unwrap().uid = 0;
290+
name.uid = 0;
291+
}
292+
renamer.insert_globals(&module, name.uid);
285293
let Module {
286-
name: name,
294+
name: _,
287295
imports: imports,
288296
classes : classes,
289297
dataDefinitions: data_definitions,
@@ -405,7 +413,7 @@ pub fn rename_module_(renamer: &mut Renamer, module: Module<InternedStr>) -> Mod
405413
.collect();
406414

407415
Module {
408-
name: renamer.make_unique(name),
416+
name: name,
409417
imports: FromVec::from_vec(imports2),
410418
classes : FromVec::from_vec(classes2),
411419
dataDefinitions: FromVec::from_vec(data_definitions2),

vm.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -960,23 +960,25 @@ fn import() {
960960

961961
#[test]
962962
fn pattern_bind() {
963-
let result = execute_main(
964-
r"data Bool = True | False
963+
let result = execute_main_string(
964+
r"
965+
import Prelude
965966
966967
test :: [Bool] -> Bool
967968
test (True:[]) = False
968969
test (True:y:ys) = y
969970
test [] = False
970971
971972
main = test [True, True]
972-
".chars());
973+
")
974+
.unwrap_or_else(|err| fail!(err));
973975
assert_eq!(result, Some(ConstructorResult(0, Vec::new())));
974976
}
975977
#[test]
976978
fn pattern_guards() {
977-
let result = execute_main(
979+
let result = execute_main_string(
978980
r"
979-
data Bool = True | False
981+
import Prelude
980982
981983
test :: Int -> [a] -> Int
982984
test 2 _ = 2
@@ -987,15 +989,16 @@ test x _ = x
987989
988990
main = (test 2 [], test 100 [], test 100 ['c'])
989991
990-
".chars());
992+
")
993+
.unwrap_or_else(|err| fail!(err));
991994
assert_eq!(result, Some(ConstructorResult(0, vec!(IntResult(2), IntResult(1), IntResult(100)))));
992995
}
993996

994997
#[test]
995998
fn pattern_guards_nested() {
996-
let result = execute_main(
999+
let result = execute_main_string(
9971000
r"
998-
data Bool = True | False
1001+
import Prelude
9991002
10001003
test :: Int -> [Int] -> Int
10011004
test 2 _ = 2
@@ -1006,7 +1009,8 @@ test x _ = x
10061009
10071010
main = (test 2 [], test 100 [0], test 100 [0, 123])
10081011
1009-
".chars());
1012+
")
1013+
.unwrap_or_else(|err| fail!(err));
10101014
assert_eq!(result, Some(ConstructorResult(0, vec!(IntResult(2), IntResult(1), IntResult(100)))));
10111015
}
10121016
#[test]

0 commit comments

Comments
 (0)