fully working resolve method

parent 8a7f93d4
......@@ -42,7 +42,7 @@
<mc.grammars.assembly.version>0.0.6-SNAPSHOT</mc.grammars.assembly.version>
<SIUnit.version>0.0.6-SNAPSHOT</SIUnit.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 .................................................. -->
<guava.version>18.0</guava.version>
<junit.version>4.12</junit.version>
......
......@@ -31,20 +31,23 @@ abstract public class ArchAbstractSequenceExpression extends ArchExpressionSymbo
super();
}
abstract public Optional<List<List<ArchSimpleExpressionSymbol>>> getElements();
abstract public boolean isParallelSequence();
abstract public boolean isSerialSequence();
abstract public Optional<Integer> getParallelLength();
abstract public Optional<Integer> getMaxSerialLength();
@Override
public boolean isSequence(){
return true;
}
@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());
List<Object> serialValues = new ArrayList<>();
for (ArchSimpleExpressionSymbol element : serialElements){
serialValues.add(element.getValue().get());
}
......@@ -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 @@
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.CommonSymbol;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope;
import de.monticore.symboltable.Symbol;
import de.se_rwth.commons.logging.Log;
import java.util.*;
......@@ -31,12 +34,16 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
private Set<String> unresolvableNames = null;
public ArchExpressionSymbol() {
super("", KIND);
}
protected Boolean isResolvable(){
Set<String> set = getUnresolvableNames();
return set != null && set.isEmpty();
}
public Set<String> getUnresolvableNames() {
if (unresolvableNames == null){
checkIfResolvable();
......@@ -44,17 +51,12 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
return unresolvableNames;
}
public boolean isResolvable(){
return getUnresolvableNames().isEmpty();
protected void setUnresolvableNames(Set<String> unresolvableNames){
this.unresolvableNames = unresolvableNames;
}
public void checkIfResolvable(){
if (isResolved()){
unresolvableNames = new HashSet<>();
}
else {
unresolvableNames = computeUnresolvableNames();
}
setUnresolvableNames(computeUnresolvableNames());
}
/**
......@@ -62,9 +64,7 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
*
* @return returns true iff the value of the resolved expression will be a boolean.
*/
public boolean isBoolean(){
return false;
}
abstract public boolean isBoolean();
/**
* Checks whether the value is a number.
......@@ -72,9 +72,7 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
*
* @return returns true iff the value of the resolved expression will be a number.
*/
public boolean isNumber(){
return false;
}
abstract public boolean isNumber();
/**
* Checks whether the value is a Tuple.
......@@ -83,9 +81,7 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
*
* @return returns true iff the value of the expression will be a tuple.
*/
public boolean isTuple(){
return false;
}
abstract public boolean isTuple();
/**
* 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 {
/**
* 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.
* 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.
......@@ -137,7 +133,7 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
/**
* 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.
* These Objects can either be Integer, Double or Boolean.
* Sequences of size 1 or 0 are counted as serial sequences.
......@@ -157,6 +153,10 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
return false;
}
public boolean isSequence(){
return false;
}
/**
*
* @return returns true if this object is instance of ArchSimpleExpressionSymbol
......@@ -259,11 +259,46 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
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.
*/
public void resolveOrError(Scope resolvingScope){
resolve(resolvingScope);
public void resolveOrError(){
resolve();
if (!isResolved()){
throw new IllegalStateException("The following names could not be resolved: " + getUnresolvableNames());
}
......@@ -286,12 +321,29 @@ abstract public class ArchExpressionSymbol extends CommonSymbol {
*
* @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 protected Set<String> computeUnresolvableNames();
/**
* @return returns true if the expression is resolved.
*/
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 @@
*/
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
......@@ -32,7 +35,7 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
private List<List<ArchSimpleExpressionSymbol>> elements = null;
public ArchRangeExpressionSymbol() {
protected ArchRangeExpressionSymbol() {
super();
}
......@@ -40,7 +43,7 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
return startSymbol;
}
public void setStartSymbol(ArchSimpleExpressionSymbol startSymbol) {
protected void setStartSymbol(ArchSimpleExpressionSymbol startSymbol) {
this.startSymbol = startSymbol;
}
......@@ -48,11 +51,11 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
return endSymbol;
}
public void setEndSymbol(ArchSimpleExpressionSymbol endSymbol) {
protected void setEndSymbol(ArchSimpleExpressionSymbol endSymbol) {
this.endSymbol = endSymbol;
}
protected boolean isParallel() {
public boolean isParallel() {
return parallel;
}
......@@ -70,7 +73,7 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
return !isParallel();
}
private Optional<Integer> getLength(){
/*private Optional<Integer> getLength(){
Optional<Integer> optLength = Optional.empty();
if (isResolved()){
Object startValue = getEndSymbol().getValue().get();
......@@ -82,36 +85,16 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
}
}
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
public Set<String> resolve(Scope resolvingScope) {
public Set<String> resolve() {
if (!isResolved()){
checkIfResolvable();
if (isResolvable()){
getStartSymbol().resolveOrError(resolvingScope);
getEndSymbol().resolveOrError(resolvingScope);
getStartSymbol().resolveOrError();
getEndSymbol().resolveOrError();
}
}
return getUnresolvableNames();
......@@ -128,23 +111,26 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
if (isResolved()){
int start = startSymbol.getIntValue().get();
int end = endSymbol.getIntValue().get();
int step = 1;
if (end < start){
step = -1;
List<Integer> range;
if (start <= end){
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()){
for (int i = start; i <= end; i = i + step){
for (int element : range){
List<ArchSimpleExpressionSymbol> values = new ArrayList<>(1);
values.add(ArchSimpleExpressionSymbol.of(i));
values.add(ArchSimpleExpressionSymbol.of(element));
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));
List<ArchSimpleExpressionSymbol> values = new ArrayList<>();
for (int element : range){
values.add(ArchSimpleExpressionSymbol.of(element));
}
elementList.add(values);
}
......@@ -162,4 +148,27 @@ public class ArchRangeExpressionSymbol extends ArchAbstractSequenceExpression {
unresolvableNames.addAll(getEndSymbol().computeUnresolvableNames());
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 @@
*/
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Scope;
import java.util.*;
......@@ -28,7 +29,7 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
private List<List<ArchSimpleExpressionSymbol>> elements;
public ArchSequenceExpressionSymbol() {
protected ArchSequenceExpressionSymbol() {
super();
}
......@@ -42,7 +43,7 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
return elements;
}
public void setElements(List<List<ArchSimpleExpressionSymbol>> elements) {
protected void setElements(List<List<ArchSimpleExpressionSymbol>> elements) {
this.elements = elements;
}
......@@ -63,35 +64,14 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
}
@Override
public Optional<Integer> getParallelLength() {
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) {
public Set<String> resolve() {
if (!isResolved()){
checkIfResolvable();
if (isResolvable()){
for (List<ArchSimpleExpressionSymbol> serialList : _getElements()) {
for (ArchSimpleExpressionSymbol element : serialList) {
element.resolveOrError(resolvingScope);
element.resolveOrError();
}
}
}
......@@ -122,4 +102,35 @@ public class ArchSequenceExpressionSymbol extends ArchAbstractSequenceExpression
}
return unresolvableNames;
}
public ArchSequenceExpressionSymbol copy(){
ArchSequenceExpressionSymbol copy = new ArchSequenceExpressionSymbol();
List<List<ArchSimpleExpressionSymbol>> elementsCopy = new ArrayList<>(getElements().get().size());
for (List<ArchSimpleExpressionSymbol> serialList : getElements().get()){
List<ArchSimpleExpressionSymbol> serialListCopy = new ArrayList<>(serialList.size());
for (ArchSimpleExpressionSymbol element : serialList){
serialListCopy.add(element.copy());
}
elementsCopy.add(serialListCopy);
}
copy.setElements(getElements().get());
copy.setUnresolvableNames(getUnresolvableNames());
return copy;
}
@Override
protected void putInScope(MutableScope scope) {
super.putInScope(scope);
for (List<ArchSimpleExpressionSymbol> serialList : _getElements()){
for (ArchSimpleExpressionSymbol element : serialList){
element.putInScope(scope);
}
}
}
public static ArchSequenceExpressionSymbol of(List<List<ArchSimpleExpressionSymbol>> elements){
ArchSequenceExpressionSymbol sym = new ArchSequenceExpressionSymbol();
sym.setElements(elements);
return sym;
}
}
......@@ -21,16 +21,15 @@
/* generated by template symboltable.ScopeSpanningSymbol*/
package de.monticore.lang.monticar.cnnarch._symboltable;
import static de.monticore.symboltable.Symbols.sortSymbolsByPosition;
import java.util.Collection;
import java.util.List;
public class ArchitectureSymbol extends ArchitectureSymbolTOP {
private LayerSymbol body;
private List<IODeclarationSymbol> inputs;
private List<IODeclarationSymbol> outputs;
public ArchitectureSymbol(String name) {
super(name);
......@@ -49,5 +48,21 @@ public class ArchitectureSymbol extends ArchitectureSymbolTOP {
this.body = body;
}
//todo
public List<IODeclarationSymbol> getInputs() {
return inputs;
}
public void setInputs(List<IODeclarationSymbol> inputs) {
this.inputs = inputs;
}
public List<IODeclarationSymbol> getOutputs() {
return outputs;
}
public void setOutputs(List<IODeclarationSymbol> outputs) {
this.outputs = outputs;
}
}
......@@ -21,8 +21,11 @@
package de.monticore.lang.monticar.cnnarch._symboltable;
import de.monticore.symboltable.CommonSymbol;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Symbol;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
......@@ -57,9 +60,19 @@ public class ArgumentSymbol extends CommonSymbol {
this.rhs = rhs;
}
//do not call if value is a sequence
public void set(){
if (getRhs().isSimpleValue()){
getParameter().setExpression((ArchSimpleExpressionSymbol) getRhs());
}
else {
throw new IllegalStateException("The value of the parameter is set to a sequence. This should never happen.");
}
}
public List<List<ArgumentSymbol>> split(){
if (getRhs().isRange()){
getRhs().resolveOrError(getEnclosingScope());
getRhs().resolveOrError();
}
List<List<ArchSimpleExpressionSymbol>> elements = getRhs().getElements().get();
List<List<ArgumentSymbol>> arguments = new ArrayList<>(elements.size());
......@@ -75,37 +88,28 @@ public class ArgumentSymbol extends CommonSymbol {
return arguments;
}
//do not call if value is a sequence
public void set(){
if (getRhs().isSimpleValue()){
getParameter().setExpression((ArchSimpleExpressionSymbol) getRhs());
}
else {
throw new IllegalStateException("The value of the parameter is set to a sequence. This should never happen.");
}
}
public Optional<List<List<ArgumentSymbol>>> expandedSplit(int parallelLength, int serialLength){
//todo: serial layers does not need be the same or one
public Optional<List<List<ArgumentSymbol>>> expandedSplit(int parallelLength, List<Integer> serialLengths){
List<List<ArgumentSymbol>> splitArguments = split();
boolean valid = splitArguments.size() == parallelLength || splitArguments.size() == 1;
int k = 0;
for (List<ArgumentSymbol> serialArgumentList : splitArguments){
if (serialArgumentList.size() != serialLength && serialArgumentList.size() != 1){
if (serialArgumentList.size() != serialLengths.get(k) && serialArgumentList.size() != 1){
valid = false;
}
k++;
}
if (valid){
List<List<ArgumentSymbol>> expandedArguments = new ArrayList<>(parallelLength);
for (int i = 0; i < parallelLength; i++){
List<ArgumentSymbol> expandedSerialArgumentList = new ArrayList<>(serialLength);
List<ArgumentSymbol> expandedSerialArgumentList = new ArrayList<>(serialLengths.get(i));
List<ArgumentSymbol> serialArgumentList = splitArguments.size() != 1
? splitArguments.get(i)
: splitArguments.get(0);
for (int j = 0; j < serialLength; j++){
for (int j = 0; j < serialLengths.get(i); j++){
ArgumentSymbol argument = serialArgumentList.size() != 1
? serialArgumentList.get(j)
: serialArgumentList.get(0);
......@@ -120,11 +124,20 @@ public class ArgumentSymbol extends CommonSymbol {
}
}
protected void putInScope(MutableScope scope){