reworked resolve method for expressions

parent 0e823454
......@@ -21,11 +21,14 @@
package de.monticore.lang.monticar.cnnarch;
import de.monticore.lang.monticar.cnnarch._symboltable.VariableSymbol;
import de.monticore.lang.monticar.cnnarch._symboltable.VariableType;
public class PredefinedVariables {
public static final String IF_NAME = "_if";
public static final String FOR_NAME = "_for";
public static final String TRUE_NAME = "true";
public static final String FALSE_NAME = "false";
public static VariableSymbol createIfParameter(){
return new VariableSymbol.Builder()
......@@ -42,5 +45,21 @@ public class PredefinedVariables {
.build();
}
//todo true and false
//necessary because true is currently only a name in MontiMath and it needs to be evaluated at compile time for this language
public static VariableSymbol createTrueConstant(){
return new VariableSymbol.Builder()
.name(TRUE_NAME)
.type(VariableType.CONSTANT)
.defaultValue(true)
.build();
}
//necessary because false is currently only a name in MontiMath and it needs to be evaluated at compile time for this language
public static VariableSymbol createFalseConstant() {
return new VariableSymbol.Builder()
.name(FALSE_NAME)
.type(VariableType.CONSTANT)
.defaultValue(false)
.build();
}
}
......@@ -26,6 +26,8 @@ import de.monticore.lang.monticar.cnnarch._symboltable.VariableSymbol;
public class CheckVariableConstraints implements CNNArchASTVariableCoCo {
//todo: make coco to check tuple elements (no tuple inside of tuple)
@Override
public void check(ASTVariable node) {
if (node == null || !node.getSymbol().isPresent()){
......@@ -34,7 +36,7 @@ public class CheckVariableConstraints implements CNNArchASTVariableCoCo {
VariableSymbol variable = (VariableSymbol) node.getSymbol().get();
for (Constraint constraint : variable.getConstraints()){
constraint.check(variable.getValueSymbol());
constraint.check(variable.getExpression());
}
}
}
......@@ -20,6 +20,7 @@
*/
package de.monticore.lang.monticar.cnnarch._symboltable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
......@@ -34,10 +35,26 @@ abstract public class ArchAbstractSequenceExpression extends ArchExpressionSymbo
abstract public boolean isSerialSequence();
//todo no Optional
abstract public Optional<Integer> getParallelLength();
//todo no Optional
abstract public Optional<Integer> getSerialLength();
abstract public Optional<Integer> getMaxSerialLength();
@Override
public Optional<Object> getValue() {
if (isResolved()){
List<List<Object>> parallelValues = new ArrayList<>(getParallelLength().get());
for (List<ArchSimpleExpressionSymbol> serialElements : getElements().get()){
List<Object> serialValues = new ArrayList<>(getMaxSerialLength().get());
for (ArchSimpleExpressionSymbol element : serialElements){
serialValues.add(element.getValue().get());
}
parallelValues.add(serialValues);
}
return Optional.of(parallelValues);
}
else{
return Optional.empty();
}
}
}
......@@ -20,6 +20,8 @@
*/
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.Scope;
import java.util.*;
public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
......@@ -27,6 +29,7 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
private ArchSimpleExpressionSymbol startSymbol;
private ArchSimpleExpressionSymbol endSymbol;
private boolean parallel;
private List<List<ArchSimpleExpressionSymbol>> elements = null;
public ArchRangeExpressionSymbol() {
......@@ -69,7 +72,7 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
private Optional<Integer> getLength(){
Optional<Integer> optLength = Optional.empty();
if (isFullyResolved()) {
if (isResolved()){
Object startValue = getEndSymbol().getValue().get();
Object endValue = getEndSymbol().getValue().get();
if (startValue instanceof Integer && endValue instanceof Integer) {
......@@ -92,7 +95,7 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
}
@Override
public Optional<Integer> getSerialLength() {
public Optional<Integer> getMaxSerialLength() {
if (isSerialSequence()) {
return getLength();
}
......@@ -102,70 +105,61 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
}
@Override
public Optional<Object> getValue() {
if (isFullyResolved()){
//todo check in CoCo: startSymbol.isInt() && endSymbol.isInt()
int startInt = (Integer) startSymbol.getValue().get();
int endInt = (Integer) endSymbol.getValue().get();
int step = 1;
if (endInt < startInt){
step = -1;
}
List<List<Integer>> valueLists = new ArrayList<>();
public Set<String> resolve(Scope resolvingScope) {
if (!isResolved()){
checkIfResolvable();
if (isResolvable()){
if (isParallel()){
for (int i = startInt; i <= endInt; i = i + step){
List<Integer> values = new ArrayList<>(1);
values.add(i);
valueLists.add(values);
}
getStartSymbol().resolveOrError(resolvingScope);
getEndSymbol().resolveOrError(resolvingScope);
}
else {
List<Integer> values = new ArrayList<>();
for (int i = startInt; i <= endInt; i = i + step){
values.add(i);
}
valueLists.add(values);
}
return Optional.of(valueLists);
}
else {
return Optional.empty();
}
return getUnresolvableNames();
}
@Override
public Set<String> resolve() {
Set<String> unresolvableSet = new HashSet<>();
if (!isFullyResolved()){
unresolvableSet.addAll(startSymbol.resolve());
unresolvableSet.addAll(endSymbol.resolve());
if (unresolvableSet.isEmpty()) {
setFullyResolved(true);
}
}
return unresolvableSet;
public boolean isResolved() {
return getStartSymbol().isResolved() && getEndSymbol().isResolved();
}
@Override
protected void checkIfResolved() {
startSymbol.checkIfResolved();
endSymbol.checkIfResolved();
setFullyResolved(startSymbol.isFullyResolved() && endSymbol.isFullyResolved());
}
public Optional<List<List<ArchSimpleExpressionSymbol>>> getElements() {
if (elements == null){
if (isResolved()){
int start = startSymbol.getIntValue().get();
int end = endSymbol.getIntValue().get();
int step = 1;
if (end < start){
step = -1;
}
@Override
public boolean isResolved() {
//todo
return false;
List<List<ArchSimpleExpressionSymbol>> elementList = new ArrayList<>(getParallelLength().get());
if (isParallel()){
for (int i = start; i <= end; i = i + step){
List<ArchSimpleExpressionSymbol> values = new ArrayList<>(1);
values.add(ArchSimpleExpressionSymbol.of(i));
elementList.add(values);
}
}
else {
List<ArchSimpleExpressionSymbol> values = new ArrayList<>(getMaxSerialLength().get());
for (int i = start; i <= end; i = i + step){
values.add(ArchSimpleExpressionSymbol.of(i));
}
elementList.add(values);
}
this.elements = elementList;
}
}
return Optional.ofNullable(elements);
}
@Override
public List<List<ArchSimpleExpressionSymbol>> getElements() {
//todo
return null;
protected Set<String> computeUnresolvableNames() {
Set<String> unresolvableNames = new HashSet<>();
unresolvableNames.addAll(getStartSymbol().computeUnresolvableNames());
unresolvableNames.addAll(getEndSymbol().computeUnresolvableNames());
return unresolvableNames;
}
}
......@@ -20,6 +20,8 @@
*/
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.Scope;
import java.util.*;
public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression {
......@@ -31,7 +33,12 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
}
public List<List<ArchSimpleExpressionSymbol>> getElements() {
@Override
public Optional<List<List<ArchSimpleExpressionSymbol>>> getElements() {
return Optional.of(_getElements());
}
protected List<List<ArchSimpleExpressionSymbol>> _getElements(){
return elements;
}
......@@ -42,7 +49,7 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
@Override
public boolean isSerialSequence(){
boolean isSerial = !isParallelSequence();
for (List<ArchSimpleExpressionSymbol> serialElement : getElements()){
for (List<ArchSimpleExpressionSymbol> serialElement : _getElements()){
if (serialElement.size() >= 2){
isSerial = true;
}
......@@ -61,10 +68,10 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
}
@Override
public Optional<Integer> getSerialLength() {
public Optional<Integer> getMaxSerialLength() {
if (!elements.isEmpty()){
int maxLenght = 0;
for (List<ArchSimpleExpressionSymbol> element : getElements()){
for (List<ArchSimpleExpressionSymbol> element : _getElements()){
if (maxLenght < element.size()){
maxLenght = element.size();
}
......@@ -77,65 +84,42 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
}
@Override
public Optional<Object> getValue() {
if (isFullyResolved()){
List<List<Object>> valueLists = new ArrayList<>(4);
for (List<ArchSimpleExpressionSymbol> serialList : getElements()) {
List<Object> values = new ArrayList<>(4);
for (ArchSimpleExpressionSymbol element : serialList) {
values.add(element.getValue().get());
public Set<String> resolve(Scope resolvingScope) {
if (!isResolved()){
checkIfResolvable();
if (isResolvable()){
for (List<ArchSimpleExpressionSymbol> serialList : _getElements()) {
for (ArchSimpleExpressionSymbol element : serialList) {
element.resolveOrError(resolvingScope);
}
}
valueLists.add(values);
}
if (valueLists.isEmpty()) {
valueLists.add(new ArrayList<Object>(2));
}
return Optional.of(valueLists);
}
else{
return Optional.empty();
}
return getUnresolvableNames();
}
@Override
public Set<String> resolve() {
Set<String> unresolvableSet = new HashSet<>();
if (!isFullyResolved()){
for (List<ArchSimpleExpressionSymbol> serialList : getElements()) {
for (ArchSimpleExpressionSymbol element : serialList) {
unresolvableSet.addAll(element.resolve());
}
}
if (unresolvableSet.isEmpty()) {
setFullyResolved(true);
}
}
return unresolvableSet;
}
@Override
protected void checkIfResolved() {
public boolean isResolved() {
boolean isResolved = true;
for (List<ArchSimpleExpressionSymbol> serialList : getElements()) {
for (ArchSimpleExpressionSymbol element : serialList) {
element.checkIfResolved();
if (!element.isFullyResolved()){
for (List<ArchSimpleExpressionSymbol> serialElements : _getElements()){
for (ArchSimpleExpressionSymbol element : serialElements){
if (!element.isResolved()){
isResolved = false;
}
}
}
setFullyResolved(isResolved);
return isResolved;
}
@Override
public boolean isResolved() {
//todo
return false;
protected Set<String> computeUnresolvableNames() {
Set<String> unresolvableNames = new HashSet<>();
for (List<ArchSimpleExpressionSymbol> serialElements : _getElements()){
for (ArchSimpleExpressionSymbol element : serialElements){
unresolvableNames.addAll(element.computeUnresolvableNames());
}
}
return unresolvableNames;
}
}
......@@ -30,15 +30,24 @@ import java.util.Collection;
public class ArchitectureSymbol extends ArchitectureSymbolTOP {
private LayerSymbol body;
public ArchitectureSymbol(String name) {
super(name);
}
@Override
protected ArchitectureScope createSpannedScope() {
return new ArchitectureScope();
}
public LayerSymbol getBody() {
return body;
}
public void setBody(LayerSymbol body) {
this.body = body;
}
//todo
}
......@@ -20,10 +20,7 @@
*/
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.lang.monticar.cnnarch.ErrorMessages;
import de.monticore.symboltable.CommonSymbol;
import de.se_rwth.commons.logging.Log;
import javolution.testing.AssertionException;
import java.util.ArrayList;
import java.util.List;
......@@ -61,7 +58,10 @@ public class ArgumentSymbol extends CommonSymbol {
}
public List<List<ArgumentSymbol>> split(){
List<List<ArchSimpleExpressionSymbol>> elements = getRhs().getElements();
if (getRhs().isRange()){
getRhs().resolveOrError(getEnclosingScope());
}
List<List<ArchSimpleExpressionSymbol>> elements = getRhs().getElements().get();
List<List<ArgumentSymbol>> arguments = new ArrayList<>(elements.size());
for (List<ArchSimpleExpressionSymbol> serialElementList : elements){
......@@ -78,7 +78,7 @@ public class ArgumentSymbol extends CommonSymbol {
//do not call if value is a sequence
public void set(){
if (getRhs().isSimpleValue()){
getParameter().setValue((ArchSimpleExpressionSymbol) getRhs());
getParameter().setExpression((ArchSimpleExpressionSymbol) getRhs());
}
else {
throw new IllegalStateException("The value of the parameter is set to a sequence. This should never happen.");
......
......@@ -25,6 +25,7 @@ import de.monticore.lang.math.math._ast.ASTMathExpression;
import de.monticore.lang.math.math._symboltable.MathSymbolTableCreator;
import de.monticore.lang.math.math._symboltable.expression.MathExpressionSymbol;
import de.monticore.lang.monticar.cnnarch.PredefinedMethods;
import de.monticore.lang.monticar.cnnarch.PredefinedVariables;
import de.monticore.lang.monticar.cnnarch._ast.*;
import de.monticore.lang.monticar.cnnarch._visitor.CNNArchVisitor;
import de.monticore.lang.monticar.cnnarch._visitor.CommonCNNArchDelegatorVisitor;
......@@ -112,34 +113,24 @@ public class CNNArchSymbolTableCreator extends de.monticore.symboltable.CommonSy
setEnclosingScopeOfNodes(ast);
}
public void visit(final ASTArchitecture architecture) {
ArchitectureSymbol architectureSymbol = new ArchitectureSymbol(
architecture.getName()
);
public void visit(final ASTArchitecture node) {
ArchitectureSymbol architecture = new ArchitectureSymbol(node.getName());
addToScopeAndLinkWithNode(architectureSymbol, architecture);
addToScopeAndLinkWithNode(architecture, node);
createPredefinedConstants();
}
public void endVisit(final ASTArchitecture architecture) {
public void endVisit(final ASTArchitecture node) {
ArchitectureSymbol architecture = (ArchitectureSymbol) node.getSymbol().get();
architecture.setBody((LayerSymbol) node.getBody().getSymbol().get());
removeCurrentScope();
}
private void createPredefinedConstants(){
VariableSymbol trueConstant = new VariableSymbol.Builder()
.name("true")
.type(VariableType.CONSTANT)
.defaultValue(true)
.build();
VariableSymbol falseConstant = new VariableSymbol.Builder()
.name("false")
.type(VariableType.CONSTANT)
.defaultValue(false)
.build();
addToScope(trueConstant);
addToScope(falseConstant);
addToScope(PredefinedVariables.createTrueConstant());
addToScope(PredefinedVariables.createFalseConstant());
}
@Override
......@@ -238,7 +229,7 @@ public class CNNArchSymbolTableCreator extends de.monticore.symboltable.CommonSy
public void endVisit(ASTParameter ast) {
VariableSymbol variable = (VariableSymbol) ast.getSymbol().get();
if (ast.getDefault().isPresent()){
variable.setDefaultValueSymbol((ArchSimpleExpressionSymbol) ast.getDefault().get().getSymbol().get());
variable.setDefaultExpression((ArchSimpleExpressionSymbol) ast.getDefault().get().getSymbol().get());
}
}
......@@ -252,7 +243,7 @@ public class CNNArchSymbolTableCreator extends de.monticore.symboltable.CommonSy
@Override
public void endVisit(ASTConstant node) {
VariableSymbol constant = (VariableSymbol) node.getSymbol().get();
constant.setDefaultValueSymbol((ArchSimpleExpressionSymbol) node.getRhs().getSymbol().get());
constant.setDefaultExpression((ArchSimpleExpressionSymbol) node.getRhs().getSymbol().get());
}
@Override
......@@ -274,9 +265,8 @@ public class CNNArchSymbolTableCreator extends de.monticore.symboltable.CommonSy
mathExp = (MathExpressionSymbol) ast.getTupleExpression().get().getSymbol().get();
}
ArchSimpleExpressionSymbol sym = new ArchSimpleExpressionSymbol();
addToScopeAndLinkWithNode(sym, ast);
sym.setExpression(mathExp);
sym.checkIfResolved();
addToScopeAndLinkWithNode(sym, ast);
}
@Override
......@@ -301,7 +291,6 @@ public class CNNArchSymbolTableCreator extends de.monticore.symboltable.CommonSy
sym.setParallel(node.getParallel().isPresent());
sym.setStartSymbol((ArchSimpleExpressionSymbol) node.getStart().getSymbol().get());
sym.setEndSymbol((ArchSimpleExpressionSymbol) node.getEnd().getSymbol().get());
sym.checkIfResolved();
}
@Override
......@@ -323,7 +312,6 @@ public class CNNArchSymbolTableCreator extends de.monticore.symboltable.CommonSy
elements.add(serialElements);
}
sym.setElements(elements);
sym.checkIfResolved();
}
@Override
......
......@@ -105,7 +105,8 @@ public class CompositeLayerSymbol extends LayerSymbol {
protected Set<String> computeUnresolvableNames() {
Set<String> unresolvableSet = new HashSet<>();
for (LayerSymbol layer : getLayers()){
unresolvableSet.addAll(layer.computeUnresolvableNames());
layer.checkIfResolvable();
unresolvableSet.addAll(layer.getUnresolvableNames());
}
return unresolvableSet;
}
......
......@@ -73,7 +73,7 @@ public class IOLayerSymbol extends LayerSymbol {
public boolean isResolved() {
boolean isResolved = true;
if (getArrayAccess().isPresent()){
if (!getArrayAccess().get().isFullyResolved()){
if (!getArrayAccess().get().isResolved()){
isResolved = false;
}
}
......@@ -138,7 +138,7 @@ public class IOLayerSymbol extends LayerSymbol {
@Override
protected void resolveExpressions() {
if (getArrayAccess().isPresent()){
getArrayAccess().get().resolve();
getArrayAccess().get().resolve(getSpannedScope());
}
}
......
......@@ -89,7 +89,12 @@ public abstract class LayerSymbol extends CommonScopeSpanningSymbol {
}
public void checkIfResolvable(){
unresolvableNames = computeUnresolvableNames();
if (isResolved()){
unresolvableNames = new HashSet<>();
}
else {
unresolvableNames = computeUnresolvableNames();
}
}
public void resolveOrError(){
......
......@@ -97,21 +97,23 @@ public class MethodDeclarationSymbol extends CommonScopeSpanningSymbol {
return res;
}
public Optional<LayerSymbol> call(MethodLayerSymbol layer) {
if (isPredefined()){
return Optional.of(layer);
}
else {
for (VariableSymbol param : getParameters()){
param.reset();
public void call(MethodLayerSymbol layer) {
if (layer.getParallelLength() == 1 && layer.getSerialLength() == 1){
if (isPredefined()){
layer.setResolvedThis(layer);
}
for (ArgumentSymbol arg : layer.getArguments()){
arg.set();
else {
for (VariableSymbol param : getParameters()){
param.reset();
}
for (ArgumentSymbol arg : layer.getArguments()){
arg.set();
}
LayerSymbol bodyCopy = getBody().copy();
layer.setResolvedThis(bodyCopy);
bodyCopy.resolve();
}
LayerSymbol resolvedCopy = getBody().copy();
resolvedCopy.resolveExpressions();
return Optional.of(resolvedCopy);
}
}
......
......@@ -67,7 +67,7 @@ public class MethodLayerSymbol extends LayerSymbol {
private void setMethod(MethodDeclarationSymbol method) {
if (method.isPredefined()){
resolvedThis = this;
setResolvedThis(this);
}