/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This file is part of SimpleC. * * See the file "SimpleC-LICENSE" for Copyright information and * * the terms and conditions for copying, distribution and * * modification of SimpleC. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ package org.sablecc.simplec; import java.util.*; class Variable { static int next_number; final int number = next_number++; static LinkedList variables = new LinkedList(); Type type; Variable parent = this; int rank = 0; LinkedList pending = new LinkedList(); Variable(Type t) { type = t; variables.add(this); } Variable union(Variable var) { Variable x = ecr(); Variable y = var.ecr(); if(x == y) { return x; } if(x.rank > y.rank) { y.parent = x; return x; } x.parent = y; if(x.rank == y.rank) { y.rank++; } return y; } Variable ecr() { if(this != parent) { parent = parent.ecr(); } return parent; } void setType(Type t) { Variable e = ecr(); e.type = t; for(Iterator i = e.pending.iterator(); i.hasNext();) { e.join((Variable) i.next()); } } void cjoin(Variable v) { Variable e1 = ecr(); Variable e2 = v.ecr(); if(e2.type == Bottom.instance) { e2.pending.add(e1); } else { e1.join(e2); } } void join(Variable v) { Variable e1 = ecr(); Variable e2 = v.ecr(); Type t1 = e1.type; Type t2 = e2.type; Variable e = e1.union(e2); if(t1 == Bottom.instance) { e.type = t2; if(t2 == Bottom.instance) { e1.pending.addAll(e2.pending); e.pending = e1.pending; } else { for(Iterator i = e1.pending.iterator(); i.hasNext();) { e.join((Variable) i.next()); } } } else { e.type = t1; if(t2 == Bottom.instance) { for(Iterator i = e2.pending.iterator(); i.hasNext();) { e.join((Variable) i.next()); } } else { t1.unify(t2); } } } public String toString() { return "T" + number + " = " + type; } }