From eb7fb04d2dfadd004fa86f28da42681e0038df06 Mon Sep 17 00:00:00 2001 From: geno Date: Thu, 27 Jun 2024 18:20:34 +0200 Subject: left, right = 1, 2 assignment covered sfaccimm ho fatto refactoring dell exprlist --- src/ast/nodes/TestlistCompNode.java | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/ast/nodes/TestlistCompNode.java (limited to 'src/ast/nodes/TestlistCompNode.java') diff --git a/src/ast/nodes/TestlistCompNode.java b/src/ast/nodes/TestlistCompNode.java new file mode 100644 index 0000000..070b1a1 --- /dev/null +++ b/src/ast/nodes/TestlistCompNode.java @@ -0,0 +1,61 @@ +package ast.nodes; + +import ast.types.*; +import java.util.ArrayList; +import semanticanalysis.SemanticError; +import semanticanalysis.SymbolTable; + +/** + * Node for the `testlist_comp` statement of the grammar. + */ +public class TestlistCompNode implements Node { + + private final ArrayList exprs; + + public TestlistCompNode(ArrayList _exprs) { + this.exprs = _exprs; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + for (var param : exprs) { + errors.addAll(param.checkSemantics(ST, _nesting)); + } + + return errors; + } + + public int getSize() { + return exprs.size(); + } + + public Node getElem(int i) { + return exprs.get(i); + } + + @Override + public Type typeCheck() { + return new VoidType(); + } + + // TODO: code generation for expr list + @Override + public String codeGeneration() { + return ""; + } + + @Override + public String toPrint(String prefix) { + String str = prefix + "Testlist_comp\n"; + + prefix += " "; + for (var param : exprs) { + str += param.toPrint(prefix); + } + + return str; + } + +} -- cgit v1.2.3-71-g8e6c From fd85e9980c0305c6dfb91aaeb199430a89163c3e Mon Sep 17 00:00:00 2001 From: geno Date: Thu, 27 Jun 2024 20:49:56 +0200 Subject: fixed comp_for and added reseved word type --- src/ast/Python3VisitorImpl.java | 44 +++++++++++++++++++++++++-- src/ast/nodes/ArglistNode.java | 38 ++++++++++++----------- src/ast/nodes/AtomNode.java | 13 +++++--- src/ast/nodes/CompForNode.java | 59 ++++++++++++++++++++++++++++++++++++ src/ast/nodes/CompIterNode.java | 50 ++++++++++++++++++++++++++++++ src/ast/nodes/TestlistCompNode.java | 19 +++++++++--- src/ast/types/ContinueBreakType.java | 10 ------ src/ast/types/NoneType.java | 16 ++++++++++ src/ast/types/ReservedWordsType.java | 10 ++++++ 9 files changed, 221 insertions(+), 38 deletions(-) create mode 100644 src/ast/nodes/CompForNode.java create mode 100644 src/ast/nodes/CompIterNode.java delete mode 100644 src/ast/types/ContinueBreakType.java create mode 100644 src/ast/types/NoneType.java create mode 100644 src/ast/types/ReservedWordsType.java (limited to 'src/ast/nodes/TestlistCompNode.java') diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index 5b812ea..c5f1bfa 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -434,6 +434,8 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { Testlist_compContext tlc = ctx.testlist_comp(); if (ctx.NUMBER() != null) { return new AtomNode(ctx.NUMBER().toString(), null); + } else if (ctx.NONE() != null) { + return new AtomNode(ctx.NONE().toString(), null); } else if (ctx.TRUE() != null) { return new AtomNode(ctx.TRUE().toString(), null); } else if (ctx.FALSE() != null) { @@ -453,7 +455,7 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { } else if (ctx.OPEN_PAREN() != null && ctx.CLOSE_PAREN() != null) { return manageTlc(tlc); } - return new AtomNode(ctx.NONE().toString(), null); + return new AtomNode(null, null); } public AtomNode manageTlc(Testlist_compContext tlc) { @@ -533,7 +535,45 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { for (ExprContext c : ctx.expr()) { exprlist.add(visit(c)); } + Comp_forContext cfc = ctx.comp_for(); + if (cfc != null) { + Node comp = visit(ctx.comp_for()); + return new TestlistCompNode(exprlist, comp); + } + return new TestlistCompNode(exprlist, null); + } + + /** + * Returns a `CompForNode`. + * + * ``` comp_for : 'for' exprlist 'in' expr comp_iter? ;``` + */ + public Node visitComp_for(Comp_forContext ctx) { + Node exprlist = visit(ctx.exprlist()); + Node expr = visit(ctx.expr()); + Comp_iterContext cic = ctx.comp_iter(); - return new TestlistCompNode(exprlist); + if (cic != null) { + Node comp = visit(ctx.comp_iter()); + return new CompForNode(exprlist, expr, comp); + } + return new CompForNode(exprlist, expr, null); + } + + /** + * Returns a `CompIterNode`. + * + * ``` comp_iter : comp_for | comp_if ; ;``` + */ + public Node visitComp_iter(Comp_iterContext ctx) { + // TODO: Implement comp_if + // Node iter = visit(ctx.comp_if()); + Comp_forContext cfc = ctx.comp_for(); + if (cfc != null) { + Node forNode = visit(ctx.comp_for()); + return new CompIterNode(forNode); + + } + return new CompIterNode(null); } } diff --git a/src/ast/nodes/ArglistNode.java b/src/ast/nodes/ArglistNode.java index 983d150..c29a4e0 100644 --- a/src/ast/nodes/ArglistNode.java +++ b/src/ast/nodes/ArglistNode.java @@ -22,25 +22,27 @@ public class ArglistNode implements Node { ArrayList errors = new ArrayList(); for (var arg : arguments) { - ExprNode argExpr = (ExprNode) arg; - String argName = argExpr.getId(); - - // TODO: check fucking IntType for params - // TODO: remove fucking comments - if (argName != null) { - if (Arrays.asList(bif).contains(argName)) { - continue; + if (arg instanceof ExprNode) { + ExprNode argExpr = (ExprNode) arg; + String argName = argExpr.getId(); + + // TODO: check fucking IntType for params + // TODO: remove fucking comments + if (argName != null) { + if (Arrays.asList(bif).contains(argName)) { + continue; + } + + if (ST.lookup(argName) != null && ST.lookup(argName).getType() instanceof ImportType) { + continue; + } + + if (ST.nslookup(argName) < 0 && argExpr.typeCheck() instanceof AtomType) { + errors.add(new SemanticError("name '" + argName + "' is not defined.")); + } + } else { + errors.addAll(arg.checkSemantics(ST, _nesting)); } - - if (ST.lookup(argName) != null && ST.lookup(argName).getType() instanceof ImportType) { - continue; - } - - if (ST.nslookup(argName) < 0 && argExpr.typeCheck() instanceof AtomType) { - errors.add(new SemanticError("name '" + argName + "' is not defined.")); - } - } else { - errors.addAll(arg.checkSemantics(ST, _nesting)); } } diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index 592aac4..16281d6 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -50,23 +50,28 @@ public class AtomNode implements Node { return new VoidType(); } + Pattern noneVariable = Pattern.compile("^(None)$"); Pattern booleanVariable = Pattern.compile("^(True|False)$"); - Pattern continueBreakVariable = Pattern.compile("^(continue|break)$"); + Pattern reservedWords = Pattern.compile("^(continue|break|int|float)$"); // this regex should match every possible atom name written in this format: CHAR (CHAR | DIGIT)* Pattern simpleVariable = Pattern.compile("^[a-zA-Z][a-zA-Z0-9]*$", Pattern.CASE_INSENSITIVE); + Matcher noneVariableMatcher = noneVariable.matcher(this.val); Matcher booleanVariableMatcher = booleanVariable.matcher(this.val); - Matcher continueBreakVariableMatcher = continueBreakVariable.matcher(this.val); + Matcher reservedWordsMatcher = reservedWords.matcher(this.val); Matcher simpleVariableMatcher = simpleVariable.matcher(this.val); + boolean matchFoundNone = noneVariableMatcher.find(); boolean matchFoundBoolean = booleanVariableMatcher.find(); - boolean matchFoundContinueBreak = continueBreakVariableMatcher.find(); + boolean matchFoundContinueBreak = reservedWordsMatcher.find(); boolean matchFoundSimpleVariable = simpleVariableMatcher.find(); if (matchFoundBoolean) { return new BoolType(); } else if (matchFoundContinueBreak) { - return new ContinueBreakType(); + return new ReservedWordsType(); + } else if (matchFoundNone) { + return new NoneType(); } else if (matchFoundSimpleVariable) { return new AtomType(); // could be a variable or a fuction } else { diff --git a/src/ast/nodes/CompForNode.java b/src/ast/nodes/CompForNode.java new file mode 100644 index 0000000..d5c50d6 --- /dev/null +++ b/src/ast/nodes/CompForNode.java @@ -0,0 +1,59 @@ +package ast.nodes; + +import ast.types.*; +import java.util.ArrayList; +import java.util.Arrays; +import semanticanalysis.SemanticError; +import semanticanalysis.SymbolTable; + +/** + * Node for the `comp_for` statement of the grammar. 'for' exprlist 'in' expr + * comp_iter? + */ +public class CompForNode implements Node { + + protected ExprListNode exprlist; + protected ExprNode single_expr; + protected CompIterNode comp_iter; + + public CompForNode(Node exprlist, Node single_expr, Node comp_iter) { + this.exprlist = (ExprListNode) exprlist; + this.single_expr = (ExprNode) single_expr; + this.comp_iter = (CompIterNode) comp_iter; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + errors.addAll(exprlist.checkSemantics(ST, _nesting)); + errors.addAll(single_expr.checkSemantics(ST, _nesting)); + if (comp_iter != null) { + errors.addAll(comp_iter.checkSemantics(ST, _nesting)); + } + return errors; + } + + @Override + public Type typeCheck() { + return new VoidType(); + } + + // TODO: add code generation for arglist node + @Override + public String codeGeneration() { + return ""; + } + + @Override + public String toPrint(String prefix) { + String str = prefix + "CompForNode\n"; + + prefix += " "; + str += exprlist.toPrint(prefix); + str += single_expr.toPrint(prefix); + str += comp_iter.toPrint(prefix); + return str; + } + +} diff --git a/src/ast/nodes/CompIterNode.java b/src/ast/nodes/CompIterNode.java new file mode 100644 index 0000000..6704bd7 --- /dev/null +++ b/src/ast/nodes/CompIterNode.java @@ -0,0 +1,50 @@ +package ast.nodes; + +import ast.types.*; +import java.util.ArrayList; +import semanticanalysis.SemanticError; +import semanticanalysis.SymbolTable; + +/** + * Node for the `comp_iter` statement of the grammar. + */ +public class CompIterNode implements Node { + + protected CompForNode comp_for; + // protected CompIfNode compIfNode; + + public CompIterNode(Node comp_for) { + this.comp_for = (CompForNode) comp_for; + } + + @Override + public ArrayList checkSemantics(SymbolTable ST, int _nesting) { + ArrayList errors = new ArrayList(); + + if (comp_for != null) { + errors.addAll(comp_for.checkSemantics(ST, _nesting)); + } + return errors; + } + + @Override + public Type typeCheck() { + return new VoidType(); + } + + // TODO: add code generation for arglist node + @Override + public String codeGeneration() { + return ""; + } + + @Override + public String toPrint(String prefix) { + String str = prefix + "CompIterNode\n"; + + prefix += " "; + str += comp_for.toPrint(prefix); + return str; + } + +} diff --git a/src/ast/nodes/TestlistCompNode.java b/src/ast/nodes/TestlistCompNode.java index 070b1a1..bcaca83 100644 --- a/src/ast/nodes/TestlistCompNode.java +++ b/src/ast/nodes/TestlistCompNode.java @@ -11,17 +11,28 @@ import semanticanalysis.SymbolTable; public class TestlistCompNode implements Node { private final ArrayList exprs; + private final CompForNode comp; - public TestlistCompNode(ArrayList _exprs) { - this.exprs = _exprs; + public TestlistCompNode(ArrayList exprs, Node comp) { + this.exprs = exprs; + this.comp = (CompForNode) comp; } @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { ArrayList errors = new ArrayList(); - for (var param : exprs) { - errors.addAll(param.checkSemantics(ST, _nesting)); + if (comp != null) { + // if comp is set, then we save the atom in the ST (we assume the first expr is an atom) + String id = ((ExprNode) exprs.get(0)).getId(); + Type t = ((ExprNode) exprs.get(0)).typeCheck(); + ST.insert(id, t, _nesting, ""); + // errors.addAll(comp.checkSemantics(ST, _nesting)); + } else { + // if comp is not set, then exprs is a list of 1 or more element + for (var param : exprs) { + errors.addAll(param.checkSemantics(ST, _nesting)); + } } return errors; diff --git a/src/ast/types/ContinueBreakType.java b/src/ast/types/ContinueBreakType.java deleted file mode 100644 index dfcf1f2..0000000 --- a/src/ast/types/ContinueBreakType.java +++ /dev/null @@ -1,10 +0,0 @@ -package ast.types; - -/** - * A type for the continue and break statements. - */ -public class ContinueBreakType extends Type { - public String toPrint(String prefix) { - return prefix + "ContinueBreak\n"; - } -} diff --git a/src/ast/types/NoneType.java b/src/ast/types/NoneType.java new file mode 100644 index 0000000..42f7fd7 --- /dev/null +++ b/src/ast/types/NoneType.java @@ -0,0 +1,16 @@ +package ast.types; + +/** + * A none type. None return unit. + */ +public class NoneType extends Type { + + public String toPrint(String prefix) { + return prefix + "None\n"; + } + + @Override + public String toString() { + return "None"; + } +} diff --git a/src/ast/types/ReservedWordsType.java b/src/ast/types/ReservedWordsType.java new file mode 100644 index 0000000..f5f7ac5 --- /dev/null +++ b/src/ast/types/ReservedWordsType.java @@ -0,0 +1,10 @@ +package ast.types; + +/** + * A type for the continue and break statements. + */ +public class ReservedWordsType extends Type { + public String toPrint(String prefix) { + return prefix + "ReservedWords\n"; + } +} -- cgit v1.2.3-71-g8e6c From 3f29e5101d9c6e670dd05715297f2060707dc6c7 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Thu, 27 Jun 2024 22:18:55 +0200 Subject: Fix trailer with parentheeses `for (key, val) in ...` Resolves test `a228.py` --- src/ast/nodes/TestlistCompNode.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/ast/nodes/TestlistCompNode.java') diff --git a/src/ast/nodes/TestlistCompNode.java b/src/ast/nodes/TestlistCompNode.java index bcaca83..4ae77c9 100644 --- a/src/ast/nodes/TestlistCompNode.java +++ b/src/ast/nodes/TestlistCompNode.java @@ -23,7 +23,8 @@ public class TestlistCompNode implements Node { ArrayList errors = new ArrayList(); if (comp != null) { - // if comp is set, then we save the atom in the ST (we assume the first expr is an atom) + // if comp is set, then we save the atom in the ST (we assume the first expr is + // an atom) String id = ((ExprNode) exprs.get(0)).getId(); Type t = ((ExprNode) exprs.get(0)).typeCheck(); ST.insert(id, t, _nesting, ""); @@ -31,6 +32,8 @@ public class TestlistCompNode implements Node { } else { // if comp is not set, then exprs is a list of 1 or more element for (var param : exprs) { + var exp = (ExprNode) param; + ST.insert(exp.getId(), exp.typeCheck(), _nesting, ""); errors.addAll(param.checkSemantics(ST, _nesting)); } } -- cgit v1.2.3-71-g8e6c From 37665fb6d0bc1eb29396ae949354cf7d6f9d54ca Mon Sep 17 00:00:00 2001 From: geno Date: Fri, 28 Jun 2024 12:23:28 +0200 Subject: resolving all the comments santo made --- src/Main.java | 15 +-------------- src/ParseAll.java | 11 +---------- src/ast/Python3VisitorImpl.java | 19 +++++++++++-------- src/ast/nodes/AtomNode.java | 4 ++++ src/ast/nodes/ExprListNode.java | 9 ++++++++- src/ast/nodes/ExprNode.java | 14 ++++++++++++-- src/ast/nodes/ForStmtNode.java | 20 ++++++++++++++++++++ src/ast/nodes/TestlistCompNode.java | 11 +++++++++-- src/ast/types/FunctionType.java | 9 +++++---- src/semanticanalysis/Share.java | 33 +++++++++++++++++++++++++++------ 10 files changed, 98 insertions(+), 47 deletions(-) (limited to 'src/ast/nodes/TestlistCompNode.java') diff --git a/src/Main.java b/src/Main.java index 04f7183..4f5d45f 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,7 +1,4 @@ -import java.io.BufferedReader; -import java.io.FileReader; -import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import javax.swing.*; @@ -27,7 +24,7 @@ public class Main { try { String fileStr = args[0]; System.out.println(fileStr); - System.out.println(readFile(fileStr)); + System.out.println(Share.readFile(fileStr)); CharStream cs = CharStreams.fromFileName(fileStr); Python3Lexer lexer = new Python3Lexer(cs); CommonTokenStream tokens = new CommonTokenStream(lexer); @@ -71,14 +68,4 @@ public class Main { } } - private static String readFile(String filePath) throws IOException { - StringBuilder content = new StringBuilder(); - try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { - String line; - while ((line = reader.readLine()) != null) { - content.append(line).append("\n"); - } - } - return content.toString(); - } } diff --git a/src/ParseAll.java b/src/ParseAll.java index 2662a85..568c89c 100644 --- a/src/ParseAll.java +++ b/src/ParseAll.java @@ -22,7 +22,7 @@ public class ParseAll { // fileStr = "./progs/wrong.py"; try { - if (!file.isFile() || !getExtension(file.getName()).equals("py")) { + if (!file.isFile() || !Share.getExtension(file.getName()).equals("py")) { System.err.println("Wont parse: " + fileStr); continue; } @@ -56,13 +56,4 @@ public class ParseAll { } } } - - public static String getExtension(String fileName) { - int extensionIndex = fileName.lastIndexOf('.'); - if (extensionIndex == -1) { - return fileName; - } else { - return fileName.substring(extensionIndex + 1); - } - } } diff --git a/src/ast/Python3VisitorImpl.java b/src/ast/Python3VisitorImpl.java index 42e2537..1cf15b7 100644 --- a/src/ast/Python3VisitorImpl.java +++ b/src/ast/Python3VisitorImpl.java @@ -424,7 +424,7 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { } /** - * Returns an `AtomNode`. FIXME: add support for testlist_comp + * Returns an `AtomNode`. * * ``` atom : '(' testlist_comp? ')' | '[' testlist_comp? ']' | '{' * testlist_comp? '}' | NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | @@ -449,22 +449,26 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { } return new AtomNode(varName, null); } else if (ctx.OPEN_BRACE() != null && ctx.CLOSE_BRACE() != null) { - return manageTlc(tlc); + return manageCompListContext(tlc); } else if (ctx.OPEN_BRACK() != null && ctx.CLOSE_BRACK() != null) { - return manageTlc(tlc); + return manageCompListContext(tlc); } else if (ctx.OPEN_PAREN() != null && ctx.CLOSE_PAREN() != null) { - return manageTlc(tlc); + return manageCompListContext(tlc); } return new AtomNode(null, null); } - public AtomNode manageTlc(Testlist_compContext tlc) { + /** + * Supporting function for `visitAtom`. Returns an `AtomNode` with + * `testlist_comp` set if the context is not null. Otherwise, returns an + * `AtomNode` with nulls. + */ + public AtomNode manageCompListContext(Testlist_compContext tlc) { if (tlc != null) { Node testlist_comp = visit(tlc); return new AtomNode(null, testlist_comp); - } else { - return new AtomNode(null, null); } + return new AtomNode(null, null); } /** @@ -529,7 +533,6 @@ public class Python3VisitorImpl extends Python3ParserBaseVisitor { * ``` testlist_comp : expr (comp_for | (',' expr)* ','?) ; ``` */ public Node visitTestlist_comp(Testlist_compContext ctx) { - // TODO: implement comp_for ArrayList exprlist = new ArrayList(); for (ExprContext c : ctx.expr()) { diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index cea617f..fef72ec 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -20,6 +20,10 @@ public class AtomNode implements Node { this.exprlist = (TestlistCompNode) exprlist; } + /** + * Returns the identifier of the `AtomNode` if it's not `null`, otherwise + * returns `null`. + */ public String getId() { return this.val; } diff --git a/src/ast/nodes/ExprListNode.java b/src/ast/nodes/ExprListNode.java index acdf679..800f4be 100644 --- a/src/ast/nodes/ExprListNode.java +++ b/src/ast/nodes/ExprListNode.java @@ -31,7 +31,14 @@ public class ExprListNode implements Node { return exprs.size(); } + /** + * Returns the i-th expressions of `exprs` field. If the index is greater or + * equals than the size return `null`. + */ public Node getElem(int i) { + if (i >= this.exprs.size()) { + return null; + } return exprs.get(i); } @@ -48,7 +55,7 @@ public class ExprListNode implements Node { @Override public String toPrint(String prefix) { - String str = prefix + "Paramlist\n"; + String str = prefix + "ExprList\n"; prefix += " "; for (var param : exprs) { diff --git a/src/ast/nodes/ExprNode.java b/src/ast/nodes/ExprNode.java index 591ee37..8e3b896 100644 --- a/src/ast/nodes/ExprNode.java +++ b/src/ast/nodes/ExprNode.java @@ -26,6 +26,10 @@ public class ExprNode implements Node { this.trailers = trailers; } + /** + * Returns the i-th expressions of `exprs` field. If the index is greater or + * equals than the size return `null`. + */ public Node getExpr(int i) { if (i >= this.exprs.size()) { return null; @@ -34,6 +38,10 @@ public class ExprNode implements Node { return this.exprs.get(i); } + /** + * Returns the identifier of the `AtomNode` if it's not `null`, otherwise + * returns `null`. + */ public String getId() { if (atom != null) { return ((AtomNode) this.atom).getId(); @@ -65,6 +73,7 @@ public class ExprNode implements Node { for (var t : trailers) { errors.addAll(t.checkSemantics(ST, _nesting)); } + } else { FunctionType ft = (FunctionType) fun.getType(); int paramNumber = ft.getParamNumber(); @@ -80,6 +89,7 @@ public class ExprNode implements Node { for (var trailer : trailers) { errors.addAll(trailer.checkSemantics(ST, _nesting)); } + } } else if (atom != null) { errors.addAll(atom.checkSemantics(ST, _nesting)); @@ -126,11 +136,11 @@ public class ExprNode implements Node { } for (var expr : exprs) { - str += expr.toPrint(prefix); + str = expr.toPrint(prefix); } for (var trailer : trailers) { - str += trailer.toPrint(prefix); + str = trailer.toPrint(prefix); } if (op != null) { diff --git a/src/ast/nodes/ForStmtNode.java b/src/ast/nodes/ForStmtNode.java index c5301ee..d4c5e56 100644 --- a/src/ast/nodes/ForStmtNode.java +++ b/src/ast/nodes/ForStmtNode.java @@ -18,18 +18,38 @@ public class ForStmtNode implements Node { this.block = block; } + /** + * This methods check the semantics of the operation `for i in list: block`. + * + * After the `for` keyword, it's parsed as a list of expression. We verified + * that the last expression is always gonna be `expr comp_op expr`. We + * comp_op must be the `in` keyword. `i` could be of any lenght (e.g. `a, b, + * c`). + * + * In this function we define the following approch: we save in the + * SymbolTable every atom until the last expression in the expression's + * list. For the last element, we know that is in the `expr comp_op expr` + * format, so we take the left element and save it in the SymbolicTable as + * an atom. + */ @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { ArrayList errors = new ArrayList(); + // Save every atom in the expression's list, except the last one var l = (ExprListNode) exprList; for (int i = 0; i < l.getSize() - 1; ++i) { var e = (ExprNode) l.getElem(i); ST.insert(e.getId(), e.typeCheck(), _nesting, ""); } + // Manage the last expression of expression's list + // Get the ExprNode var left = (ExprNode) l.getElem(l.getSize() - 1); + // Get the left side expression of the operation, that + // corresponds to the first element of the expression list. var atomLeft = (ExprNode) left.getExpr(0); + // ENHANCE: check that the comp_op is the `in` keyword ST.insert(atomLeft.getId(), atomLeft.typeCheck(), _nesting, ""); errors.addAll(exprList.checkSemantics(ST, _nesting)); diff --git a/src/ast/nodes/TestlistCompNode.java b/src/ast/nodes/TestlistCompNode.java index 4ae77c9..244f4ef 100644 --- a/src/ast/nodes/TestlistCompNode.java +++ b/src/ast/nodes/TestlistCompNode.java @@ -10,8 +10,8 @@ import semanticanalysis.SymbolTable; */ public class TestlistCompNode implements Node { - private final ArrayList exprs; - private final CompForNode comp; + private ArrayList exprs; + private CompForNode comp; public TestlistCompNode(ArrayList exprs, Node comp) { this.exprs = exprs; @@ -45,7 +45,14 @@ public class TestlistCompNode implements Node { return exprs.size(); } + /** + * Returns the i-th expressions of `exprs` field. If the index is greater or + * equals than the size return `null`. + */ public Node getElem(int i) { + if (i >= this.exprs.size()) { + return null; + } return exprs.get(i); } diff --git a/src/ast/types/FunctionType.java b/src/ast/types/FunctionType.java index 464bb9c..ef50641 100644 --- a/src/ast/types/FunctionType.java +++ b/src/ast/types/FunctionType.java @@ -1,18 +1,19 @@ package ast.types; /** - * An tom type. TODO: do I need to use this one? + * A Function type. */ public class FunctionType extends Type { - private final int paramNumber; - private final Type returnType; + private int paramNumber; + private Type returnType; public FunctionType(int paramNumber, Type returnType) { this.paramNumber = paramNumber; this.returnType = returnType; } + // Return the length of the parameters public int getParamNumber() { return paramNumber; } @@ -22,6 +23,6 @@ public class FunctionType extends Type { } public String toPrint(String prefix) { - return prefix + "Atom\n"; + return prefix + "Function\n"; } } diff --git a/src/semanticanalysis/Share.java b/src/semanticanalysis/Share.java index c1f03c2..c98fc53 100644 --- a/src/semanticanalysis/Share.java +++ b/src/semanticanalysis/Share.java @@ -1,13 +1,18 @@ package semanticanalysis; -import java.util.ArrayList; +import java.util.*; +import java.io.*; public class Share { - public static ArrayList removeDuplicates(ArrayList list) { - ArrayList newList = new ArrayList(); + /** + * Removes the duplicate elements in a list of Semantic Errors. It's not + * generic because it's used a custom contains function. + */ + public static ArrayList removeDuplicates(ArrayList list) { + ArrayList newList = new ArrayList(); - for (T element : list) { + for (SemanticError element : list) { if (!customContains(newList, element)) { newList.add(element); } @@ -15,9 +20,14 @@ public class Share { return newList; } - public static boolean customContains(ArrayList list, T e) { + /** + * Normal contains did not work, so we made a custom contains function. + * Returns `true` if the String rappresentation of an object in the list is + * equal to the element given in input. + */ + private static boolean customContains(ArrayList list, SemanticError e) { String e1 = e.toString(); - for (T element : list) { + for (SemanticError element : list) { String e2 = element.toString(); if (e2.equals(e1)) { return true; @@ -34,4 +44,15 @@ public class Share { return fileName.substring(extensionIndex + 1); } } + + public static String readFile(String filePath) throws IOException { + StringBuilder content = new StringBuilder(); + try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { + String line; + while ((line = reader.readLine()) != null) { + content.append(line).append("\n"); + } + } + return content.toString(); + } } -- cgit v1.2.3-71-g8e6c From aaa97e2e41c0aa17a6f99099dd39bb73a935fe02 Mon Sep 17 00:00:00 2001 From: L0P0P Date: Sat, 29 Jun 2024 09:54:08 +0200 Subject: Fixed some warnings --- src/ast/nodes/AssignmentNode.java | 4 +++- src/ast/nodes/CompForNode.java | 1 - src/ast/nodes/ExprListNode.java | 2 +- src/ast/nodes/TestlistCompNode.java | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) (limited to 'src/ast/nodes/TestlistCompNode.java') diff --git a/src/ast/nodes/AssignmentNode.java b/src/ast/nodes/AssignmentNode.java index 35c47b3..74d7283 100644 --- a/src/ast/nodes/AssignmentNode.java +++ b/src/ast/nodes/AssignmentNode.java @@ -29,7 +29,9 @@ public class AssignmentNode implements Node { errors.addAll(rhr.checkSemantics(ST, _nesting)); int lsize = lhr.getSize(); - int rsize = rhr.getSize(); + + // FIXME: unused variable + // int rsize = rhr.getSize(); // if (lsize == rsize) { for (int i = 0; i < lsize; i++) { diff --git a/src/ast/nodes/CompForNode.java b/src/ast/nodes/CompForNode.java index d5c50d6..06c7aee 100644 --- a/src/ast/nodes/CompForNode.java +++ b/src/ast/nodes/CompForNode.java @@ -2,7 +2,6 @@ package ast.nodes; import ast.types.*; import java.util.ArrayList; -import java.util.Arrays; import semanticanalysis.SemanticError; import semanticanalysis.SymbolTable; diff --git a/src/ast/nodes/ExprListNode.java b/src/ast/nodes/ExprListNode.java index 800f4be..4760db8 100644 --- a/src/ast/nodes/ExprListNode.java +++ b/src/ast/nodes/ExprListNode.java @@ -18,7 +18,7 @@ public class ExprListNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList<>(); for (var expr : exprs) { errors.addAll(expr.checkSemantics(ST, _nesting)); diff --git a/src/ast/nodes/TestlistCompNode.java b/src/ast/nodes/TestlistCompNode.java index 244f4ef..cba056c 100644 --- a/src/ast/nodes/TestlistCompNode.java +++ b/src/ast/nodes/TestlistCompNode.java @@ -20,7 +20,7 @@ public class TestlistCompNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList<>(); if (comp != null) { // if comp is set, then we save the atom in the ST (we assume the first expr is -- cgit v1.2.3-71-g8e6c From afff6a80cd58f7787efa1398f7c8cbdce8989323 Mon Sep 17 00:00:00 2001 From: geno Date: Sun, 30 Jun 2024 13:36:37 +0200 Subject: fixed warnings, better formatting, final added where possible --- src/Main.java | 2 +- src/ParseAll.java | 2 +- src/ast/nodes/ArglistNode.java | 5 ++--- src/ast/nodes/AssignmentNode.java | 13 ++++++------- src/ast/nodes/AtomNode.java | 2 +- src/ast/nodes/AugassignNode.java | 5 +++-- src/ast/nodes/BlockNode.java | 10 ++++------ src/ast/nodes/CompForNode.java | 2 +- src/ast/nodes/CompIterNode.java | 2 +- src/ast/nodes/CompNode.java | 4 ++-- src/ast/nodes/CompoundNode.java | 10 +++++----- src/ast/nodes/DottedNameNode.java | 3 ++- src/ast/nodes/ExprNode.java | 12 ++++++------ src/ast/nodes/ForStmtNode.java | 6 +++--- src/ast/nodes/FuncdefNode.java | 11 ++++++----- src/ast/nodes/IfNode.java | 15 ++++++++------- src/ast/nodes/ImportNode.java | 19 ++++++++++--------- src/ast/nodes/Node.java | 10 ++++------ src/ast/nodes/ParamdefNode.java | 2 +- src/ast/nodes/ParamlistNode.java | 7 +++---- src/ast/nodes/ReturnStmtNode.java | 8 ++++---- src/ast/nodes/RootNode.java | 4 ++-- src/ast/nodes/SimpleStmtNode.java | 11 ++++++----- src/ast/nodes/SimpleStmtsNode.java | 5 +++-- src/ast/nodes/TestlistCompNode.java | 4 ++-- src/ast/nodes/TrailerNode.java | 14 +++++++------- src/ast/nodes/WhileStmtNode.java | 10 +++++----- src/ast/types/AtomType.java | 3 ++- src/ast/types/BoolType.java | 2 ++ src/ast/types/ErrorType.java | 2 ++ src/ast/types/FunctionType.java | 5 +++-- src/ast/types/ImportType.java | 3 ++- src/ast/types/IntType.java | 2 ++ src/ast/types/NoneType.java | 1 + src/ast/types/ReservedWordsType.java | 2 ++ src/ast/types/Type.java | 11 +++++------ src/ast/types/VoidType.java | 1 + src/semanticanalysis/STentry.java | 7 ++++--- src/semanticanalysis/SemanticError.java | 4 +++- src/semanticanalysis/Share.java | 4 ++-- src/semanticanalysis/SymbolTable.java | 20 ++++++-------------- 41 files changed, 136 insertions(+), 129 deletions(-) (limited to 'src/ast/nodes/TestlistCompNode.java') diff --git a/src/Main.java b/src/Main.java index 4f5d45f..f53b410 100644 --- a/src/Main.java +++ b/src/Main.java @@ -54,7 +54,7 @@ public class Main { Node ast = visitor.visit(tree); ArrayList errorsWithDup = ast.checkSemantics(ST, 0); ArrayList errors = Share.removeDuplicates(errorsWithDup); - if (errors.size() > 0) { + if (!errors.isEmpty()) { System.out.println("You had " + errors.size() + " errors:"); for (SemanticError e : errors) { System.out.println("\t" + e); diff --git a/src/ParseAll.java b/src/ParseAll.java index 568c89c..7def6f4 100644 --- a/src/ParseAll.java +++ b/src/ParseAll.java @@ -41,7 +41,7 @@ public class ParseAll { Node ast = visitor.visit(tree); ArrayList errorsWithDup = ast.checkSemantics(ST, 0); ArrayList errors = Share.removeDuplicates(errorsWithDup); - if (errors.size() > 0) { + if (!errors.isEmpty()) { System.out.println(); System.out.println(fileStr); System.out.println("You had " + errors.size() + " errors:"); diff --git a/src/ast/nodes/ArglistNode.java b/src/ast/nodes/ArglistNode.java index c29a4e0..55a568d 100644 --- a/src/ast/nodes/ArglistNode.java +++ b/src/ast/nodes/ArglistNode.java @@ -19,11 +19,10 @@ public class ArglistNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); for (var arg : arguments) { - if (arg instanceof ExprNode) { - ExprNode argExpr = (ExprNode) arg; + if (arg instanceof ExprNode argExpr) { String argName = argExpr.getId(); // TODO: check fucking IntType for params diff --git a/src/ast/nodes/AssignmentNode.java b/src/ast/nodes/AssignmentNode.java index 74d7283..558e392 100644 --- a/src/ast/nodes/AssignmentNode.java +++ b/src/ast/nodes/AssignmentNode.java @@ -10,9 +10,9 @@ import semanticanalysis.SymbolTable; */ public class AssignmentNode implements Node { - private ExprListNode lhr; - private Node assign; - private ExprListNode rhr; + private final ExprListNode lhr; + private final Node assign; + private final ExprListNode rhr; public AssignmentNode(Node lhr, Node assign, Node rhr) { this.lhr = (ExprListNode) lhr; @@ -22,7 +22,7 @@ public class AssignmentNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); // errors.addAll(lhr.checkSemantics(ST, _nesting)); errors.addAll(assign.checkSemantics(ST, _nesting)); @@ -32,7 +32,6 @@ public class AssignmentNode implements Node { // FIXME: unused variable // int rsize = rhr.getSize(); - // if (lsize == rsize) { for (int i = 0; i < lsize; i++) { ExprNode latom = (ExprNode) lhr.getElem(i); @@ -40,8 +39,8 @@ public class AssignmentNode implements Node { // ExprNode ratom = (ExprNode) rhr.getElem(i); } // } else { - // FIX: sgravata da più problemi che altro - // errors.add(new SemanticError("ValueError: different size of left or right side assignment")); + // FIX: sgravata da più problemi che altro + // errors.add(new SemanticError("ValueError: different size of left or right side assignment")); // } return errors; diff --git a/src/ast/nodes/AtomNode.java b/src/ast/nodes/AtomNode.java index fef72ec..47a15d7 100644 --- a/src/ast/nodes/AtomNode.java +++ b/src/ast/nodes/AtomNode.java @@ -30,7 +30,7 @@ public class AtomNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - var errors = new ArrayList(); + var errors = new ArrayList(); if (val != null) { if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) { diff --git a/src/ast/nodes/AugassignNode.java b/src/ast/nodes/AugassignNode.java index 629fbcd..7ced63e 100644 --- a/src/ast/nodes/AugassignNode.java +++ b/src/ast/nodes/AugassignNode.java @@ -12,7 +12,8 @@ import org.antlr.v4.runtime.tree.TerminalNode; * Node for the `augassign` statement of the grammar. */ public class AugassignNode implements Node { - private TerminalNode val; + + private final TerminalNode val; public AugassignNode(TerminalNode val) { this.val = val; @@ -20,7 +21,7 @@ public class AugassignNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - return new ArrayList(); + return new ArrayList(); } // FIXME: use the right type diff --git a/src/ast/nodes/BlockNode.java b/src/ast/nodes/BlockNode.java index a38b4ea..d9a151d 100644 --- a/src/ast/nodes/BlockNode.java +++ b/src/ast/nodes/BlockNode.java @@ -1,23 +1,22 @@ package ast.nodes; -import java.util.ArrayList; - import ast.types.*; +import java.util.ArrayList; import semanticanalysis.SemanticError; import semanticanalysis.SymbolTable; /** - * Node for `block` statement of the grammar. - * It extends the `RootNode`. + * Node for `block` statement of the grammar. It extends the `RootNode`. */ public class BlockNode extends RootNode { + public BlockNode(ArrayList childs) { super(childs); } @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); // Check semantics for each child for (Node child : childs) { @@ -27,7 +26,6 @@ public class BlockNode extends RootNode { return errors; } - @Override public Type typeCheck() { return new VoidType(); diff --git a/src/ast/nodes/CompForNode.java b/src/ast/nodes/CompForNode.java index 06c7aee..b7c6924 100644 --- a/src/ast/nodes/CompForNode.java +++ b/src/ast/nodes/CompForNode.java @@ -23,7 +23,7 @@ public class CompForNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); errors.addAll(exprlist.checkSemantics(ST, _nesting)); errors.addAll(single_expr.checkSemantics(ST, _nesting)); diff --git a/src/ast/nodes/CompIterNode.java b/src/ast/nodes/CompIterNode.java index 6704bd7..8de9f2f 100644 --- a/src/ast/nodes/CompIterNode.java +++ b/src/ast/nodes/CompIterNode.java @@ -19,7 +19,7 @@ public class CompIterNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); if (comp_for != null) { errors.addAll(comp_for.checkSemantics(ST, _nesting)); diff --git a/src/ast/nodes/CompNode.java b/src/ast/nodes/CompNode.java index 8be6ea1..22906df 100644 --- a/src/ast/nodes/CompNode.java +++ b/src/ast/nodes/CompNode.java @@ -12,7 +12,7 @@ import org.antlr.v4.runtime.tree.TerminalNode; */ public class CompNode implements Node { - private TerminalNode op; + private final TerminalNode op; public CompNode(TerminalNode op) { this.op = op; @@ -20,7 +20,7 @@ public class CompNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - return new ArrayList(); + return new ArrayList(); } // TODO: it should be boolean, right? diff --git a/src/ast/nodes/CompoundNode.java b/src/ast/nodes/CompoundNode.java index d21c2c7..845f05e 100644 --- a/src/ast/nodes/CompoundNode.java +++ b/src/ast/nodes/CompoundNode.java @@ -10,10 +10,10 @@ import semanticanalysis.SymbolTable; */ public class CompoundNode implements Node { - private Node ifNode; - private Node funcDef; - private Node forStmt; - private Node whileStmt; + private final Node ifNode; + private final Node funcDef; + private final Node forStmt; + private final Node whileStmt; public CompoundNode(Node ifNode, Node funcDef, Node forStmt, Node whileStmt) { this.ifNode = ifNode; @@ -24,7 +24,7 @@ public class CompoundNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); if (ifNode != null) { errors.addAll(ifNode.checkSemantics(ST, _nesting)); diff --git a/src/ast/nodes/DottedNameNode.java b/src/ast/nodes/DottedNameNode.java index df86c99..23f14c1 100644 --- a/src/ast/nodes/DottedNameNode.java +++ b/src/ast/nodes/DottedNameNode.java @@ -11,6 +11,7 @@ import org.antlr.v4.runtime.tree.TerminalNode; * Node for the `dooted_name` statement of the grammar. */ public class DottedNameNode implements Node { + protected ArrayList names; public DottedNameNode(ArrayList names) { @@ -19,7 +20,7 @@ public class DottedNameNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); for (int i = 0; i < names.size(); ++i) { ST.insert(names.get(i).toString(), this.typeCheck(), _nesting, null); diff --git a/src/ast/nodes/ExprNode.java b/src/ast/nodes/ExprNode.java index 8e3b896..873d537 100644 --- a/src/ast/nodes/ExprNode.java +++ b/src/ast/nodes/ExprNode.java @@ -12,11 +12,11 @@ import semanticanalysis.SymbolTable; */ public class ExprNode implements Node { - private AtomNode atom; - private Node compOp; - private String op; - private ArrayList exprs; - private ArrayList trailers; + private final AtomNode atom; + private final Node compOp; + private final String op; + private final ArrayList exprs; + private final ArrayList trailers; public ExprNode(Node atom, Node compOp, ArrayList exprs, String op, ArrayList trailers) { this.atom = (AtomNode) atom; @@ -52,7 +52,7 @@ public class ExprNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); // check if the atom is a function if (atom != null && !trailers.isEmpty()) { diff --git a/src/ast/nodes/ForStmtNode.java b/src/ast/nodes/ForStmtNode.java index d4c5e56..74c6ffc 100644 --- a/src/ast/nodes/ForStmtNode.java +++ b/src/ast/nodes/ForStmtNode.java @@ -10,8 +10,8 @@ import semanticanalysis.SymbolTable; */ public class ForStmtNode implements Node { - private Node exprList; - private Node block; + private final Node exprList; + private final Node block; public ForStmtNode(Node exprList, Node block) { this.exprList = exprList; @@ -34,7 +34,7 @@ public class ForStmtNode implements Node { */ @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); // Save every atom in the expression's list, except the last one var l = (ExprListNode) exprList; diff --git a/src/ast/nodes/FuncdefNode.java b/src/ast/nodes/FuncdefNode.java index 8985e08..c4f5846 100644 --- a/src/ast/nodes/FuncdefNode.java +++ b/src/ast/nodes/FuncdefNode.java @@ -14,9 +14,9 @@ import org.antlr.v4.runtime.tree.TerminalNode; */ public class FuncdefNode implements Node { - private TerminalNode name; - private Node paramlist; - private Node block; + private final TerminalNode name; + private final Node paramlist; + private final Node block; public FuncdefNode(TerminalNode name, Node paramlist, Node block) { this.name = name; @@ -26,14 +26,14 @@ public class FuncdefNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); int paramNumber = ((ParamlistNode) paramlist).getParamNumber(); Type returnType = this.block.typeCheck(); FunctionType ft = new FunctionType(paramNumber, returnType); ST.insert(this.name.toString(), ft, _nesting, ""); - HashMap HM = new HashMap(); + HashMap HM = new HashMap(); ST.add(HM); @@ -66,6 +66,7 @@ public class FuncdefNode implements Node { return ""; } + @Override public String toPrint(String prefix) { String str = prefix + "Funcdef(" + name + ")\n"; diff --git a/src/ast/nodes/IfNode.java b/src/ast/nodes/IfNode.java index f51d486..b0e1ddb 100644 --- a/src/ast/nodes/IfNode.java +++ b/src/ast/nodes/IfNode.java @@ -9,9 +9,10 @@ import semanticanalysis.SymbolTable; * Node for the `if` statement of the grammar. */ public class IfNode implements Node { - private Node guard; - private Node thenbranch; - private Node elsebranch; + + private final Node guard; + private final Node thenbranch; + private final Node elsebranch; public IfNode(Node guard, Node thenbranch, Node elsebranch) { this.guard = guard; @@ -21,7 +22,7 @@ public class IfNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); errors.addAll(guard.checkSemantics(ST, _nesting)); errors.addAll(thenbranch.checkSemantics(ST, _nesting)); @@ -38,9 +39,9 @@ public class IfNode implements Node { if (guard.typeCheck() instanceof BoolType) { Type thenexp = thenbranch.typeCheck(); Type elseexp = elsebranch.typeCheck(); - if (thenexp.getClass().equals(elseexp.getClass())) - return thenexp; - else { + if (thenexp.getClass().equals(elseexp.getClass())) { + return thenexp; + }else { System.out.println("Type Error: incompatible types in then and else branches."); return new ErrorType(); } diff --git a/src/ast/nodes/ImportNode.java b/src/ast/nodes/ImportNode.java index 900a909..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 names; + + private final Node dottedName; + private final boolean isFrom; + private final boolean importAs; + private final boolean importAll; + private final ArrayList names; public ImportNode(Node dottedName, boolean isFrom, boolean importAs, boolean importAll, ArrayList names) { @@ -27,7 +27,7 @@ public class ImportNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); if (isFrom) { for (int i = 0; i < names.size(); ++i) { @@ -75,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"; } diff --git a/src/ast/nodes/Node.java b/src/ast/nodes/Node.java index 77ba669..9fb58b8 100644 --- a/src/ast/nodes/Node.java +++ b/src/ast/nodes/Node.java @@ -1,10 +1,9 @@ package ast.nodes; +import ast.types.*; import java.util.ArrayList; - import semanticanalysis.SemanticError; import semanticanalysis.SymbolTable; -import ast.types.*; /** * Base interface for a Node. @@ -87,8 +86,7 @@ public interface Node { /** * Checks semantics for a given node for a SymbolTable ST and a level of - * nesting. - * Returns a list of `SemanticError`. + * nesting. Returns a list of `SemanticError`. */ ArrayList checkSemantics(SymbolTable ST, int _nesting); @@ -104,8 +102,8 @@ public interface Node { String codeGeneration(); /** - * Returns a string for a given node with a prefix. - * It used when an AST wants to be visualized on screen. + * Returns a string for a given node with a prefix. It used when an AST + * wants to be visualized on screen. */ String toPrint(String prefix); } diff --git a/src/ast/nodes/ParamdefNode.java b/src/ast/nodes/ParamdefNode.java index adadf72..6cdb2d0 100644 --- a/src/ast/nodes/ParamdefNode.java +++ b/src/ast/nodes/ParamdefNode.java @@ -17,7 +17,7 @@ public class ParamdefNode extends AtomNode { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - var errors = new ArrayList(); + var errors = new ArrayList(); String paramName = this.getId(); if (!ST.top_lookup(paramName)) { diff --git a/src/ast/nodes/ParamlistNode.java b/src/ast/nodes/ParamlistNode.java index 0a9696f..cf75d08 100644 --- a/src/ast/nodes/ParamlistNode.java +++ b/src/ast/nodes/ParamlistNode.java @@ -1,17 +1,16 @@ package ast.nodes; +import ast.types.*; import java.util.ArrayList; - import semanticanalysis.SemanticError; import semanticanalysis.SymbolTable; -import ast.types.*; /** * Node for the `param_list` statement of the grammar. */ public class ParamlistNode implements Node { - private ArrayList params; + private final ArrayList params; public ParamlistNode(ArrayList _params) { params = _params; @@ -19,7 +18,7 @@ public class ParamlistNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); for (var param : params) { errors.addAll(param.checkSemantics(ST, _nesting)); diff --git a/src/ast/nodes/ReturnStmtNode.java b/src/ast/nodes/ReturnStmtNode.java index 0f2dd74..8e34813 100644 --- a/src/ast/nodes/ReturnStmtNode.java +++ b/src/ast/nodes/ReturnStmtNode.java @@ -1,16 +1,16 @@ package ast.nodes; +import ast.types.*; import java.util.ArrayList; - import semanticanalysis.SemanticError; import semanticanalysis.SymbolTable; -import ast.types.*; /** * Node for the `return_stmt` statement of the grammar. */ public class ReturnStmtNode implements Node { - private Node exprList; + + private final Node exprList; public ReturnStmtNode(Node exprList) { this.exprList = exprList; @@ -18,7 +18,7 @@ public class ReturnStmtNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); if (this.exprList != null) { errors.addAll(this.exprList.checkSemantics(ST, _nesting)); diff --git a/src/ast/nodes/RootNode.java b/src/ast/nodes/RootNode.java index 4b29e8d..5bbce8c 100644 --- a/src/ast/nodes/RootNode.java +++ b/src/ast/nodes/RootNode.java @@ -20,10 +20,10 @@ public class RootNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); // Create a new HashMap for the current scope - HashMap HM = new HashMap(); + HashMap HM = new HashMap(); // Add the HashMap to the SymbolTable ST.add(HM); diff --git a/src/ast/nodes/SimpleStmtNode.java b/src/ast/nodes/SimpleStmtNode.java index b7e5455..b2bc880 100644 --- a/src/ast/nodes/SimpleStmtNode.java +++ b/src/ast/nodes/SimpleStmtNode.java @@ -9,10 +9,11 @@ import semanticanalysis.SymbolTable; * 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; + + private final Node assignment; + private final Node expr; + private final Node returnStmt; + private final Node importStmt; public SimpleStmtNode(Node assignment, Node expr, Node returnStmt, Node importStmt) { this.assignment = assignment; @@ -23,7 +24,7 @@ public class SimpleStmtNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); if (assignment != null) { errors.addAll(assignment.checkSemantics(ST, _nesting)); diff --git a/src/ast/nodes/SimpleStmtsNode.java b/src/ast/nodes/SimpleStmtsNode.java index 97bffff..ae1044b 100644 --- a/src/ast/nodes/SimpleStmtsNode.java +++ b/src/ast/nodes/SimpleStmtsNode.java @@ -9,7 +9,8 @@ import semanticanalysis.SymbolTable; * Node for the `simple_stmts` statement of the grammar. */ public class SimpleStmtsNode implements Node { - private ArrayList stmts; + + private final ArrayList stmts; public SimpleStmtsNode(ArrayList stmts) { this.stmts = stmts; @@ -17,7 +18,7 @@ public class SimpleStmtsNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); for (Node stmt : stmts) { errors.addAll(stmt.checkSemantics(ST, _nesting)); diff --git a/src/ast/nodes/TestlistCompNode.java b/src/ast/nodes/TestlistCompNode.java index cba056c..32049f5 100644 --- a/src/ast/nodes/TestlistCompNode.java +++ b/src/ast/nodes/TestlistCompNode.java @@ -10,8 +10,8 @@ import semanticanalysis.SymbolTable; */ public class TestlistCompNode implements Node { - private ArrayList exprs; - private CompForNode comp; + private final ArrayList exprs; + private final CompForNode comp; public TestlistCompNode(ArrayList exprs, Node comp) { this.exprs = exprs; diff --git a/src/ast/nodes/TrailerNode.java b/src/ast/nodes/TrailerNode.java index 22e43a0..aaa72f3 100644 --- a/src/ast/nodes/TrailerNode.java +++ b/src/ast/nodes/TrailerNode.java @@ -12,11 +12,11 @@ import org.antlr.v4.runtime.tree.TerminalNode; */ public class TrailerNode implements Node { - private Node arglist; - private ArrayList exprs; - private TerminalNode methodCall; - private boolean isParenthesis; - private boolean isEmpty; + private final Node arglist; + private final ArrayList exprs; + private final TerminalNode methodCall; + private final boolean isParenthesis; + private final boolean isEmpty; public TrailerNode(Node arglist, ArrayList exprs, TerminalNode methodCall, boolean isParenthesis) { this.arglist = arglist; @@ -24,12 +24,12 @@ public class TrailerNode implements Node { this.methodCall = methodCall; this.isParenthesis = isParenthesis; - this.isEmpty = (this.arglist == null && this.exprs.size() == 0 && this.methodCall == null); + this.isEmpty = (this.arglist == null && this.exprs.isEmpty() && this.methodCall == null); } @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); if (arglist != null) { errors.addAll(arglist.checkSemantics(ST, _nesting)); diff --git a/src/ast/nodes/WhileStmtNode.java b/src/ast/nodes/WhileStmtNode.java index 6353ec1..1db01ea 100644 --- a/src/ast/nodes/WhileStmtNode.java +++ b/src/ast/nodes/WhileStmtNode.java @@ -1,17 +1,17 @@ package ast.nodes; +import ast.types.*; import java.util.ArrayList; - import semanticanalysis.SemanticError; import semanticanalysis.SymbolTable; -import ast.types.*; /** * Node for the `while_stmt` statement of the grammar. */ public class WhileStmtNode implements Node { - private Node expr; - private Node block; + + private final Node expr; + private final Node block; public WhileStmtNode(Node expr, Node block) { this.expr = expr; @@ -20,7 +20,7 @@ public class WhileStmtNode implements Node { @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { - ArrayList errors = new ArrayList(); + ArrayList errors = new ArrayList(); errors.addAll(expr.checkSemantics(ST, _nesting)); errors.addAll(block.checkSemantics(ST, _nesting)); diff --git a/src/ast/types/AtomType.java b/src/ast/types/AtomType.java index 0387ec1..fc1c69e 100644 --- a/src/ast/types/AtomType.java +++ b/src/ast/types/AtomType.java @@ -2,9 +2,10 @@ package ast.types; /** * An tom type. - * TODO: do I need to use this one? */ public class AtomType extends Type { + + @Override public String toPrint(String prefix) { return prefix + "Atom\n"; } diff --git a/src/ast/types/BoolType.java b/src/ast/types/BoolType.java index 20c2750..01e2cd5 100644 --- a/src/ast/types/BoolType.java +++ b/src/ast/types/BoolType.java @@ -4,6 +4,8 @@ package ast.types; * A boolean type. A bool is True or False. */ public class BoolType extends Type { + + @Override public String toPrint(String prefix) { return prefix + "Bool\n"; } diff --git a/src/ast/types/ErrorType.java b/src/ast/types/ErrorType.java index 4a7a0cf..71176d1 100644 --- a/src/ast/types/ErrorType.java +++ b/src/ast/types/ErrorType.java @@ -4,6 +4,8 @@ package ast.types; * Error type. */ public class ErrorType extends Type { + + @Override public String toPrint(String prefix) { return prefix + "Error\n"; } diff --git a/src/ast/types/FunctionType.java b/src/ast/types/FunctionType.java index ef50641..5e6adaa 100644 --- a/src/ast/types/FunctionType.java +++ b/src/ast/types/FunctionType.java @@ -5,8 +5,8 @@ package ast.types; */ public class FunctionType extends Type { - private int paramNumber; - private Type returnType; + private final int paramNumber; + private final Type returnType; public FunctionType(int paramNumber, Type returnType) { this.paramNumber = paramNumber; @@ -22,6 +22,7 @@ public class FunctionType extends Type { return returnType; } + @Override public String toPrint(String prefix) { return prefix + "Function\n"; } diff --git a/src/ast/types/ImportType.java b/src/ast/types/ImportType.java index 6852bfa..892de10 100644 --- a/src/ast/types/ImportType.java +++ b/src/ast/types/ImportType.java @@ -4,8 +4,9 @@ package ast.types; * A type for the imported names. */ public class ImportType extends Type { + + @Override public String toPrint(String prefix) { return prefix + "Import\n"; } } - diff --git a/src/ast/types/IntType.java b/src/ast/types/IntType.java index a29518b..6483d93 100644 --- a/src/ast/types/IntType.java +++ b/src/ast/types/IntType.java @@ -4,6 +4,8 @@ package ast.types; * An integer type. */ public class IntType extends Type { + + @Override public String toPrint(String prefix) { return prefix + "Int\n"; } diff --git a/src/ast/types/NoneType.java b/src/ast/types/NoneType.java index 42f7fd7..730add9 100644 --- a/src/ast/types/NoneType.java +++ b/src/ast/types/NoneType.java @@ -5,6 +5,7 @@ package ast.types; */ public class NoneType extends Type { + @Override public String toPrint(String prefix) { return prefix + "None\n"; } diff --git a/src/ast/types/ReservedWordsType.java b/src/ast/types/ReservedWordsType.java index f5f7ac5..da72890 100644 --- a/src/ast/types/ReservedWordsType.java +++ b/src/ast/types/ReservedWordsType.java @@ -4,6 +4,8 @@ package ast.types; * A type for the continue and break statements. */ public class ReservedWordsType extends Type { + + @Override public String toPrint(String prefix) { return prefix + "ReservedWords\n"; } diff --git a/src/ast/types/Type.java b/src/ast/types/Type.java index 6bff8b9..6190106 100644 --- a/src/ast/types/Type.java +++ b/src/ast/types/Type.java @@ -1,26 +1,25 @@ package ast.types; +import ast.nodes.*; import java.util.ArrayList; - import semanticanalysis.SemanticError; import semanticanalysis.SymbolTable; -import ast.nodes.*; /** * A node which represents a type class. */ public class Type implements Node { + public boolean isEqual(Type A, Type B) { - if (A.getClass().equals(B.getClass())) - return true; - else - return false; + return A.getClass().equals(B.getClass()); } + @Override public String toPrint(String s) { return s; } + @Override public ArrayList checkSemantics(SymbolTable ST, int _nesting) { // It is never invoked return null; diff --git a/src/ast/types/VoidType.java b/src/ast/types/VoidType.java index 8e3f9b2..2d21da6 100644 --- a/src/ast/types/VoidType.java +++ b/src/ast/types/VoidType.java @@ -5,6 +5,7 @@ package ast.types; */ public class VoidType extends Type { + @Override public String toPrint(String prefix) { return prefix + "Void\n"; } diff --git a/src/semanticanalysis/STentry.java b/src/semanticanalysis/STentry.java index 07b62c8..51fb109 100644 --- a/src/semanticanalysis/STentry.java +++ b/src/semanticanalysis/STentry.java @@ -6,9 +6,10 @@ import ast.types.Type; * Entry class for the symbol table. */ public class STentry { - private Type type; - private int offset; - private int nesting; + + private final Type type; + private final int offset; + private final int nesting; private String label; public STentry(Type type, int offset, int nesting) { diff --git a/src/semanticanalysis/SemanticError.java b/src/semanticanalysis/SemanticError.java index 745e3fb..ac2dc4c 100644 --- a/src/semanticanalysis/SemanticError.java +++ b/src/semanticanalysis/SemanticError.java @@ -4,12 +4,14 @@ package semanticanalysis; * Class respresents a semantic error. */ public class SemanticError { - private String msg; + + private final String msg; public SemanticError(String msg) { this.msg = msg; } + @Override public String toString() { return msg; } diff --git a/src/semanticanalysis/Share.java b/src/semanticanalysis/Share.java index c98fc53..7542cf7 100644 --- a/src/semanticanalysis/Share.java +++ b/src/semanticanalysis/Share.java @@ -1,7 +1,7 @@ package semanticanalysis; -import java.util.*; import java.io.*; +import java.util.*; public class Share { @@ -10,7 +10,7 @@ public class Share { * generic because it's used a custom contains function. */ public static ArrayList removeDuplicates(ArrayList list) { - ArrayList newList = new ArrayList(); + ArrayList newList = new ArrayList(); for (SemanticError element : list) { if (!customContains(newList, element)) { diff --git a/src/semanticanalysis/SymbolTable.java b/src/semanticanalysis/SymbolTable.java index db9649c..b620af7 100644 --- a/src/semanticanalysis/SymbolTable.java +++ b/src/semanticanalysis/SymbolTable.java @@ -1,8 +1,8 @@ package semanticanalysis; +import ast.types.*; import java.util.ArrayList; import java.util.HashMap; -import ast.types.*; /** * Class representing a symbol table. It's a list of hash table symbol table. We @@ -11,12 +11,12 @@ import ast.types.*; */ public class SymbolTable { - private ArrayList> symbolTable; - private ArrayList offset; + private final ArrayList> symbolTable; + private final ArrayList offset; public SymbolTable() { - this.symbolTable = new ArrayList>(); - this.offset = new ArrayList(); + this.symbolTable = new ArrayList(); + this.offset = new ArrayList(); } /** @@ -96,7 +96,7 @@ public class SymbolTable { */ public boolean top_lookup(String id) { int n = symbolTable.size() - 1; - STentry T = null; + STentry T; HashMap H = symbolTable.get(n); T = H.get(id); return (T != null); @@ -125,14 +125,6 @@ public class SymbolTable { // We always increment the offset by 1 otherwise we need ad-hoc bytecode // operations - // FIXME: wtf is that? - // if (type.getClass().equals((new BoolType()).getClass())) { - // offs = offs + 1; - // } else if (type.getClass().equals((new IntType()).getClass())) { - // offs = offs + 1; - // } else { - // offs = offs + 1; - // } offs = offs + 1; this.offset.add(offs); -- cgit v1.2.3-71-g8e6c