Search in sources :

Example 1 with ArithmeticPartContext

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;
}
Also used : NotExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.NotExpressionContext) ExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionContext) LetExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.LetExpressionContext) BuildMilestoningVariableExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.BuildMilestoningVariableExpressionContext) PropertyExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyExpressionContext) DefaultValueExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.DefaultValueExpressionContext) FunctionVariableExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.FunctionVariableExpressionContext) CombinedExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.CombinedExpressionContext) AtomicExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.AtomicExpressionContext) SignedExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.SignedExpressionContext) PropertyOrFunctionExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyOrFunctionExpressionContext) SimpleFunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) MutableList(org.eclipse.collections.api.list.MutableList) FastList(org.eclipse.collections.impl.list.mutable.FastList) List(java.util.List) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Example 2 with ArithmeticPartContext

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;
}
Also used : ArithmeticPartContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ArithmeticPartContext) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) ExpressionPartContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionPartContext) BooleanPartContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.BooleanPartContext)

Example 3 with ArithmeticPartContext

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;
}
Also used : NotExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.NotExpressionContext) ExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionContext) LetExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.LetExpressionContext) BuildMilestoningVariableExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.BuildMilestoningVariableExpressionContext) PropertyExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyExpressionContext) DefaultValueExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.DefaultValueExpressionContext) FunctionVariableExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.FunctionVariableExpressionContext) CombinedExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.CombinedExpressionContext) AtomicExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.AtomicExpressionContext) SignedExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.SignedExpressionContext) PropertyOrFunctionExpressionContext(org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyOrFunctionExpressionContext) SimpleFunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)

Example 4 with ArithmeticPartContext

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);
}
Also used : SimpleFunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)

Example 5 with ArithmeticPartContext

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;
}
Also used : SimpleFunctionExpression(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression) PrimitiveCoreInstance(org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance) CoreInstance(org.finos.legend.pure.m4.coreinstance.CoreInstance) ValueSpecification(org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Aggregations

CoreInstance (org.finos.legend.pure.m4.coreinstance.CoreInstance)5 PrimitiveCoreInstance (org.finos.legend.pure.m4.coreinstance.primitive.PrimitiveCoreInstance)5 SimpleFunctionExpression (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.SimpleFunctionExpression)4 ValueSpecification (org.finos.legend.pure.m3.coreinstance.meta.pure.metamodel.valuespecification.ValueSpecification)4 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2 AtomicExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.AtomicExpressionContext)2 BuildMilestoningVariableExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.BuildMilestoningVariableExpressionContext)2 CombinedExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.CombinedExpressionContext)2 DefaultValueExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.DefaultValueExpressionContext)2 ExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ExpressionContext)2 FunctionVariableExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.FunctionVariableExpressionContext)2 LetExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.LetExpressionContext)2 NotExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.NotExpressionContext)2 PropertyExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyExpressionContext)2 PropertyOrFunctionExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.PropertyOrFunctionExpressionContext)2 SignedExpressionContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.SignedExpressionContext)2 List (java.util.List)1 MutableList (org.eclipse.collections.api.list.MutableList)1 FastList (org.eclipse.collections.impl.list.mutable.FastList)1 ArithmeticPartContext (org.finos.legend.pure.m3.serialization.grammar.m3parser.antlr.M3Parser.ArithmeticPartContext)1