Package org.openquark.cal.internal.javamodel.JavaStatement

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


            javaClassRep.addMethod(javaMethod);

            if (implementAtThisLevel) {
                JavaField field = new JavaField.Instance (null, fieldName, fieldType);
                if (primitiveType) {
                    javaMethod.addStatement(new ReturnStatement(SCJavaDefn.boxExpression(fieldTypeExpr, field)));
                } else {
                    if (!fieldIsStrict) {
                        // We have a non-strict field of type RTValue.  In order to reduce space usage
                        // we want to update the field value to be the result of the original suspension.
                        // If the field value is an RTResultFunction we want to re-assign the field with
                        // a call to 'getValue()'.
                        // For example:
                        //public final RTValue get_head() {
                        //    RTValue field;
                        //        if ((field = _head) instanceof RTResultFunction) {
                        //            return (_head = field.getValue());
                        //        }
                        //        return field;
                        //}
                        String localName = fieldName+"$";
                        LocalVariable localVariable = new LocalVariable(localName, fieldType);
                        LocalVariableDeclaration localDeclaration = new LocalVariableDeclaration(localVariable);
                        javaMethod.addStatement(localDeclaration);
                        Assignment localAssignment = new Assignment(localVariable, field);
                        InstanceOf checkType = new InstanceOf(localAssignment, JavaTypeNames.RTRESULT_FUNCTION);
                        MethodInvocation getValue = new MethodInvocation.Instance(localVariable, "getValue", JavaTypeNames.RTVALUE, MethodInvocation.InvocationType.VIRTUAL);
                        Assignment fieldAssignment = new Assignment(field, getValue);
                        ReturnStatement returnNewVal = new ReturnStatement(fieldAssignment);
                        IfThenElseStatement ifThen = new IfThenElseStatement(checkType, returnNewVal);
                        javaMethod.addStatement(ifThen);
                        javaMethod.addStatement(new ReturnStatement (localVariable));
                    } else {
                        javaMethod.addStatement(new ReturnStatement (field));
                    }
                }
            } else {
                // This class should throw an error for any access.  The methods will
                // be overridden by derived classes for each data constructor.
                // We can simply call the method badFieldAccessor() implemented in
                // RTCons.
                MethodInvocation mi =
                    new MethodInvocation.Instance(
                            null,
                            "badFieldAccessor",
                            LiteralWrapper.make(fieldName),
                            JavaTypeName.STRING,
                            JavaTypeNames.RTVALUE,
                            MethodInvocation.InvocationType.VIRTUAL);

                javaMethod.addStatement (new ReturnStatement (mi));
            }
        }
View Full Code Here


            javaClassRep.addMethod(javaMethod);

            if (implementAtThisLevel && isStrict) {
                JavaField field = new JavaField.Instance (null, fieldName, fieldType);
                javaMethod.addStatement(new ReturnStatement (field));
            } else {
                // This class should throw an error for any access.  The methods will
                // be overridden by derived classes for each data constructor.
                // We want to call the RTCons method badFieldAccessor_...
                // that matches the type of the field.

                String fieldTypeString = SCJavaDefn.getNameForPrimitive(fieldType);
                String castTypeString = null;
                if (!(fieldType instanceof JavaTypeName.Primitive)) {
                    castTypeString = fieldTypeString;
                    fieldTypeString = "Object";
                }
                MethodInvocation mi =
                    new MethodInvocation.Instance(
                        null,
                        "badFieldAccessor_" + fieldTypeString,
                        LiteralWrapper.make(fieldName),
                        JavaTypeName.STRING,
                        fieldType,
                        MethodInvocation.InvocationType.VIRTUAL);

                if (castTypeString != null) {
                    javaMethod.addStatement(new ReturnStatement(new JavaExpression.CastExpression(fieldType, mi)));
                } else {
                    javaMethod.addStatement (new ReturnStatement (mi));
                }
            }
        }
View Full Code Here

            // public final int getArity() {return 0;}
            int modifiers = Modifier.PUBLIC | Modifier.FINAL;
            JavaTypeName returnType = JavaTypeName.INT;
            JavaMethod javaMethod = new JavaMethod(modifiers, returnType, "getArity");
            tagDCClassRep.addMethod(javaMethod);
            javaMethod.addStatement(new ReturnStatement(LiteralWrapper.make(Integer.valueOf(0))));

            // public final int getOrdinalValue(){return tag;}
            modifiers = Modifier.PUBLIC | Modifier.FINAL;
            returnType = JavaTypeName.INT;
            javaMethod = new JavaMethod(modifiers, returnType, "getOrdinalValue");
            tagDCClassRep.addMethod(javaMethod);
            javaMethod.addStatement(new ReturnStatement(new JavaField.Instance(null, "tag", JavaTypeName.INT)));

            // public final String getModuleName() ...
            modifiers = Modifier.PUBLIC | Modifier.FINAL;
            returnType = JavaTypeName.STRING;
            javaMethod = new JavaMethod(modifiers, returnType, "getModuleName");
            tagDCClassRep.addMethod(javaMethod);
            javaMethod.addStatement(new ReturnStatement(LiteralWrapper.make (typeConstructor.getName().getModuleName().toSourceText())));

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

         *
         */
        private final void createMethod_getModuleName () {
            JavaMethod jm = new JavaMethod (Modifier.PUBLIC | Modifier.FINAL, JavaTypeName.STRING, "getModuleName");
            javaClassRep.addMethod(jm);
            jm.addStatement(new ReturnStatement(LiteralWrapper.make(module.getName().toSourceText())));
        }
