use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionContext in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method buildArithmeticWithListParam.
// Build arithmetic op which can handle list of params (like lisp eg (+ u v z y z))
private SimpleFunctionExpression buildArithmeticWithListParam(ArithmeticPartContext ctx, ArithOp op, CoreInstance initialValue, String exprName, MutableList<String> typeParametersNames, LambdaContext lambdaContext, String space, boolean wrapFlag, ImportGroup importId, boolean addLines) {
MutableList<CoreInstance> others = Lists.mutable.empty();
Function0<List<TerminalNode>> getTokens;
Function<Integer, TerminalNode> getToken;
switch(op) {
case PLUS:
getTokens = ctx::PLUS;
getToken = ctx::PLUS;
break;
case TIMES:
getTokens = ctx::STAR;
getToken = ctx::STAR;
break;
case MINUS:
getTokens = ctx::MINUS;
getToken = ctx::MINUS;
break;
default:
throw new IllegalStateException("Unexpected arithmetic operation for buildArithmeticWithListParam: " + op);
}
String op_str = op.toString().toLowerCase();
for (ExpressionContext eCtx : ctx.expression()) {
others.add(this.expression(eCtx, exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines));
}
TerminalNode newOp = getToken.apply(getTokens.value().size() - 1);
SimpleFunctionExpression sfe = SimpleFunctionExpressionInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(newOp.getSymbol()), null, null, importId, null);
sfe._functionName(op_str);
sfe._parametersValues(Lists.mutable.<ValueSpecification>of(this.doWrap(Lists.mutable.with(initialValue).withAll(others))));
return sfe;
}
use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionContext in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method buildArithmeticDivide.
// Handles divide, since dive is built up in a tree: eg x / y /z is div(x, div(y,z))
private SimpleFunctionExpression buildArithmeticDivide(ArithmeticPartContext ctx, ArithOp op, CoreInstance initialValue, String exprName, MutableList<String> typeParametersNames, LambdaContext lambdaContext, String space, boolean wrapFlag, ImportGroup importId, boolean addLines) {
MutableList<CoreInstance> others = Lists.mutable.empty();
for (ExpressionContext eCtx : ctx.expression()) {
others.add(this.expression(eCtx, exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines));
}
SimpleFunctionExpression sfe = null;
for (CoreInstance other : others) {
sfe = SimpleFunctionExpressionInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.DIVIDE(ctx.DIVIDE().size() - 1).getSymbol()), null, null, importId, null);
sfe._functionName("divide");
sfe._parametersValues(Lists.mutable.of((ValueSpecification) initialValue, (ValueSpecification) other));
initialValue = sfe;
}
return sfe;
}
use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionContext in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method expression.
private CoreInstance expression(ExpressionContext ctx, String exprName, MutableList<String> typeParametersNames, LambdaContext lambdaContext, String space, boolean wrapFlag, ImportGroup importId, boolean addLines) {
CoreInstance result = null;
CoreInstance end = null;
CoreInstance step = null;
MutableList<CoreInstance> expressions = Lists.mutable.of();
MutableList<ValueSpecification> parameters = Lists.mutable.empty();
if (ctx.combinedExpression() != null) {
return this.combinedExpression(ctx.combinedExpression(), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
}
if (ctx.atomicExpression() != null) {
result = this.atomicExpression(ctx.atomicExpression(), typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
} else if (ctx.notExpression() != null) {
result = this.notExpression(ctx.notExpression(), exprName, typeParametersNames, lambdaContext, space, importId, addLines);
} else if (ctx.signedExpression() != null) {
result = this.signedExpression(ctx.signedExpression(), exprName, typeParametersNames, lambdaContext, space, importId, addLines);
} else if (ctx.expressionsArray() != null) {
for (ExpressionContext eCtx : ctx.expressionsArray().expression()) {
expressions.add(this.expression(eCtx, exprName, typeParametersNames, lambdaContext, space, false, importId, addLines));
}
result = this.doWrap(expressions, ctx.expressionsArray().getStart().getLine(), ctx.expressionsArray().getStart().getCharPositionInLine(), ctx.getStop().getLine(), ctx.getStop().getCharPositionInLine());
} else {
switch(ctx.sliceExpression().expression().size()) {
case // :end
1:
{
end = this.expression(ctx.sliceExpression().expression(0), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
break;
}
case // start:end
2:
{
result = this.expression(ctx.sliceExpression().expression(0), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
expressions.add(result);
end = this.expression(ctx.sliceExpression().expression(1), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
break;
}
case // start:end:step
3:
{
result = this.expression(ctx.sliceExpression().expression(0), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
expressions.add(result);
end = this.expression(ctx.sliceExpression().expression(1), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
step = this.expression(ctx.sliceExpression().expression(2), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
break;
}
default:
{
// Not reachable. coded just for comment
break;
}
}
MutableList<ValueSpecification> params = Lists.mutable.empty();
if (result != null) {
params.add(this.doWrap(Lists.mutable.of(result)));
}
params.add(this.doWrap(Lists.mutable.of(end)));
if (step != null) {
params.add(this.doWrap(Lists.mutable.of(step)));
}
SimpleFunctionExpressionInstance sfe = SimpleFunctionExpressionInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(ctx.getStart()), null, null, importId, null);
sfe._functionName("range");
sfe._parametersValues(params);
result = sfe;
}
if (ctx.propertyOrFunctionExpression() != null) {
for (PropertyOrFunctionExpressionContext pfCtx : ctx.propertyOrFunctionExpression()) {
if (pfCtx.propertyExpression() != null) {
result = propertyExpression(pfCtx.propertyExpression(), result, parameters, typeParametersNames, lambdaContext, space, importId);
} else {
for (int i = 0; i < pfCtx.functionExpression().qualifiedName().size(); i++) {
parameters = this.functionExpressionParameters(pfCtx.functionExpression().functionExpressionParameters(i), typeParametersNames, importId, lambdaContext, addLines, spacePlusTabs(space, 4));
parameters.add(0, (ValueSpecification) result);
result = this.functionExpression(pfCtx.functionExpression().qualifiedName(i), parameters, importId);
}
}
}
}
if (ctx.equalNotEqual() != null) {
result = this.equalNotEqual(ctx.equalNotEqual(), (ValueSpecification) result, exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
}
return result;
}
Aggregations