Package org.openquark.cal.compiler

Examples of org.openquark.cal.compiler.DataConstructor


            // Import the top level class as a foreign type.
            // ex. data foreign unsafe import jvm "org.olap.CubeType" public JCubeType;
            makeForeignClassDeclaration(topLevelDefnsList);
           
            for (int i = 0, n = typeConstructor.getNDataConstructors(); i < n; ++i) {
                DataConstructor dc = typeConstructor.getNthDataConstructor(i);
                makeForeignClassDeclaration(dc, topLevelDefnsList);
            }
           
            TypeExprDefn.TypeCons jClassTypeExpr =
                TypeExprDefn.TypeCons.make(Name.TypeCons.makeUnqualified(typeConstructorInfo.calForeignTypeName));
           
           
            // Import the constructors for each inner class (i.e. each data constructor)
            // Pull in the constructor for the Java class.
            for (int i = 0, n = javaClass.getNInnerClasses(); i < n; ++i) {
                JavaClassRep innerClass = javaClass.getInnerClass(i);
                assert (innerClass.getNConstructors() == 1);
                JavaConstructor constructor = innerClass.getConstructor(0);
                topLevelDefnsList.add(importJavaConstructor(constructor, true));
            }
           
            // Create accessors for fields in the top level class.
            for (FieldName fn : typeConstructorInfo.commonFieldNames) {
                Set<JavaTypeName> fieldTypes = typeConstructorInfo.allFieldTypeNames.get(fn);
               
                // There will only be one type for common fields.
                Iterator<JavaTypeName> types = fieldTypes.iterator();
                JavaTypeName fieldType = types.next();
               
                String foreignFieldTypeSting = typeConstructorInfo.calFieldForeignTypes.get(fn).get(fieldType);
               
                generateFieldAccessor (
                        typeConstructorInfo.calForeignTypeName,
                        fn,
                        fieldType,
                        foreignFieldTypeSting,
                        typeConstructor.getNthDataConstructor(0),
                        topLevelDefnsList);
            }
           
            // Create accessors for the fields in the inner classes.
            for (int i = 0, n = typeConstructor.getNDataConstructors(); i < n; ++i) {
                DataConstructor dc = typeConstructor.getNthDataConstructor(i);
                DataConstructorInfo dcInfo = (DataConstructorInfo)typeConstructorInfo.dataConstructorInfo.get(dc);
               
                for (FieldName fn : dcInfo.allFieldNames) {
                    if (typeConstructorInfo.commonFieldNames.contains(fn)) {
                        continue;
                    }
                   
                    JavaTypeName fieldType = (JavaTypeName)dcInfo.fieldTypeNames.get(fn);
                    String foreignFieldTypeSting = typeConstructorInfo.calFieldForeignTypes.get(fn).get(fieldType);
                    generateFieldAccessor (
                            dcInfo.calForeignTypeName,
                            fn,
                            fieldType,
                            foreignFieldTypeSting,
                            dc,
                            topLevelDefnsList);
                }
            }
           
            // Bring in the 'getDCOrdinal()' method of the Java class.
            String getDCOrdinalName = "j" + javaClass.getClassName().getUnqualifiedJavaSourceName() + "_getDCOrdinal";
            SourceModel.TypeSignature getDCOrdinalTypeSignature =
                TypeSignature.make(
                        TypeExprDefn.Function.make(
                                jClassTypeExpr,
                                INT_TYPE_EXPR_DEFN));
           
            SourceModel.CALDoc.Comment.Function getDCOrdinalComment;
            {
                SourceModel.CALDoc.TextSegment.Plain textSegment =
                    SourceModel.CALDoc.TextSegment.Plain.make(
                            "\nRetrieve the ordinal value from an instance of " + typeConstructorInfo.calForeignTypeName + ".\n" +
                            "The ordinal can be used to determine which data constructor the " + typeConstructorInfo.calForeignTypeName + "\n" +
                            "instance corresponds to.");
               
                SourceModel.CALDoc.TextBlock textBlock =
                    SourceModel.CALDoc.TextBlock.make(new SourceModel.CALDoc.TextSegment.TopLevel[]{textSegment});
                   
                getDCOrdinalComment =
                    SourceModel.CALDoc.Comment.Function.make(
                            textBlock,
                            null);
            }
           
            SourceModel.FunctionDefn getDCOrdinal =
                FunctionDefn.Foreign.make(
                        getDCOrdinalComment,
                        getDCOrdinalName,
                        Scope.PRIVATE,
                        true,
                        "method getDCOrdinal",
                        getDCOrdinalTypeSignature);
            topLevelDefnsList.add(getDCOrdinal);
           
            // Create input function.
            // inputCube :: JCube -> Cube;
            // inputCube jCube =
            //     case (jCube_getDCOrdinal JCube) of
            //     0 -> ...;
            //     1 -> ...;
            String inputFunctionName =
                "input" + typeConstructor.getName().getUnqualifiedName();
            String inputFunctionArgName = "j" + typeConstructorInfo.calForeignTypeName.substring(1);
            Expr.Var inputFunctionArg = Expr.Var.make(Name.Function.makeUnqualified(inputFunctionArgName));

            // Do the function type declaration.
            // JCube -> Cube
            TypeExprDefn inputFunctionType =
                TypeExprDefn.Function.make(
                        TypeExprDefn.TypeCons.make(Name.TypeCons.makeUnqualified(typeConstructorInfo.calForeignTypeName)),
                        TypeExprDefn.TypeCons.make(Name.TypeCons.makeUnqualified(typeConstructorName.getUnqualifiedName())));
           
            SourceModel.CALDoc.Comment.Function inputFunctionComment;
            {
                SourceModel.CALDoc.TextSegment.Plain textSegment =
                    SourceModel.CALDoc.TextSegment.Plain.make(
                            "\nInput an instance of " + typeConstructor.getName().getUnqualifiedName() + ".\n" +
                            "Translates an instance of " + typeConstructorInfo.calForeignTypeName + " to\n" +
                            "an instance of "+ typeConstructor.getName().getUnqualifiedName() + ".");
               
                SourceModel.CALDoc.TextBlock textBlock =
                    SourceModel.CALDoc.TextBlock.make(new SourceModel.CALDoc.TextSegment.TopLevel[]{textSegment});
                   
                inputFunctionComment =
                    SourceModel.CALDoc.Comment.Function.make(
                            textBlock,
                            null);
            }
           
            SourceModel.FunctionTypeDeclaration inputFunctionTypeDecl =
                FunctionTypeDeclaration.make(
                        inputFunctionComment,
                        inputFunctionName,
                        TypeSignature.make(inputFunctionType));
           
            topLevelDefnsList.add(inputFunctionTypeDecl);

            // build up the function body
            SourceModel.Expr condition =
                Expr.Application.make(
                        new Expr[]{Expr.Var.make(Name.Function.makeUnqualified(getDCOrdinalName)),
                                inputFunctionArg});
           

            SourceModel.Expr.Case.Alt caseAlts[] =
                new SourceModel.Expr.Case.Alt[typeConstructor.getNDataConstructors()];
            for (int i = 0, n = caseAlts.length; i < n; ++i) {
                DataConstructor dc = typeConstructor.getNthDataConstructor(i);
               
                // Build up an application of the data constructor.
                SourceModel.Expr dcApplication =
                    makeDCCall(dc, inputFunctionArg);

                caseAlts[i] =
                    Expr.Case.Alt.UnpackInt.make(
                            new BigInteger[]{BigInteger.valueOf(dc.getOrdinal())},
                            dcApplication);
            }
           
            SourceModel.Expr body =
                Expr.Case.make(condition, caseAlts);
           
            SourceModel.FunctionDefn.Algebraic inputFunction =
                FunctionDefn.Algebraic.make(
                        inputFunctionName,
                        Scope.PUBLIC,
                        new SourceModel.Parameter[]{Parameter.make(inputFunctionArgName, false)},
                        body);

            topLevelDefnsList.add(inputFunction);
           
            // Create an inputCubeFromJObject function.
            String inputFromJObjectFunctionName = inputFunctionName + "FromJObject";
            createInputFromJObjectFunction(inputFromJObjectFunctionName, inputFunctionName, topLevelDefnsList);
           
            // Create Inputable instance.
            SourceModel.InstanceDefn instanceDefn =
                InstanceDefn.make(
                        Name.TypeClass.make(CAL_Prelude.TypeClasses.Inputable),
                        InstanceDefn.InstanceTypeCons.TypeCons.make(Name.TypeCons.make(typeConstructorName), null),
                        null,
                        new InstanceDefn.InstanceMethod[]{
                            InstanceDefn.InstanceMethod.make(CAL_Prelude.Functions.input.getUnqualifiedName(), Name.Function.makeUnqualified(inputFromJObjectFunctionName))
                        });
            topLevelDefnsList.add(instanceDefn);
           
           
            // Now we want to make the outputable instance.
            // Build up a function body that converts
            // from 'Cube' to 'JCube'.

            Expr.Var dcInstance = Expr.Var.makeUnqualified("dcInstance");

            caseAlts =
                new SourceModel.Expr.Case.Alt[typeConstructor.getNDataConstructors()];
            for (int i = 0, n = caseAlts.length; i < n; ++i) {
                DataConstructor dc = typeConstructor.getNthDataConstructor(i);
               
                SourceModel.Pattern patterns[] = new SourceModel.Pattern[dc.getArity()];
                Arrays.fill(patterns, SourceModel.Pattern.Wildcard.make());
               
                caseAlts[i] =
                    Expr.Case.Alt.UnpackDataCons.make(
                            Name.DataCons.make(dc.getName()),
                            patterns,
                            makeConstructorCall(dc, dcInstance, true));
            }

            SourceModel.Expr.Case conversionFunctionBody =
