Package org.openquark.cal.internal.javamodel

Examples of org.openquark.cal.internal.javamodel.JavaExpression


                    continue;
                }
                JavaTypeName type = fieldNameToType.get(fn);
                String fieldName = SCJavaDefn.getJavaFieldNameFromFieldName(fn);

                JavaExpression assign = new JavaExpression.Assignment(new JavaField.Instance(null, fieldName, type), getDefaultValueForType(type));
                constructor.addStatement(new ExpressionStatement(assign));
            }

            return constructor;
        }
View Full Code Here


                argNames[i] = argName;
                argTypes[i] = type;
                MethodVariable mv = new MethodVariable (argName);
                JavaExpression.JavaField.Instance field =
                    new JavaExpression.JavaField.Instance(null, fieldName, type);
                JavaExpression assign = new Assignment (field, mv);
                constructorBody.addStatement(new ExpressionStatement(assign));
                i++;
            }

            JavaConstructor constructor = new JavaConstructor (Modifier.PROTECTED, argNames, argTypes, className.getUnqualifiedJavaSourceName());
View Full Code Here

                } 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);
View Full Code Here

            // If we are doing a sanity check on let variable behaviour we
            // want to check/set the flag associated with this method.
            if (LECCMachineConfiguration.SANITY_CHECK_LET_VARS) {
                JavaField flag = new JavaField.Instance(null, CALToJavaNames.cleanSCName(javaDefn.getFunctionName()) + "_flag_", JavaTypeName.BOOLEAN);
                JavaExpression exception =
                    new JavaExpression.ClassInstanceCreationExpression(
                            JavaTypeName.NULL_POINTER_EXCEPTION,
                            LiteralWrapper.make("Double evaluation of " + javaDefn.getModuleName() + "." + CALToJavaNames.cleanSCName(javaDefn.getFunctionName()) + " in " + functions.getFunctionGroupName()),
                            JavaTypeName.STRING);

                JavaStatement.IfThenElseStatement check =
                    new JavaStatement.IfThenElseStatement(flag, new JavaStatement.ThrowStatement(exception));

                javaMethod.addStatement(check);

                JavaExpression assign = new JavaExpression.Assignment(flag, LiteralWrapper.make(Boolean.TRUE));
                javaMethod.addStatement(new ExpressionStatement(assign));
            }

            // Add the body
            javaMethod.addStatement(bodyBlock);
