use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ArithmeticPartContext 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.ArithmeticPartContext in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method combinedExpression.
public CoreInstance combinedExpression(CombinedExpressionContext ctx, String exprName, MutableList<String> typeParametersNames, LambdaContext lambdaContext, String space, boolean wrapFlag, ImportGroup importId, boolean addLines) {
CoreInstance result = this.expressionOrExpressionGroup(ctx.expressionOrExpressionGroup(), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
CoreInstance boolResult = result;
CoreInstance arithResult = result;
if (ctx.expressionPart() != null) {
MutableList<ArithmeticPartContext> arth = Lists.mutable.empty();
MutableList<BooleanPartContext> bool = Lists.mutable.empty();
// Invariant: arth.isEmpty() || bool.isEmpty ie they can't both contains elements at the same time
for (ExpressionPartContext epCtx : ctx.expressionPart()) {
if (epCtx.arithmeticPart() != null) {
if (!bool.isEmpty()) {
boolResult = this.booleanPart(bool, (ValueSpecification) arithResult, exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
bool.clear();
}
arth.add(epCtx.arithmeticPart());
} else if (epCtx.booleanPart() != null) {
if (!arth.isEmpty()) {
arithResult = this.arithmeticPart(arth, boolResult, exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
arth.clear();
}
bool.add(epCtx.booleanPart());
}
}
// Invariant allows us to make the choice here - either we still have arth to process or bool to process but not both
if (!arth.isEmpty()) {
result = this.arithmeticPart(arth, boolResult, exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
} else if (!bool.isEmpty()) {
result = this.booleanPart(bool, (ValueSpecification) arithResult, exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
}
}
return result;
}
use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ArithmeticPartContext 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.ArithmeticPartContext in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method processOp.
// Antlr grammar as currently defined does not handle precedence for arithmetic ops
// We take care of precedence here.
// Intuition: if we are processing an expression and the previous expression is of lower precedence, we 'snatch' the last argument from the previous expression and make it part of the current one
// For example: 1 + 2 * 4. The grammar will have led us to build plus(1,2). When looking at the multiplication, the expression should snatch 2, and replace it with mult(2,4),
// so we end up with plus(1, mult(2,4))
private SimpleFunctionExpression processOp(BuildArithmeticExpression builder, SimpleFunctionExpression sfe, ArithmeticPartContext ctx, ArithOp op, CoreInstance initialValue, String exprName, MutableList<String> typeParametersNames, LambdaContext lambdaContext, String space, boolean wrapFlag, ImportGroup importId, boolean addLines) {
String opStr = op.name().toLowerCase();
// Case where we are building from scratch
if (sfe == null) {
return builder.build(ctx, op, initialValue, exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
}
// has the last param as it's initial parameter).
if (isStrictlyLowerPrecendence(sfe.getValueForMetaPropertyToOne("functionName").getName(), opStr)) {
ListIterable<? extends CoreInstance> params = getParams(sfe);
SimpleFunctionExpression newSfe = builder.build(ctx, op, params.getLast(), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
MutableList<CoreInstance> l = Lists.mutable.withAll(params.subList(0, params.size() - 1));
// division and relational ops handle parameter values differently.
if ("divide".equals(sfe._functionName()) || isRelationalComparison(sfe._functionName())) {
sfe._parametersValues(Lists.mutable.of((ValueSpecification) l.get(0), newSfe));
} else {
sfe._parametersValues(Lists.mutable.<ValueSpecification>of(this.doWrap(l.with(newSfe))));
}
return sfe;
}
// Add the previously processed expression as the initial argument to this expression
return builder.build(ctx, op, sfe, exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
}
use of org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ArithmeticPartContext in project legend-pure by finos.
the class AntlrContextToM3CoreInstance method buildComparisonOp.
private SimpleFunctionExpression buildComparisonOp(ArithmeticPartContext ctx, ArithOp op, CoreInstance initialValue, String exprName, MutableList<String> typeParametersNames, LambdaContext lambdaContext, String space, boolean wrapFlag, ImportGroup importId, boolean addLines) {
String op_str;
Function0<TerminalNode> getToken;
switch(op) {
case LESSTHAN:
getToken = ctx::LESSTHAN;
op_str = "lessThan";
break;
case LESSTHANEQUAL:
getToken = ctx::LESSTHANEQUAL;
op_str = "lessThanEqual";
break;
case GREATERTHAN:
getToken = ctx::GREATERTHAN;
op_str = "greaterThan";
break;
case GREATERTHANEQUAL:
getToken = ctx::GREATERTHANEQUAL;
op_str = "greaterThanEqual";
break;
default:
throw new IllegalStateException("Unexpected arithmetic operation for buildComparisonOp: " + op);
}
CoreInstance other = this.expression(ctx.expression(0), exprName, typeParametersNames, lambdaContext, space, wrapFlag, importId, addLines);
SimpleFunctionExpression sfe = SimpleFunctionExpressionInstance.createPersistent(this.repository, this.sourceInformation.getPureSourceInformation(getToken.value().getSymbol()), null, null, importId, null);
sfe._functionName(op_str);
sfe._parametersValues(Lists.mutable.of((ValueSpecification) initialValue, (ValueSpecification) other));
return sfe;
}
Aggregations