Examples of SwitchStatement


Examples of org.openquark.cal.internal.javamodel.JavaStatement.SwitchStatement

        // Extract the alternatives
        Expression.Switch.SwitchAlt[] alts = eswitch.getAlts();

        // Generate the switch conditional.
        SwitchStatement switchStatement;

        // Generate code to get the int value that we are switching on.
        ExpressionContextPair ecp = generateUnboxedArgument(JavaTypeName.INT, eswitch.getSwitchExpr(), variableContext);
        switchBlock.addStatement(ecp.getContextBlock());
        JavaExpression conditionExpression = ecp.getJavaExpression();

        switchStatement = new SwitchStatement(conditionExpression);
        switchBlock.addStatement(switchStatement);

        // Populate the switch statement with case statement groups.
        for (final SwitchAlt alt : alts) {
            // Create a new child variable scope to handle the alternate and any let variables it contains.
            variableContext.pushJavaScope();

            Block caseBlock = new Block();

            // If we are switching on an integer we should never have alt variables.
            if (alt.hasVars()) {
                throw new CodeGenerationException ("Alt vars encountered in integer switch in " + getFunctionName() + ".");
            }

            JavaStatement altStatement = genS_R(alt.getAltExpr(), variableContext);
            caseBlock.addStatement(variableContext.popJavaScope());
            caseBlock.addStatement(altStatement);

            if (alt.isDefaultAlt()) {
                switchStatement.addCase(new SwitchStatement.DefaultCase(caseBlock));

            } else {
                List<Object> altTags = alt.getAltTags();
                int[] caseLabels = new int[altTags.size()];

                int index = 0;
                for (final Object tag : altTags) {
                    if (!(tag instanceof Integer)) {
                        throw new CodeGenerationException ("Unknown tag type in case statement in " + getFunctionName() + ":" + tag.getClass().getName() + ".");
                    }

                    caseLabels[index] = ((Integer)tag).intValue();
                    index++;
                }

                switchStatement.addCase(new SwitchStatement.IntCaseGroup(caseLabels, caseBlock));
            }
        }

        // Add case alternate for default case if missing.
        if (switchStatement.getDefaultStatement() == null) {
            //this will mostly be a user error e.g.
            //(\x -> case x of 1 -> "one";) (2 :: Int)
            //However, because cases on ints are also used by internal dictionary functions, and a few other situations
            //it could happen that this occurs because of an internal error.
            //todoBI encode enough information into Expression.Switch so that we know which case we're in.
            //todoBI pass the unhandled int value that occurred at runtime i.e. 2 in the above example, to the error message
            JavaStatement defaultCase = generateReturn(getUnhandledSwitchIndexForIntPatternCall(eswitch.getErrorInfo()), variableContext);
            switchStatement.addCase (new SwitchStatement.DefaultCase(defaultCase));
        }

        return switchBlock;
    }
View Full Code Here

Examples of org.openquark.cal.internal.javamodel.JavaStatement.SwitchStatement

        // Extract the alternatives
        Expression.Switch.SwitchAlt[] alts = eswitch.getAlts();

        // Generate the switch conditional.
        SwitchStatement switchStatement;

        // Generate code to get the char value that we are switching on.
        ExpressionContextPair ecp = generateUnboxedArgument(JavaTypeName.CHAR, eswitch.getSwitchExpr(), variableContext);
        switchBlock.addStatement(ecp.getContextBlock());
        JavaExpression conditionExpression = ecp.getJavaExpression();

        switchStatement = new SwitchStatement(conditionExpression);
        switchBlock.addStatement(switchStatement);

        // Populate the switch statement with case statement groups.
        for (final SwitchAlt alt : alts) {
            // Create a new child variable scope to handle the alternate and any let variables it contains.
            variableContext.pushJavaScope();

            Block caseBlock = new Block();

            // If we are switching on an character we should never have alt variables.
            if (alt.hasVars()) {
                throw new CodeGenerationException ("Alt vars encountered in character switch in " + getFunctionName() + ".");
            }

            JavaStatement altStatement = genS_R(alt.getAltExpr(), variableContext);
            caseBlock.addStatement(variableContext.popJavaScope());
            caseBlock.addStatement(altStatement);

            if (alt.isDefaultAlt()) {
                switchStatement.addCase(new SwitchStatement.DefaultCase(caseBlock));

            } else {
                List<Object> altTags = alt.getAltTags();
                int[] caseLabels = new int[altTags.size()];

                int index = 0;
                for (final Object tag : altTags) {
                    if (!(tag instanceof Character)) {
                        throw new CodeGenerationException ("Unknown tag type in case statement in " + getFunctionName() + ":" + tag.getClass().getName() + ".");
                    }

                    caseLabels[index] = ((Character)tag).charValue();
                    index++;
                }

                switchStatement.addCase(new SwitchStatement.IntCaseGroup(caseLabels, caseBlock));
            }
        }

        // Add case alternate for default case if missing.
        if (switchStatement.getDefaultStatement() == null) {
            //this will mostly be a user error e.g.
            //(\x -> case x of 'a' -> "char a";) 'b'
            //todoBI pass the unhandled char value that occurred at runtime i.e. 'b' in the above example, to the error message

            JavaStatement defaultCase = generateReturn(getUnhandledSwitchIndexForCharPatternCall(eswitch.getErrorInfo()), variableContext);

            switchStatement.addCase (new SwitchStatement.DefaultCase(defaultCase));
        }

        return switchBlock;
    }
