Skip to content

Commit

Permalink
Merge pull request #32 from VinceAAU/scope
Browse files Browse the repository at this point in the history
map generator in our language and alot of fixes to stuff that was nee…
  • Loading branch information
DeeKahy authored May 14, 2024
2 parents c70a501 + a38f204 commit 4e1c817
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ public class BoolNode extends AstNode {
public BoolNode(String value) {
this.value = Boolean.parseBoolean(value);
}
public BoolNode(Boolean value) {
this.value = value;
}
public Boolean getValue() {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dk.aau.cs_24_sw_4_16.carl.CARLBaseVisitor;
import dk.aau.cs_24_sw_4_16.carl.CARLParser;
import org.antlr.v4.runtime.tree.TerminalNode;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -73,7 +74,6 @@ public AstNode visitStructureDefinition(CARLParser.StructureDefinitionContext ct
@Override
public AstNode visitVariableDeclaration(CARLParser.VariableDeclarationContext ctx) {
String identifier = ctx.IDENTIFIER().getText();

IdentifierNode identifierNode = new IdentifierNode(identifier);

TypeNode type = (TypeNode) visit(ctx.type());
Expand All @@ -100,7 +100,7 @@ public AstNode visitArrayDeclaration(CARLParser.ArrayDeclarationContext ctx) {

List<AstNode> sizes = ctx.arrayOptionalIndex().stream()
.map(arrayOptionalIndexContext -> {
if(arrayOptionalIndexContext.expression() == null)
if (arrayOptionalIndexContext.expression() == null)
return new IntNode(-1);
else
return new ExpressionNode(visit(arrayOptionalIndexContext.expression()));
Expand Down Expand Up @@ -181,11 +181,12 @@ public AstNode visitArgumentList(CARLParser.ArgumentListContext ctx) {
public AstNode visitParameterList(CARLParser.ParameterListContext ctx) {
List<ParameterNode> parameters = new ArrayList<>();
if (ctx != null) {
for (int i = 0; i < ctx.getChildCount() / 3; i++) {
IdentifierNode identifier = new IdentifierNode(ctx.IDENTIFIER(i).getText());
TypeNode type = (TypeNode) visit(ctx.type(i));
parameters.add(new ParameterNode(identifier, type));
}
for (int i = 0; i < Math.ceilDiv(ctx.getChildCount(), 4); i++) {
System.out.println(ctx.IDENTIFIER().size());
IdentifierNode identifier = new IdentifierNode(ctx.IDENTIFIER(i).getText());
TypeNode type = (TypeNode) visit(ctx.type(i));
parameters.add(new ParameterNode(identifier, type));
}
}

return new ParameterListNode(parameters);
Expand Down Expand Up @@ -353,7 +354,6 @@ public AstNode visitIfStatement(CARLParser.IfStatementContext ctx) {
expressionNodes.add(new ExpressionNode(visit(exp)));
}
}

List<BlockNode> blocks = new ArrayList<>();
for (CARLParser.BlockContext block : ctx.block()) {
blocks.add((BlockNode) visitBlock(block));
Expand All @@ -371,6 +371,7 @@ public AstNode visitWhileLoop(CARLParser.WhileLoopContext ctx) {
} else {
left = visit(ctx.expression().getChild(0));
}

AstNode right;
if (ctx.expression().getChild(2) instanceof CARLParser.IdentifierContext) {
right = new IdentifierNode(ctx.expression().getChild(2).getText());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public static AstNode getAstNodeValue(AstNode left, AstNode right, String operat
return value;
} else if (left instanceof IdentifierNode || right instanceof IdentifierNode) {
return new RelationsAndLogicalOperatorNode(left, right, operator);

} else if (left instanceof StringNode && right instanceof StringNode) {
BoolNode bool = new BoolNode(((StringNode)left).getValue().equals( ((StringNode) right).getValue()));
return bool;
}
throw new RuntimeException("something went wrong in getAstNodeValue");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public StringNode(String value) {
}

public String getValue() {
return value;
return value.replaceAll("\"","");
}

public void setValue(String value) {
Expand All @@ -17,6 +17,6 @@ public void setValue(String value) {

@Override
public String toString() {
return value;
return value.replaceAll("\"","");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ public AstNode roomCall(MethodCallNode node) {
} else {
throw new RuntimeException("out of bounds");
}
} else {
} else if (((ArgumentListNode) node.getValue()).getList().get(0) instanceof IdentifierNode) {
IntNode in = ((IntNode) getVariable((IdentifierNode) ((ArgumentListNode) node.getValue()).getList().get(0)));
int index = in.getValue();
if (index < rooms.size()) {
return rooms.get(in.getValue()).get(node.getIdentifierNode().toString());
} else {
throw new RuntimeException("out of bounds");
}
}
{
throw new RuntimeException("parameter must be an int");
}
}
Expand Down Expand Up @@ -129,7 +138,12 @@ public AstNode visit(StatementNode node) {
public void visit(StructureDefinitionNode node) {
HashMap<String, AstNode> object = new HashMap<>();
for (var variable : node.getVariableDeclarations()) {
object.put(variable.getIdentifier().toString(), variable.getValue());
if (variable.getValue() instanceof IdentifierNode) {
object.put(variable.getIdentifier().toString(), getVariable((new IdentifierNode(variable.getValue().toString()))));
} else {

object.put(variable.getIdentifier().toString(), variable.getValue());
}
}
if (node.getType().equals("enemy")) {
tileInformationEnemy.put(node.getIdentifier().toString(), object);
Expand Down Expand Up @@ -232,15 +246,15 @@ public void visit(IfStatementNode node) {
public AstNode getVariable(IdentifierNode node) {
// for (HashMap<String, AstNode> vTable : scopes) {

int towards =!activeScope.isEmpty() ? activeScope.getLast() : 0;
int towards = !activeScope.isEmpty() ? activeScope.getLast() : 0;
for (int i = scopes.size() - 1; i >= towards; i--) {
if (scopes.get(i).containsKey(node.getIdentifier())) {
return scopes.get(i).get(node.getIdentifier());
}
}
int from = 0;
if (!activeScope.isEmpty()) {
from = activeScope.getFirst();
from = activeScope.getFirst()-1;
} else {
from = scopes.size() - 1;
}
Expand Down Expand Up @@ -268,12 +282,13 @@ public void visit(AssignmentNode node) {
}
int from = 0;
if (!activeScope.isEmpty()) {
from = activeScope.getFirst();
from = activeScope.getFirst() -1 ;
} else {
from = scopes.size() - 1;
}
System.out.println(from);
for (int i = from; i >= 0; i--) {
if (scopes.get(i).containsKey(node.getIdentifier())) {
if (scopes.get(i).containsKey(node.getIdentifier().toString())) {
AstNode nodeToChange = scopes.get(i).get(node.getIdentifier().toString());
AstNode toChange = node.getValue();
replaceValue(nodeToChange, toChange);
Expand Down Expand Up @@ -306,10 +321,22 @@ public void visit(VariableDeclarationNode node) {
} else if (toChange instanceof IdentifierNode) {
for (HashMap<String, AstNode> vTable : scopes) {
if (vTable.containsKey(toChange.toString())) {
scopes.getLast().put(node.getIdentifier().toString(), vTable.get(toChange.toString()));
AstNode variable = vTable.get(toChange.toString());
if (variable instanceof IntNode) {
scopes.getLast().put(node.getIdentifier().toString(), new IntNode(variable.toString()));
} else if (variable instanceof FloatNode) {
scopes.getLast().put(node.getIdentifier().toString(), new FloatNode(variable.toString()));
} else if (variable instanceof StringNode) {
scopes.getLast().put(node.getIdentifier().toString(), new StringNode(variable.toString()));
} else if (variable instanceof BoolNode) {
scopes.getLast().put(node.getIdentifier().toString(), new BoolNode(((BoolNode) variable).getValue()));
} else {
throw new RuntimeException("haa");
}
}
}


} else if (toChange instanceof PropertyAccessNode) {
toChange = visit((PropertyAccessNode) toChange);
scopes.getLast().put(node.getIdentifier().toString(), toChange);
Expand All @@ -320,10 +347,15 @@ public void visit(VariableDeclarationNode node) {
} else if (toChange instanceof MethodCallNode) {
toChange = visit((MethodCallNode) toChange);
scopes.getLast().put(node.getIdentifier().toString(), toChange);
} else {
scopes.getLast().put(node.getIdentifier().toString(), toChange);
} else if (toChange instanceof IntNode) {
scopes.getLast().put(node.getIdentifier().toString(), new IntNode(toChange.toString()));
} else if (toChange instanceof FloatNode) {
scopes.getLast().put(node.getIdentifier().toString(), new FloatNode(toChange.toString()));
} else if (toChange instanceof StringNode) {
scopes.getLast().put(node.getIdentifier().toString(), new StringNode(toChange.toString()));
} else if (toChange instanceof BoolNode) {
scopes.getLast().put(node.getIdentifier().toString(), new BoolNode(((BoolNode) toChange).getValue()));
}

} else {
throw new RuntimeException("variable " + node.getIdentifier() + " already exists in the current scope");
}
Expand Down Expand Up @@ -424,7 +456,7 @@ private boolean idExists(String id) {
}
int from = 0;
if (!activeScope.isEmpty()) {
from = activeScope.getFirst();
from = activeScope.getFirst() -1;
} else {
from = scopes.size() - 1;
}
Expand Down Expand Up @@ -526,6 +558,8 @@ public AstNode visit(FunctionCallNode node) {
return returnValue;
}
}
scopes.remove(localTable);
activeScope.removeLast();
}
return node;
}
Expand Down Expand Up @@ -565,31 +599,35 @@ public AstNode visit(FunctionDefinitionNode node) {
public AstNode visit(BinaryOperatorNode node) {
AstNode left = node.getLeft();
AstNode right = node.getRight();
if (left instanceof MethodCallNode) {
left = visit((MethodCallNode) left);
} else if (left instanceof PropertyAccessNode) {
left = visit((PropertyAccessNode) left);
}
if (left instanceof IdentifierNode) {
left = getVariable((IdentifierNode) left);
} else if (left instanceof BinaryOperatorNode) {
left = visit((BinaryOperatorNode) left);
} else if (left instanceof MethodCallNode) {
left = visit((MethodCallNode) left);
} else if (left instanceof PropertyAccessNode) {
left = visit((PropertyAccessNode) left);
}
if (right instanceof PropertyAccessNode) {

right = visit((PropertyAccessNode) right);
}
if (right instanceof MethodCallNode) {
right = visit((MethodCallNode) right);
}
if (right instanceof IdentifierNode) {
right = getVariable((IdentifierNode) right);
} else if (right instanceof BinaryOperatorNode) {
right = visit((BinaryOperatorNode) right);
} else if (right instanceof MethodCallNode) {
right = visit((MethodCallNode) right);
} else if (right instanceof PropertyAccessNode) {
right = visit((PropertyAccessNode) right);
}

if (left instanceof IntNode && right instanceof IntNode) {
return BinaryOperatorNode.getAstNodeValue(left, right, node.getOperator());
} else if (left instanceof FloatNode && right instanceof FloatNode) {
return BinaryOperatorNode.getAstNodeValue(left, right, node.getOperator());
}
throw new RuntimeException("BinaryOperator not implemented clause");
throw new RuntimeException("BinaryOperator not implemented clause " + left.getClass() + " " + right.getClass());
}

public AstNode visit(RelationsAndLogicalOperatorNode node) {
Expand All @@ -599,20 +637,31 @@ public AstNode visit(RelationsAndLogicalOperatorNode node) {
left = getVariable((IdentifierNode) left);
} else if (left instanceof RelationsAndLogicalOperatorNode) {
left = visit((RelationsAndLogicalOperatorNode) left);
} else if (left instanceof PropertyAccessNode) {
left = visit((PropertyAccessNode) left);
} else if (left instanceof BinaryOperatorNode) {
left = visit((BinaryOperatorNode) left);
}
if (right instanceof IdentifierNode) {
right = getVariable((IdentifierNode) right);
} else if (right instanceof RelationsAndLogicalOperatorNode) {
right = visit((RelationsAndLogicalOperatorNode) right);
} else if (right instanceof PropertyAccessNode) {
right = visit((PropertyAccessNode) right);
} else if (right instanceof BinaryOperatorNode) {
right = visit((BinaryOperatorNode) right);
}
if (left instanceof IntNode && right instanceof IntNode) {
return RelationsAndLogicalOperatorNode.getAstNodeValue(left, right, node.getOperator());
} else if (left instanceof FloatNode && right instanceof FloatNode) {
return RelationsAndLogicalOperatorNode.getAstNodeValue(left, right, node.getOperator());
} else if (left instanceof BoolNode && right instanceof BoolNode) {
return RelationsAndLogicalOperatorNode.getAstNodeValue(left, right, node.getOperator());
} else if (left instanceof StringNode && right instanceof StringNode) {
return RelationsAndLogicalOperatorNode.getAstNodeValue(left, right, node.getOperator());
}
throw new RuntimeException("RelationsAndLogicalOperator not implemented clause");

throw new RuntimeException("RelationsAndLogicalOperator not implemented clause " + left.getClass() + " " + right.getClass());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void testingPrintSatement() throws Exception {
Interpreter interpreter = new Interpreter();
interpreter.visit(astRoot);

assertEquals("\"test\"".trim(), outContent.toString().trim(), "expected the output to be test");
assertEquals("test".trim(), outContent.toString().trim(), "expected the output to be test");
}

@Test
Expand Down Expand Up @@ -377,7 +377,7 @@ public void testingString() throws Exception {
Interpreter interpreter = new Interpreter();
interpreter.visit(astRoot);

assertEquals("\"test\"".trim(), outContent.toString().trim());
assertEquals("test".trim(), outContent.toString().trim());
}

@Test
Expand Down Expand Up @@ -678,7 +678,7 @@ public void testingStruct2() throws Exception {
Interpreter interpreter = new Interpreter();
interpreter.visit(astRoot);

assertEquals("\"orcacnian\"".trim(), outContent.toString().trim());
assertEquals("orcacnian".trim(), outContent.toString().trim());
}

@Test
Expand Down Expand Up @@ -707,7 +707,7 @@ public void testingStruct3() throws Exception {
Interpreter interpreter = new Interpreter();
interpreter.visit(astRoot);

assertEquals("\"orcacnian\"".trim(), outContent.toString().trim());
assertEquals("orcacnian".trim(), outContent.toString().trim());
}

@Test
Expand Down Expand Up @@ -736,7 +736,7 @@ public void testingStruct4() throws Exception {
Interpreter interpreter = new Interpreter();
interpreter.visit(astRoot);

assertEquals("\"orcacnian\"".trim(), outContent.toString().trim());
assertEquals("orcacnian".trim(), outContent.toString().trim());
}

@Test
Expand Down
Loading

0 comments on commit 4e1c817

Please sign in to comment.