View Full Code Here

            // Instance of this class for each supercombinator it
            // represents.
            for (final MachineFunction mf : functions.getTopLevelCALFunctions()) {

                final int functionIndex = functions.getFunctionIndex(mf.getName());
                final JavaExpression initializer;

                if (functions.getNFunctions() <= 1) {
                    initializer = new ClassInstanceCreationExpression(
                            className);
                } else {
                    initializer = new ClassInstanceCreationExpression(
                            className,
                            new JavaExpression[]{LiteralWrapper.make(Integer.valueOf(functionIndex)),
                                                 LiteralWrapper.make(Integer.valueOf(mf.getArity()))},
                            new JavaTypeName[]{JavaTypeName.INT,
                                               JavaTypeName.INT});
                }

                final String fieldName = CALToJavaNames.getInstanceFieldName(mf.getQualifiedName(), module);
                final int instanceModifiers;
                if (mf.getArity() == 0) {
                    //for a CAF or a zero-arity function, the instance field will never be referred to directly outside the class
                    //so declare them private for safety sake.
                    instanceModifiers = Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL;
                } else {
                    instanceModifiers = Modifier.PUBLIC | Modifier.STATIC | Modifier.FINAL;
                }
                final JavaFieldDeclaration instanceDeclaration =
                    new JavaFieldDeclaration(instanceModifiers,
                            className,
                            fieldName,
                            initializer);

                if (functions.getNFunctions() <= 1) {
                    instanceDeclaration.setJavaDoc(new JavaDocComment("Singleton instance of this class."));
                } else {
                    instanceDeclaration.setJavaDoc(new JavaDocComment("Instance of this class representing CAL function " + mf.getName() + "."));
                }
                javaClassRep.addFieldDeclaration(instanceDeclaration);
            }


            if (sharedValues.getNStaticErrorInfo() > 0) {
                javaClassRep.addComment(new JavaStatement.MultiLineComment("ErrorInfo instances."));
            }
            for (final String name : sharedValues.getStaticErrorInfoNames()) {

                final JavaExpression initializer = sharedValues.getStaticError(name);
                final int instanceModifiers = Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL;
                final JavaFieldDeclaration errorDeclaration = new JavaFieldDeclaration(instanceModifiers, JavaTypeName.ERRORINFO, name, initializer);
                javaClassRep.addFieldDeclaration(errorDeclaration);

            }

            // If this function references other supercombinators we need to set
            // up a field for each referenced SC, a flag to indicate the
            // initialization state of the referenced SC fields, and potentially an
            // object to be used as a synchronization mutex for the initialization method.
            final int referencedDCModifiers = Modifier.STATIC | Modifier.PRIVATE | Modifier.FINAL;

            // Create an instance field for each referenced data constructor.  This will give us
            // a local reference that can be used in the body function.
            //  e.g. RTFunction i_Foo;
            if (sharedValues.getNReferencedDCs() > 0) {
                javaClassRep.addComment(new JavaStatement.MultiLineComment("Data constructor class instances for all referenced data constructors."));
            }
            for (final ReferencedDCInfo rfi : sharedValues.getReferencedDCs()) {
                DataConstructor dc = rfi.getDC();
                JavaField jf = rfi.getJField();
                JavaExpression fieldInitializer;
                if (dc.getArity() > 0) {
                    // Just invoke the regular make invocation to get the singleton SC/DC instance.
                    fieldInitializer =
                        new MethodInvocation.Static(jf.getFieldType(), "make", JavaExpression.EMPTY_JAVA_EXPRESSION_ARRAY, JavaExpression.EMPTY_TYPE_NAME_ARRAY, jf.getFieldType());
                } else {
                    // This is a zero arity data constructor.  We get the singleton instance by accessing
                    // the DataType class factory method if the data type has more than one zero arity DC.
                    TypeConstructor typeCons = dc.getTypeConstructor();
                    if (SCJavaDefn.isTagDC(dc, module)) {
                        JavaTypeName typeClass = CALToJavaNames.createTypeNameFromType(typeCons, module);
                        JavaTypeName tagDCTypeName = CALToJavaNames.createTypeNameForTagDCFromType(typeCons, module);
                        Integer ordinal = Integer.valueOf(dc.getOrdinal());
                        fieldInitializer = new MethodInvocation.Static(typeClass, "getTagDC", LiteralWrapper.make(ordinal), JavaTypeName.INT, tagDCTypeName);
                    } else {
                        // Just invoke the regular make invocation to get the singleton SC/DC instance.
                        fieldInitializer =
                            new MethodInvocation.Static(jf.getFieldType(), "make", JavaExpression.EMPTY_JAVA_EXPRESSION_ARRAY, JavaExpression.EMPTY_TYPE_NAME_ARRAY, jf.getFieldType());
                    }
                }

                JavaFieldDeclaration dcDeclaration =
                    new JavaFieldDeclaration(
                            referencedDCModifiers,
                            jf.getFieldType(),
                            jf.getFieldName(),
                            fieldInitializer);
                javaClassRep.addFieldDeclaration(dcDeclaration);
            }

            if (functions.includesCAFs()) {

                javaClassRep.addComment(new JavaStatement.MultiLineComment("Mappings of execution context to CAF instances."));
                // Add an instance field to hold a Map of
                // ExecutionContext -> RTFullApp.General._0
                // A map for associating instances of this SC with execution contexts.
                // This is only created for CAF functions.  There are two reasons why CAFs
                // have an instance for each execution context.  One is thread safety.  The
                // other is so that different threads of execution can release cached CAF
                // results at will.
                for (final MachineFunction mf : functions.getTopLevelCALFunctions()) {
                    if (mf.isCAF()) {

                        //we do not synchronize the instance map, but rather the methods in the CAF class that mutate it.
                        //this is because the make method has check-then-modify semantics, and so needs to be synchronized at the method
                        //level anyways.
                        JavaExpression instancesMapInit = new ClassInstanceCreationExpression(JavaTypeName.WEAK_HASH_MAP);

                        String mapPrefix = functions.getFNamePrefix(mf.getName());
                        JavaFieldDeclaration instancesMap =
                            new JavaFieldDeclaration (
                                Modifier.STATIC | Modifier.PRIVATE | Modifier.FINAL,
View Full Code Here

                            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));

                        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,
View Full Code Here

                            callLastRef(args[i], lv);
                    }
                }

                String methodNamePrefix2 = functions.getFnNamePrefix(javaDefn.getFunctionName());
                JavaExpression mi =
                    new MethodInvocation.Instance (
                            null,
                            methodNamePrefix2 + "f" + arity + "S",
                            args,
                            argTypes,
View Full Code Here

                    } else {
                        fixedUpVarName = basicVarName;
                    }
                }

                final JavaExpression initializer;

                if (i == arity - 1) {
                    //$rootNode.getArgValue();
                    initializer = SCJavaDefn.createInvocation(METHODVAR_ROOT_NODE, SCJavaDefn.GETARGVALUE);

                } else if (i == arity - 2) {

                    if (arity > 2) {
                        //RTValue $currentRootNode;
                        argExtractorBlock.addStatement(new LocalVariableDeclaration(LOCALVAR_CURRENT_ROOT_NODE));
                        //($currentRootNode = $rootNode.prevArg()).getArgValue();
                        initializer =
                            SCJavaDefn.createInvocation(
                                new Assignment(LOCALVAR_CURRENT_ROOT_NODE, SCJavaDefn.createInvocation(METHODVAR_ROOT_NODE, SCJavaDefn.PREVARG)),
                                SCJavaDefn.GETARGVALUE);
                    } else {
                        //$rootNode.prevArg().getArgValue();
                        initializer =
                            SCJavaDefn.createInvocation(
                                SCJavaDefn.createInvocation(METHODVAR_ROOT_NODE, SCJavaDefn.PREVARG),
                                SCJavaDefn.GETARGVALUE);
                    }

                } else if (i == 0) {

                    //$currentRootNode.prevArg().getArgValue();
                    initializer =
                        SCJavaDefn.createInvocation(
                            SCJavaDefn.createInvocation(LOCALVAR_CURRENT_ROOT_NODE, SCJavaDefn.PREVARG),
                            SCJavaDefn.GETARGVALUE);

                } else {

                    //($currentRootNode = $currentRootNode.prevArg()).getArgValue();
                    initializer =
                        SCJavaDefn.createInvocation(
                            new Assignment(LOCALVAR_CURRENT_ROOT_NODE, SCJavaDefn.createInvocation(LOCALVAR_CURRENT_ROOT_NODE, SCJavaDefn.PREVARG)),
                            SCJavaDefn.GETARGVALUE);
                }

                LocalVariableDeclaration varDecl = new LocalVariableDeclaration(new LocalVariable(fixedUpVarName, JavaTypeNames.RTVALUE), initializer);

                argExtractorBlock.addStatement(varDecl);
            }

            // 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(
                        new MethodVariable(ROOT_NODE),
                        "clearMembers",
                        JavaTypeName.VOID,
                        MethodInvocation.InvocationType.VIRTUAL);
