From ce49d20a5fe3726e1800bc495a25c7617212abf4 Mon Sep 17 00:00:00 2001 From: Emanuele Grasso <96300448+L0P0P@users.noreply.github.com> Date: Sun, 14 Jul 2024 18:48:09 +0200 Subject: Reaching definition analysis (#17) Co-authored-by: Santo Cariotti Co-authored-by: geno Co-authored-by: geno --- src/ast/nodes/AssignmentNode.java | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'src/ast/nodes/AssignmentNode.java') diff --git a/src/ast/nodes/AssignmentNode.java b/src/ast/nodes/AssignmentNode.java index 13e7a33..688be76 100644 --- a/src/ast/nodes/AssignmentNode.java +++ b/src/ast/nodes/AssignmentNode.java @@ -16,15 +16,20 @@ public class AssignmentNode implements Node { private final Node assign; private final ExprListNode rhr; + private final int startIndex; + private final int endIndex; + // Useful for code gen private int offset; private boolean alreadyDef; - public AssignmentNode(Node lhr, Node assign, Node rhr) { + public AssignmentNode(Node lhr, Node assign, Node rhr, int startIndex, int endIndex) { this.lhr = (ExprListNode) lhr; this.assign = assign; this.rhr = (ExprListNode) rhr; this.alreadyDef = false; + this.startIndex = startIndex; + this.endIndex = endIndex; } @Override @@ -42,11 +47,10 @@ public class AssignmentNode implements Node { STentry e = ST.lookup(leftAtom.getId()); if (ft != null) { ft.addLocalVar(); - } else { - Label.addGlobalVar(); } if (e == null) { + Label.addGlobalVar(); ST.insert(leftAtom.getId(), new AtomType(), _nesting, ""); e = ST.lookup(leftAtom.getId()); } else { @@ -58,9 +62,9 @@ public class AssignmentNode implements Node { offset = e.getOffset(); } return errors; + } - // TODO: check it out for this type @Override public Type typeCheck() { return rhr.typeCheck(); @@ -91,10 +95,32 @@ public class AssignmentNode implements Node { return rhrString + lhrString; } + @Override + public String printAST(String prefix) { + return prefix + "Assignment\n" + lhr.printAST(prefix + " ") + assign.printAST(prefix + " ") + + rhr.printAST(prefix + " "); + } + @Override public String toPrint(String prefix) { - return prefix + "Assignment\n" + lhr.toPrint(prefix + " ") + assign.toPrint(prefix + " ") - + rhr.toPrint(prefix + " "); + String str = prefix; + str += lhr.toPrint("") + " " + assign.toPrint("") + " " + rhr.toPrint(""); + return str; } + public ExprListNode getLhr() { + return lhr; + } + + public ExprListNode getRhr() { + return rhr; + } + + public int getLhrIndex() { + return this.startIndex; + } + + public int getRhrIndex() { + return this.endIndex; + } } -- cgit v1.2.3-18-g5258