summaryrefslogtreecommitdiff
path: root/src/ast/nodes/ImportNode.java
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2024-06-30 13:45:57 +0200
committerGitHub <noreply@github.com>2024-06-30 13:45:57 +0200
commit8aa8b5834953cab15c0687608f4fafea2e6c61be (patch)
tree1835ae7c789136b4a6c44c12efd4247a0b96d893 /src/ast/nodes/ImportNode.java
parent7125bb27fedaafd5a56b5122e4251b182448ddac (diff)
parent06d7c8dee25c065b1a569337f34d3cd5e892a31d (diff)
Merge pull request #10 from boozec/check-semantics
Co-Authored-By: geno <gabriele.genovese2@studio.unibo.it> Co-Authored-By: L0P0P <grassoemanuele@live.com>
Diffstat (limited to 'src/ast/nodes/ImportNode.java')
-rw-r--r--src/ast/nodes/ImportNode.java33
1 files changed, 23 insertions, 10 deletions
diff --git a/src/ast/nodes/ImportNode.java b/src/ast/nodes/ImportNode.java
index e264c99..7e95ee3 100644
--- a/src/ast/nodes/ImportNode.java
+++ b/src/ast/nodes/ImportNode.java
@@ -1,20 +1,20 @@
package ast.nodes;
+import ast.types.*;
import java.util.ArrayList;
-
import semanticanalysis.SemanticError;
import semanticanalysis.SymbolTable;
-import ast.types.*;
/**
* Node for the `import_stmt` statement of the grammar.
*/
public class ImportNode implements Node {
- private Node dottedName;
- private boolean isFrom;
- private boolean importAs;
- private boolean importAll;
- private ArrayList<String> names;
+
+ private final Node dottedName;
+ private final boolean isFrom;
+ private final boolean importAs;
+ private final boolean importAll;
+ private final ArrayList<String> names;
public ImportNode(Node dottedName, boolean isFrom, boolean importAs, boolean importAll,
ArrayList<String> names) {
@@ -27,14 +27,26 @@ public class ImportNode implements Node {
@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
- ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
+ ArrayList<SemanticError> errors = new ArrayList();
+
+ if (isFrom) {
+ for (int i = 0; i < names.size(); ++i) {
+ ST.insert(names.get(i), this.typeCheck(), _nesting, null);
+ }
+ } else {
+ errors.addAll(dottedName.checkSemantics(ST, _nesting));
+ }
+
+ if (importAs) {
+ ST.insert(names.get(names.size() - 1), this.typeCheck(), _nesting, null);
+ }
return errors;
}
@Override
public Type typeCheck() {
- return new VoidType();
+ return new ImportType();
}
// NOTE: we do not want to provide a code generation for this statement
@@ -63,8 +75,9 @@ public class ImportNode implements Node {
}
for (int i = 0; i < names.size(); ++i) {
- if (i == 0 && importAs)
+ if (i == 0 && importAs) {
continue;
+ }
str += prefix + names.get(i) + "\n";
}