View Full Code Here

                    if (javaDefn.isArgStrict(argIndex)) {
                        String fixedUpVarName = javaDefn.getJavaArgumentName(argIndex);
                        if (javaDefn.isArgUnboxable(argIndex)) {
                            // In this case the argument was named with an appended $L so that we can declare a
                            // local variable with the CAL argument name that is the unboxed primitive value.
                            JavaExpression evaluate = SCJavaDefn.createInvocation(new LocalName(fixedUpVarName + "$L", JavaTypeNames.RTVALUE), SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR);
                            evaluate = SCJavaDefn.unboxValue(javaDefn.getArgumentTypeName(argIndex), evaluate);
                            LocalVariableDeclaration lvd = new LocalVariableDeclaration(new LocalVariable(fixedUpVarName, javaDefn.getArgumentTypeName(argIndex)), evaluate);
                            block.addStatement(lvd);
                        } else {
                            JavaExpression.Nameable argVar = new LocalName(fixedUpVarName, JavaTypeNames.RTVALUE);
                            JavaExpression evaluate = SCJavaDefn.createInvocation(argVar, SCJavaDefn.EVALUATE, SCJavaDefn.EXECUTION_CONTEXT_VAR);
                            Assignment assign = new Assignment(argVar, evaluate);
                            block.addStatement(new ExpressionStatement (assign));
                        }
                    }
                }
View Full Code Here

                }

                args[arity] = SCJavaDefn.EXECUTION_CONTEXT_VAR;
                argTypes[arity] = JavaTypeNames.RTEXECUTION_CONTEXT;

                JavaExpression mi =
                    new MethodInvocation.Instance (
                            null,
                            methodNamePrefix + "f" + arity + "S",
                            args,
                            argTypes,
View Full Code Here

TOP

Related Classes of org.openquark.cal.internal.javamodel.JavaExpression

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.