org.openprovenance.prov.template.emitter.minilanguage.Expression Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prov-template-compiler Show documentation
Show all versions of prov-template-compiler Show documentation
A template system for PROV bundles.
The newest version!
package org.openprovenance.prov.template.emitter.minilanguage;
import org.openprovenance.prov.template.emitter.Element;
import org.openprovenance.prov.template.emitter.Pair;
import org.openprovenance.prov.template.emitter.Token;
import org.openprovenance.prov.template.emitter.minilanguage.emitters.Python;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import static org.openprovenance.prov.template.compiler.common.CompilerCommon.*;
import static org.openprovenance.prov.template.emitter.Pair.*;
public class Expression extends Statement {
public Expression(List elements) {
super(elements);
}
static public Expression makeExpression(List elements) {
if (elements.size()==1 && elements.get(0) instanceof Pair && getFormat(elements.get(0)).equals("$N")) {
return new Symbol((String)getArg(elements.get(0)), elements);
} else if (elements.size()==1 && elements.get(0) instanceof Pair && getFormat(elements.get(0)).equals("$S")) {
return new Constant((String)getArg(elements.get(0)), elements);
} else if (elements.size()==1 && elements.get(0) instanceof Pair && getFormat(elements.get(0)).equals("$L")) {
return ((List) getArg(elements.get(0))).get(0);
} else if (elements.size()>1
&& isPair(elements.get(0))
&& MARKER_LAMBDA.equals(getArg(elements.get(0)))) {
List ll=elements.subList(1,elements.size());
assert ll.size()==1;
List parameters_and_body=((List) getArg(ll.get(0))).stream().map(x->makeStatement(x.getElements())).collect(Collectors.toList());
List parameterElements=parameters_and_body.get(0).getElements();
assert parameterElements.size()>4;
assert parameterElements.get(0).equals(new Token("("));
assert parameterElements.get(1) instanceof Pair;
assert parameterElements.get(2).equals(new Token(") -> "));
List _params=((List)getArg(parameterElements.get(1))).get(0).getElements();
List parameters=new LinkedList<>();
Parameter parameter=null;
for (Element e: _params) {
if (e instanceof Pair) {
Pair p=(Pair)e;
if (p.getFormatPart().equals("$T")) {
parameter=new Parameter(null, p.getArg().toString());
} else if (p.getFormatPart().equals("$N")) {
assert parameter != null;
parameter.name = p.getArg().toString();
parameters.add(parameter);
}
} else if (e instanceof Token) {
assert getToken(e).equals(", ");
} else {
throw new UnsupportedOperationException("Cannot handle "+e);
}
}
List body=parameters_and_body.subList(1,parameters_and_body.size());
return new Lambda(parameters, body, elements);
} else if (elements.size()>1
&& isPair(elements.get(0))
&& "new".equals(getArg(elements.get(0)))) {
if (elements.size()>3
&& isPair(elements.get(2))
&& MARKER_ARRAY.equals(getArg(elements.get(2)))) {
List allArgs=elements.subList(3, elements.size());
return new Constructor(getArg(elements.get(1)).toString(), makeExpressions(allArgs), elements);
} else {
return new Constructor(getArg(elements.get(1)).toString(), elements);
}
} else if (elements.size()>3 && elements.get(1) instanceof Token && getToken(elements.get(1)).equals(".")) {
List allArgsIncludingMarkers = elements.subList(3, elements.size());
if (allArgsIncludingMarkers.size()==1 && allArgsIncludingMarkers.get(0).equals(new Token("()"))) {
// method call with no arguments
return new MethodCall(((Pair)elements.get(0)).getArg(), (String) ((Pair)elements.get(2)).getArg(), new LinkedList<>(), elements);
} else if (allArgsIncludingMarkers.size()==3
&& allArgsIncludingMarkers.get(0).equals(new Token("("))
&& allArgsIncludingMarkers.get(2).equals(new Token(")"))) {
// method call with one argument
Object arg=getArg( allArgsIncludingMarkers.get(1));
String format=getFormat( allArgsIncludingMarkers.get(1));
if (arg instanceof List) {
List ll = (List) arg;
assert ll.size() == 1;
List _elements = ll.get(0).getElements();
List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy