6.3 Type Equivalence

While type checking, we will have to check equivalence of types. For simple types, it is a matter of merely comparing type(np1) with type(np2), where np1 and np2 are pointers to nodes representing the two program components.

When we have complex data types, we need to ensure that the two type trees have exactly the same structure. We do this by a int tree_comp(Node *np1, Node *np2) function:

int tree_comp(Node *np1, Node *np2){
   if(type(np1) == type(np2)){
       if(type(np1) < TREE) return 1;
       retrun tree_comp_rec(ttree(np1), ttree(np2));
   }
   return 0;
}
int tree_comp_rec(Node *np1, Node *np2){
   return tree_comp(np1, np2);
}

Get Compilers: Principles and Practice now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.