View Full Code Here

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

                MethodInvocation mi =
                    new MethodInvocation.Static (JavaTypeNames.RTVALUE,
                                                   "badValue_Object",
                                                   new JavaExpression[]{LiteralWrapper.NULL, LiteralWrapper.make("Bad index in getUnQualifiedName()")},
                                                   new JavaTypeName[]{JavaTypeName.ERRORINFO, JavaTypeName.STRING},
                                                   JavaTypeName.OBJECT);
                jm.addStatement(new ReturnStatement(new CastExpression(JavaTypeName.STRING, mi)));
            } else {
                for (final MachineFunction mf : functions.getTopLevelCALFunctions()) {
                    jm.addStatement(new ReturnStatement(LiteralWrapper.make(mf.getName())));
                }

            }
        }
View Full Code Here

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

                jm.addStatement(switchStatement);

                MethodInvocation mi =
                    new MethodInvocation.Static (JavaTypeNames.RTVALUE,
                                                   "badValue_Object",
                                                   new JavaExpression[]{LiteralWrapper.NULL, LiteralWrapper.make("Bad index in getQualifiedName()")},
                                                   new JavaTypeName[]{JavaTypeName.ERRORINFO, JavaTypeName.STRING},
                                                   JavaTypeName.OBJECT);
                jm.addStatement(new ReturnStatement(new CastExpression(JavaTypeName.STRING, mi)));
            } else {
                for (final MachineFunction mf : functions.getTopLevelCALFunctions()) {
                    jm.addStatement( new ReturnStatement(LiteralWrapper.make(mf.getQualifiedName().getQualifiedName())));
                }
            }
        }
View Full Code Here

                            argTypes,
                            JavaTypeNames.RTVALUE,
                            MethodInvocation.InvocationType.VIRTUAL);

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

            javaMethod.addStatement(switchStatement);

            // Handle the fallthrough.
            // If this is the lazy version of the 'f' method we want to defer to the
            // version in the superclass (i.e. RTValue).  This can occur when there
            // is a lazy application of a functional argument, or an oversaturation.
            if (strict) {
                MethodInvocation mi =
                    new MethodInvocation.Static (JavaTypeNames.RTVALUE,
                                                   "badValue",
                                                   LiteralWrapper.make("Bad scTag in 'f'."),
                                                   JavaTypeName.STRING,
                                                   JavaTypeNames.RTVALUE);
                javaMethod.addStatement(new ReturnStatement(mi));
            } else {
                MethodInvocation mi =
                    new MethodInvocation.Instance(
                            null,
                            "f" + arity + "L",
                            this.javaClassRep.getSuperclassName(),
                            argValues,
                            argTypes,
                            JavaTypeNames.RTVALUE,
                            MethodInvocation.InvocationType.SPECIAL);

                javaMethod.addStatement(new JavaStatement.LineComment("This is an oversaturated lazy application."));
                javaMethod.addStatement(new JavaStatement.LineComment("Usually this occurs when dealing with a lazy application of a function type argument."));
                javaMethod.addStatement(new JavaStatement.LineComment("Defer to the base implementation in the super class."));

                javaMethod.addStatement(new ReturnStatement(mi));
            }
        }
View Full Code Here

                            MethodInvocation.InvocationType.VIRTUAL);

                switchStatement.addCase(
                    new SwitchStatement.IntCaseGroup(
                            functions.getFunctionIndex(mf.getName()),
                            new ReturnStatement(mi)));
            }

            javaMethod.addStatement(switchStatement);

            // Handle the fallthrough.
            final MethodInvocation mi =
                new MethodInvocation.Static (JavaTypeNames.RTVALUE,
                                               "badValue",
                                               LiteralWrapper.make("Bad scTag in 'f'."),
                                               JavaTypeName.STRING,
                                               JavaTypeNames.RTVALUE);
            javaMethod.addStatement(new ReturnStatement(mi));
        }
View Full Code Here

                        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(
                                        functionIndex,
                                        b));

                    }

                }

                MethodInvocation mi =
                    new MethodInvocation.Static (JavaTypeNames.RTVALUE,
                                                   "badValue",
                                                   new JavaExpression[]{LiteralWrapper.NULL, LiteralWrapper.make("Illegal fall through to default case in " + functions.getFunctionGroupQualifiedName() + ".make().")},
                                                   new JavaTypeName[]{JavaTypeName.ERRORINFO, JavaTypeName.STRING},
                                                   JavaTypeNames.RTVALUE);
                switchStatement.addCase(
                        new SwitchStatement.DefaultCase(new ReturnStatement(new CastExpression(JavaTypeNames.RTFUNCTION, mi))));



                javaMethod.addStatement(switchStatement);
            }
View Full Code Here

            javaClassRep.addMethod(javaMethod);

            // Add the body
            if (functions.getNFunctions() > 1) {
                JavaField arityField = new JavaField.Instance(null, "arity", JavaTypeName.INT);
                javaMethod.addStatement(new ReturnStatement(arityField));
            } else {
                int arity = functions.getTopLevelCALFunctions().iterator().next().getArity();
                javaMethod.addStatement(new ReturnStatement(LiteralWrapper.make(Integer.valueOf(arity))));
            }
        }
View Full Code Here

TOP

Related Classes of org.openquark.cal.internal.javamodel.JavaStatement.ReturnStatement

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.