From 8e7089a5d6ba1f4f50a90133bb50bc5c6c554aff Mon Sep 17 00:00:00 2001 From: Santo Cariotti <santo@dcariotti.me> Date: Thu, 13 Jun 2024 11:00:06 +0200 Subject: Set up visitor (#3) Co-authored-by: geno <gabriele.genovese2@studio.unibo.it> --- src/ast/nodes/TrailerNode.java | 78 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/ast/nodes/TrailerNode.java (limited to 'src/ast/nodes/TrailerNode.java') diff --git a/src/ast/nodes/TrailerNode.java b/src/ast/nodes/TrailerNode.java new file mode 100644 index 0000000..fa7e1ed --- /dev/null +++ b/src/ast/nodes/TrailerNode.java @@ -0,0 +1,78 @@ +package com.clp.project.ast.nodes; + +import java.util.ArrayList; + +import com.clp.project.semanticanalysis.SemanticError; +import com.clp.project.semanticanalysis.SymbolTable; +import com.clp.project.ast.types.*; +import org.antlr.v4.runtime.tree.TerminalNode; + +/** + * Node for the `trailer` statement of the grammar. + */ +public class TrailerNode implements Node { + private Node arglist; + private ArrayList<Node> exprs; + private TerminalNode methodCall; + private boolean isEmpty; + + public TrailerNode(Node arglist, ArrayList<Node> exprs, TerminalNode methodCall) { + this.arglist = arglist; + this.exprs = exprs; + this.methodCall = methodCall; + + this.isEmpty = (this.arglist == null && this.exprs.size() == 0 && this.methodCall == null); + } + + @Override + public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) { + ArrayList<SemanticError> errors = new ArrayList<SemanticError>(); + + if (arglist != null) { + errors.addAll(arglist.checkSemantics(ST, _nesting)); + } + + for (var expr : exprs) { + errors.addAll(expr.checkSemantics(ST, _nesting)); + } + + return errors; + } + + @Override + public Type typeCheck() { + return new VoidType(); + } + + // TODO: add code generation for trailer node + @Override + public String codeGeneration() { + return ""; + } + + @Override + public String toPrint(String prefix) { + String str = prefix + "TrailerNode\n"; + + prefix += " "; + + if (arglist != null) { + str += arglist.toPrint(prefix); + } + + for (var expr : exprs) { + str += expr.toPrint(prefix); + } + + if (methodCall != null) { + str += prefix + "Method(" + methodCall + ")\n"; + } + + if (isEmpty) { + str += prefix + "()\n"; + } + + return str; + } + +} -- cgit v1.2.3-18-g5258