Skip to content

Commit

Permalink
The context and return types of a contributed operation can be a union
Browse files Browse the repository at this point in the history
  • Loading branch information
pkourouklidis committed Oct 16, 2024
1 parent 31141a5 commit e3ce708
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
******************************************************************************/
package org.eclipse.epsilon.eol.execute.operations.contributors;

import java.util.Arrays;
import java.util.List;
import java.util.stream.LongStream;

import org.eclipse.epsilon.eol.types.EolPrimitiveType;
import org.eclipse.epsilon.eol.types.EolType;
import org.eclipse.epsilon.eol.types.NumberUtil;

public class NumberOperationContributor extends OperationContributor {
Expand All @@ -19,6 +24,11 @@ public boolean contributesTo(Object target) {
return target instanceof Number;
}

@Override
public List<EolType> contributesToType() {
return Arrays.asList(EolPrimitiveType.Real, EolPrimitiveType.Integer);
}

@Override
protected Number getTarget() {
return (Number) super.getTarget();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
package org.eclipse.epsilon.eol.execute.operations.contributors;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import org.eclipse.epsilon.common.parse.AST;
import org.eclipse.epsilon.eol.dom.Expression;
import org.eclipse.epsilon.eol.execute.context.IEolContext;
import org.eclipse.epsilon.eol.execute.introspection.java.ObjectMethod;
import org.eclipse.epsilon.eol.types.EolAnyType;
import org.eclipse.epsilon.eol.types.EolType;
import org.eclipse.epsilon.eol.util.ReflectionUtil;

/**
Expand All @@ -33,6 +36,10 @@ public abstract class OperationContributor implements AutoCloseable {

public abstract boolean contributesTo(Object target);

public List<EolType> contributesToType() {
return Arrays.asList(EolAnyType.Instance);
}

public ObjectMethod findContributedMethodForUnevaluatedParameters(Object target, String name, List<Expression> parameterExpressions, IEolContext context) {
// Note that the last parameter is false: we only want to retrieve methods that take an AST as an argument
// and not methods that take a supertype of AST (such as Object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.io.FileOutputStream;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
Expand All @@ -35,8 +36,8 @@ public boolean contributesTo(Object target) {
}

@Override
public EolType contributesToType() {
return EolPrimitiveType.String;
public List<EolType> contributesToType() {
return Arrays.asList(EolPrimitiveType.String);
}

public Object toEnum() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,14 @@ public void preValidate(IEolModule imodule) {
//Parse builtin operations
List<OperationContributor> operationContributors = context.operationContributorRegistry.stream().collect(Collectors.toList());
for(OperationContributor oc: operationContributors) {
EolType contextType = toStaticAnalyserType(oc.contributesToType());
List<EolType> contextTypes = oc.contributesToType().stream().map(t -> toStaticAnalyserType(t)).collect(Collectors.toList());
EolType contextType;
if (contextTypes.size() == 1) {
contextType = contextTypes.get(0);
}
else {
contextType = new EolUnionType(contextTypes);
}

for(Method m: oc.getClass().getDeclaredMethods()) {
List<EolType> operationParameterTypes = new ArrayList<EolType>();
Expand Down Expand Up @@ -1251,6 +1258,8 @@ public EolType javaClassToEolType(Class<?> javaClass) {
} else if (javaClass == Double.class || javaClass == double.class || javaClass == Float.class
|| javaClass == float.class) {
return EolPrimitiveType.Real;
} else if (javaClass == Number.class) {
return new EolUnionType(EolPrimitiveType.Real, EolPrimitiveType.Integer);
} else if (javaClass == boolean.class || javaClass == Boolean.class) {
return EolPrimitiveType.Boolean;
} else if (javaClass == java.util.Collection.class) {
Expand Down Expand Up @@ -1508,6 +1517,15 @@ public boolean canBeCompatible(EolType targetType, EolType valueType) {
return false;
}

if (targetType instanceof EolUnionType) {
for (EolType t : ((EolUnionType)targetType).containedTypes) {
if (canBeCompatible(t, valueType)) {
return true;
}
}
return false;
}

while (true) {

if (!(targetType.equals(valueType)) && !(valueType instanceof EolAnyType)) {
Expand Down

0 comments on commit e3ce708

Please sign in to comment.