Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
boozec committed Jun 25, 2024
1 parent 54b2c77 commit 6bdf1fc
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 32 deletions.
19 changes: 3 additions & 16 deletions progs/test.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
import math
from re import f4, r3
x = 1

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
if x == 1:
print("a")
12 changes: 7 additions & 5 deletions src/ast/nodes/AssignmentNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@
* Node for the `assignment` statement of the grammar.
*/
public class AssignmentNode implements Node {
private Node lhr;
private ExprNode lhr;
private Node assign;
private Node rhr;
private ExprNode rhr;

public AssignmentNode(Node lhr, Node assign, Node rhr) {
this.lhr = lhr;
this.lhr = (ExprNode) lhr;
this.assign = assign;
this.rhr = rhr;
this.rhr = (ExprNode) rhr;
}

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

errors.addAll(lhr.checkSemantics(ST, _nesting));
// errors.addAll(lhr.checkSemantics(ST, _nesting));
errors.addAll(assign.checkSemantics(ST, _nesting));
errors.addAll(rhr.checkSemantics(ST, _nesting));

ST.insert(lhr.getId(), rhr.typeCheck(), _nesting, "");

return errors;
}

Expand Down
22 changes: 20 additions & 2 deletions src/ast/nodes/AtomNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,33 @@ public AtomNode(String val) {
this.val = val;
}

public String getId() {
return this.val;
}

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
return new ArrayList<SemanticError>();
var errors = new ArrayList<SemanticError>();
System.out.println(getId() + " " + _nesting + " " + ST.nslookup(getId()));
if (!(this.typeCheck() instanceof IntType) && !ST.top_lookup(this.getId())) {
System.out.println(!(this.typeCheck() instanceof IntType) + " " + !ST.top_lookup(this.getId()));
errors.add(new SemanticError("Undefined name `" + this.getId() + "`"));
}

return errors;
}

// FIXME: this type for atom
@Override
public Type typeCheck() {
return new AtomType();
try {
Integer.parseInt(this.val);
System.out.println(this.val + " is int");
return new IntType();
} catch (NumberFormatException e) {
System.out.println(this.val + " is atom");
return new AtomType();
}
}

// TODO: add code generation for atom node
Expand Down
9 changes: 8 additions & 1 deletion src/ast/nodes/ExprNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ public ExprNode(Node atom, Node compOp, ArrayList<Node> exprs, String op, ArrayL
this.trailers = trailers;
}

public String getId() {
return ((AtomNode) this.atom).getId();
}

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

if (atom != null) {
errors.addAll(atom.checkSemantics(ST, _nesting));
}
Expand All @@ -50,6 +53,10 @@ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
// FIXME: type for the expr
@Override
public Type typeCheck() {
if (this.atom != null) {
return this.atom.typeCheck();
}

return new VoidType();
}

Expand Down
12 changes: 11 additions & 1 deletion src/ast/nodes/FuncdefNode.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ast.nodes;

import java.util.ArrayList;
import java.util.HashMap;

import semanticanalysis.SemanticError;
import semanticanalysis.SymbolTable;
Expand All @@ -24,11 +25,20 @@ public FuncdefNode(TerminalNode name, Node paramlist, Node block) {
@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList<SemanticError>();
ST.insert(this.name.toString(), this.block.typeCheck(), _nesting, "");

HashMap<String, STentry> HM = new HashMap<String, STentry>();
ArrayList<Type> partypes = new ArrayList<Type>();

ST.add(HM);

if (paramlist != null) {
errors.addAll(paramlist.checkSemantics(ST, _nesting));
errors.addAll(paramlist.checkSemantics(ST, _nesting + 1));
}

// Offset is increased for the possible return value
ST.increaseoffset();

errors.addAll(block.checkSemantics(ST, _nesting));

return errors;
Expand Down
7 changes: 7 additions & 0 deletions src/ast/nodes/ParamdefNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ public ParamdefNode(String val) {
super(val);
}

@Override
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ST.insert(this.getId(), this.typeCheck(), _nesting, "");

return new ArrayList<SemanticError>();
}

// FIXME: it should returns the param' type
@Override
public Type typeCheck() {
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 @@ -21,7 +21,7 @@ public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList<SemanticError>();

for (var param : params) {
errors.addAll(param.checkSemantics(ST, _nesting));
errors.addAll(param.checkSemantics(ST, _nesting + 1));
}

return errors;
Expand Down
12 changes: 10 additions & 2 deletions src/ast/nodes/RootNode.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ast.nodes;

import java.util.ArrayList;
import java.util.HashMap;

import semanticanalysis.SemanticError;
import semanticanalysis.SymbolTable;
Expand All @@ -24,13 +25,20 @@ public RootNode(ArrayList<Node> stmts, ArrayList<Node> compoundStmts) {
public ArrayList<SemanticError> checkSemantics(SymbolTable ST, int _nesting) {
ArrayList<SemanticError> errors = new ArrayList<SemanticError>();

for (Node stmt : stmts) {
HashMap<String, STentry> HM = new HashMap<String, STentry>();

ST.add(HM);

for (Node stmt : compoundStmts) {
errors.addAll(stmt.checkSemantics(ST, _nesting));
}
for (Node stmt : compoundStmts) {

for (Node stmt : stmts) {
errors.addAll(stmt.checkSemantics(ST, _nesting));
}

ST.remove();

return errors;
}

Expand Down
9 changes: 5 additions & 4 deletions src/semanticanalysis/SymbolTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public STentry lookup(String id) {
HashMap<String, STentry> H = this.symbolTable.get(n);
T = H.get(id);
if (T != null) {
found = true;
}else {
found = true;
} else {
n = n - 1;
}
}
Expand All @@ -60,8 +60,8 @@ public Integer nslookup(String id) {
while ((n >= 0) && !found) {
HashMap<String, STentry> H = this.symbolTable.get(n);
if (H.get(id) != null) {
found = true;
}else {
found = true;
} else {
n = n - 1;
}
}
Expand Down Expand Up @@ -125,6 +125,7 @@ public void insert(String id, Type type, int _nesting, String _label) {

// 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())) {
Expand Down

0 comments on commit 6bdf1fc

Please sign in to comment.