From 8e7089a5d6ba1f4f50a90133bb50bc5c6c554aff Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 13 Jun 2024 11:00:06 +0200 Subject: Set up visitor (#3) Co-authored-by: geno --- src/ast/nodes/SimpleStmtNode.java | 84 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/ast/nodes/SimpleStmtNode.java (limited to 'src/ast/nodes/SimpleStmtNode.java') diff --git a/src/ast/nodes/SimpleStmtNode.java b/src/ast/nodes/SimpleStmtNode.java new file mode 100644 index 0000000..3f4dec3 --- /dev/null +++ b/src/ast/nodes/SimpleStmtNode.java @@ -0,0 +1,84 @@ +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.*; + +/** + * Node for the `simple_stmt` statement of the grammar. + */ +public class SimpleStmtNode implements Node { + private Node assignment; + private Node expr; + private Node returnStmt; + private Node importStmt; + + public SimpleStmtNode(Node assignment, Node expr, Node returnStmt, Node importStmt) { + this.assignment = assignment; + this.expr = expr; + this.returnStmt = returnStmt; + this.importStmt = importStmt; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + if (assignment != null) { + errors.addAll(assignment.checkSemantics(ST, _nesting)); + } + + if (expr != null) { + errors.addAll(expr.checkSemantics(ST, _nesting)); + } + + if (returnStmt != null) { + errors.addAll(returnStmt.checkSemantics(ST, _nesting)); + } + + if (importStmt != null) { + errors.addAll(importStmt.checkSemantics(ST, _nesting)); + } + + return errors; + } + + @Override + public Type typeCheck() { + return new VoidType(); + } + + // TODO: add code generation for SimpleStmtNode + @Override + public String codeGeneration() { + return ""; + } + + @Override + public String toPrint(String prefix) { + String str = prefix + "SimpleStmt\n"; + + prefix += " "; + + if (assignment != null) { + str += assignment.toPrint(prefix); + } + + if (expr != null) { + str += expr.toPrint(prefix); + } + + if (returnStmt != null) { + str += returnStmt.toPrint(prefix); + } + + if (importStmt != null) { + str += importStmt.toPrint(prefix); + } + + return str; + } + +} -- cgit v1.2.3-18-g5258