Skip to content

Commit

Permalink
Merge branch '384-introduce-new-variable-list-system-for-expressions'…
Browse files Browse the repository at this point in the history
… into 'master'

Resolve "introduce new variable/list system for expressions"

Closes #384

See merge request se2/litterbox!460
  • Loading branch information
gofraser committed Aug 10, 2021
2 parents 897d251 + 3f98d89 commit 4599d77
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import de.uni_passau.fim.se2.litterbox.ast.ParsingException;
import de.uni_passau.fim.se2.litterbox.ast.model.Key;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.ComparableExpr;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.Expression;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.bool.*;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.list.ExpressionList;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.num.AsNumber;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.num.UnspecifiedNumExpr;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.string.AsString;
Expand All @@ -43,6 +45,8 @@
import de.uni_passau.fim.se2.litterbox.ast.parser.symboltable.ExpressionListInfo;
import de.uni_passau.fim.se2.litterbox.utils.Preconditions;

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

import static de.uni_passau.fim.se2.litterbox.ast.Constants.*;
Expand Down Expand Up @@ -249,16 +253,18 @@ static BoolExpr parseBlockBoolExpr(String blockId, JsonNode exprBlock, JsonNode
exprBlock.get(FIELDS_KEY).get(LIST_KEY).get(LIST_NAME_POS).asText();
Identifier containingVar;
String currentActorName = ActorDefinitionParser.getCurrentActor().getName();
Optional<ExpressionListInfo> list
= ProgramParser.symbolTable.getList(identifier, listName, currentActorName);

if (list.isPresent()) {
ExpressionListInfo variableInfo = list.get();
containingVar = new Qualified(new StrId(variableInfo.getActor()),
new ScratchList(new StrId((variableInfo.getVariableName()))));
} else {
throw new ParsingException("Variable / List ID not specified in JSON.");
if (ProgramParser.symbolTable.getList(identifier, listName, currentActorName).isEmpty()) {
List<Expression> listEx = new ArrayList<>();
ExpressionList expressionList = new ExpressionList(listEx);
ProgramParser.symbolTable.addExpressionListInfo(identifier, listName, expressionList, true, "Stage");
}
Optional<ExpressionListInfo> list = ProgramParser.symbolTable.getList(identifier, listName, currentActorName);

Preconditions.checkArgument(list.isPresent());
ExpressionListInfo variableInfo = list.get();
containingVar = new Qualified(new StrId(variableInfo.getActor()),
new ScratchList(new StrId((variableInfo.getVariableName()))));
contained = StringExprParser.parseStringExpr(exprBlock, ITEM_KEY, allBlocks);
return new ListContains(containingVar, contained, metadata);
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public static Expression parseDataExpr(JsonNode containingBlock, String inputKey
} else if (isList) {
return parseScratchList(exprArray);
} else {
throw new ParsingException("Variable / List ID not specified in JSON.");
ProgramParser.symbolTable.addVariable(idString, idName, new StringType(), true, "Stage");
return parseVariable(exprArray);
}
}
throw new IllegalArgumentException("The block does not contain a DataExpr.");
Expand Down Expand Up @@ -142,7 +143,7 @@ private static Parameter parseParameter(ArrayNode
/**
* Parses a dead parameter which is not inside of a script.
*
* @param blockId The id of the param node.
* @param blockId The id of the param node.
* @param paramNode The node holding the name of the parameter.
* @return The parameter corresponding to the param node.
* @throws ParsingException If parsing the metadata fails.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.fasterxml.jackson.databind.node.TextNode;
import de.uni_passau.fim.se2.litterbox.ast.ParsingException;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.Expression;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.list.ExpressionList;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.num.*;
import de.uni_passau.fim.se2.litterbox.ast.model.identifier.Identifier;
import de.uni_passau.fim.se2.litterbox.ast.model.identifier.Qualified;
Expand All @@ -38,6 +39,8 @@
import de.uni_passau.fim.se2.litterbox.utils.Preconditions;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static de.uni_passau.fim.se2.litterbox.ast.Constants.*;
Expand Down Expand Up @@ -174,17 +177,19 @@ static NumExpr parseBlockNumExpr(String blockId, JsonNode exprBlock, JsonNode al
case data_lengthoflist:
String identifier =
exprBlock.get(FIELDS_KEY).get(LIST_KEY).get(LIST_IDENTIFIER_POS).asText();
String idName = exprBlock.get(FIELDS_KEY).get(LIST_KEY).get(LIST_NAME_POS).asText();
String listName = exprBlock.get(FIELDS_KEY).get(LIST_KEY).get(LIST_NAME_POS).asText();
Identifier var;
Optional<ExpressionListInfo> list
= ProgramParser.symbolTable.getList(identifier, idName, currentActorName);
if (list.isPresent()) {
ExpressionListInfo variableInfo = list.get();
var = new Qualified(new StrId(variableInfo.getActor()),
new ScratchList(new StrId(variableInfo.getVariableName())));
} else {
throw new ParsingException("Variable / List ID not specified in JSON.");
if (ProgramParser.symbolTable.getList(identifier, listName, currentActorName).isEmpty()) {
List<Expression> listEx = new ArrayList<>();
ExpressionList expressionList = new ExpressionList(listEx);
ProgramParser.symbolTable.addExpressionListInfo(identifier, listName, expressionList, true, "Stage");
}
Optional<ExpressionListInfo> list = ProgramParser.symbolTable.getList(identifier, listName, currentActorName);

Preconditions.checkArgument(list.isPresent());
ExpressionListInfo variableInfo = list.get();
var = new Qualified(new StrId(variableInfo.getActor()),
new ScratchList(new StrId(variableInfo.getVariableName())));
return new LengthOfVar(var, metadata);
case sensing_current:
TimeComp timeComp = TimecompParser.parse(exprBlock);
Expand Down Expand Up @@ -214,15 +219,18 @@ static NumExpr parseBlockNumExpr(String blockId, JsonNode exprBlock, JsonNode al
Expression item = parseExpr(exprBlock, ITEM_KEY, allBlocks);
identifier =
exprBlock.get(FIELDS_KEY).get(LIST_KEY).get(LIST_IDENTIFIER_POS).asText();
idName = exprBlock.get(FIELDS_KEY).get(LIST_KEY).get(LIST_NAME_POS).asText();
list = ProgramParser.symbolTable.getList(identifier, idName, currentActorName);
if (list.isPresent()) {
ExpressionListInfo variableInfo = list.get();
var = new Qualified(new StrId(variableInfo.getActor()),
new ScratchList(new StrId(variableInfo.getVariableName())));
} else {
throw new ParsingException("Variable / List ID not specified in JSON.");
listName = exprBlock.get(FIELDS_KEY).get(LIST_KEY).get(LIST_NAME_POS).asText();
if (ProgramParser.symbolTable.getList(identifier, listName, currentActorName).isEmpty()) {
List<Expression> listEx = new ArrayList<>();
ExpressionList expressionList = new ExpressionList(listEx);
ProgramParser.symbolTable.addExpressionListInfo(identifier, listName, expressionList, true, "Stage");
}
list = ProgramParser.symbolTable.getList(identifier, listName, currentActorName);

Preconditions.checkArgument(list.isPresent());
variableInfo = list.get();
var = new Qualified(new StrId(variableInfo.getActor()),
new ScratchList(new StrId(variableInfo.getVariableName())));
return new IndexOf(item, var, metadata);
default:
throw new ParsingException(opcodeString + " is not covered by parseBlockNumExpr");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import de.uni_passau.fim.se2.litterbox.ast.ParsingException;
import de.uni_passau.fim.se2.litterbox.ast.model.elementchoice.ElementChoice;
import de.uni_passau.fim.se2.litterbox.ast.model.elementchoice.WithExpr;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.Expression;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.list.ExpressionList;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.num.NumExpr;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.string.*;
import de.uni_passau.fim.se2.litterbox.ast.model.expression.string.attributes.Attribute;
Expand All @@ -43,6 +45,8 @@
import de.uni_passau.fim.se2.litterbox.ast.parser.symboltable.ExpressionListInfo;
import de.uni_passau.fim.se2.litterbox.utils.Preconditions;

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

import static de.uni_passau.fim.se2.litterbox.ast.Constants.*;
Expand Down Expand Up @@ -157,19 +161,22 @@ static StringExpr parseBlockStringExpr(String blockId, JsonNode exprBlock, JsonN
return new Username(metadata);
case data_itemoflist:
NumExpr index = NumExprParser.parseNumExpr(exprBlock, INDEX_KEY, allBlocks);
String id =
String identifier =
exprBlock.get(FIELDS_KEY).get(LIST_KEY).get(LIST_IDENTIFIER_POS).asText();
String idName = exprBlock.get(FIELDS_KEY).get(LIST_KEY).get(LIST_NAME_POS).asText();
String listName = exprBlock.get(FIELDS_KEY).get(LIST_KEY).get(LIST_NAME_POS).asText();
Identifier var;
String currentActorName = ActorDefinitionParser.getCurrentActor().getName();
Optional<ExpressionListInfo> list = ProgramParser.symbolTable.getList(id, idName, currentActorName);
if (list.isPresent()) {
ExpressionListInfo variableInfo = list.get();
var = new Qualified(new StrId(variableInfo.getActor()),
new ScratchList(new StrId((variableInfo.getVariableName()))));
} else {
throw new ParsingException("Variable / List ID not specified in JSON.");
if (ProgramParser.symbolTable.getList(identifier, listName, currentActorName).isEmpty()) {
List<Expression> listEx = new ArrayList<>();
ExpressionList expressionList = new ExpressionList(listEx);
ProgramParser.symbolTable.addExpressionListInfo(identifier, listName, expressionList, true, "Stage");
}
Optional<ExpressionListInfo> list = ProgramParser.symbolTable.getList(identifier, listName, currentActorName);

Preconditions.checkArgument(list.isPresent());
ExpressionListInfo variableInfo = list.get();
var = new Qualified(new StrId(variableInfo.getActor()),
new ScratchList(new StrId((variableInfo.getVariableName()))));
return new ItemOfVariable(index, var, metadata);
case looks_costumenumbername:
String numberName = exprBlock.get(FIELDS_KEY).get(NUMBER_NAME_KEY).get(0).asText();
Expand Down

0 comments on commit 4599d77

Please sign in to comment.