fully working resolve method

parent 8a7f93d4
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
<mc.grammars.assembly.version>0.0.6-SNAPSHOT</mc.grammars.assembly.version> <mc.grammars.assembly.version>0.0.6-SNAPSHOT</mc.grammars.assembly.version>
<SIUnit.version>0.0.6-SNAPSHOT</SIUnit.version> <SIUnit.version>0.0.6-SNAPSHOT</SIUnit.version>
<Common-MontiCar.version>0.0.3-SNAPSHOT</Common-MontiCar.version> <Common-MontiCar.version>0.0.3-SNAPSHOT</Common-MontiCar.version>
<Math.version>0.0.3-SNAPSHOT-REWORK</Math.version> <Math.version>0.0.7.1</Math.version>
<!-- .. Libraries .................................................. --> <!-- .. Libraries .................................................. -->
<guava.version>18.0</guava.version> <guava.version>18.0</guava.version>
<junit.version>4.12</junit.version> <junit.version>4.12</junit.version>
......
...@@ -31,20 +31,23 @@ abstract public class ArchAbstractSequenceExpression extends ArchExpressionSymbo ...@@ -31,20 +31,23 @@ abstract public class ArchAbstractSequenceExpression extends ArchExpressionSymbo
super(); super();
} }
abstract public Optional<List<List<ArchSimpleExpressionSymbol>>> getElements();
abstract public boolean isParallelSequence(); abstract public boolean isParallelSequence();
abstract public boolean isSerialSequence(); abstract public boolean isSerialSequence();
abstract public Optional<Integer> getParallelLength(); @Override
public boolean isSequence(){
abstract public Optional<Integer> getMaxSerialLength(); return true;
}
@Override @Override
public Optional<Object> getValue() { public Optional<Object> getValue() {
if (isResolved()){ if (isResolved()){
List<List<Object>> parallelValues = new ArrayList<>(getParallelLength().get()); List<List<Object>> parallelValues = new ArrayList<>(getParallelLength().get());
for (List<ArchSimpleExpressionSymbol> serialElements : getElements().get()){ for (List<ArchSimpleExpressionSymbol> serialElements : getElements().get()){
List<Object> serialValues = new ArrayList<>(getMaxSerialLength().get()); List<Object> serialValues = new ArrayList<>();
for (ArchSimpleExpressionSymbol element : serialElements){ for (ArchSimpleExpressionSymbol element : serialElements){
serialValues.add(element.getValue().get()); serialValues.add(element.getValue().get());
} }
...@@ -57,4 +60,19 @@ abstract public class ArchAbstractSequenceExpression extends ArchExpressionSymbo ...@@ -57,4 +60,19 @@ abstract public class ArchAbstractSequenceExpression extends ArchExpressionSymbo
} }
} }
@Override
public boolean isBoolean(){
return false;
}
@Override
public boolean isNumber(){
return false;
}
@Override
public boolean isTuple(){
return false;
}
} }
...@@ -21,7 +21,10 @@ ...@@ -21,7 +21,10 @@
package de.monticore.lang.monticar.cnnarch._symboltable; package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.CommonSymbol; import de.monticore.symboltable.CommonSymbol;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope; import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
import de.se_rwth.commons.logging.Log;
import java.util.*; import java.util.*;
...@@ -31,12 +34,16 @@ abstract public class ArchExpressionSymbol extends CommonSymbol { ...@@ -31,12 +34,16 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
private Set<String> unresolvableNames = null; private Set<String> unresolvableNames = null;
public ArchExpressionSymbol() { public ArchExpressionSymbol() {
super("", KIND); super("", KIND);
} }
protected Boolean isResolvable(){
Set<String> set = getUnresolvableNames();
return set != null && set.isEmpty();
}
public Set<String> getUnresolvableNames() { public Set<String> getUnresolvableNames() {
if (unresolvableNames == null){ if (unresolvableNames == null){
checkIfResolvable(); checkIfResolvable();
...@@ -44,17 +51,12 @@ abstract public class ArchExpressionSymbol extends CommonSymbol { ...@@ -44,17 +51,12 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
return unresolvableNames; return unresolvableNames;
} }
public boolean isResolvable(){ protected void setUnresolvableNames(Set<String> unresolvableNames){
return getUnresolvableNames().isEmpty(); this.unresolvableNames = unresolvableNames;
} }
public void checkIfResolvable(){ public void checkIfResolvable(){
if (isResolved()){ setUnresolvableNames(computeUnresolvableNames());
unresolvableNames = new HashSet<>();
}
else {
unresolvableNames = computeUnresolvableNames();
}
} }
/** /**
...@@ -62,9 +64,7 @@ abstract public class ArchExpressionSymbol extends CommonSymbol { ...@@ -62,9 +64,7 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
* *
* @return returns true iff the value of the resolved expression will be a boolean. * @return returns true iff the value of the resolved expression will be a boolean.
*/ */
public boolean isBoolean(){ abstract public boolean isBoolean();
return false;
}
/** /**
* Checks whether the value is a number. * Checks whether the value is a number.
...@@ -72,9 +72,7 @@ abstract public class ArchExpressionSymbol extends CommonSymbol { ...@@ -72,9 +72,7 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
* *
* @return returns true iff the value of the resolved expression will be a number. * @return returns true iff the value of the resolved expression will be a number.
*/ */
public boolean isNumber(){ abstract public boolean isNumber();
return false;
}
/** /**
* Checks whether the value is a Tuple. * Checks whether the value is a Tuple.
...@@ -83,9 +81,7 @@ abstract public class ArchExpressionSymbol extends CommonSymbol { ...@@ -83,9 +81,7 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
* *
* @return returns true iff the value of the expression will be a tuple. * @return returns true iff the value of the expression will be a tuple.
*/ */
public boolean isTuple(){ abstract public boolean isTuple();
return false;
}
/** /**
* Checks whether the value is an integer. This can only be checked if the expression is resolved. * Checks whether the value is an integer. This can only be checked if the expression is resolved.
...@@ -124,7 +120,7 @@ abstract public class ArchExpressionSymbol extends CommonSymbol { ...@@ -124,7 +120,7 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
/** /**
* Checks whether the value is a parallel Sequence. * Checks whether the value is a parallel Sequence.
* If true, getRhs() will return (if present) a List of Lists of Objects. * If true, getValue() will return (if present) a List of Lists of Objects.
* These Objects can either be Integer, Double or Boolean. * These Objects can either be Integer, Double or Boolean.
* If isSerialSequence() returns false, the second List will always have a size smaller than 2. * If isSerialSequence() returns false, the second List will always have a size smaller than 2.
* Sequences of size 1 or 0 cannot be parallel sequences. * Sequences of size 1 or 0 cannot be parallel sequences.
...@@ -137,7 +133,7 @@ abstract public class ArchExpressionSymbol extends CommonSymbol { ...@@ -137,7 +133,7 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
/** /**
* Checks whether the value is a serial Sequence. * Checks whether the value is a serial Sequence.
* If true, getRhs() will return (if present) a List(parallel) of Lists(serial) of Objects. * If true, getValue() will return (if present) a List(parallel) of Lists(serial) of Objects.
* If isParallelSequence() is false, the first list will be of size 1. * If isParallelSequence() is false, the first list will be of size 1.
* These Objects can either be Integer, Double or Boolean. * These Objects can either be Integer, Double or Boolean.
* Sequences of size 1 or 0 are counted as serial sequences. * Sequences of size 1 or 0 are counted as serial sequences.
...@@ -157,6 +153,10 @@ abstract public class ArchExpressionSymbol extends CommonSymbol { ...@@ -157,6 +153,10 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
return false; return false;
} }
public boolean isSequence(){
return false;
}
/** /**
* *
* @return returns true if this object is instance of ArchSimpleExpressionSymbol * @return returns true if this object is instance of ArchSimpleExpressionSymbol
...@@ -259,11 +259,46 @@ abstract public class ArchExpressionSymbol extends CommonSymbol { ...@@ -259,11 +259,46 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
return Optional.empty(); return Optional.empty();
} }
public Optional<Integer> getParallelLength(){
Optional<List<List<ArchSimpleExpressionSymbol>>> elements = getElements();
return elements.map(e -> e.isEmpty() ? 1 : e.size());
}
public Optional<List<Integer>> getSerialLengths(){
Optional<List<List<ArchSimpleExpressionSymbol>>> elements = getElements();
if (elements.isPresent()){
List<Integer> serialLengths = new ArrayList<>();
for (List<ArchSimpleExpressionSymbol> serialList : getElements().get()){
serialLengths.add(serialList.size());
}
return Optional.of(serialLengths);
}
else {
return Optional.empty();
}
}
public Optional<Integer> getMaxSerialLength(){
int max = 0;
Optional<List<Integer>> optLens = getSerialLengths();
if (optLens.isPresent()){
for (int len : optLens.get()){
if (len > max){
max = len;
}
}
}
else {
return Optional.empty();
}
return Optional.of(max);
}
/** /**
* Same as resolve() but throws an error if it was not successful. * Same as resolve() but throws an error if it was not successful.
*/ */
public void resolveOrError(Scope resolvingScope){ public void resolveOrError(){
resolve(resolvingScope); resolve();
if (!isResolved()){ if (!isResolved()){
throw new IllegalStateException("The following names could not be resolved: " + getUnresolvableNames()); throw new IllegalStateException("The following names could not be resolved: " + getUnresolvableNames());
} }
...@@ -286,12 +321,29 @@ abstract public class ArchExpressionSymbol extends CommonSymbol { ...@@ -286,12 +321,29 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
* *
* @return returns a set of all names which could not be resolved. * @return returns a set of all names which could not be resolved.
*/ */
abstract public Set<String> resolve(Scope resolvingScope); abstract public Set<String> resolve();
/**
* @return returns a optional of a list(parallel) of lists(serial) of simple expressions in this sequence.
* These lists will only contain one element if this is not a sequence.
* If the optional is not present that means this expression is a range which is not resolved.
*/
abstract public Optional<List<List<ArchSimpleExpressionSymbol>>> getElements(); abstract public Optional<List<List<ArchSimpleExpressionSymbol>>> getElements();
abstract protected Set<String> computeUnresolvableNames(); abstract protected Set<String> computeUnresolvableNames();
/**
* @return returns true if the expression is resolved.
*/
abstract public boolean isResolved(); abstract public boolean isResolved();
abstract public ArchExpressionSymbol copy();
protected void putInScope(MutableScope scope){
Collection<Symbol> symbolsInScope = scope.getLocalSymbols().get(getName());
if (symbolsInScope == null || !symbolsInScope.contains(this)) {
scope.add(this);
}
}
} }
...@@ -20,9 +20,12 @@ ...@@ -20,9 +20,12 @@
*/ */
package de.monticore.lang.monticar.cnnarch._symboltable; package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope; import de.monticore.symboltable.Scope;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression { public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
...@@ -32,7 +35,7 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression { ...@@ -32,7 +35,7 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
private List<List<ArchSimpleExpressionSymbol>> elements = null; private List<List<ArchSimpleExpressionSymbol>> elements = null;
public ArchRangeExpressionSymbol() { protected ArchRangeExpressionSymbol() {
super(); super();
} }
...@@ -40,7 +43,7 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression { ...@@ -40,7 +43,7 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
return startSymbol; return startSymbol;
} }
public void setStartSymbol(ArchSimpleExpressionSymbol startSymbol) { protected void setStartSymbol(ArchSimpleExpressionSymbol startSymbol) {
this.startSymbol = startSymbol; this.startSymbol = startSymbol;
} }
...@@ -48,11 +51,11 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression { ...@@ -48,11 +51,11 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
return endSymbol; return endSymbol;
} }
public void setEndSymbol(ArchSimpleExpressionSymbol endSymbol) { protected void setEndSymbol(ArchSimpleExpressionSymbol endSymbol) {
this.endSymbol = endSymbol; this.endSymbol = endSymbol;
} }
protected boolean isParallel() { public boolean isParallel() {
return parallel; return parallel;
} }
...@@ -70,7 +73,7 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression { ...@@ -70,7 +73,7 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
return !isParallel(); return !isParallel();
} }
private Optional<Integer> getLength(){ /*private Optional<Integer> getLength(){
Optional<Integer> optLength = Optional.empty(); Optional<Integer> optLength = Optional.empty();
if (isResolved()){ if (isResolved()){
Object startValue = getEndSymbol().getValue().get(); Object startValue = getEndSymbol().getValue().get();
...@@ -82,36 +85,16 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression { ...@@ -82,36 +85,16 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
} }
} }
return optLength; return optLength;
} }*/
@Override
public Optional<Integer> getParallelLength() {
if (isParallelSequence()) {
return getLength();
}
else {
return Optional.of(1);
}
}
@Override
public Optional<Integer> getMaxSerialLength() {
if (isSerialSequence()) {
return getLength();
}
else {
return Optional.of(1);
}
}
@Override @Override
public Set<String> resolve(Scope resolvingScope) { public Set<String> resolve() {
if (!isResolved()){ if (!isResolved()){
checkIfResolvable(); checkIfResolvable();
if (isResolvable()){ if (isResolvable()){
getStartSymbol().resolveOrError(resolvingScope); getStartSymbol().resolveOrError();
getEndSymbol().resolveOrError(resolvingScope); getEndSymbol().resolveOrError();
} }
} }
return getUnresolvableNames(); return getUnresolvableNames();
...@@ -128,23 +111,26 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression { ...@@ -128,23 +111,26 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
if (isResolved()){ if (isResolved()){
int start = startSymbol.getIntValue().get(); int start = startSymbol.getIntValue().get();
int end = endSymbol.getIntValue().get(); int end = endSymbol.getIntValue().get();
int step = 1; List<Integer> range;
if (end < start){ if (start <= end){
step = -1; range = IntStream.rangeClosed(start, end).boxed().collect(Collectors.toList());
}
else {
range = IntStream.rangeClosed(-start, -end).map(e -> -e).boxed().collect(Collectors.toList());
} }
List<List<ArchSimpleExpressionSymbol>> elementList = new ArrayList<>(getParallelLength().get()); List<List<ArchSimpleExpressionSymbol>> elementList = new ArrayList<>();
if (isParallel()){ if (isParallel()){
for (int i = start; i <= end; i = i + step){ for (int element : range){
List<ArchSimpleExpressionSymbol> values = new ArrayList<>(1); List<ArchSimpleExpressionSymbol> values = new ArrayList<>(1);
values.add(ArchSimpleExpressionSymbol.of(i)); values.add(ArchSimpleExpressionSymbol.of(element));
elementList.add(values); elementList.add(values);
} }
} }
else { else {
List<ArchSimpleExpressionSymbol> values = new ArrayList<>(getMaxSerialLength().get()); List<ArchSimpleExpressionSymbol> values = new ArrayList<>();
for (int i = start; i <= end; i = i + step){ for (int element : range){
values.add(ArchSimpleExpressionSymbol.of(i)); values.add(ArchSimpleExpressionSymbol.of(element));
} }
elementList.add(values); elementList.add(values);
} }
...@@ -162,4 +148,27 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression { ...@@ -162,4 +148,27 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
unresolvableNames.addAll(getEndSymbol().computeUnresolvableNames()); unresolvableNames.addAll(getEndSymbol().computeUnresolvableNames());
return unresolvableNames; return unresolvableNames;
} }
public ArchRangeExpressionSymbol copy(){
ArchRangeExpressionSymbol copy = new ArchRangeExpressionSymbol();
copy.setParallel(isParallel());
copy.setStartSymbol(getStartSymbol().copy());
copy.setEndSymbol(getEndSymbol().copy());
copy.setUnresolvableNames(getUnresolvableNames());
return copy;
}
@Override
protected void putInScope(MutableScope scope) {
super.putInScope(scope);
getStartSymbol().putInScope(scope);
getEndSymbol().putInScope(scope);
}
public static ArchRangeExpressionSymbol of(ArchSimpleExpressionSymbol start, ArchSimpleExpressionSymbol end){
ArchRangeExpressionSymbol sym = new ArchRangeExpressionSymbol();
sym.setStartSymbol(start);
sym.setEndSymbol(end);
return sym;
}
} }
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
*/ */
package de.monticore.lang.monticar.cnnarch._symboltable; package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope; import de.monticore.symboltable.Scope;
import java.util.*; import java.util.*;
...@@ -28,7 +29,7 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression ...@@ -28,7 +29,7 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
private List<List<ArchSimpleExpressionSymbol>> elements; private List<List<ArchSimpleExpressionSymbol>> elements;
public ArchSequenceExpressionSymbol() { protected ArchSequenceExpressionSymbol() {
super(); super();
} }
...@@ -42,7 +43,7 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression ...@@ -42,7 +43,7 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
return elements; return elements;
} }
public void setElements(List<List<ArchSimpleExpressionSymbol>> elements) { protected void setElements(List<List<ArchSimpleExpressionSymbol>> elements) {
this.elements = elements; this.elements = elements;
} }
...@@ -63,35 +64,14 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression ...@@ -63,35 +64,14 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
} }
@Override @Override
public Optional<Integer> getParallelLength() { public Set<String> resolve() {
return Optional.of(elements.size());
}
@Override
public Optional<Integer> getMaxSerialLength() {
if (!elements.isEmpty()){
int maxLenght = 0;
for (List<ArchSimpleExpressionSymbol> element : _getElements()){
if (maxLenght < element.size()){
maxLenght = element.size();
}
}
return Optional.of(maxLenght);
}
else {
return Optional.of(0);
}
}
@Override
public Set<String> resolve(Scope resolvingScope) {
if (!isResolved()){ if (!isResolved()){
checkIfResolvable(); checkIfResolvable();
if (isResolvable()){ if (isResolvable()){
for (List<ArchSimpleExpressionSymbol> serialList : _getElements()) { for (List<ArchSimpleExpressionSymbol> serialList : _getElements()) {
for (ArchSimpleExpressionSymbol element : serialList) { for (ArchSimpleExpressionSymbol element : serialList) {
element.resolveOrError(resolvingScope); element.resolveOrError();
} }
} }
} }
...@@ -122,4 +102,35 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression ...@@ -122,4 +102,35 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
} }
return unresolvableNames; return unresolvableNames;