Skip to content

Commit 4660436

Browse files
committed
More debugging of polymorphism
1 parent 6f9f6fb commit 4660436

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

tiger/src/ast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ pub type InnerTypeWithPos = WithPos<InnerType>;
221221
pub struct TypeDec {
222222
pub name: SymbolWithPos,
223223
pub ty: TyWithPos,
224+
/// Generic types.
224225
pub ty_vars: TypeVars,
225226
}
226227

tiger/src/semant.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,11 @@ impl<'a, F: Clone + Debug + Frame + PartialEq> SemanticAnalyzer<'a, F> {
181181
fn actual_ty(&self, typ: &Type) -> Type {
182182
match *typ {
183183
Type::Var(ref type_var) => {
184-
println!("1: {:?}: {}", typ, self.symbols.name(type_var.0));
184+
//println!("1: {:?}: {}", typ, self.symbols.name(type_var.0));
185185
if let Some(actual_type) = self.env.look_type(type_var.0) {
186-
println!("2");
186+
//println!("2");
187187
if typ != actual_type {
188-
println!("3");
188+
//println!("3");
189189
return self.actual_ty(actual_type);
190190
}
191191
}
@@ -600,6 +600,7 @@ impl<'a, F: Clone + Debug + Frame + PartialEq> SemanticAnalyzer<'a, F> {
600600
}
601601

602602
for &WithPos { node: TypeDec { ref name, ref ty, ref ty_vars, .. }, .. } in type_declarations {
603+
println!("======\nType declaration name: {}", self.symbols.name(name.node));
603604
let has_type_args = !ty_vars.idents.is_empty();
604605
if has_type_args {
605606
self.env.begin_type_scope();
@@ -620,6 +621,7 @@ impl<'a, F: Clone + Debug + Frame + PartialEq> SemanticAnalyzer<'a, F> {
620621
None
621622
},
622623
Declaration::VariableDeclaration { ref init, name, ref typ, .. } => {
624+
println!("********\nVar {}", self.symbols.name(name));
623625
let exp = self.trans_exp(init, parent_level, done_label, true);
624626
let is_collectable = type_is_collectable(&exp.ty);
625627
let escape = self.env.look_escape(name);
@@ -641,6 +643,7 @@ impl<'a, F: Clone + Debug + Frame + PartialEq> SemanticAnalyzer<'a, F> {
641643
self.add_error(Error::RecordType { pos: declaration.pos });
642644
return None;
643645
}
646+
println!("===> ({}) Type: {:?}", self.symbols.name(name), exp.ty);
644647
let var = var_dec(&access, exp.exp);
645648
self.env.enter_var(name, Entry::Var { access, typ: exp.ty });
646649
Some(var)
@@ -727,6 +730,7 @@ impl<'a, F: Clone + Debug + Frame + PartialEq> SemanticAnalyzer<'a, F> {
727730
}
728731
},
729732
Expr::Call { ref args, ref function, ref type_args } => {
733+
// TODO: use type_args.
730734
match function.node {
731735
Expr::Variable(ref func) => {
732736
match self.env.look_var(func.node).cloned() { // TODO: remove this clone.
@@ -936,6 +940,7 @@ impl<'a, F: Clone + Debug + Frame + PartialEq> SemanticAnalyzer<'a, F> {
936940
EXP_TYPE_ERROR
937941
},
938942
Expr::Field { ref ident, ref this } => {
943+
println!(" | Field {:?}.{:?}", this.node, self.symbols.name(ident.node));
939944
let var = self.trans_exp(this, level, done_label, true);
940945
match var.ty {
941946
Type::App(TypeConstructor::Unique(inner_type, _), _) => {
@@ -954,6 +959,7 @@ impl<'a, F: Clone + Debug + Frame + PartialEq> SemanticAnalyzer<'a, F> {
954959
TypeConstructor::Record { name: record_type, ref types, .. } => {
955960
for (index, &(name, ref typ)) in types.iter().enumerate() {
956961
if name == ident.node {
962+
println!("Record field: {}: {:?}", self.symbols.name(name), typ);
957963
return ExpTy {
958964
exp: field_access::<F>(var.exp, index, FieldType::Record),
959965
ty: typ.clone(),
@@ -1195,6 +1201,7 @@ impl<'a, F: Clone + Debug + Frame + PartialEq> SemanticAnalyzer<'a, F> {
11951201
}
11961202
},
11971203
Expr::Record { ref fields, ref typ, ref type_args } => {
1204+
// TODO: use type_args to replace the generic types in the record type.
11981205
let ty = self.get_type(typ, AddError);
11991206
let mut field_exprs = vec![];
12001207
let data_layout =

tiger/src/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ pub struct TyVar(pub Symbol);
6262

6363
impl TyVar {
6464
pub fn from_symbol(symbol: Symbol) -> Self {
65+
if symbol == 78 {
66+
println!("****************************************************************************************************\nHERE");
67+
}
6568
Self(symbol)
6669
}
6770
}

tiger/tests/polymorphism.tig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,12 @@ in
6565
print(string_pair.second);
6666
print("\n");
6767
printi(two4s.head);
68+
/* FIXME: second get the type `a` declared in the type `pair`. It should be resolved to its actual type. */
6869
let var second := struct_pair.second
6970
in
70-
printi(second.c)
71+
printi(second.c) /* FIXME: this line causes the panic.
72+
Maybe this is because the type of second is not the correct one.
73+
*/
7174
end
7275
/* TODO: correct code generation (monomorphisation) for polymorphic code. */
7376
/*f(string_pair); /* Should be an error. */ */

0 commit comments

Comments
 (0)