Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
Add redefinition error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mads256h committed Apr 22, 2022
1 parent 81bb463 commit 2882dea
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/main/java/org/flowsoft/flowg/RedeclarationException.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ public RedeclarationException(Location left, Location right) {
_left = left;
_right = right;
}

public Location GetLeft() {
return _left;
}

public Location GetRight() {
return _right;
}
}
4 changes: 4 additions & 0 deletions src/main/java/org/flowsoft/flowg/main.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public static void main(String[] args) {
System.err.format("%s:%d:%d: error: type mismatch between %s and %s\n", args[0], e.GetLeft().getLine(), e.GetLeft().getColumn(), TypeHelper.TypeToString(e.GetLeftType()), TypeHelper.TypeToString(e.GetRightType()));
System.err.println(GetLine(file, e.GetLeft(), e.GetRight()));
}
catch (RedeclarationException e) {
System.err.format("%s:%d:%d: error: symbol redefinition\n", args[0], e.GetLeft().getLine(), e.GetLeft().getColumn());
System.err.println(GetLine(file, e.GetLeft(), e.GetRight()));
}
catch (Exception e) {
System.out.println("Could not parse");
e.printStackTrace();
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/org/flowsoft/flowg/symboltables/SymbolTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
import java.util.Map;

public class SymbolTable implements Cloneable<SymbolTable> {
//TODO: remove this
private static final Location N = new Location("fixme", 0, 0, 0);

private final SymbolTable _parent;

private final Map<String, VariableEntry> _variableEntries;
Expand All @@ -33,21 +30,21 @@ private SymbolTable(SymbolTable parent, Map<String, VariableEntry> variableEntri
_functionEntries = functionEntries;
}

public void Enter(String identifier, Type type) throws TypeException {
public void Enter(String identifier, Type type, Location left, Location right) throws TypeException {
if (!_variableEntries.containsKey(identifier)) {
_variableEntries.put(identifier, new VariableEntry(identifier, type));
}
else {
throw new RedeclarationException(N, N);
throw new RedeclarationException(left, right);
}
}

public void Enter(Type returnType, String identifier, ArrayList<FormalParameterNode> formalParameters, StatementListNode functionBody, SymbolTable parent) throws TypeException {
public void Enter(Type returnType, String identifier, ArrayList<FormalParameterNode> formalParameters, StatementListNode functionBody, SymbolTable parent, Location left, Location right) throws TypeException {
if (!_functionEntries.containsKey(identifier)) {
_functionEntries.put(identifier, new FunctionEntry(returnType, identifier, formalParameters, functionBody, parent));
}
else {
throw new RedeclarationException(N, N);
throw new RedeclarationException(left, right);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public Type Visit(DeclarationNode declarationNode) throws TypeException {
var typeNodeType = typeNode.Accept(this);
var expressionNodeType = expressionNode.Accept(this);
if (typeNodeType == expressionNodeType) {
_symbolTable.Enter(identifierNode.GetValue(), typeNodeType);
_symbolTable.Enter(identifierNode.GetValue(), typeNodeType, declarationNode.GetLeft(), declarationNode.GetRight());
return typeNodeType;
}
else {
Expand All @@ -191,18 +191,19 @@ public Type Visit(DeclarationNode declarationNode) throws TypeException {
@Override
public Type Visit(FunctionDefinitionNode functionDefinitionNode) throws TypeException {
var returnType = functionDefinitionNode.GetTypeNode().Accept(this);
var identifier = functionDefinitionNode.GetIdentifierNode().GetValue();
var identifierNode = functionDefinitionNode.GetIdentifierNode();
var identifier = identifierNode.GetValue();
var formalParams = functionDefinitionNode.GetFormalParameterListNode();
var statementList = functionDefinitionNode.GetStatementListNode();

var parentSymbolTable = _symbolTable.Clone();
var bodySymbolTable = new SymbolTable(parentSymbolTable);
for (var param : formalParams.GetChildren()) {
bodySymbolTable.Enter(param.GetRightChild().GetValue(), param.GetLeftChild().GetValue());
bodySymbolTable.Enter(param.GetRightChild().GetValue(), param.GetLeftChild().GetValue(), param.GetLeft(), param.GetRight());
}

parentSymbolTable.Enter(returnType, identifier, (ArrayList<FormalParameterNode>) formalParams.GetChildren(), functionDefinitionNode.GetStatementListNode(), bodySymbolTable);
_symbolTable.Enter(returnType, identifier, (ArrayList<FormalParameterNode>) formalParams.GetChildren(), functionDefinitionNode.GetStatementListNode(), bodySymbolTable);
parentSymbolTable.Enter(returnType, identifier, (ArrayList<FormalParameterNode>) formalParams.GetChildren(), functionDefinitionNode.GetStatementListNode(), bodySymbolTable, identifierNode.GetLeft(), identifierNode.GetRight());
_symbolTable.Enter(returnType, identifier, (ArrayList<FormalParameterNode>) formalParams.GetChildren(), functionDefinitionNode.GetStatementListNode(), bodySymbolTable, identifierNode.GetLeft(), identifierNode.GetRight());

var oldFunctionReturnType = _functionReturnType;
var oldSymbolTable = _symbolTable;
Expand Down

0 comments on commit 2882dea

Please sign in to comment.