Package org.openquark.cal.internal.javamodel

Examples of org.openquark.cal.internal.javamodel.JavaStatement$ThrowStatement


            // 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


            // 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

        // Then
        Block thenBlock;
        if (trueAlt == null) {
            LiteralWrapper badValueMessageWrapper = LiteralWrapper.make("Illegal fallthrough to default case.");
            JavaExpression returnValueExpression = getBadValueCall(eSwitch.getErrorInfo(), badValueMessageWrapper);
            JavaStatement badValueReturnStatement = generateReturn(returnValueExpression, variableContext);
            thenBlock = new Block();
            thenBlock.addStatement (badValueReturnStatement);
        } else {
            variableContext.pushJavaScope();
            JavaStatement thenPart = genS_R (trueAlt, variableContext);

            thenBlock = variableContext.popJavaScope();
            thenBlock.addStatement(thenPart);
        }

        // Else
        Block elseBlock;
        if (falseAlt == null) {
            LiteralWrapper badValueMessageWrapper = LiteralWrapper.make("Illegal fallthrough to default case in function.");
            JavaStatement badValueReturnStatement = generateReturn(getBadValueCall(eSwitch.getErrorInfo(), badValueMessageWrapper), variableContext);
            elseBlock = new Block();
            elseBlock.addStatement (badValueReturnStatement);
        } else {
            variableContext.pushJavaScope();
            JavaStatement elsePart = genS_R (falseAlt, variableContext);

            elseBlock = variableContext.popJavaScope();
            elseBlock.addStatement(elsePart);
        }

        JavaStatement ite = new IfThenElseStatement(pair.getJavaExpression(), thenBlock, elseBlock);

        Block contextBlock = pair.getContextBlock();
        contextBlock.addStatement(ite);
        return contextBlock;
    }
View Full Code Here

                    }
                }
            }
        }

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

        return switchBlock;
View Full Code Here

        // For all but the last expression we want to generate a Java statement
        // which will evaluate to WHNF.
        for (int i = 0, n = expressions.length - 1; i < n; ++i) {
            Expression e = expressions[i];
            JavaStatement js = makeStrictStatementFromSeqArg(e, variableContext);
            block.addStatement(js);
        }

        // For the last expression we simply generate a return expression using the
        // usual top level compilation scheme.
