Skip to content

Commit

Permalink
Unify user-defined operations and simple contributed operations
Browse files Browse the repository at this point in the history
  • Loading branch information
pkourouklidis committed Oct 1, 2024
1 parent 6404fc7 commit b85e0b6
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import javax.xml.transform.stream.StreamResult;

import org.eclipse.epsilon.common.util.CollectionUtil;
import org.eclipse.epsilon.eol.execute.operations.OperationInfo;
import org.eclipse.epsilon.eol.types.EolPrimitiveType;
import org.eclipse.epsilon.eol.types.EolType;
import org.w3c.dom.Document;
import org.w3c.dom.Text;

Expand All @@ -31,6 +34,11 @@ public boolean contributesTo(Object target) {
return (target instanceof String || target instanceof Character);
}

@Override
public EolType contributesToType() {
return EolPrimitiveType.String;
}

public Object toEnum() throws Exception {
return getContext().getModelRepository().getEnumerationValue(getTarget() + "");
}
Expand All @@ -57,6 +65,7 @@ public String firstToUpperCase() {
return value.substring(0,1).toUpperCase() + value.substring(1, value.length());
}

@OperationInfo
public String characterAt(int index) {
String value = (String) getTarget();
return value.charAt(index) + "";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.eclipse.epsilon.eol.staticanalyser;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -85,6 +86,7 @@
import org.eclipse.epsilon.eol.dom.XorOperatorExpression;
import org.eclipse.epsilon.eol.staticanalyser.execute.context.FrameStack;
import org.eclipse.epsilon.eol.execute.context.FrameType;
import org.eclipse.epsilon.eol.execute.operations.contributors.OperationContributor;
import org.eclipse.epsilon.eol.staticanalyser.execute.context.Variable;
import org.eclipse.epsilon.eol.m3.MetaClass;
import org.eclipse.epsilon.eol.m3.Metamodel;
Expand Down Expand Up @@ -731,7 +733,7 @@ public void visit(OperationCallExpression operationCallExpression) {
// Number of parameters check
temp = new ArrayList<IStaticOperation>();
for (IStaticOperation op: resolvedOperations) {
List<Parameter> reqParams = op.getParameters();
List<EolType> reqParams = op.getParameterTypes();
if (reqParams.size() == parameterExpressions.size()) {
temp.add(op);
}
Expand All @@ -746,14 +748,13 @@ public void visit(OperationCallExpression operationCallExpression) {
temp = new ArrayList<IStaticOperation>();
for (IStaticOperation op: resolvedOperations) {
int index = 0;
List<Parameter> reqParams = op.getParameters();
List<EolType> reqParamTypess = op.getParameterTypes();
boolean compatible = true;
for (Parameter reqParam : reqParams) {
EolType reqParameter = getResolvedType(reqParam.getTypeExpression());
EolType provParameter = getResolvedType(parameterExpressions.get(index));
for (EolType reqParamType : reqParamTypess) {
EolType provParamType = getResolvedType(parameterExpressions.get(index));
index++;
if (!isCompatible(reqParameter, provParameter)
&& !canBeCompatible(reqParameter, provParameter)) {
if (!isCompatible(reqParamType, provParamType)
&& !canBeCompatible(reqParamType, provParamType)) {
compatible = false;
break;
}
Expand Down Expand Up @@ -1190,8 +1191,40 @@ public void preValidate(IEolModule imodule) {
}

module.getDeclaredOperations().forEach(o -> operationPreVisitor(o));
module.getDeclaredOperations().forEach(o -> operations.add(new UserDefinedOperation(o)));
module.getDeclaredOperations().forEach(o -> operations.add(new SimpleOperation(o)));
module.getDeclaredOperations().forEach(o -> o.accept(this));

//Parse builtin operations
List<OperationContributor> operationContributors = context.operationContributorRegistry.stream().collect(Collectors.toList());
for(OperationContributor oc: operationContributors) {
EolType contextType = toStaticAnalyserType(oc.contributesToType());

for(Method m: oc.getClass().getDeclaredMethods()) {
List<EolType> operationParameterTypes = new ArrayList<EolType>();
Class<?>[] javaParameterTypes = m.getParameterTypes();
for (Class<?> javaParameterType : javaParameterTypes) {
operationParameterTypes.add(javaClassToEolType(javaParameterType));
}
EolType returnType = javaClassToEolType(m.getReturnType());
operations.add(new SimpleOperation(m.getName(), contextType, returnType, operationParameterTypes));
}
}
}

public EolType javaClassToEolType(Class<?> javaClass) {

if (javaClass == String.class || javaClass == char.class) {
return EolPrimitiveType.String;
} else if (javaClass == Integer.class || javaClass == int.class) {
return EolPrimitiveType.Integer;
} else if (javaClass == Double.class || javaClass == double.class || javaClass == Float.class
|| javaClass == float.class) {
return EolPrimitiveType.Real;
} else if (javaClass == boolean.class || javaClass == Boolean.class) {
return EolPrimitiveType.Boolean;
}

return EolAnyType.Instance;
}

public void mainValidate(IEolModule module) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.List;

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

public interface IStaticOperation {
Expand All @@ -11,7 +10,8 @@ public interface IStaticOperation {

public EolType getContextType();

//TODO: We might need the parameter types for this
public EolType getReturnType();

public List<Parameter> getParameters();
public List<EolType> getParameterTypes();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.eclipse.epsilon.eol.staticanalyser;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.epsilon.eol.dom.Operation;
import org.eclipse.epsilon.eol.dom.Parameter;
import org.eclipse.epsilon.eol.staticanalyser.types.EolType;

public class SimpleOperation implements IStaticOperation {
private String name;
private EolType contextType;
private EolType returnType;
private List<EolType> parameterTypes;

public SimpleOperation(Operation op) {
name = op.getName();
contextType = (EolType) op.getData().get("contextType");
returnType = (EolType) op.getData().get("returnType");
this.parameterTypes = new ArrayList<EolType>();
for (Parameter p: op.getFormalParameters()) {
EolType parameterType = (EolType) p.getTypeExpression().getData().get("resolvedType");
parameterTypes.add(parameterType);
}
}

public SimpleOperation(String name, EolType contextType, EolType returnType, List<EolType> parameterTypes) {
this.name = name;
this.contextType = contextType;
this.returnType = returnType;
this.parameterTypes = parameterTypes;
}

@Override
public String getName() {
return name;
}

@Override
public EolType getContextType() {
return contextType;
}

@Override
public EolType getReturnType() {
return returnType;
}

@Override
public List<EolType> getParameterTypes() {
return parameterTypes;
}

}

This file was deleted.

0 comments on commit b85e0b6

Please sign in to comment.