Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the print of the ast and some warnings #19

Merged
merged 4 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 3 additions & 17 deletions progs/test.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
import math
from re import f4, r3

from abs import *


def find_first_duplicate(nums, x):
num_set = set()
no_duplicate = -1

for i in range(len(nums)):
if nums[i] in num_set:
return nums[i]
else:
num_set.add(nums[i])

return no_duplicate
m = 1
for i in range(10):
m = m + 1
2 changes: 1 addition & 1 deletion src/ast/nodes/ArglistNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ArglistNode(ArrayList<Node> arguments) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

for (var arg : arguments) {
if (arg instanceof ExprNode) {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/AssignmentNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public AssignmentNode(Node lhr, Node assign, Node rhr) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

// errors.addAll(lhr.checkSemantics(ST, _nesting));
errors.addAll(assign.checkSemantics(ST, _nesting));
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/AtomNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public String getId() {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
var errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

if (val != null) {
if ((this.typeCheck() instanceof AtomType) && ST.nslookup(this.getId()) < 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/AugassignNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public AugassignNode(TerminalNode val) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
return new ArrayList();
return new ArrayList<>();
}

// FIXME: use the right type
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/BlockNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public BlockNode(ArrayList<Node> childs) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

// Check semantics for each child
for (Node child : childs) {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/CompForNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public CompForNode(Node exprlist, Node single_expr, Node comp_iter) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

errors.addAll(exprlist.checkSemantics(ST, _nesting));
errors.addAll(single_expr.checkSemantics(ST, _nesting));
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/CompIterNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public CompIterNode(Node comp_for) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

if (comp_for != null) {
errors.addAll(comp_for.checkSemantics(ST, _nesting));
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/CompNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public CompNode(TerminalNode op) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
return new ArrayList();
return new ArrayList<>();
}

// TODO: it should be boolean, right?
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/CompoundNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public CompoundNode(Node ifNode, Node funcDef, Node forStmt, Node whileStmt) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

if (ifNode != null) {
errors.addAll(ifNode.checkSemantics(ST, _nesting));
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/DottedNameNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public DottedNameNode(ArrayList<TerminalNode> names) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

for (int i = 0; i < names.size(); ++i) {
ST.insert(names.get(i).toString(), this.typeCheck(), _nesting, null);
Expand Down
23 changes: 13 additions & 10 deletions src/ast/nodes/ExprNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public String getId() {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

// check if the atom is a function
if (atom != null && !trailers.isEmpty()) {
Expand Down Expand Up @@ -130,19 +130,22 @@ public String toPrint(String prefix) {
if (atom != null) {
str += atom.toPrint(prefix);
}

if (compOp != null) {
str += compOp.toPrint(prefix);
}

for (var expr : exprs) {
str = expr.toPrint(prefix);
}

for (var trailer : trailers) {
str = trailer.toPrint(prefix);

if (exprs != null) {
for (var expr : exprs) {
str += expr.toPrint(prefix);
}
}


if (trailers != null) {
for (var trailer : trailers) {
str += trailer.toPrint(prefix);
}
}

if (op != null) {
str += prefix + "Op(" + op + ")\n";
}
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/ForStmtNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ForStmtNode(Node exprList, Node block) {
*/
@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

// Save every atom in the expression's list, except the last one
var l = (ExprListNode) exprList;
Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/FuncdefNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public FuncdefNode(TerminalNode name, Node paramlist, Node block) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> 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<String, STentry> HM = new HashMap();
HashMap<String, STentry> HM = new HashMap<>();

ST.add(HM);

Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/IfNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public IfNode(Node guard, Node thenbranch, Node elsebranch) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

errors.addAll(guard.checkSemantics(ST, _nesting));
errors.addAll(thenbranch.checkSemantics(ST, _nesting));
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/ImportNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ImportNode(Node dottedName, boolean isFrom, boolean importAs, boolean imp

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

if (isFrom) {
for (int i = 0; i < names.size(); ++i) {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/ParamdefNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ParamdefNode(String val) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
var errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();
String paramName = this.getId();

if (!ST.top_lookup(paramName)) {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/ParamlistNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ParamlistNode(ArrayList<Node> _params) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

for (var param : params) {
errors.addAll(param.checkSemantics(ST, _nesting));
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/ReturnStmtNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ReturnStmtNode(Node exprList) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

if (this.exprList != null) {
errors.addAll(this.exprList.checkSemantics(ST, _nesting));
Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/RootNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public RootNode(ArrayList<Node> childs) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

// Create a new HashMap for the current scope
HashMap<String, STentry> HM = new HashMap();
HashMap<String, STentry> HM = new HashMap<>();

// Add the HashMap to the SymbolTable
ST.add(HM);
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/SimpleStmtNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public SimpleStmtNode(Node assignment, Node expr, Node returnStmt, Node importSt

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

if (assignment != null) {
errors.addAll(assignment.checkSemantics(ST, _nesting));
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/SimpleStmtsNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public SimpleStmtsNode(ArrayList<Node> stmts) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

for (Node stmt : stmts) {
errors.addAll(stmt.checkSemantics(ST, _nesting));
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/TrailerNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public TrailerNode(Node arglist, ArrayList<Node> exprs, TerminalNode methodCall,

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

if (arglist != null) {
errors.addAll(arglist.checkSemantics(ST, _nesting));
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/WhileStmtNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public WhileStmtNode(Node expr, Node block) {

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList();
ArrayList<SemanticError> errors = new ArrayList<>();

errors.addAll(expr.checkSemantics(ST, _nesting));
errors.addAll(block.checkSemantics(ST, _nesting));
Expand Down
2 changes: 1 addition & 1 deletion src/semanticanalysis/Share.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Share {
* generic because it's used a custom contains function.
*/
public static ArrayList<SemanticError> removeDuplicates(ArrayList<SemanticError> list) {
ArrayList<SemanticError> newList = new ArrayList();
ArrayList<SemanticError> newList = new ArrayList<>();

for (SemanticError element : list) {
if (!customContains(newList, element)) {
Expand Down
4 changes: 2 additions & 2 deletions src/semanticanalysis/SymbolTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class SymbolTable {
private final ArrayList<Integer> offset;

public SymbolTable() {
this.symbolTable = new ArrayList();
this.offset = new ArrayList();
this.symbolTable = new ArrayList<>();
this.offset = new ArrayList<>();
}

/**
Expand Down