View Full Code Here


            Map<String, String> enumFieldFunctionNames = new LinkedHashMap<String, String>();
           
            // Now we need to bring in each instance of the enumeration class.
            // These will be public static fields of the class.
            for (int i = 0, n = typeConstructor.getNDataConstructors(); i < n; ++i) {
                DataConstructor dc = typeConstructor.getNthDataConstructor(i);
                String enumFieldName = createEnumFieldName(dc.getName().getUnqualifiedName());
               
                // Example:
                // foreign unsafe import jvm "static field org.olap.CubeType.TYPE"
                //     public jCubeType_TYPE :: JCubeType;
                SourceModel.TypeSignature typeSignature =
                    TypeSignature.make(jClassTypeExpr);

                String functionName = "j" + javaClass.getClassName().getUnqualifiedJavaSourceName() + "_" + enumFieldName;
               
                SourceModel.CALDoc.Comment.Function foreignFieldComment;
                {
                    SourceModel.CALDoc.TextSegment.Plain textSegment =
                        SourceModel.CALDoc.TextSegment.Plain.make(
                                "\nThe Java static field " + javaClass.getClassName().getUnqualifiedJavaSourceName() + "." + enumFieldName + ".\n" +
                                "This field is used to represent instances of the data constructor " + dc.getName().getUnqualifiedName() + ".\n");
                   
                    SourceModel.CALDoc.TextBlock textBlock =
                        SourceModel.CALDoc.TextBlock.make(new SourceModel.CALDoc.TextSegment.TopLevel[]{textSegment});
                       
                    foreignFieldComment =
                        SourceModel.CALDoc.Comment.Function.make(
                                textBlock,
                                null);
                }
                               
                SourceModel.FunctionDefn enumInstance =
                    FunctionDefn.Foreign.make(
                            foreignFieldComment,
                            functionName,
                            Scope.PRIVATE,
                            true,
                            "static field " + javaClass.getClassName().getFullJavaSourceName() + "." + enumFieldName,
                                typeSignature);
                topLevelDefnsList.add (enumInstance);
                enumFieldFunctionNames.put(dc.getName().getUnqualifiedName(), functionName);   
            }
           
            // Bring in the getDCOrdinal() method.
            importGetDCOrdinal(topLevelDefnsList);
           
            // Create an function that converts a JCubeType_ to a CubeType
            // inputCubeType :: JCubeType_ -> CubeType;
            // public inputCubeType jCubeType_ =
            //     case (jCubeType_getDCOrdinal jCubeType_) of
            //     0 -> ...;
            //     1 -> ...;
           
            String inputArgumentName = "j" + javaClass.getClassName().getUnqualifiedJavaSourceName();
            String inputFunctionName = "input" + typeConstructor.getName().getUnqualifiedName();

            Expr.Var inputArgument = Expr.Var.make(Name.Function.makeUnqualified(inputArgumentName));
           
            SourceModel.Expr condition =
                Expr.Application.make(
                        new Expr[]{Expr.Var.make(Name.Function.makeUnqualified("j" + javaClass.getClassName().getUnqualifiedJavaSourceName() + "_getDCOrdinal")),
                                inputArgument});
           
            SourceModel.Expr.Case.Alt caseAlts[] =
                new SourceModel.Expr.Case.Alt[typeConstructor.getNDataConstructors()];
            for (int i = 0, n = caseAlts.length; i < n; ++i) {
                DataConstructor dc = typeConstructor.getNthDataConstructor(i);
                caseAlts[i] =
                    Expr.Case.Alt.UnpackInt.make(
                            new BigInteger[]{BigInteger.valueOf(dc.getOrdinal())},
                            Expr.DataCons.make(Name.DataCons.make(dc.getName())));
            }
           
            SourceModel.Expr.Case body =
                Expr.Case.make(condition, caseAlts);
           
            SourceModel.FunctionDefn.Algebraic inputFunction =
                FunctionDefn.Algebraic.make(
                        inputFunctionName,
                        Scope.PUBLIC,
                        new SourceModel.Parameter[]{Parameter.make(inputArgumentName, false)},
                        body);

            TypeExprDefn inputFunctionType =
                TypeExprDefn.Function.make(
                        TypeExprDefn.TypeCons.make(Name.TypeCons.makeUnqualified(typeConstructorInfo.calForeignTypeName)),
                        TypeExprDefn.TypeCons.make(Name.TypeCons.makeUnqualified(typeConstructor.getName().getUnqualifiedName())));
           
            SourceModel.CALDoc.Comment.Function inputFunctionComment;
            {
                SourceModel.CALDoc.TextSegment.Plain textSegment =
                    SourceModel.CALDoc.TextSegment.Plain.make(
                            "\nInput an instance of " + typeConstructor.getName().getUnqualifiedName() + ".\n" +
                            "Translates an instance of " + typeConstructorInfo.calForeignTypeName + " to\n" +
                            "an instance of "+ typeConstructor.getName().getUnqualifiedName() + ".");
               
                SourceModel.CALDoc.TextBlock textBlock =
                    SourceModel.CALDoc.TextBlock.make(new SourceModel.CALDoc.TextSegment.TopLevel[]{textSegment});
                   
                inputFunctionComment =
                    SourceModel.CALDoc.Comment.Function.make(
                            textBlock,
                            null);
            }
           
            SourceModel.FunctionTypeDeclaration inputFunctionTypeDecl =
                FunctionTypeDeclaration.make(
                        inputFunctionComment,
                        inputFunctionName,
                        TypeSignature.make(inputFunctionType));
           
            topLevelDefnsList.add(inputFunctionTypeDecl);
            topLevelDefnsList.add(inputFunction);
           
            // Create a function which creates a CubeType from a JObject.
            String inputFromJObjectFunctionName = inputFunctionName + "FromJObject";
            createInputFromJObjectFunction(inputFromJObjectFunctionName, inputFunctionName, topLevelDefnsList);
           
            // Now we need to declare an instance of inputable.
            // instance Inputable CubeType where
            //     input = jObjectToCubeType;
            //     ;
            SourceModel.InstanceDefn instanceDefn =
                InstanceDefn.make(
                        Name.TypeClass.make(CAL_Prelude.TypeClasses.Inputable),
                        InstanceDefn.InstanceTypeCons.TypeCons.make(Name.TypeCons.make(typeConstructor.getName()), null),
                        null,
                        new InstanceDefn.InstanceMethod[]{
                            InstanceDefn.InstanceMethod.make(CAL_Prelude.Functions.input.getUnqualifiedName(), Name.Function.makeUnqualified(inputFromJObjectFunctionName))
                        });
           
            topLevelDefnsList.add(instanceDefn);
           
            // Create a conversion function for CAL type to corresponding foreign type.
            // cubeTypeToJCubeType :: CubeType -> JCubeType_;
            // cubeTypeToJCubeType dcInstance =
            //     case dcInstance of
            //     DC1 -> unsafeCoerce jCubeType_DC1;
            //     ...
            //     ;
            condition =
                Expr.Var.makeUnqualified("dcInstance");
           
            caseAlts =
                new SourceModel.Expr.Case.Alt[typeConstructor.getNDataConstructors()];
           
            for (int i = 0, n = caseAlts.length; i < n; ++i) {
                DataConstructor dc = typeConstructor.getNthDataConstructor(i);
                Expr enumFieldFunction =
                    Expr.Var.make(
                        Name.Function.makeUnqualified((String)enumFieldFunctionNames.get(dc.getName().getUnqualifiedName())));
               
                caseAlts[i] =
                    Expr.Case.Alt.UnpackDataCons.make(
                        Name.DataCons.makeUnqualified(dc.getName().getUnqualifiedName()),
                        enumFieldFunction);
                   
            }
           
            body =
