use of com.dexels.navajo.parser.NamedExpression in project navajo by Dexels.
the class ASTReactiveScriptNode method interpretToLambda.
@Override
public ContextExpression interpretToLambda(List<String> problems, String originalExpression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
int start = hasHeader ? headers : 0;
if (hasHeader) {
for (int i = 0; i < headers; i++) {
ASTKeyValueNode hdr = (ASTKeyValueNode) jjtGetChild(i);
NamedExpression ne = (NamedExpression) hdr.interpretToLambda(problems, originalExpression, functionClassifier, mapResolver);
String key = ne.name;
headerMap.put(key, ne.apply());
}
}
int count = jjtGetNumChildren();
List<Node> unnamedPipes = new ArrayList<>();
Map<String, Node> namedPipes = new HashMap<>();
for (int i = start; i < count; i++) {
Node child = jjtGetChild(i);
// ASTReactivePipe pipe = null;
if (child instanceof ASTPipeDefinition) {
unnamedPipes.add((ASTPipeDefinition) child);
} else if (child instanceof ASTKeyValueNode) {
ASTKeyValueNode kvNode = (ASTKeyValueNode) child;
String streamName = kvNode.val;
Node namedPipe = kvNode.jjtGetChild(0);
// assert value types perhaps? TODO
namedPipes.put(streamName, namedPipe);
}
}
List<ReactivePipeNode> pipes = unnamedPipes.stream().map(p -> (ReactivePipeNode) p.interpretToLambda(problems, originalExpression, functionClassifier, name -> {
Optional<Node> initial = Optional.ofNullable(namedPipes.get(name));
if (initial.isPresent()) {
return initial;
} else {
return mapResolver.apply(name);
}
})).collect(Collectors.toList());
return new ContextExpression() {
@Override
public Optional<String> returnType() {
return Optional.of(Reactive.ReactiveItemType.REACTIVE_SCRIPT.toString());
}
@Override
public boolean isLiteral() {
return true;
}
@Override
public String expression() {
return "";
}
@Override
public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
return Operand.ofCustom(pipes, Reactive.ReactiveItemType.REACTIVE_SCRIPT.toString());
}
};
}
use of com.dexels.navajo.parser.NamedExpression in project navajo by Dexels.
the class ASTFunctionNode method interpretToLambda.
@Override
public ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
List<ContextExpression> unnamed = new LinkedList<>();
// TODO make lazy?
Map<String, ContextExpression> named = new HashMap<>();
for (int i = 0; i < jjtGetNumChildren(); i++) {
Node sn = jjtGetChild(i);
ContextExpression cn = sn.interpretToLambda(problems, expression, functionClassifier, mapResolver);
if (cn instanceof NamedExpression) {
NamedExpression ne = (NamedExpression) cn;
named.put(ne.name, ne.expression);
} else {
unnamed.add(cn);
}
}
FunctionClassification mode = functionClassifier.apply(functionName);
switch(mode) {
case REACTIVE_HEADER:
break;
case REACTIVE_SOURCE:
return new ReactiveParseItem(functionName, Reactive.ReactiveItemType.REACTIVE_SOURCE, named, unnamed, expression, this);
case REACTIVE_TRANSFORMER:
return new ReactiveParseItem(functionName, Reactive.ReactiveItemType.REACTIVE_TRANSFORMER, named, unnamed, expression, this);
case REACTIVE_REDUCER:
return new ReactiveParseItem(functionName, Reactive.ReactiveItemType.REACTIVE_MAPPER, named, unnamed, expression, this);
case DEFAULT:
default:
}
return resolveNormalFunction(unnamed, named, problems, expression);
}
use of com.dexels.navajo.parser.NamedExpression in project navajo by Dexels.
the class ASTKeyValueNode method interpretToLambda.
@Override
public ContextExpression interpretToLambda(List<String> problems, String originalExpression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
int num = jjtGetNumChildren();
if (num != 1) {
problems.add("Incorrect # of params in named parameter");
}
ContextExpression exp = jjtGetChild(0).interpretToLambda(problems, originalExpression, functionClassifier, mapResolver);
return new NamedExpression(val, exp);
}
use of com.dexels.navajo.parser.NamedExpression in project navajo by Dexels.
the class TestCompiledExpression method testNamedExpression.
@Test
public void testNamedExpression() throws ParseException {
String expression = "aap=1+1";
StringReader sr = new StringReader(expression);
CompiledParser cp = new CompiledParser(sr);
cp.KeyValue();
ASTKeyValueNode atn = (ASTKeyValueNode) cp.getJJTree().rootNode();
List<String> problems = new ArrayList<>();
NamedExpression ne = (NamedExpression) atn.interpretToLambda(problems, expression, fn -> FunctionClassification.DEFAULT, name -> Optional.empty());
Assert.assertEquals(0, problems.size());
Assert.assertEquals("aap", ne.name);
Assert.assertEquals(2, ne.apply().value);
}
Aggregations