#Need help understanding how two classes are working with each other

30 messages · Page 1 of 1 (latest)

kindred folio
#

So I got a few methods:

plush thunderBOT
#

This post has been reserved for your question.

Hey @kindred folio! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

kindred folio
#
    private List<Symbol> inheritanceList(ClassNode type) {
        List<Symbol> ancestors = new ArrayList<>();
        if (type != null && type.getParent() != null) {
            ClassNode parent = classTableMap.get(type.getParent());
            while (parent != null) {
                ancestors.add(parent.getName());
                parent = classTableMap.get(parent.getParent().getName());
            }
        }
        return ancestors;
    }```
#

and also:

#
    public Symbol findLUB(Symbol t1, Symbol t2) {
        List<Symbol> t1Ancestors = inheritanceList(classTable.get(t1));
        List<Symbol> t2Ancestors = inheritanceList(classTable.get(t2));

        List<Symbol> higherUp = new ArrayList<>();
        List<Symbol> lowerDown = new ArrayList<>();

        if (t1Ancestors.size() > t2Ancestors.size()) {
            higherUp = t1Ancestors;
            lowerDown = t2Ancestors;
        }
        else {
            higherUp = t2Ancestors;
            lowerDown = t1Ancestors;
        }

        for (Symbol t1Ancestor : higherUp) {
            for (Symbol t2Ancestor : lowerDown) {
                if (t1Ancestor.equals(t2Ancestor))
                    return t1Ancestor;
            }
        }
        return TreeConstants.Object_;
    }```
kindred folio
kindred folio
#

i'm getting an error in the first two lines in the second method

#

bceause it can't resolve inheritanceList(classTable.get(t1/2));

#

which i get but i'm not sure how to get the two classes to work together

#

program enters from Semant.java which looks like:

#
import ast.ProgramNode;

class Semant {

    public static ClassTable classTable;
    public static SymbolTable symtable = new SymbolTable();

    public static void analyze(ProgramNode program) {
        //System.out.println("Hello from semant.java");
        classTable = new ClassTable(program.getClasses());

        ScopeCheckingVisitor scopecheckVisitor = new ScopeCheckingVisitor();
        program.accept(scopecheckVisitor, null);
        TypeCheckingVisitor typecheckVisitor = new TypeCheckingVisitor();
        program.accept(typecheckVisitor, null);

        /*if (Utilities.errors()) {
            Utilities.fatalError(Utilities.ErrorCode.ERROR_SEMANT);
        }*/
    }

}
#

i mean i'm assuming it's because

#

TypeCheckingVisitor has no way of knowing what's going on in ClassTable.java

#

and vice versa

#

but i'm not sure how to get them to communicate with each othere

#

here

#

ok i changed a few things

#

the first method now looks like:

#
    private List<Symbol> inheritanceList(Symbol type) {
        ClassNode node = classTableMap.get(type);
        List<Symbol> ancestors = new ArrayList<>();
        if (node != null && node.getParent() != null) {
            ClassNode parent = classTableMap.get(node.getParent());
            while (parent != null) {
                ancestors.add(parent.getName());
                parent = classTableMap.get(parent.getParent().getName());
            }
        }
        return ancestors;
    }```
#

the second now looks like:

#
    public Symbol findLUB(Symbol t1, Symbol t2) {
        List<Symbol> t1Ancestors = inheritanceList(t1);
        List<Symbol> t2Ancestors = inheritanceList(t2);

        List<Symbol> higherUp = new ArrayList<>();
        List<Symbol> lowerDown = new ArrayList<>();

        if (t1Ancestors.size() > t2Ancestors.size()) {
            higherUp = t1Ancestors;
            lowerDown = t2Ancestors;
        }
        else {
            higherUp = t2Ancestors;
            lowerDown = t1Ancestors;
        }

        for (Symbol t1Ancestor : higherUp) {
            for (Symbol t2Ancestor : lowerDown) {
                if (t1Ancestor.equals(t2Ancestor))
                    return t1Ancestor;
            }
        }
        return TreeConstants.Object_;
    }```
#

everything else remains unchanged

#

classTableMap is a field in ClassTable.java

#

im not sure why the instance of ClassTable made in Semant.java serve as a global instance in typechecking visitor

#

or if that's how ur meant to do it?

dark phoenix
#

this is quite honestly incoherent

#

what exactly is the issue you're encountering, what classes do you have and what methods do you have in those classes? (just the names/signatures, you don't need to show the full class)

plush thunderBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.