View Full Code Here

           
            if (parent.isDocForTypeConsGenerated(typeCons.getName().getModuleName(), typeCons.getName().getUnqualifiedName())) {
                labels.add(labelMaker.getLabel(typeCons));
               
                for (int j = 0, m = typeCons.getNDataConstructors(); j < m; j++) {
                    DataConstructor dataCons = typeCons.getNthDataConstructor(j);
                    if (parent.isDocForDataConsGenerated(dataCons.getName().getModuleName(), dataCons.getName().getUnqualifiedName())) {
                        labels.add(labelMaker.getLabel(dataCons));
                    }
                }
            }
        }
View Full Code Here

        Expression.Var var = spineNode.asVar();
        if (var == null) {
            return null;
        }
          
        DataConstructor dc = var.getDataConstructor ();
       
        if (dc == null) {
            return null;
        }

        // Check that the full number of arguments are supplied.
        int nExpectedArguments = dc.getArity();
           
        if (nExpectedArguments != nArguments) {
            return null;
        }           
       
View Full Code Here

            programFeatureNameSet.add(CALFeatureName.getScopedEntityFeatureName(typeConstructor));
           
            // Data constructors.
            int dataConstructorCount = typeConstructor.getNDataConstructors();
            for (int n = 0; n < dataConstructorCount; n++) {
                DataConstructor dataConstructor = typeConstructor.getNthDataConstructor(n);
                programFeatureNameSet.add(CALFeatureName.getScopedEntityFeatureName(dataConstructor));
            }
        }
       
       