View Full Code Here

Examples of org.openquark.cal.internal.javamodel.JavaStatement.SwitchStatement

                if (!missing) {
                    variablesOfInterest.put(varName, variablesOfInterestForDefault.get(varName));
                }
            }

            SwitchStatement newSwitch = new SwitchStatement (
                    (JavaExpression)switchStatement.getCondition().accept(this, arg));


            for (int i = 0, n = newCases.size(); i < n; ++i) {
                newSwitch.addCase(newCases.get(i));
            }

            if (newDefault != null) {
                newSwitch.addCase(newDefault);
            }

            return newSwitch;
        }
View Full Code Here

Examples of org.openquark.cal.internal.javamodel.JavaStatement.SwitchStatement

                               "dcOrdinal",
                               JavaTypeName.INT,
                               false, "getDCNameByOrdinal");
            javaClassRep.addMethod(javaMethod);

            SwitchStatement sw =
                new SwitchStatement(new MethodVariable("dcOrdinal"));

            for (int i = 0, n = dataConsList.size(); i < n; ++i) {
                DataConstructor dc = dataConsList.get(i);
                SwitchStatement.IntCaseGroup icg =
                    new SwitchStatement.IntCaseGroup(
                            dc.getOrdinal(),
                            new ReturnStatement(LiteralWrapper.make(dc.getName().getUnqualifiedName())));
                sw.addCase(icg);
            }

            javaMethod.addStatement(sw);

            // If the argument doesn't match the ordinal for any DC we
View Full Code Here