View Full Code Here

                Void arg) {

            // Treat each branch independently with regards to the variables to be realeased.
            Map<String, JavaTypeName> variablesOfInterestForThen = new HashMap<String, JavaTypeName>(variablesOfInterest);
            VarReleaser vr = new VarReleaser(variablesOfInterestForThen);
            JavaStatement newThen = (JavaStatement)ifThenElse.getThenStatement().accept(vr, null);

            JavaStatement newElse = null;
            Map<String, JavaTypeName> variablesOfInterestForElse = new HashMap<String, JavaTypeName>(variablesOfInterest);
            if (ifThenElse.getElseStatement() != null) {
                vr = new VarReleaser(variablesOfInterestForElse);
                newElse = (JavaStatement)ifThenElse.getElseStatement().accept(vr, null);
            }
View Full Code Here

                                        "get",
                                        SCJavaDefn.EXECUTION_CONTEXT_VAR,
                                        JavaTypeName.OBJECT,
                                        JavaTypeName.OBJECT,
                                        MethodInvocation.InvocationType.INTERFACE));
                        JavaStatement decl = new JavaStatement.LocalVariableDeclaration(newInstanceVar, initializer);
                        b.addStatement(decl);

                        // If no instance exists for the execution context create one and cache it.

                        // newInstance == null
                        JavaExpression comparison = new JavaExpression.OperatorExpression.Binary(JavaOperator.EQUALS_OBJECT, newInstanceVar, JavaExpression.LiteralWrapper.NULL);

                        // newInstance = new RTFullApp.General_0($instance);
                        // $instancesMap.put($ec, newInstance);
                        JavaStatement.Block then = new JavaStatement.Block();
                        then.addStatement(new ExpressionStatement(new JavaExpression.Assignment(newInstanceVar, new JavaExpression.ClassInstanceCreationExpression(_0TypeName, instanceField, JavaTypeNames.RTSUPERCOMBINATOR))));
                        JavaExpression cacheValue = new MethodInvocation.Instance(
                                instanceMapField,
                                "put",
                                new JavaExpression[] {
                                        SCJavaDefn.EXECUTION_CONTEXT_VAR,
                                        newInstanceVar },
                                new JavaTypeName[] {
                                        JavaTypeName.OBJECT,
                                        JavaTypeName.OBJECT },
                                JavaTypeName.OBJECT,
                                MethodInvocation.InvocationType.INTERFACE);
                        then.addStatement(new ExpressionStatement(cacheValue));

                        // Put the whole if expression together.
                        JavaStatement ifthen = new JavaStatement.IfThenElseStatement(comparison, then);
                        b.addStatement(ifthen);
                        b.addStatement(new ReturnStatement(newInstanceVar));

                        javaMethod.addStatement(b);
                        break;
                    } else if (mf.getArity() == 0) {

                        Block b = new Block();

                        // This is an unsafe method.  i.e. a non-CAF function of arity zero.  Since the
                        // function can have side effects we need to create a new instance of the class
                        // each time so that a previously evaluated value doesn't get re-used in place
                        // of actually executing the function.
                        JavaExpression.LocalVariable newInstanceVar = new JavaExpression.LocalVariable("newInstance", returnType);
                        JavaExpression initializer = new ClassInstanceCreationExpression(_0TypeName, instanceField, JavaTypeNames.RTSUPERCOMBINATOR);
                        JavaStatement decl = new JavaStatement.LocalVariableDeclaration(newInstanceVar, initializer);
                        b.addStatement(decl);
                        b.addStatement(new ReturnStatement(newInstanceVar));

                        javaMethod.addStatement(b);
                        break;
                    }
                }

            } else {
                SwitchStatement switchStatement =
                    new SwitchStatement(new MethodVariable("scIndex"));

                for (final MachineFunction mf : functions.getTopLevelCALFunctions()) {
                    JavaField instanceField = new JavaField.Static(className, CALToJavaNames.getInstanceFieldName(mf.getQualifiedName(), module), className);
                    int functionIndex = functions.getFunctionIndex(mf.getName());
                    if (mf.isCAF()) {

                        Block b = new Block();
                        // This is a CAF (constant applicative form) we want to use the cached instance associated
                        // with the execution context, if it exists.
                        // If it doesn't exist we will create an instance and add it to the cache.

                        String instanceMapFieldName = functions.getFNamePrefix(mf.getName()) + "$instancesMap";
                        JavaField instanceMapField =
                            new JavaField.Static(className, instanceMapFieldName, JavaTypeName.MAP);


                        // RTFunction newInstance = (RTFunction)$instancesMap.get($ec);
                        JavaExpression.LocalVariable newInstanceVar = new JavaExpression.LocalVariable("newInstance", returnType);
                        JavaExpression initializer =
                                new JavaExpression.CastExpression(returnType, new MethodInvocation.Instance (instanceMapField,
                                                    "get",
                                                    SCJavaDefn.EXECUTION_CONTEXT_VAR,
                                                    JavaTypeName.OBJECT,
                                                    JavaTypeName.OBJECT,
                                                    MethodInvocation.InvocationType.INTERFACE));
                        JavaStatement decl = new JavaStatement.LocalVariableDeclaration(newInstanceVar, initializer);
                        b.addStatement(decl);

                        // If no instance exists for the execution context create one and cache it.

                        // newInstance == null
                        JavaExpression comparison = new JavaExpression.OperatorExpression.Binary(JavaOperator.EQUALS_OBJECT, newInstanceVar, JavaExpression.LiteralWrapper.NULL);

                        // newInstance = new RTFullApp.General_0($instance);
                        // $instancesMap.put($ec, newInstance);
                        JavaStatement.Block then = new JavaStatement.Block();
                        then.addStatement(new ExpressionStatement(new JavaExpression.Assignment(newInstanceVar, new JavaExpression.ClassInstanceCreationExpression(_0TypeName, instanceField, JavaTypeNames.RTSUPERCOMBINATOR))));
                        JavaExpression cacheValue =
                            new MethodInvocation.Instance(instanceMapField,
                                                 "put",
                                                 new JavaExpression[]{SCJavaDefn.EXECUTION_CONTEXT_VAR, newInstanceVar},
                                                 new JavaTypeName[]{JavaTypeName.OBJECT, JavaTypeName.OBJECT},
                                                 JavaTypeName.OBJECT,
                                                 MethodInvocation.InvocationType.INTERFACE);
                        then.addStatement(new ExpressionStatement(cacheValue));

                        // Put the whole if expression together.
                        JavaStatement ifthen = new JavaStatement.IfThenElseStatement(comparison, then);
                        b.addStatement(ifthen);
                        b.addStatement(new ReturnStatement(newInstanceVar));

                        switchStatement.addCase(
                                new SwitchStatement.IntCaseGroup(
View Full Code Here

            javaClassRep.addMethod(javaMethod);

            if (consolidateFunctions) {

                // Extract the arguments from the application chain.
                JavaStatement argumentExtraction = generateArgumentExtractors(javaDefn);
                javaMethod.addStatement(argumentExtraction);

                final JavaExpression[] args = new JavaExpression[arity + 1];
                final JavaTypeName[] argTypes = new JavaTypeName[arity + 1];
                Arrays.fill (argTypes, JavaTypeNames.RTVALUE);
                args[arity] = SCJavaDefn.EXECUTION_CONTEXT_VAR;
                argTypes[arity] = JavaTypeNames.RTEXECUTION_CONTEXT;

                // Call fnS with the extracted arguments.
                for (int i = 0; i < arity; ++i) {
                    String javaName = javaDefn.getJavaArgumentName(i);
                    if (javaDefn.isArgStrict(i) && javaDefn.isArgUnboxable(i)) {
                        // We append the names of the arguments with $L to allow the declared name to
                        // be used with the unboxed primitive value.
                        javaName += "$L";
                    }

                    JavaTypeName argType = JavaTypeNames.RTVALUE;
                    if (javaDefn.isArgStrict(i) && javaDefn.isArgUnboxable(i)) {
                        argType = javaDefn.getArgumentTypeName(i);
                    }
                    LocalVariable lv = new LocalVariable (javaName, JavaTypeNames.RTVALUE);
                    args[i] = lv;
                    if (javaDefn.isArgStrict(i)) {
                        args[i] = SCJavaDefn.createInvocation(args[i], SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR);
                        args[i] =
                            callLastRef(args[i], lv);
                        if (javaDefn.isArgUnboxable(i)) {
                            args[i] = SCJavaDefn.unboxValue(javaDefn.getArgumentTypeName(i), args[i]);
                            argTypes[i] = argType;
                        }
                    } else {
                        args[i] =
                            callLastRef(args[i], lv);
                    }
                }

                String methodNamePrefix2 = functions.getFnNamePrefix(javaDefn.getFunctionName());
                JavaExpression mi =
                    new MethodInvocation.Instance (
                            null,
                            methodNamePrefix2 + "f" + arity + "S",
                            args,
                            argTypes,
                            JavaTypeNames.RTVALUE,
                            MethodInvocation.InvocationType.VIRTUAL);

                javaMethod.addStatement(new JavaStatement.ReturnStatement(mi));

            } else {
                // Add statistics generation.
                addStatsBlock (javaMethod, javaDefn);


                // Extract function arguments from the application chain.
                JavaStatement argumentExtraction = generateArgumentExtractors(javaDefn);
                javaMethod.addStatement(argumentExtraction);

                javaMethod.addStatement (generateStrictArgEvaluationBlock(javaDefn));

View Full Code Here

            }

            // Make a call on the root node to free its member fields.
            // This frees them up for potential garbage collection.

            JavaStatement comment =
                new JavaStatement.LineComment("Release the fields in the root node to open them to garbage collection");
            argExtractorBlock.addStatement(comment);

            JavaExpression clearMembers =
                new MethodInvocation.Instance(
View Full Code Here

                        JavaTypeName.STRING,
                        JavaTypeName.BOOLEAN,
                        MethodInvocation.InvocationType.VIRTUAL);

            JavaStatement.Block debuggingNeededThenBlock = new Block();
            JavaStatement isDebuggingNeededIfStatement =
                new JavaStatement.IfThenElseStatement(isDebuggingNeededCheck, debuggingNeededThenBlock);

            final int arity = javaDefn.getArity();

            //new RTValue[]{CAL_Int.make(take$nElements$1), take$list$2}
View Full Code Here

TOP

Related Classes of org.openquark.cal.internal.javamodel.JavaStatement$ThrowStatement

Copyright © 2018 www.massapicom. 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.