Skip to content

Commit

Permalink
add type calculator for aggregate operation
Browse files Browse the repository at this point in the history
  • Loading branch information
pkourouklidis committed Jan 17, 2025
1 parent 98a3f66 commit 512c8d6
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.eclipse.epsilon.eol.execute.operations;

import java.util.List;

import org.eclipse.epsilon.eol.staticanalyser.types.EolType;

public interface ITypeCalculator {
public EolType calculateType(EolType contextType, EolType iteratorType, EolType expressionType);
public EolType calculateType(EolType contextType, EolType iteratorType, List<EolType> expressionTypes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import org.eclipse.epsilon.eol.execute.context.FrameType;
import org.eclipse.epsilon.eol.execute.context.IEolContext;
import org.eclipse.epsilon.eol.execute.context.Variable;
import org.eclipse.epsilon.eol.execute.operations.TypeCalculator;
import org.eclipse.epsilon.eol.types.EolMap;

@TypeCalculator(klass = AggregateTypeCalculator.class)
public class AggregateOperation extends FirstOrderOperation {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.eclipse.epsilon.eol.execute.operations.declarative;

import java.util.List;

import org.eclipse.epsilon.eol.execute.operations.ITypeCalculator;
import org.eclipse.epsilon.eol.staticanalyser.types.EolMapType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolType;

public class AggregateTypeCalculator implements ITypeCalculator {

@Override
public EolType calculateType(EolType contextType, EolType iteratorType, List<EolType> expressionTypes) {
return new EolMapType(expressionTypes.get(0), expressionTypes.get(1));
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.eclipse.epsilon.eol.execute.operations.declarative;

import java.util.List;

import org.eclipse.epsilon.eol.execute.operations.ITypeCalculator;
import org.eclipse.epsilon.eol.staticanalyser.types.EolPrimitiveType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolType;

public class BooleanTypeCalculator implements ITypeCalculator {

@Override
public EolType calculateType(EolType contextType, EolType iteratorType, EolType expressionType) {
public EolType calculateType(EolType contextType, EolType iteratorType, List<EolType> expressionTypes) {
return EolPrimitiveType.Boolean;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.eclipse.epsilon.eol.execute.operations.declarative;

import java.util.List;

import org.eclipse.epsilon.eol.execute.operations.ITypeCalculator;
import org.eclipse.epsilon.eol.staticanalyser.types.EolCollectionType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolType;

public class CollectTypeCalculator implements ITypeCalculator {

@Override
public EolType calculateType(EolType contextType, EolType iteratorType, EolType expressionType) {
public EolType calculateType(EolType contextType, EolType iteratorType, List<EolType> expressionTypes) {
String collectionName = ((EolCollectionType)contextType).getName();
String newCollectionName = null;
if (collectionName.equals("Bag") || collectionName.equals("Set")) {
Expand All @@ -19,7 +21,7 @@ else if (collectionName.equals("Sequence") || collectionName.equals("OrderedSet"
else {
throw new RuntimeException("Unknown collection name");
}
return new EolCollectionType(newCollectionName, expressionType);
return new EolCollectionType(newCollectionName, expressionTypes.get(0));
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.eclipse.epsilon.eol.execute.operations.declarative;

import java.util.List;

import org.eclipse.epsilon.eol.execute.operations.ITypeCalculator;
import org.eclipse.epsilon.eol.staticanalyser.types.EolPrimitiveType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolType;

public class IntegerTypeCalculator implements ITypeCalculator {

@Override
public EolType calculateType(EolType contextType, EolType iteratorType, EolType expressionType) {
public EolType calculateType(EolType contextType, EolType iteratorType, List<EolType> expressionTypes) {
return EolPrimitiveType.Integer;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.eclipse.epsilon.eol.execute.operations.declarative;

import java.util.List;

import org.eclipse.epsilon.eol.execute.operations.ITypeCalculator;
import org.eclipse.epsilon.eol.staticanalyser.types.EolCollectionType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolMapType;
Expand All @@ -8,8 +10,8 @@
public class MapByTypeCalculator implements ITypeCalculator {

@Override
public EolType calculateType(EolType contextType, EolType iteratorType, EolType expressionType) {
return new EolMapType(expressionType, new EolCollectionType("Sequence", iteratorType));
public EolType calculateType(EolType contextType, EolType iteratorType, List<EolType> expressionTypes) {
return new EolMapType(expressionTypes.get(0), new EolCollectionType("Sequence", iteratorType));
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.eclipse.epsilon.eol.execute.operations.declarative;

import java.util.List;

import org.eclipse.epsilon.eol.execute.operations.ITypeCalculator;
import org.eclipse.epsilon.eol.staticanalyser.types.EolType;

public class SelectOneTypeCalculator implements ITypeCalculator {

@Override
public EolType calculateType(EolType contextType, EolType iteratorType, EolType expressionType) {
public EolType calculateType(EolType contextType, EolType iteratorType, List<EolType> expressionType) {
return iteratorType;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.eclipse.epsilon.eol.execute.operations.declarative;

import java.util.List;

import org.eclipse.epsilon.eol.execute.operations.ITypeCalculator;
import org.eclipse.epsilon.eol.staticanalyser.types.EolCollectionType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolType;

public class SelectTypeCalculator implements ITypeCalculator {

@Override
public EolType calculateType(EolType contextType, EolType iteratorType, EolType expressionType) {
public EolType calculateType(EolType contextType, EolType iteratorType, List<EolType> expressionTypes) {
String collectionName = ((EolCollectionType)contextType).getName();
return new EolCollectionType(collectionName, iteratorType);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.eclipse.epsilon.eol.execute.operations.declarative;

import java.util.List;

import org.eclipse.epsilon.eol.execute.operations.ITypeCalculator;
import org.eclipse.epsilon.eol.staticanalyser.types.EolCollectionType;
import org.eclipse.epsilon.eol.staticanalyser.types.EolType;

public class SortByTypeCalculator implements ITypeCalculator {

@Override
public EolType calculateType(EolType contextType, EolType iteratorType, EolType expressionType) {
public EolType calculateType(EolType contextType, EolType iteratorType, List<EolType> expressionTypes) {
return new EolCollectionType("Sequence", iteratorType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,16 @@ public void visit(FirstOrderOperationCallExpression firstOrderOperationCallExpre

context.getFrameStack().enterLocal(FrameType.UNPROTECTED, firstOrderOperationCallExpression,
new Variable(iterator.getName(), iteratorType));
Expression expression = firstOrderOperationCallExpression.getExpressions().get(0);
expression.accept(this);
EolType expressionType = getResolvedType(expression);
List<Expression> expressions = firstOrderOperationCallExpression.getExpressions();
for (Expression expression: expressions) {
expression.accept(this);
}

List<EolType> expressionTypes = expressions.stream().map(e -> getResolvedType(e)).collect(Collectors.toList());
context.getFrameStack().leaveLocal(firstOrderOperationCallExpression);

try {
EolType returnType = tc.klass().newInstance().calculateType(contextType, iteratorType, expressionType);
EolType returnType = tc.klass().newInstance().calculateType(contextType, iteratorType, expressionTypes);
setResolvedType(firstOrderOperationCallExpression, returnType);
} catch (Exception e) {
setResolvedType(firstOrderOperationCallExpression, EolAnyType.Instance);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*Map<Integer, Integer>*/Bag{0..9}.aggregate(i:Integer|i,i);
/*Map<Integer, Integer>*/Sequence{0..9}.aggregate(i:Integer|i,i);
/*Map<Integer, Integer>*/Set{0..9}.aggregate(i:Integer|i,i);
/*Map<Integer, Integer>*/OrderedSet{0..9}.aggregate(i:Integer|i,i);

0 comments on commit 512c8d6

Please sign in to comment.