Examples of org.openquark.cal.internal.javamodel.JavaStatement.SwitchStatement

            // Add the body..
            // switch (ordinal) {
            //     case 1: ...;
            // }
            SwitchStatement ordSwitch = new SwitchStatement(METHODVAR_ORDINAL);
            for (final DataConstructor dc : dataConsList) {
                if (dc.getArity() == 0) {
                    // Return the static TagDC instance for this ordinal.
                    String fieldName = CALToJavaNames.fixupVarName(dc.getName().getUnqualifiedName());
                    JavaField field = new JavaField.Static(className, fieldName, tagDCTypeName);
                    SwitchStatement.SwitchCase sc = new SwitchStatement.IntCaseGroup(dc.getOrdinal(), new ReturnStatement(field));
                    ordSwitch.addCase(sc);
                } else {
                    // This is a valid ordinal for the data type but does not correspond to a zero arity DC.
                    LiteralWrapper badValueMessageWrapper = LiteralWrapper.make ("Attempt to treat " + dc.getName() + " as a zero arity data constructor.");
                    Block block = new Block();
                    block.addStatement(new JavaStatement.LineComment(dc.getName().getQualifiedName()));
                    JavaExpression castExpression = new CastExpression(tagDCTypeName, new MethodInvocation.Static(JavaTypeNames.RTVALUE, "badValue", badValueMessageWrapper, JavaTypeName.STRING, JavaTypeNames.RTVALUE));
                    block.addStatement(new ReturnStatement(castExpression));
                    ordSwitch.addCase(new SwitchStatement.IntCaseGroup (dc.getOrdinal(), block));
                }
            }

            // Add a default case in the switch to throw an error if an invalid ordinal value is used.
            Block defaultBlock = new Block();
            LocalVariable bf = new LocalVariable("bf", JavaTypeName.STRING_BUILDER);
            defaultBlock.addStatement(new LocalVariableDeclaration (bf, new ClassInstanceCreationExpression(JavaTypeName.STRING_BUILDER)));
            LiteralWrapper badValueMessageWrapper1 = LiteralWrapper.make("Invalid ordinal value of ");
            JavaExpression message = new MethodInvocation.Instance(bf, "append", badValueMessageWrapper1, JavaTypeName.STRING, JavaTypeName.STRING_BUILDER, MethodInvocation.InvocationType.VIRTUAL);
            message = new  MethodInvocation.Instance(message, "append", METHODVAR_ORDINAL, JavaTypeName.INT, JavaTypeName.STRING_BUILDER, MethodInvocation.InvocationType.VIRTUAL);
            LiteralWrapper badValueMessageWrapper2 = LiteralWrapper.make(" in " + className.toString() + ".getTagDC().");
            message = new MethodInvocation.Instance(message, "append", badValueMessageWrapper2, JavaTypeName.STRING, JavaTypeName.STRING_BUILDER, MethodInvocation.InvocationType.VIRTUAL);
            defaultBlock.addStatement (new ExpressionStatement(message));
            message = new MethodInvocation.Instance(bf, "toString", JavaTypeName.STRING, MethodInvocation.InvocationType.VIRTUAL);
            defaultBlock.addStatement (new ReturnStatement(new CastExpression(tagDCTypeName, new MethodInvocation.Static(JavaTypeNames.RTVALUE, "badValue", message, JavaTypeName.STRING, JavaTypeNames.RTVALUE))));
            ordSwitch.addCase(new SwitchStatement.DefaultCase (defaultBlock));

            // Add the switch statement to the method.
            javaMethod.addStatement(ordSwitch);
        }
View Full Code Here

Examples of org.openquark.cal.internal.javamodel.JavaStatement.SwitchStatement

            // public final String getUnqualifiedName() ...
            modifiers = Modifier.PUBLIC | Modifier.FINAL;
            returnType = JavaTypeName.STRING;
            javaMethod = new JavaMethod(modifiers, returnType, "getUnqualifiedName");
            tagDCClassRep.addMethod(javaMethod);
            SwitchStatement sw = new SwitchStatement (new JavaField.Instance(null, "tag", JavaTypeName.INT));
            for (int i = 0, nDCs = dataConsList.size(); i < nDCs; ++i) {
                DataConstructor dc = dataConsList.get (i);
                sw.addCase(new SwitchStatement.IntCaseGroup(dc.getOrdinal(), new ReturnStatement (LiteralWrapper.make(dc.getName().getUnqualifiedName()))));
            }
            javaMethod.addStatement (sw);
            javaMethod.addStatement(new ReturnStatement(LiteralWrapper.make ("Unknown data constructor")));

            // public final String getQualfiedName() ...
            modifiers = Modifier.PUBLIC | Modifier.FINAL;
            returnType = JavaTypeName.STRING;
            javaMethod = new JavaMethod(modifiers, returnType, "getQualifiedName");
            tagDCClassRep.addMethod(javaMethod);
            sw = new SwitchStatement (new JavaField.Instance(null, "tag", JavaTypeName.INT));
            for (int i = 0, nDCs = dataConsList.size(); i < nDCs; ++i) {
                DataConstructor dc = dataConsList.get (i);
                sw.addCase(new SwitchStatement.IntCaseGroup(dc.getOrdinal(), new ReturnStatement (LiteralWrapper.make(dc.getName().getQualifiedName()))));
            }
            javaMethod.addStatement (sw);
            javaMethod.addStatement(new ReturnStatement(LiteralWrapper.make ("Unknown data constructor")));

            return tagDCClassRep;
View Full Code Here