View Full Code Here

       
        List<DataConstructor> dataConsList = new ArrayList<DataConstructor>();
                      
        for (int n = 0, numDataCons = typeCons.getNDataConstructors(); n < numDataCons; n++) {
           
            DataConstructor dataCons = typeCons.getNthDataConstructor(n);
           
            if (currentModuleTypeInfo.isEntityVisible(dataCons)) {
                dataConsList.add(dataCons);
            }
        }
View Full Code Here

        boolean hasVisibleConstructor = false;

        for (int i = 0; i < nDataCons; ++i) {
           
            DataConstructor dataCons = typeCons.getNthDataConstructor(i);

            if (currentModuleTypeInfo.isEntityVisible(dataCons)) {
 
                int arity = dataCons.getArity();
                if (arity > 0) {
                    return false;
                }
                           
                hasVisibleConstructor = true;
View Full Code Here

            // Now we need to generate inner classes for each data constructor.
            // This is only necessary if the data type is not an enumeration.
            if (!typeConstructorInfo.isEnumerationType) {
               
                for (int i = 0, n = typeConstructor.getNDataConstructors(); i < n; ++i) {
                    DataConstructor dc = typeConstructor.getNthDataConstructor(i);
                    JavaClassRep dcClass = new InnerDCClassGenerator().generateInnerDCClass(dc, typeConstructorInfo, typeConstructorClassName);
                   
                    outerTypeDefinition.addInnerClass(dcClass);
                }
            }
View Full Code Here

                assert (fieldTypes.size() == 1);
                commonFieldTypes.put(fn, fieldTypes.iterator().next());
            }
           
            for (int i = 0, n = typeConstructor.getNDataConstructors(); i < n; ++i) {
                DataConstructor dc = typeConstructor.getNthDataConstructor(i);
                for (int j = 0, k = dc.getArity(); j < k; ++j) {
                    FieldName fn = dc.getNthFieldName(j);

                    Map<DataConstructor, String> calAccessorNames = calFieldAccessorFunctionNames.get(fn);
                    if (calAccessorNames == null) {
                        calAccessorNames = new LinkedHashMap<DataConstructor, String>();
                        calFieldAccessorFunctionNames.put(fn, calAccessorNames);
                    }

                    String javaAccessorName = (String)fieldJavaAccessorMethodNames.get(fn);
                   
                    String className;
                    if (commonFieldNames.contains(fn)) {
                        className = javaClassName;
                    } else {
                        className = fixupClassName(dc.getName().getUnqualifiedName());
                    }
                    String calAccessorName = "j" + className + "_" + javaAccessorName;
                    calAccessorNames.put(dc, calAccessorName);
                   
                }
View Full Code Here

                // build up a switch.
                JavaStatement.SwitchStatement switchStatement =
                    new JavaStatement.SwitchStatement(new MethodVariable("ordinal"));
               
                for (int i = 0, n = typeConstructorInfo.typeConstructor.getNDataConstructors(); i < n; ++i) {
                    DataConstructor dc = typeConstructorInfo.typeConstructor.getNthDataConstructor(i);
                   
                    String enumFieldName = createEnumFieldName(dc.getName().getUnqualifiedName());
                    JavaField field = new JavaField.Static(dataType_TypeName, enumFieldName, dataType_TypeName);
                   
                    JavaStatement.SwitchStatement.IntCaseGroup intCase =
                        new SwitchStatement.IntCaseGroup(
                                dc.getOrdinal(),
                                new ReturnStatement(field));
                    switchStatement.addCase(intCase);
                }
               
                // Throw an error if ordinal is outside accepted range.
View Full Code Here

TOP

Related Classes of org.openquark.cal.compiler.DataConstructor

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.