Examples of org.openquark.cal.internal.javamodel.JavaStatement.SwitchStatement

                                JavaTypeName.STRING,
                                "getUnqualifiedName");
            javaClassRep.addMethod(jm);

            if (functions.getNFunctions() > 1) {
                SwitchStatement switchStatement =
                    new SwitchStatement(new JavaField.Instance(null, SCDefinitionBuilder.functionTagFieldName, JavaTypeName.INT));

                for (final MachineFunction mf : functions.getTopLevelCALFunctions()) {
                    switchStatement.addCase(
                            new SwitchStatement.IntCaseGroup(
                                    functions.getFunctionIndex(mf.getName()),
                                    new ReturnStatement(LiteralWrapper.make(mf.getName()))));
                }
                jm.addStatement(switchStatement);
View Full Code Here

Examples of org.openquark.cal.internal.javamodel.JavaStatement.SwitchStatement

        private final void createMethod_getQualifiedName () {
            JavaMethod jm = new JavaMethod (Modifier.PUBLIC | Modifier.FINAL, JavaTypeName.STRING, "getQualifiedName");
            javaClassRep.addMethod(jm);

            if (functions.getNFunctions() > 1) {
                SwitchStatement switchStatement =
                    new SwitchStatement(new JavaField.Instance(null, SCDefinitionBuilder.functionTagFieldName, JavaTypeName.INT));

                for (final MachineFunction mf : functions.getTopLevelCALFunctions()) {
                    switchStatement.addCase(
                            new SwitchStatement.IntCaseGroup(
                                    functions.getFunctionIndex(mf.getName()),
                                    new ReturnStatement(LiteralWrapper.make(mf.getQualifiedName().getQualifiedName()))));
                }
View Full Code Here

Examples of org.openquark.cal.internal.javamodel.JavaStatement.SwitchStatement

            // Add the throws declaration
            javaMethod.addThrows(JavaTypeName.CAL_EXECUTOR_EXCEPTION);

            // At this point we want to switch based on the scTag and dispatch to the appropriate
            // sc specific f method.
            SwitchStatement switchStatement =
                new SwitchStatement(new JavaField.Instance(null, SCDefinitionBuilder.functionTagFieldName, JavaTypeName.INT));

            for (int i = 0, n = javaDefns.size(); i < n; ++i) {
                SCJavaDefn javaDefn = javaDefns.get(i);
                if (javaDefn.getArity() != arity) {
                    continue;
                }

                if (functions.getMachineFunction(javaDefn.getFunctionName()) instanceof LECCLiftedLetVarMachineFunction) {
                    continue;
                }

                if (strict &&
                    javaDefn.hasStrictUnboxableArguments()) {
                    continue;
                }

                methodName = functions.getFNamePrefix(javaDefn.getFunctionName()) + "f" + arity + (strict ? "S" : "L");
                MethodInvocation mi =
                    new MethodInvocation.Instance(
                            null,
                            methodName,
                            argValues,
                            argTypes,
                            JavaTypeNames.RTVALUE,
                            MethodInvocation.InvocationType.VIRTUAL);

                switchStatement.addCase(
                    new SwitchStatement.IntCaseGroup(functions.getFunctionIndex(javaDefn.getFunctionName()), new ReturnStatement(mi)));
            }

            javaMethod.addStatement(switchStatement);
View Full Code Here

Examples of org.openquark.cal.internal.javamodel.JavaStatement.SwitchStatement

            // Add the throws declaration
            javaMethod.addThrows(JavaTypeName.CAL_EXECUTOR_EXCEPTION);

            // At this point we want to switch based on the scTag and dispatch to the appropriate
            // sc specific f method.
            SwitchStatement switchStatement =
                new SwitchStatement(new JavaField.Instance(null, SCDefinitionBuilder.functionTagFieldName, JavaTypeName.INT));

            for (final MachineFunction mf : functions.getTopLevelCALFunctions()) {
                methodName = functions.getFNamePrefix(mf.getName()) + "f";
                MethodInvocation mi =
                    new MethodInvocation.Instance(
                            null,
                            methodName,
                            new JavaExpression[]{new MethodVariable(ROOT_NODE), SCJavaDefn.EXECUTION_CONTEXT_VAR},
                            argTypes,
                            JavaTypeNames.RTVALUE,
                            MethodInvocation.InvocationType.VIRTUAL);

                switchStatement.addCase(
                    new SwitchStatement.IntCaseGroup(
                            functions.getFunctionIndex(mf.getName()),
                            new ReturnStatement(mi)));
            }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.