Wiki

Ticket #275: java-jvm-7.patch

File java-jvm-7.patch, 161.2 KB (added by hopscc, 12 years ago)
  • Source/BackEndObjC/ObjcBackEnd.cobra

     
    4747            #m = ObjCModule(filename, verbosity) 
    4848        return m 
    4949 
     50    get baseExeName as String is override 
     51        """Resulting executable file name for compiled file(s) sans extension.""" 
     52        return .compiler.clrBaseExeFileName      
     53     
     54    get fullExeName as String is override 
     55        """Resulting full executable file name (pathname+extn) for compiled file(s).""" 
     56        return .compiler.clrFullExeFileName 
     57 
    5058    def setupRunProcess(baseExe as String, fullExe as String) as Process is override 
    5159        p = Process() 
    52         p.startInfo.fileName = baseExe 
     60        p.startInfo.fileName = fullExe 
    5361        return p 
    5462         
    5563    def setDefaultUseDirectives(ns as NameSpace) is override 
     
    130138 
    131139    #def handleNameSpaceNameCollision(ns as NameSpace, token as IToken, name as String) as NameSpace is override 
    132140 
     141    def getEnumeratorMember(box as Box) as IMember? is override 
     142        """ Find iterator method member in given Box.""" 
     143        return nil  #box.getIteratorMemberObjc 
    133144         
     145    def getEnumeratorMemberType(box as Box, rt as IType) as IType? is override 
     146        """Return type from lookup of iterator 'next' field in given Box.""" 
     147        return nil #    box.getIteratorNextTypeObjc(rt) 
     148     
     149    def validatePrintDestType(t as IType, s as Stmt) is override 
     150        """Validate that the given Type is a valid destination for a print statement redirection.""" 
     151        pass 
     152         
     153         
    134154class GenerateObjcCodePhase inherits Phase 
    135155 
    136156    cue init(c as Compiler) 
  • Source/Compiler.cobra

     
    5555    Novell 
    5656 
    5757 
    58 class BackEnd is abstract  
    59     """ 
    60     Holder for items specific to the backEnd implementation 
    61     """ 
    62      
    63     var _tagToTypeName = Dictionary<of String, String>() 
    64         """ Map from Typename tags used in compiler to backend specific qualified Type name""" 
    65      
    66     cue init(compiler as Compiler) 
    67         base.init 
    68         _name = '' 
    69         _runTimeLibFileName = 'rtl' 
    70         _runTimeLibNativeSourceFileName = 'rtlSrc.ext' 
    71         __compiler = compiler 
    72  
    73     get name from var as String 
    74      
    75     get cobraRuntimeLibFileName from _runTimeLibFileName as String  
    76         """Name of the Cobra RunTime Library file for this backend.""" 
    77  
    78     get runTimeLibNativeSourceFileName from _runTimeLibNativeSourceFileName as String 
    79         """Name of the backEnd source file containing any native code support for Cobra.Core.""" 
    80  
    81     get compiler from __compiler as Compiler 
    82      
    83     get tagToTypeName from var 
    84         """ Map from TypeName tags used in compiler to backend specific qualified TypeName""" 
    85      
    86     def resolveTypeTag(qualifiedNameOrTag as String) as String 
    87         if qualifiedNameOrTag.contains('.')          
    88             qualifiedName = qualifiedNameOrTag 
    89         else     
    90             qualifiedName = .tagToTypeName[qualifiedNameOrTag]  # type tag to backend qualifiedName 
    91         return qualifiedName 
    92          
    93     def makePhases(phases as IList<of Phase>) is abstract 
    94         """ 
    95         Given a list of core phases for compilation complete it with additions (or even removals and 
    96         rearrangements if necessary) for this given backend. 
    97         """ 
    98          
    99     def getRecommendedBuiltInType(parentNameSpace as NameSpace?, name as String) as String? is abstract 
    100         """ 
    101         If parentNameSpace is the main system namespace and name is a backEnd builtin type  
    102         return the Cobra equivalent language type name (or nil).  
    103         """ 
    104          
    105     def computeOutName as String is abstract 
    106         """ 
    107         Return the binary file output name for compilation of files for this backend. 
    108         """ 
    109          
    110     def genNativeModule(filename as String, verbosity as int) as Module? is abstract     
    111         """ 
    112         Check if a filename is a Native module and if so generate and return the Native module type for it 
    113         otherwise return nil. 
    114         """ 
    115      
    116     def setupRunProcess(baseExe as String, fullExe as String) as Process is abstract 
    117         """ 
    118         Create and initialise the process to run the compiled program post compilation 
    119         setup varies on the backend (.Clr/Jvm) and platform. 
    120         """ 
    121      
    122     def setDefaultUseDirectives(ns as NameSpace) is abstract 
    123         """ 
    124         Set the default Use directives into the given Namespace (usually topNamespace) for this backend. 
    125         """ 
    126      
    127     def fixLibExtension(libRef as String) as String is abstract 
    128         """ 
    129         Augment given lib reference string with backend extension if not already have one. 
    130         """ 
    131  
    132     def loadLibReference(reference as String) as String? is abstract 
    133         """ 
    134         Load the given library reference file  using the current backend paradigms.  
    135         Returns an error message, or nil if the reference was loaded correctly. 
    136         """ 
    137      
    138     def readSystemTypes is abstract 
    139         """ Read and Load System Types for backend for Compiler to make available""" 
    140          
    141     def fixMemberSigs is abstract 
    142         """          
    143         Most backends dont natively support nilablility, this marks/fixes the common backend 
    144         class signatures that should be marked nilable. 
    145         """ 
    146      
    147     def installNativeMethods(box as Box, nativeType as NativeType) is abstract 
    148         """ 
    149         Setup so that static methods (on Primitives and some others) get installed/treated as normal methods 
    150         on the instance. 
    151         """ 
    152              
    153     def isRunnableFile( fullExeFileName as String) as bool is abstract 
    154         """Test if given filename is an executable ( vs a library or something else) """ 
    155  
    156     # Native Types access 
    157     def cobraNameForNativeBoxName(name as String) as String is abstract 
    158         """ 
    159         Returns name from backend library entries converted to cobra naming form. 
    160         """ 
    161      
    162     get objectTypeProxy as AbstractTypeProxy is abstract 
    163         """BE TypeProxy for root of back end Object hierarchy.""" 
    164  
    165     get typeTypeProxy as AbstractTypeProxy is abstract 
    166         """BE TypeProxy for BE notion of a class describing a Type.""" 
    167  
    168     def nativeTypeProxy(type as NativeType) as NativeTypeProxy  is abstract 
    169         """ 
    170         Return a Proxy placeHolder for a BackEnd Native Type. 
    171         """ 
    172          
    173     def nativeType(type as dynamic) as NativeType is abstract 
    174         """ 
    175         Return a Native Type wrapped so we can use it without explicitly knowing what it is anywhere 
    176         else but the providing back end. Used by the backends to generate Types from Libraries. 
    177         """ 
    178      
    179     def nativeTypeByName(qualifiedName as String) as NativeType is abstract 
    180         """ 
    181         Return a Native Type corresponding to the fully qualified name. 
    182         Abstract Type Literal tags used in the compiler directly can also be obtained through this 
    183         otherwise the qualified names are expected to conform to the platform back end naming.  
    184         """ 
    185      
    186     def prepSystemObjectClass(box as Box) is abstract 
    187         """Setup additional or convenience members on the System Object class. """ 
    188          
    189     def scanGenericArgs(box as Box) is abstract 
    190         """Scan a loaded Dll (generic) type and translate from native any generic Args.""" 
    191          
    192     def scanNativeType(box as Box) is abstract 
    193         """Scan a loaded Dll type and convert its native Type info to Cobras form """ 
    194          
    195     def scanNativeType(edcl as EnumDecl) is abstract 
    196         """Scan a loaded Dll Enum type and convert its native info to Cobras form """ 
    197          
    198     def setUnderlyingType(edcl as EnumDecl)  is abstract 
    199         """Set underlying Storage Type of the value of an Enum.""" 
    200          
    201     def determineExtnNativeType(extn as Extension, nativeType as NativeType) as NativeType 
    202         """ 
    203         The real extended type is the type of the first argument of any method. 
    204         Find and make that type from the param list. 
    205         """ 
    206         throw Exception('Need implementation of determineExtnNativeType for backend to determine type for an extension') 
    207  
    208     def handleNameSpaceNameCollision(ns as NameSpace, token as IToken, name as String) as NameSpace 
    209         """  
    210             What to do if a namespace name collides with an existing symbol. 
    211             Some backends disallow this situation so its an error. 
    212             Others keep namespace and symbol tables separate so its perfectly 
    213             allowable. 
    214         """ 
    215         throw Exception('In [ns.name] there is a already non-namespace declaration named "[name]".') 
    216      
    217  
    218 class FakeBackEnd inherits BackEnd 
    219     """ Stub BackEnd for tests. """ 
    220      
    221     cue init(compiler as Compiler) 
    222         base.init(compiler) 
    223         _name = 'c#-clr(basic)' 
    224         _runTimeLibFileName = 'cobraRTL.libext' 
    225         _runTimeLibNativeSourceFileName = 'cobraRTL.ext' 
    226          
    227         _tagToTypeName = { 
    228             'Object': 'System.Object', 
    229             'Type'  : 'System.Type', 
    230         } 
    231          
    232     def makePhases(phases as IList<of Phase>) is override 
    233         pass 
    234          
    235     def getRecommendedBuiltInType(parentNameSpace as NameSpace?, name as String) as String? is override 
    236         return nil 
    237  
    238     def computeOutName as String  is override 
    239         return 'FakeOut' 
    240          
    241     def genNativeModule(filename as String, verbosity as int) as Module?  is override 
    242         return nil 
    243      
    244     def setupRunProcess(baseExe as String, fullExe as String) as Process  is override 
    245         p = Process() 
    246         p.startInfo.fileName = 'echo'  
    247         p.startInfo.arguments = 'basic BackEnd process - [baseExe]' 
    248         return p         
    249      
    250     def setDefaultUseDirectives(ns as NameSpace)  is override 
    251         pass 
    252      
    253     def fixLibExtension(libRef as String) as String is override 
    254         if not libRef.endsWith('.libext') 
    255             libRef += '.libext' 
    256         return libRef    
    257      
    258     def loadLibReference(reference as String) as String? is override 
    259         return nil 
    260      
    261     def readSystemTypes is override 
    262         pass 
    263      
    264     def fixMemberSigs is override 
    265         pass 
    266      
    267     def installNativeMethods(box as Box, nativeType as NativeType) is override 
    268         pass 
    269          
    270     def isRunnableFile(fullExeFileName as String) as bool is override 
    271         return true 
    272          
    273     # Native Type access 
    274     def cobraNameForNativeBoxName(name as String) as String is override 
    275         return name + '_BBE' 
    276      
    277     def prepSystemObjectClass(box as Box) is override 
    278         pass 
    279          
    280     def scanGenericArgs(box as Box) is override 
    281         pass 
    282          
    283     def scanNativeType(box as Box) is override 
    284         pass 
    285          
    286     def scanNativeType(edcl as EnumDecl) is override 
    287         pass 
    288          
    289     def setUnderlyingType(edcl as EnumDecl)  is override 
    290         pass 
    291  
    292     # Types 
    293     get objectTypeProxy as AbstractTypeProxy is override 
    294         return ClrTypeProxy(Object) # for testing 
    295              
    296     get typeTypeProxy  as AbstractTypeProxy is override 
    297         return ClrTypeProxy(Type )# for testing 
    298  
    299     def nativeTypeProxy(type as NativeType) as NativeTypeProxy is override 
    300         return ClrTypeProxy(type) #TODO: fix this to something non BE specific Tmp 
    301          
    302     def nativeType(type) as NativeType is override 
    303         return ClrNativeType(type) #TODO: fix this to something non BE specific Tmp 
    304      
    305     def nativeTypeByName(qualifiedName as String) as NativeType is override 
    306         return ClrNativeType(System.Object) #TODO: fix this to something non BE specific Tmp 
    307  
    308  
    30958class Compiler implements ITypeProvider, IWarningRecorder, IErrorRecorder, ICompilerForNodes is partial 
    31059    """ 
    31160    General notes: 
     
    34695    var _intermediateFileNames as List<of String> 
    34796    var _loadedReferences as List<of String> 
    34897 
    349  
    35098    # caches 
    35199    var _primitiveToITypeCache as IDictionary<of dynamic, IType>? # Key is backend type for a Type ( e.g System.Type) 
    352100 
     
    405153        Returns a cache mapping BackEnd types to their corresponding ITypes. 
    406154        Populated and used by the backend TypeProxy implementations ( {Clr,Jvm,...}TypeProxy) 
    407155        """ 
     156         
    408157    set primitiveCache as IDictionary<of dynamic, IType> 
    409158        assert value.count <> 0 
    410159        _primitiveToITypeCache = value 
     
    502251    pro linesCompiled from var as int  
    503252    pro nodesCompiled from var as int  
    504253    pro tokensCompiled from var as int  
     254     
    505255 
     256    get baseExeFileName as String 
     257        """Returns the executable file name sans extension.""" 
     258        return .backEnd.baseExeName 
     259 
     260    get fullExeFileName as String 
     261        """Returns the executable file name path with extension.""" 
     262        return .backEnd.fullExeName 
     263     
    506264    def recordError(error as SourceException) 
    507265        """ 
    508266        Node calls this to record errors. 
     
    13351093 
    13361094    def setOfType as Class 
    13371095        return _libraryClass('Set<of>') 
    1338  
     1096         
     1097    def keyValuePairOfType as Box 
     1098        return _libraryBox('KeyValuePair<of,>')  
     1099         
    13391100    def libraryType(qualifiedNameOrTag as String) as IType 
    13401101        """ 
    13411102        Implemented for ITypeProvider, but use the more specific methods such as .stringType instead. 
     
    13661127        thing as IContainer? = nil 
    13671128        for name in names 
    13681129            possible = (thing ? ns).declForName(name) 
    1369             #print name 
    13701130            # dbg 
    1371             #if not possible 
    1372             #   trace thing ? ns 
    1373             #   ((thing ? ns) to NameSpace).dumpDeclsNameKeys 
     1131            if Utils.isDevMachine and not possible 
     1132                ctnr = (thing ? ns) 
     1133                print 'NAME_LOOKUP_FAILURE: Cannot find "[name]" in namespace/Container "[ctnr.name]"'  
     1134                trace name, ctnr 
     1135                (ctnr to NameSpace).dumpDeclsNameKeys 
    13741136            assert possible, name 
    13751137            if possible inherits IContainer 
    13761138                thing = possible 
     
    14091171    #special type proxies for common Types -  Object and Type 
    14101172    def objectTypeProxy as AbstractTypeProxy  
    14111173        """ 
    1412         Type proxy for BE notion of a class for the root of Object hierarchy 
     1174        Type proxy for backEnd notion of a class for the root of Object hierarchy 
    14131175            e.g. Object in dotNet 
    14141176        """ 
    1415         #return ClrTypeProxy(Object) 
    14161177        return .backEnd.objectTypeProxy 
    14171178 
    14181179    def typeTypeProxy as AbstractTypeProxy  
    14191180        """ 
    1420         Type proxy for BE notion of a class for a Type 
     1181        Type proxy for backEnd notion of a class for a Type 
    14211182            e.g. System.Type in dotNet, java.lang.class in Java 
    14221183        """ 
    1423         #return ClrTypeProxy(Type) 
    14241184        return .backEnd.typeTypeProxy 
    14251185         
    14261186    def nativeType(qualifiedName as String) as NativeType 
  • Source/Expr.cobra

     
    10191019                else 
    10201020                    arg.recordError('Cannot find "[arg.name]" in enumeration "[.name]"') 
    10211021 
     1022        defn = _type to EnumDecl 
     1023        if _args.count > 1 and not defn.isUsedAsSet 
     1024            defn.isUsedAsSet = true 
     1025            _type = defn 
    10221026 
    10231027class ForExpr inherits Expr is partial 
    10241028 
  • Source/Boxes.cobra

     
    600600# TODO: can a for loop go through an IEnumerator<of>? 
    601601#       if .isConstructed and .genericDef is .compiler.enumeratorOfType 
    602602#           return .genericParams[0] 
    603         getEnum as IMember? 
    604         if .declForName('getEnumerator') is nil 
    605             # Comes up for IList<of T> which has multiple 'getEnumerator' methods in ancestor interfaces 
    606             for member in .allMembersForName('getEnumerator') 
    607                 if member inherits Method and member.parentBox.isGeneric 
    608                     getEnum = member 
    609                     break 
    610         if getEnum is nil 
    611             getEnum = .symbolForName('getEnumerator', true) 
     603        getEnum  = .backEnd.getEnumeratorMember(this) # Enumerator/Iterator 
    612604        if getEnum 
    613605            assert getEnum.didBindInt 
     606            # CLR specific  
    614607            # can have two getEnumerators -- one generic and the other not. favor the generic one 
    615608            if getEnum inherits MemberOverload 
    616609                for member in getEnum.members 
     
    619612                            # implementing IEnumerable<of T> which requires two `getEnumerator` members 
    620613                            getEnum = member 
    621614                            break 
    622             rt = getEnum.resultType 
     615            rt as IType? = getEnum.resultType 
    623616            if rt.isDynamicOrPassThrough 
    624617                return rt 
    625618            rt = rt.nonNil  # nilable is not a concern; unwrap it 
    626             if rt inherits Box and (rt to Box).isGeneric 
    627                 # don't take the first argument of the result type -- that won't work for a nested type in a generic class, like ValueCollection, which gets the generic params of its parents 
    628                 rt = rt.memberForName('current').resultType 
    629                 return rt 
    630             else 
    631                 if rt.isDescendantOf(.compiler.dictEnumeratorType) 
    632                     return rt.memberForName('entry').resultType 
    633                 if rt.isDescendantOf(.compiler.enumeratorType) 
    634                     rt = rt.memberForName('current').resultType 
    635                     if rt.nonNil.isSystemObjectClass 
    636                         # can we do better with indexer returnType? (e.g. MatchCollection) 
    637                         indexer = .symbolForName(r'[]', true) 
    638                         if indexer, rt = indexer.resultType 
    639                     return rt 
    640                 else 
    641                     throw FallThroughException({'rt': rt, 'this': this, 'getEnum': getEnum}) 
     619            rt = .backEnd.getEnumeratorMemberType(this, rt to !) # type for method returning enumerator/iterator item 
     620            return rt 
    642621        return nil 
    643  
     622             
    644623    def isEquatableTo(t as IType) as bool 
    645624        r = base.isEquatableTo(t) 
    646625        if not r and t inherits CharType and this is .compiler.stringType  # TODO: hacky? could this be determined by looking for a static == operator overload? 
     
    11031082        return t 
    11041083 
    11051084    def __constructTypeFor(typeArgs as List<of IType>) as Box 
    1106         """ Creates the constructed type. Does not issue any .bindInh, .bindInt, or _prepFoo. """ 
     1085        """ Creates the constructed generic instance type. Does not issue any .bindInh, .bindInt, or _prepFoo. """ 
    11071086        t = .memberwiseClone to Box 
    11081087        assert t is not this 
    11091088        argNames = (for type in typeArgs get type.name).join(',') 
  • Source/Container.cobra

     
    160160                _declsByNameCI[decl.name.toLower] = decl 
    161161            else 
    162162                _declsByNameCI.add(decl.name.toLower, decl) 
    163  
     163                 
     164    def rmDecl(decl as TMember) 
     165        """ 
     166        Drop an existing Decl. 
     167        This is for some platforms with different naming/collision rules than C#/.Net. 
     168        We have detected a collision and choose to handle it by clearing the existing decl   
     169        """ 
     170        require 
     171            decl.name.length 
     172            .declsInOrder.contains(decl) 
     173            .declForName(decl.name) is not nil 
     174        ensure 
     175            not .declsInOrder.contains(decl) 
     176            .declForName(decl.name) is nil 
     177            .declForNameCI(decl.name) is nil 
     178            .declsInOrder.count == old .declsInOrder.count - 1 
     179        body 
     180            _declsInOrder.remove(decl) 
     181            _declsByName.remove(decl.name) 
     182            _declsByNameCI.remove(decl.name.toLower) 
     183         
    164184    def declForName(name as String) as TMember? 
    165185        require 
    166186            name.length 
  • Source/BinaryOpExpr.cobra

     
    808808        base._bindImp 
    809809        _type = .compiler.boolType 
    810810        if not .hasError 
    811             if .right.type.innerType is nil 
     811            if not .right.type inherits EnumDecl and .right.type.innerType is nil 
    812812                .recordError('The right-hand expression for "in" is of type "[.right.type.name]" which contains nothing.') 
    813813            else if .left.type.nonNil inherits CharType and .right.type is .compiler.stringType 
    814814                # special case because there is no String.contains that takes a char 
     
    852852        if contains inherits Method 
    853853            if contains.params.count == 1 and _isAssignableTo(whatType, contains.params[0].type) 
    854854                return true 
     855        if containerType inherits EnumDecl and _isAssignableTo(whatType, containerType), return true 
    855856        return false 
    856857 
    857858    def _isAssignableTo(a as IType, b as IType) as bool 
  • Source/Node.cobra

     
    12261226    def exceptionType as Class 
    12271227    def idictionaryType as Box 
    12281228    def idictionaryOfType as Box 
     1229    def keyValuePairOfType as Box 
    12291230    def ilistType as Box 
    12301231    def ilistOfType as Box 
    12311232    def listOfType as Class 
  • Source/BackEnd.cobra

     
     1""" 
     2Abstract Base class for defining items that have different implementations 
     3    for each back end. 
     4     
     5        Used in Compiler,  
     6        Backends defined/implemented in BackEnd<BE>/<be>BackEnd  
     7            e.g. BackEndJvm/JvmBackEnd.cobra, BackendClr/ClrBackEnd 
     8    Still subject to change.         
     9""" 
     10 
     11use System.Diagnostics 
     12 
     13class BackEnd is abstract  
     14    """ 
     15    Holder for items and actions specific to the backEnd implementation 
     16    """ 
     17     
     18    var _tagToTypeName = Dictionary<of String, String>() 
     19        """ Map from Typename tags used in compiler to backend specific qualified Type name""" 
     20     
     21    get name from _name as String 
     22        """Name of this backend: format is 'language-platform' e.g 'c#-clr'."""  
     23 
     24    get cobraRuntimeLibFileName from _runTimeLibFileName as String  
     25        """Name of the Cobra RunTime Library file for this backend.""" 
     26 
     27    get runTimeLibNativeSourceFileName from _runTimeLibNativeSourceFileName as String 
     28        """Name of the backEnd source file containing any native code support for Cobra.Core.""" 
     29             
     30    get compiler from __compiler as Compiler 
     31     
     32     
     33    cue init(compiler as Compiler) 
     34        base.init 
     35        _name = '' 
     36        # these two are unused defaults, overidden in concrete classes 
     37        _runTimeLibFileName = 'rtl' 
     38        _runTimeLibNativeSourceFileName = 'rtlSrc.ext' 
     39        __compiler = compiler 
     40 
     41    get tagToTypeName from var 
     42        """ Map from TypeName tags used in compiler to backend specific qualified TypeName""" 
     43     
     44    def resolveTypeTag(qualifiedNameOrTag as String) as String 
     45        """Turn qualified type name or a type tag into backEnd className.""" 
     46        if qualifiedNameOrTag.contains('.')          
     47            qualifiedName = qualifiedNameOrTag 
     48        else     
     49            qualifiedName = .tagToTypeName[qualifiedNameOrTag]  # type tag to backend qualifiedName 
     50        return qualifiedName 
     51         
     52    def makePhases(phases as IList<of Phase>) is abstract 
     53        """ 
     54        Given a list of core phases for compilation complete it with additions (or even removals and 
     55        rearrangements if necessary) for this given backend. 
     56        """ 
     57         
     58    def getRecommendedBuiltInType(parentNameSpace as NameSpace?, name as String) as String? is abstract 
     59        """ 
     60        If parentNameSpace is the main system namespace and name is a backEnd builtin type  
     61        return the Cobra equivalent language type name (or nil).  
     62        """ 
     63         
     64    def computeOutName as String is abstract 
     65        """ 
     66        Return the binary file output name for compilation of files for this backend. 
     67        """ 
     68         
     69    get baseExeName as String  
     70        """Resulting Executable file name for compiled file(s) sans extension.""" 
     71        return '' 
     72             
     73    get fullExeName as String 
     74        """Resulting executable file name (pathname+extn) for compiled file(s).""" 
     75        return '' 
     76         
     77    def genNativeModule(filename as String, verbosity as int) as Module? is abstract     
     78        """ 
     79        Check if a filename is a Native module and if so generate and return the Native module type 
     80         for it otherwise return nil. 
     81        """ 
     82     
     83    def setupRunProcess(baseExe as String, fullExe as String) as Process is abstract 
     84        """ 
     85        Create and initialise the process to run the compiled program post compilation 
     86        setup varies on the backend (.Clr/Jvm) and platform. 
     87        """ 
     88     
     89    def setDefaultUseDirectives(ns as NameSpace) is abstract 
     90        """ 
     91        Set the default Use directives into the given Namespace (usually topNamespace) for this backend. 
     92        """ 
     93     
     94    def fixLibExtension(libRef as String) as String is abstract 
     95        """ 
     96        Augment given lib reference string with backend extension if not already have one. 
     97        """ 
     98 
     99    def loadLibReference(reference as String) as String? is abstract 
     100        """ 
     101        Load the given library reference file using the current backend paradigms.  
     102        Returns an error message, or nil if the reference was loaded correctly. 
     103        """ 
     104     
     105    def readSystemTypes is abstract 
     106        """ Read and Load the System Types for this backend for the compiler to make available.""" 
     107         
     108    def fixMemberSigs is abstract 
     109        """          
     110        Most backends dont natively support nilablility, this marks/fixes the common backend 
     111        class signatures that should be marked nilable. 
     112        """ 
     113     
     114    def installNativeMethods(box as Box, nativeType as NativeType) is abstract 
     115        """ 
     116        Setup so that static methods (on Primitives and some others) get installed/treated as  
     117        normal methods on the instance. 
     118        """ 
     119             
     120    def isRunnableFile( fullExeFileName as String) as bool is abstract 
     121        """Test if given filename is an executable ( vs a library or something else) """ 
     122 
     123    # Native Types access 
     124    def cobraNameForNativeBoxName(name as String) as String is abstract 
     125        """ 
     126        Returns name from backend library entries converted to cobra naming form. 
     127        """ 
     128     
     129    get objectTypeProxy as AbstractTypeProxy is abstract 
     130        """Return backEnd TypeProxy for root of back end Object hierarchy.""" 
     131 
     132    get typeTypeProxy as AbstractTypeProxy is abstract 
     133        """Return backEnd TypeProxy for BackEnd notion of a class describing a Type.""" 
     134 
     135    def nativeTypeProxy(type as NativeType) as NativeTypeProxy  is abstract 
     136        """ 
     137        Return a proxy placeHolder for a BackEnd Native Type. 
     138        """ 
     139         
     140    def nativeType(type as dynamic) as NativeType is abstract 
     141        """ 
     142        Return a Native Type wrapped so we can use it without explicitly knowing what it is anywhere 
     143        else but the providing back end. Used by the backends to generate Types from Libraries. 
     144        """ 
     145     
     146    def nativeTypeByName(qualifiedName as String) as NativeType is abstract 
     147        """ 
     148        Return a Native Type corresponding to the fully qualified name. 
     149        Abstract Type literal tags used in the compiler directly can also be obtained through this 
     150        otherwise the qualified names are expected to conform to the platform back end naming.  
     151        """ 
     152     
     153    def prepSystemObjectClass(box as Box) is abstract 
     154        """Setup additional or convenience members on the System Object class. """ 
     155         
     156    def scanGenericArgs(box as Box) is abstract 
     157        """Scan a loaded Dll (generic) type and translate from native any generic Args.""" 
     158         
     159    def scanNativeType(box as Box) is abstract 
     160        """Scan a loaded Dll type and convert its native Type info to Cobras form """ 
     161         
     162    def scanNativeType(edcl as EnumDecl) is abstract 
     163        """Scan a loaded Dll Enum type and convert its native info to Cobras form """ 
     164         
     165    def setUnderlyingType(edcl as EnumDecl)  is abstract 
     166        """Set underlying Storage Type of the value of an Enum.""" 
     167         
     168    def determineExtnNativeType(extn as Extension, nativeType as NativeType) as NativeType 
     169        """ 
     170        The real extended type is the type of the first argument of any method. 
     171        Find and make that type from the param list. 
     172        """ 
     173        throw Exception('Need implementation of determineExtnNativeType for backend to determine type for an extension') 
     174 
     175    def handleNameSpaceNameCollision(ns as NameSpace, token as IToken, name as String) as NameSpace 
     176        """  
     177        What to do if a namespace name collides with an existing symbol. 
     178        Some backends disallow this situation so its an error. 
     179        Others keep namespace and symbol tables separate so its perfectly allowable. 
     180        """ 
     181        throw Exception('In [ns.name] there is a already non-namespace declaration named "[name]".') 
     182     
     183    def getEnumeratorMember(box as Box) as IMember? is abstract 
     184        """Find enumerator/iterator method member in given Box.""" 
     185         
     186    def getEnumeratorMemberType(box as Box, rt as IType) as IType? is abstract 
     187        """ 
     188        Return the type from lookup of enumerator/iterator 'next-item' field for  
     189        provided enumerator/iterator (rt) from iterator for given Box. 
     190        """ 
     191     
     192    def validatePrintDestType(t as IType, s as Stmt) is abstract 
     193        """ 
     194        Validate the given Type as a valid destination for a print statement redirection. 
     195        If not record an error message against the stmt node. 
     196        """ 
     197     
     198         
     199class FakeBackEnd inherits BackEnd 
     200    """ Stub BackEnd for tests. """ 
     201     
     202    cue init(compiler as Compiler) 
     203        base.init(compiler) 
     204        _name = 'c#-clr(test)' 
     205        _runTimeLibFileName = 'cobraRTL.libext' 
     206        _runTimeLibNativeSourceFileName = 'cobraRTL.ext' 
     207         
     208        _tagToTypeName = { 
     209            'Object': 'System.Object', 
     210            'Type'  : 'System.Type', 
     211        } 
     212         
     213    def makePhases(phases as IList<of Phase>) is override 
     214        pass 
     215         
     216    def getRecommendedBuiltInType(parentNameSpace as NameSpace?, name as String) as String? is override 
     217        return nil 
     218 
     219    def computeOutName as String  is override 
     220        return 'FakeOut' 
     221         
     222    def genNativeModule(filename as String, verbosity as int) as Module?  is override 
     223        return nil 
     224     
     225    def setupRunProcess(baseExe as String, fullExe as String) as Process  is override 
     226        p = Process() 
     227        p.startInfo.fileName = 'echo'  
     228        p.startInfo.arguments = 'basic BackEnd process - [fullExe]' 
     229        return p         
     230     
     231    def setDefaultUseDirectives(ns as NameSpace)  is override 
     232        pass 
     233     
     234    def fixLibExtension(libRef as String) as String is override 
     235        if not libRef.endsWith('.libext') 
     236            libRef += '.libext' 
     237        return libRef    
     238     
     239    def loadLibReference(reference as String) as String? is override 
     240        return nil 
     241     
     242    def readSystemTypes is override 
     243        pass 
     244     
     245    def fixMemberSigs is override 
     246        pass 
     247     
     248    def installNativeMethods(box as Box, nativeType as NativeType) is override 
     249        pass 
     250         
     251    def isRunnableFile(fullExeFileName as String) as bool is override 
     252        return true 
     253         
     254    # Native Type access 
     255    def cobraNameForNativeBoxName(name as String) as String is override 
     256        return name + '_BBE' 
     257     
     258    def prepSystemObjectClass(box as Box) is override 
     259        pass 
     260         
     261    def scanGenericArgs(box as Box) is override 
     262        pass 
     263         
     264    def scanNativeType(box as Box) is override 
     265        pass 
     266         
     267    def scanNativeType(edcl as EnumDecl) is override 
     268        pass 
     269         
     270    def setUnderlyingType(edcl as EnumDecl)  is override 
     271        pass 
     272 
     273    # Types 
     274    get objectTypeProxy as AbstractTypeProxy is override 
     275        return ClrTypeProxy(Object) # for testing 
     276             
     277    get typeTypeProxy  as AbstractTypeProxy is override 
     278        return ClrTypeProxy(Type )# for testing 
     279 
     280    def nativeTypeProxy(type as NativeType) as NativeTypeProxy is override 
     281        return ClrTypeProxy(type) #TODO: fix this to something non BE specific Tmp 
     282         
     283    def nativeType(type) as NativeType is override 
     284        return ClrNativeType(type) #TODO: fix this to something non BE specific Tmp 
     285     
     286    def nativeTypeByName(qualifiedName as String) as NativeType is override 
     287        return ClrNativeType(System.Object) #TODO: fix this to something non BE specific Tmp 
     288         
     289    def getEnumeratorMember(box as Box) as IMember? is override 
     290        return nil 
     291         
     292    def getEnumeratorMemberType(box as Box, rt as IType) as IType? is override 
     293        return nil 
     294         
     295    def validatePrintDestType(t as IType, s as Stmt) is override 
     296        pass 
  • Source/BackEndClr/SharpGenerator.cobra

     
    1010 
    1111class Compiler is partial 
    1212 
     13    # executable filenames 
    1314    var _baseExeFileName as String = '' 
    1415    var _fullExeFileName as String = '' 
    1516 
    16  
     17    get clrBaseExeFileName from _baseExeFileName 
     18    get clrFullExeFileName from _fullExeFileName 
     19     
    1720    # The next two properties are set in GenerateSharpCodePhase 
    1821     
    1922    pro nativeCompiler from var = '' 
     
    8487 
    8588    var _didWriteSharpInfoClass as bool 
    8689 
    87     get baseExeFileName from var 
    88         """ 
    89         Returns the exe file name sans extension. 
    90         """ 
    91  
    92     get fullExeFileName from var 
    93         """ 
    94         Returns the exe file name with extension. 
    95         """ 
    96  
    9790    var _platform as PlatformEnum? 
    9891 
    9992    get platform as PlatformEnum 
     
    10396        return _platform to ! 
    10497 
    10598    def computeOutNameSharp as String 
    106         ensure result == .fullExeFileName 
     99        ensure result == _fullExeFileName 
    107100        outName = '' 
    108101        if .options.boolValue('test') 
    109102            outName = _modules.last.sharpFileName 
     
    240233 
    241234            _deleteIntermediateFiles 
    242235             
    243             _copyCore(outName) 
     236            _copyCoreClr(outName) 
    244237 
    245     def _copyCore(outName as String) 
     238    def _copyCoreClr(outName as String) 
    246239        if not .options.boolValue('copy-core'), return 
    247240        sourceDir = Path.getDirectoryName(Assembly.getEntryAssembly.location) ? '' 
    248241        outDir = Path.getDirectoryName(outName) ? '' 
     
    286279                throw StopCompilation(this) 
    287280        _cobraSharpProxy.reset 
    288281 
    289         backEndOptions.add('[optChar]out:[.fullExeFileName]') 
     282        backEndOptions.add('[optChar]out:[_fullExeFileName]') 
    290283 
    291284        args = List<of String>(backEndOptions) 
    292285        args.addRange(sharpFileNameList) 
     
    318311            pass 
    319312        else if File.exists(cscPath+'.exe') 
    320313            cscPath += '.exe' 
    321         backEndOptions += ' "[optChar]out:[.fullExeFileName]"' 
     314        backEndOptions += ' "[optChar]out:[_fullExeFileName]"' 
    322315        p = System.Diagnostics.Process() 
    323316        p.startInfo.fileName = cscPath 
    324317        sharpFileNames = (for fileName in sharpFileNameList get '"' + fileName + '"').join(' ') 
     
    343336        cp.compilerOptions = backEndOptions 
    344337        cp.referencedAssemblies.add('System.dll') 
    345338        cp.generateExecutable = .options.getDefault('target', 'exe') in ['exe', 'winexe'] 
    346         cp.outputAssembly = .fullExeFileName 
     339        cp.outputAssembly = _fullExeFileName 
    347340        if _verbosity >= 2 
    348341            print 'Code Provider =', provider 
    349342            print '    compilerOptions =', backEndOptions 
    350343            print '    referencedAssemblies =', cp.referencedAssemblies 
    351344            print '    generateExecutable =', cp.generateExecutable 
    352             print '    outputAssembly =', .fullExeFileName 
     345            print '    outputAssembly =', _fullExeFileName 
    353346            print '    sharpFileNameList =', sharpFileNameList 
    354347        cr = provider.compileAssemblyFromFile(cp, sharpFileNameList.toArray) 
    355348        if false 
     
    47204713                    sw.write(')') 
    47214714                    if v, sw.write('/* A */') 
    47224715                else 
    4723                     .writeInCheck(sw, false, v) 
     4716                    .writeInCheckSharp(sw, false, v) 
    47244717                    if v, sw.write('/* B */') 
    47254718                sw.write(')') 
    47264719            else 
     
    47464739                    if v, sw.write('/* D */') 
    47474740                sw.write(' })') 
    47484741        else 
    4749             .writeInCheck(sw, parens, v) 
     4742            .writeInCheckSharp(sw, parens, v) 
    47504743            if v, sw.write('/* E */') 
    47514744 
    4752     def writeInCheck(sw as CurlyWriter, parens as bool, v as bool) 
     4745    def writeInCheckSharp(sw as CurlyWriter, parens as bool, v as bool) 
    47534746        if .containsExpr 
    47544747            .containsExpr.writeSharpDef(sw, parens) 
    47554748            if v, sw.write('/* F */') 
  • Source/BackEndClr/ScanClrType.cobra

     
    457457        meth = Method(TokenFix.empty, TokenFix.empty, this, 'getType', List<of Param>(), .compiler.typeTypeProxy, nil, ['shared'], AttributeList(), 'Returns the Type instance that defines this type.') 
    458458        meth.sharedMethodBacking = 'typeof' 
    459459        overload.addMember(meth) 
    460          
     460     
     461    # Enumeration lookup 
     462    def getEnumeratorMemberClr as IMember? 
     463        """ 
     464        Step1 of 2 step lookup for Enumerator type - return getEnumeratorMember for current box  
     465        """ 
     466        getEnum as IMember?  
     467        if .declForName('getEnumerator') is nil 
     468            # Comes up for IList<of T> which has multiple 'getEnumerator' methods in ancestor interfaces 
     469            for member in .allMembersForName('getEnumerator') 
     470                if member inherits Method and member.parentBox.isGeneric 
     471                    getEnum = member 
     472                    break 
     473        if getEnum is nil 
     474            getEnum = .symbolForName('getEnumerator', true) 
     475        if getEnum 
     476            assert getEnum.didBindInt 
     477            # can have two getEnumerators -- one generic and the other not. favor the generic one 
     478            if getEnum inherits MemberOverload 
     479                for member in getEnum.members 
     480                    if member inherits Method 
     481                        if member.resultType <> .compiler.objectType 
     482                            # implementing IEnumerable<of T> which requires two `getEnumerator` members 
     483                            getEnum = member 
     484                            break 
     485        return getEnum 
     486             
     487    def getEnumeratorMemberTypeClr(rt as IType) as IType? 
     488        """ 
     489        Step2 of 2 step lookup for Enumerator type - return the type for Enumerator member for current box  
     490        rt is the resultType from getEnumerator lookup above - i.e the Enumerator for this Box 
     491        """ 
     492        if rt inherits Box and (rt to Box).isGeneric 
     493            # don't take the first argument of the result type -- that won't work for a nested type in a generic class, like ValueCollection, which gets the generic params of its parents 
     494            return rt.memberForName('current').resultType 
     495        else 
     496            if rt.isDescendantOf(.compiler.dictEnumeratorType) 
     497                return rt.memberForName('entry').resultType 
     498            if rt.isDescendantOf(.compiler.enumeratorType) 
     499                rt = rt.memberForName('current').resultType 
     500                if rt.nonNil.isSystemObjectClass 
     501                    # can we do better with indexer returnType? (e.g. MatchCollection) 
     502                    indexer = .symbolForName(r'[]', true) 
     503                    if indexer, rt = indexer.resultType 
     504                return rt 
     505            else 
     506                throw FallThroughException({'rt': rt, 'this': this }) 
     507        return nil 
     508     
     509    # dll scanning 
    461510    def scanNativeTypeClr 
    462511        """ 
    463512        Subclasses should invoke base and then invoke the various _scanFoo methods that are appropriate for them. 
  • Source/BackEndClr/ClrBackEnd.cobra

     
    3636            'IDictionary':      'System.Collections.IDictionary', 
    3737            'IDictionary<of,>': 'System.Collections.Generic.IDictionary<of,>', 
    3838            'Dictionary<of,>' : 'System.Collections.Generic.Dictionary<of,>', 
     39            'KeyValuePair<of,>':'System.Collections.Generic.KeyValuePair<of,>', 
    3940            'Set<of>':          'Cobra.Core.Set<of>', 
    4041 
    4142            'bool'  :  'System.Boolean', 
     
    8687            m = SharpModule(filename, verbosity) 
    8788        return m 
    8889     
     90    get baseExeName as String is override 
     91        """Resulting executable file name for compiled file(s) sans extension.""" 
     92        return .compiler.clrBaseExeFileName 
     93     
     94    get fullExeName as String is override 
     95        """Resulting full executable file name (pathname+extn) for compiled file(s).""" 
     96        return .compiler.clrFullExeFileName 
     97             
    8998    def setupRunProcess(baseExe as String, fullExe as String) as Process is override 
    9099        p = Process() 
    91100        branch .compiler.platform 
     
    114123     
    115124    def fixLibExtension(libRef as String) as String is override 
    116125        """ 
    117         Augment given lib reference string with backend extension if not already have one 
     126        Augment the given library filename reference with backend extension if not already have one. 
    118127        """ 
    119128        or require libRef.length 
    120129        and ensure result.endsWith('.dll') or result.endsWith('.exe') 
     
    143152                .compiler.clrReadAssembly(a) 
    144153        else 
    145154            # TODO: support targeting a specific CLR version, but not below 2.0 
    146             #.compiler.readAssembly(Assembly.load('mscorlib.dll') to !) 
    147155            .compiler.clrReadAssembly(Assembly.load('mscorlib.dll') to !) 
    148156 
    149157            # TODO: .readAssembly(Assembly.loadFrom('System.dll') to !) 
    150158            #       gives: Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.dll' or one of its dependencies. The system cannot find the file specified. 
    151159 
    152160            t = System.Diagnostics.Process.getType 
    153             #.compiler.readAssembly(t.assembly) # System.dll 
    154161            .compiler.clrReadAssembly(t.assembly) # System.dll 
    155162         
    156163    def fixMemberSigs is override 
     
    179186        qualifiedName = .resolveTypeTag(qualifiedNameOrTag) 
    180187        return ClrNativeType(.compiler.clrTypeByName(qualifiedName)) 
    181188        /# 
    182         These are the type tags used directly by the compiler code. 
     189        These are the type tags used directly by cobra code. 
    183190             
    184191            bool        'System.Boolean' 
    185192            char        'System.Char' 
     
    222229        """Clr doesnt allow colliding names.""" 
    223230        throw Exception('In [ns.name] there is a already non-namespace declaration named "[name]".') 
    224231         
    225          
    226          
     232    def getEnumeratorMember(box as Box) as IMember? is override 
     233        """ Find enumerator method member in given Box.""" 
     234        return box.getEnumeratorMemberClr 
     235     
     236    def getEnumeratorMemberType(box as Box, enumResultType as IType) as IType? is override 
     237        """Return type from lookup of enumerator 'current' field from in given Box.""" 
     238        return box.getEnumeratorMemberTypeClr(enumResultType) 
     239     
     240    def validatePrintDestType(t as IType, n as Stmt) is override 
     241        """ 
     242        Check that the given Type is a valid destination for a print statement redirection. 
     243        If not record an error message 
     244        """ 
     245        if  not  t.isDescendantOf(.compiler.libraryType('System.IO.TextWriter')) 
     246            # actually only needs a write/writeLine call 
     247            n.recordError('Invalid destination of type "[t.name]" for "print". Use a TextWriter or subclass thereof instead.') 
     248             
    227249class GenerateSharpCodePhase inherits Phase 
    228250 
    229251    cue init(c as Compiler) 
  • Source/files-to-compile.text

     
    55 
    66CommandLine 
    77TestifyRunner 
     8BackEnd 
    89Compiler 
    910 
    1011KeywordSpecs 
  • Source/BackEndJvm/JvmJarSig.cobra

     
    3636            """ 
    3737            Remap unique virtualised java class name to an actual existing Java class name. 
    3838            """ 
    39  
     39             
     40        def reset 
     41            """Clear all static caches""" 
     42            JarSig.classByNameCache.clear 
     43                 
    4044        def lookupClassByCobraName(fullCobraName as String) as JavaClassType 
    4145            """ 
    4246            This is intended to be the only way to access the JarSig classCache contents from outside this file. 
    4347            """ 
    4448            assert fullCobraName[0].isUpper 
    4549            #fullCobraName = .virtualNameRemaps.get(fullCobraName, fullCobraName) 
    46             parts = fullCobraName.split('.') 
    47             for i in 0: parts.length-1 
    48                 parts[i] = parts[i][0].toLower.toString + parts[i][1:] 
    49             fullName = parts.join('.') 
     50            fullName = _convertCobraName(fullCobraName) 
    5051            #print 'Lookup "[fullCobraName]" as "[fullName]"' 
    5152            if not JarSig.classByNameCache.containsKey(fullName) # dbg 
    5253                print 'Class [fullName] not in JarSig ClassName cache' 
     54                #for k in JarSig.classByNameCache.keys, print k 
    5355                throw Exception('Cannot find class [fullName]') 
    5456            return JarSig.classByNameCache[fullName] 
     57 
     58        def _convertCobraName(fullCobraName as String) as String 
     59            #if fullCobraName.contains('<') # genericInstDef  Aaa.Bbb.C<Jjj.Uuu.C> 
     60            #   genParts = fullCobraName.split('<') 
     61            #   for i in 0 : genParts.length 
     62            #       genParts[i] = _xvertName(genParts[i]) 
     63            #   fullName = genParts.join('<')  
     64            #else    
     65            #   fullName = _xvertName(fullCobraName) 
     66            #return fullName 
     67            # 
     68        #def _xvertName(fullCobraName as String) as String 
     69            #"""Turn names of form Aaa.Bbb.Class to aaa.bbb.Class. Cobra form to java.""" 
     70            parts = fullCobraName.split('.') 
     71            for i in 0: parts.length-1 
     72                parts[i] = parts[i][0].toLower.toString + parts[i][1:] 
     73            fullName = parts.join('.') 
     74            return fullName  
    5575                 
    5676        def lookupClass(fullName as String) as JavaClassType 
    5777            """  
     
    636656            idxrProp =  _genAProp('[.canonicalName]_synthesizedIdxr', getMethod, setMethod, true) 
    637657            _props.add(idxrProp) 
    638658            _indexer = idxrProp 
    639             sb = StringBuilder('= ') 
    640             if getMethod 
    641                 sb.append(getterName) 
    642                 if setMethod, sb.append(',') 
    643             if setMethod 
    644                 sb.append(setterName) 
    645             print 'dbg: [.canonicalName] indexerMethods [sb.toString] ' 
     659            #sb = StringBuilder('= ') 
     660            #if getMethod 
     661            #   sb.append(getterName) 
     662            #   if setMethod, sb.append(',') 
     663            #if setMethod 
     664            #   sb.append(setterName) 
     665            #print 'dbg: [.canonicalName] [.getHashCode] indexerMethods [sb.toString] ' 
    646666        #else 
    647667        #   print 'dbg: No IndexerMethods [getterName]/[setterName] on [.canonicalName]' 
    648                  
     668        #/ 
     669     
    649670    def _lookForMethods( getterName as String, setterName as String) as List<of JavaMethodInfo?> 
    650671        getter as JavaMethodInfo? = nil 
    651672        setter as JavaMethodInfo? = nil 
     
    653674            if m.name == getterName, getter = m 
    654675            if m.name == setterName, setter = m 
    655676        return [getter, setter]  
    656                  
     677     
     678    def lookupIndexer as JavaFieldInfo? 
     679        """ 
     680        Search this class and superclass chain for an indexer. Return first hit or nil. 
     681        Used to map indexing '[]' cobra code to java method calls. 
     682        """ 
     683        #depth=0 
     684        #print depth, .getHashCode, .canonicalName, .superclass 
     685        idxr = .indexer 
     686        if idxr 
     687            return idxr 
     688        super = .getSuperClass      # otherwise try superclass chain 
     689        while super 
     690            idxr = super.indexer 
     691            if idxr  
     692                break 
     693            super = super.getSuperClass 
     694        return idxr 
    657695         
    658696    cue init(name as String, pkg as String, type as JavaType, super as String, ifcs as List<of String>, modifiers as List<of String> ) 
    659697        """Create a normal Class or GenericClass Defn.   MyClass or MyClass<T>""" 
     
    880918        #return  JvmNativeType(_type.getSuperClass) 
    881919        if JarSig.classByNameCache.containsKey(.superclass) 
    882920            return JarSig.lookupClass(.superclass) 
     921        else if .superclass.contains('`') # isGeneric 
     922            key = .superclass.before('`')        
     923            return JarSig.lookupClass(key) 
    883924        else  
    884925            return JarSig.lookupClass('java.lang.Object') 
    885926            # e.g. Abstract base classes with default access - java.lang.AbstractStringBuilder   
     
    10291070        assert false, 'dump Indexers NYI' 
    10301071     
    10311072class JavaMemberInfo 
     1073    """Baseclass for java fields and properties, constructors and methods""" 
    10321074    var name = ''    
    10331075    var _attributes as List<of String>? 
    10341076    var modifiers = List<of String>() # public, default (=protected+pkg), protected, private 
     
    10611103        return .attributes.contains('Override') 
    10621104 
    10631105    get isNonNullable 
     1106        if .isFinal 
     1107            return true # arbitrarily declare final members non nilable till get attributes working. 
    10641108        return .attributes.contains('NotNull') or .attributes.contains('NonNull') or .attributes.contains('Nonnull') 
    10651109        #for name in .attributes # in java these are annotations, - via pkgSig treated as strings 
    10661110        #   if name.endsWith == 'NotNull' or name.endsWith == 'NonNull'  or name.endsWith == 'Nonnull'  
     
    11141158            else if .isReadable, prefix = 'get' 
    11151159            else if .isWritable, prefix = 'set' 
    11161160 
    1117         sb = StringBuilder('[prefix] [.name]') 
     1161        sb = StringBuilder('[prefix].[.name]') 
    11181162        if .typeName.length, sb.append(' as [.type]') 
    11191163        if .modifiers.count  
    11201164            sb.append(' is ') 
  • Source/BackEndJvm/ScanJvmType.cobra

     
    4444            if not sigFilePath 
    4545                # eventually may also search CLASSPATH               
    4646                if rv, print 'Returning false for __jvmLoadReference("[reference]").' 
    47                 return 'to-do: what to put here? sigFilePath not found?'  
     47                return 'to-do: what to put here? sigFilePath "[sigFilePath]" not found?'  
    4848 
    4949            if rv, print 'Will read jarsig: [sigFilePath]' 
    5050            try 
     
    9898 
    9999    def javaReadJar(jSig as JarSig) 
    100100        """ 
    101         Reads the contents of a jar (actually JarSig) file so that they are accessible to the program. 
     101        Reads the contents of a jar (actually JarSig) file and make the symbols accessible  
     102        to the compiler for the program being compiled. 
    102103        """ 
    103104        if jSig.name in _didReadJars, return 
    104105        _didReadJars.add(jSig.name) 
     
    113114        try 
    114115            _modules.add(module) 
    115116            for jvmType in jSig.getExportedTypes 
    116                 if jvmType.isNested #or jvmType.declaringType 
     117                if jvmType.isNested # or jvmType.declaringType 
    117118                    nestedTypes.add(jvmType) 
    118119                    continue 
    119120                typeNamespace = jvmType.packageAsCobra  #Capitalized 
     
    183184         
    184185        _fix('Java.Lang.Object', 'toString getClass clone') 
    185186            # ^ regarding .toString, not technically true, but common enough and life is too painful when the return type is nilable 
    186         #_fix('java.lang.System', 'out') #stdout 
     187        _fix('Java.Lang.System', 'in out err lineSeparator') 
    187188        _fix('Java.Lang.String', r'[] length charAt concat remove replace replaceAll replaceFirst substring toLowerCase toUpperCase trim format') 
    188189        #_fix('System.Type', 'assembly name toString') 
    189190            # namespace can return nil if the Type is a generic parameter 
     
    225226        #_fix('Java.Util.AbstractMap.SimpleEntry<of,>', r'getKey getValue') 
    226227        #_fix('Java.Util.AbstractMap.SimpleImmutableEntry<of,>', r'getKey getValue') 
    227228        _fix('Java.Util.Properties', r'getProperty') 
     229        _fix('Java.Util.Calendar', 'getInstance getTimeZone') 
    228230         
    229231        #_fix('System.IO.File', 'create createText open openRead openText openWrite readAllBytes readAllLines readAllText') 
    230232        _fix('Java.Io.File', 'create createText open openRead openText openWrite readAllBytes readAllLines readAllText getPath getAbsolutePath') 
     
    292294     
    293295    def installJvmNativeMethods(box as Box, nativeType as NativeType)  
    294296        """Install entries for native static methods on box instances.""" 
    295         pass 
     297        pass  #TODO support native static methods on box instances 
    296298        #print '++ TODO scanJvmType.installJvmNativeMethods' 
    297299/# 
    298300        meths = List<of Method>() 
     
    352354        #overload.addMember(meth) 
    353355        .addDecl(meth) 
    354356                 
     357         
     358    def getIteratorMemberJvm as IMember? 
     359        """ 
     360        Step1 of 2 step lookup for iterator/enumerator type - return iterator Member for current box  
     361        """ 
     362        getEnum as IMember?  
     363        if .declForName('iterator') is not nil # Collections 
     364            getEnum = .symbolForName('iterator', true) 
     365            return getEnum 
     366             
     367        if .declForName('entrySet') is not nil # Map 
     368            es = .symbolForName('entrySet', true) 
     369            est = es.resultType.nonNil to Box 
     370            getEnum = est.symbolForName('iterator', true) 
     371        # could look for  Enumerable here also 
     372         
     373        return getEnum 
     374         
     375    def getIteratorNextTypeJvm(rt as IType) as IType? 
     376        """ 
     377        Step2 of 2 step lookup for iterator/Enumerator type - return the type for iterator.next member for current box  
     378        rt is the resultType from iterator lookup above - i.e the Iterator for this Box 
     379        """ 
     380        if rt inherits Box and (rt to Box).isGeneric 
     381            rType = rt.memberForName('next').resultType 
     382        else 
     383            throw FallThroughException('getEnumeratorMemberType - rt not genericBox [rt]') 
     384        return rType 
     385         
    355386    def scanNativeTypeJvm 
    356387        """ 
    357388        Subclasses should invoke base and then invoke the various _scanXXX methods that are appropriate for them. 
     
    550581                        break 
    551582            if skip, continue 
    552583            name = Utils.cobraNameForNativeMemberName(methInfo.name) 
     584            other = .declForName(name) 
    553585            genericParams = List<of IType>() 
    554586            for genArg in methInfo.getGenericArguments 
    555587                genericParams.add(GenericParam(JvmNativeType(genArg))) 
     
    570602                    overload = MemberOverload(other) 
    571603                    .registerOverload(overload to !) 
    572604                else 
    573                     print 'Unexpected overload for name:', name, other 
    574                     throw FallThroughException([this, method, other]) 
     605                    if not other inherits BoxVar 
     606                        print 'Unexpected overload (existing named decl) for method "[name]":', other 
     607                        throw FallThroughException([this, method, other]) 
     608                         
     609                    # here we have a field name (BoxVar) shadowing a method name.  
     610                    # In java this is unambiguous cos all methods are called with () suffix.  
     611                    # In cobra we have to do something else... 
     612                    # currently will choose to drop ( make inaccessible) the field...  
     613                    # ...stupid field begone 
     614                    print 'DUP field "[name]" is shadowing the method "[name]" on [.name],' 
     615                    print '    Dropping the field. It will not be accessible from cobra code' 
     616                    .rmDecl(other) 
     617                 
    575618            if overload 
    576619                overload.addMember(method) 
    577620            else 
     
    653696    def _jvmMemberTypeProxy(jvmType as JavaClassType?, notNull as bool) as ITypeProxy        
    654697        """ 
    655698        Returns a type proxy for a member type such as a parameter type. 
    656         In Java, reference types are nilable by default, but you can pass `true` for `notNull` to indicate there was a NotNull Annotation in the jar metadata. 
     699        In Java, reference types are nilable by default, but you can pass `true` for `notNull` to  
     700        indicate there was a NotNull Annotation in the jar metadata. 
    657701        """ 
    658702        if jvmType is nil 
    659703            return .compiler.voidType 
     
    695739    def _fixNestedNilables 
    696740        if .parentBox  # nested 
    697741            query = '[.parentBox.qualifiedName].[.name]' 
    698             print 'fixNest', query 
     742            #print 'fixNest', query 
    699743            memberNames = _nestedUnNil.get(query, '') 
    700744            if memberNames.length 
    701745                .parentBox.membersToUnNil = memberNames 
     
    789833    def scanNativeTypeJvm 
    790834        # TODO: read attribs 
    791835        _needScanNativeType = false 
    792         _storageType = .compiler.anyIntType # tmp ?? 
     836        _storageType = .compiler.anyIntType # Not relevant ?? 
     837     
     838        #TODO fake up support for values() and valueOf() and getValue() synthesized in codegen 
     839        # - or not - how much of C#/Cobra enum capability to support?? 
    793840         
    794841        /# Enable when have Enum and JavaEnumType in PkgSig and JarSig 
    795842        jvmType = (_nativeType to JvmNativeType).backEndType  
    796843        assert jvmType inherits JavaEnumType # temporary 
    797844        for name, value in jvmType.getConstantValues    # list of names and values 
    798             intValue = value to int 
    799             member = EnumMember(name, intValue) 
     845            member = EnumMember(name, value) 
    800846            member.enumDecl = this 
    801847            .addDecl(member) 
    802848        #/ 
    803849             
    804         /# 
    805         isByte  = Enum.getUnderlyingType(jvmType).name == 'Byte' 
    806         is64    = Enum.getUnderlyingType(jvmType).name == 'Int64' 
    807         isU32   = Enum.getUnderlyingType(jvmType).name == 'UInt32' 
    808         isU64   = Enum.getUnderlyingType(jvmType).name == 'UInt64' 
    809         values  = Enum.getValues(jvmType) 
    810         i = 0 
    811         for name in Enum.getNames(jvmType) 
    812             value = values.getValue(i) 
    813             # CC: lameness follows 
    814             if isByte 
    815                 intValue = int.parse((value to uint8).toString) 
    816             else if is64 
    817                 try 
    818                     intValue = int.parse((value to int64).toString) 
    819                 catch OverflowException 
    820                     intValue = 999  # CC: omg. but probably doesn't affect anything. we're reading the DLL here, not creating one 
    821             else if isU32 
    822                 try 
    823                     intValue = int.parse((value to uint32).toString) 
    824                 catch OverflowException 
    825                     intValue = 2147483647 
    826             else if isU64 
    827                 try 
    828                     intValue = int.parse((value to uint64).toString) 
    829                 catch OverflowException 
    830                     intValue = 2147483647 
    831             else 
    832                 intValue = value to int 
    833             member = EnumMember(name, intValue) 
    834             member.enumDecl = this 
    835             .addDecl(member) 
    836             i += 1 
    837850#/       
    838851 
    839852 
  • Source/BackEndJvm/JvmBackEnd.cobra

     
    1717            'Object': 'Java.Lang.Object', 
    1818            'Type'  : 'Java.Lang.Class<of>',    # java.lang.Type is a marker interface 
    1919            'String'    :       'Java.Lang.String', 
    20             'Exception' :       'Java.Lang.RuntimeException', 
     20            #'Exception' :      'Java.Lang.RuntimeException', 
     21            'Exception' :       'Java.Lang.Exception', 
    2122 
    22             'Delegate'  :       'Cobra.Core.Delegate', 
     23            'Delegate'  :       'Cobra.Core.DelegateO', 
     24            #'Delegate<of>' :   'Cobra.Core.Delegate<of>', 
    2325            'Attribute' :       'Java.Lang.Annotation.Annotation', 
    2426            'ICloneable':       'Java.Lang.Cloneable',  
    2527            'IEnumerable':      'Java.Lang.Iterable', 
    2628            'IEnumerator':      'Java.Lang.Iterator', 
    2729            'IEnumerable<of>' : 'Java.Lang.Iterable<of>', 
    2830            'IEnumerator<of>' : 'Java.Lang.Iterator<of>', 
    29             'IDictionaryEnumerator' : 'Java.Lang.Iterator', 
    30             #'IDictionaryEnumerator' : 'System.Collections.IDictionaryEnumerator', 
    31             'ICollection':      'Java.Util.Collection', # non generic collection interface 
     31            'IDictionaryEnumerator' : 'Java.Lang.Iterator', # non generic Dictionary enumer interface - Non existant 
     32            'ICollection':      'Java.Util.Collection',     # non generic collection interface Non existant in java 
    3233            'ICollection<of>':  'Java.Util.Collection<of>', 
    3334            'IList' :           'Java.Util.List<of Object>',    # non generic List interface 
    3435            'IList<of>' :       'Java.Util.List<of>', 
    3536            'List<of>':         'Java.Util.ArrayList<of>', 
    36             'IDictionary':      'Java.Util.Map<of Object,Object>', # Non Generic Map/Dict interface 
     37            #'IDictionary':     'Java.Util.Map<of Object,Object>', # Non Generic Map/Dict interface 
     38            'IDictionary':      'Java.Util.Map<of,>', # Non Generic Map/Dict interface  
    3739            'IDictionary<of,>': 'Java.Util.Map<of,>', 
    3840            'Dictionary<of,>' : 'Java.Util.HashMap<of,>', 
     41            'KeyValuePair<of,>':'Java.Util.Map<of,>.Entry<of,>', 
    3942            'Set<of>':          'Java.Lang.HashSet<of>', 
    4043 
    4144            'bool'  :  'Java.Lang.Boolean',  # boolean 
     
    100103            m = JavaModule(filename, verbosity) 
    101104        return m 
    102105         
     106    get baseExeName as String is override 
     107        """Resulting Executable file name for compiled file(s) sans extension.""" 
     108        return .compiler.javaMainClass 
     109     
     110    get fullExeName as String is override 
     111        """Resulting executable file name (pathname+extn) for compiled file(s).""" 
     112        return .compiler.fullJarFileName 
     113             
    103114    def setupRunProcess(baseExe as String, fullExe as String) as Process is override 
     115        """ 
     116        Setup compiled java process to run assuming using an executable jar file. 
     117        All info needed should be already set in jarfile manifest - i.e main entry point  
     118        and any classpath dependencies 
     119        """ 
    104120        p = Process() 
    105121        p.startInfo.fileName = 'java' 
     122        # below is same as  
     123        #args = ' -jar [.compiler.fullJarFileName]' 
     124        args = ' -jar [fullExe]' 
     125     
     126        p.startInfo.arguments = args + ' ' 
     127        #print p.startInfo.fileName, p.startInfo.arguments 
     128        return p         
     129     
     130    def setupRunProcessClass(baseExe as String, fullExe as String) as Process #is override 
     131        """ 
     132        Setup running a java class file(s), using CobraCore.jar and assuming jarfiles 
     133        needed set in CLASSPATH env var ('.') 
     134        Superseded by setupRunProcess above.     
     135        """ 
     136        p = Process() 
     137        p.startInfo.fileName = 'java' 
    106138        clPath = Path.getDirectoryName(CobraCore.exePath) to ! 
    107139        clPath = Path.combine(clPath, 'CobraCore.jar') 
    108140        classpath = Environment.getEnvironmentVariable('CLASSPATH') 
     
    115147        p.startInfo.arguments = args + ' ' 
    116148        #print p.startInfo.fileName, p.startInfo.arguments 
    117149        return p         
    118      
     150 
    119151    def setDefaultUseDirectives(ns as NameSpace) is override 
    120152        # java packages available by default 
    121153        useToken = Token('(implicit)', 1, 1, 1, 'USE', 'use', nil) 
     
    153185     
    154186    def readSystemTypes is override 
    155187        # Initially we will rely on a external java tool to have been run to produce a precis 
    156         # file in a known place (same as cobra compiler perhaps) containing exported types for 
     188        # file in a known place (same as cobra compiler) containing exported types for 
    157189        # items in a jar file. 
    158190         
    159191        # JarSig is a holder for a jarfile or package extended with info on contained classes. 
    160192        # javaReadJar loads info on contained classes into Cobra 
    161193        # rt.jar is only one we need 
     194        JarSig.reset 
    162195        .compiler.javaReadJar(JarSig('rt.jar')) 
    163196     
    164197    def fixMemberSigs is override 
     
    178211 
    179212    # Types 
    180213    get objectTypeProxy as AbstractTypeProxy  is override 
    181         """Type proxy for BE root of Object hierarchy.""" 
     214        """Type proxy for backEnd root of Object hierarchy.""" 
    182215        if  not _objectTypeProxy 
    183216            _objectTypeProxy = JvmTypeProxy(JarSig.lookupClassByCobraName('Java.Lang.Object')) 
    184217        return _objectTypeProxy to ! 
    185218             
    186219    get typeTypeProxy  as AbstractTypeProxy  is override 
    187         """Type proxy for BE notion of a class describing a Type.""" 
     220        """Type proxy for BackEnd notion of a class describing a Type.""" 
    188221        if  not _typeTypeProxy 
    189222            _typeTypeProxy = JvmTypeProxy(JarSig.lookupClassByCobraName('Java.Lang.Class')) 
    190223        return _typeTypeProxy to !   
     
    196229        return JvmNativeType(type) 
    197230     
    198231    def nativeTypeByName(qualifiedNameOrTag as String) as NativeType is override 
    199         #map qualified name of .net aliased types  to java equivalents 
     232        # map the qualified name of .net aliased types  to java equivalents 
    200233        # bool, char, decimal, single, double, sbyte, int{16,32,64}, byte, uint{16,32,64},  
    201234        qualifiedName = .resolveTypeTag(qualifiedNameOrTag) 
    202235        return JvmNativeType(.compiler.jvmTypeByName(qualifiedName)) 
     
    235268        #print 'Any use of the namespace in cobra code must be changed accordingly.' 
    236269 
    237270        #print 'Cobra name collision in namespace "[ns.name]" for child namespace "[name]" and an existing declaration. Use name "[name]NS" for java namespace/package "[name]" .' 
    238         print 'In namespace "[ns.name]" java namespace "[name]" renamed in cobra to "[name]NS". (cobra name collision)' 
     271        print 'Java "[ns.name].[name]" renamed in cobra to "[ns.name].[name]NS". (cobra name collision)' 
    239272        name = name+'NS' 
    240273        return ns.getOrMakeNameSpaceNamed(token, name) # recursive 
    241274         
     
    254287         
    255288    def setUnderlyingType(edcl as EnumDecl)  is override 
    256289        edcl.setUnderlyingTypeJvm 
     290     
     291    def getEnumeratorMember(box as Box) as IMember? is override 
     292        """ Find iterator method member in given Box.""" 
     293        return box.getIteratorMemberJvm 
    257294         
     295    def getEnumeratorMemberType(box as Box, rt as IType) as IType? is override 
     296        """Return type from lookup of iterator 'next' field in given Box.""" 
     297        return box.getIteratorNextTypeJvm(rt) 
     298 
     299    def validatePrintDestType(t as IType, n as Stmt) is override 
     300        """ 
     301        Check that the given Type is a valid destination for a print statement redirection. 
     302        If not record an error message 
     303        """ 
     304        t = t.nonNil 
     305        if  t.isDescendantOf(.compiler.libraryType('Java.Io.Writer')) 
     306            return  # commonly PrintWriter or StringWriter 
     307        if  t.isDescendantOf(.compiler.libraryType('Java.Io.PrintStream')) 
     308            return  # for System.out 
     309        n.recordError('Invalid destination of type "[t.name]" for "print". Use a Java.Io.Writer or subclass thereof (PrintWriter, StringWriter) instead.') 
     310             
     311                 
    258312class GenerateJavaCodePhase inherits Phase 
    259313 
    260314    cue init(c as Compiler) 
  • Source/BackEndJvm/JavaGenerator.cobra

     
    1111## 
    1212 
    1313class Compiler is partial 
     14    var _javaMainClass as String = '' 
     15    var _fullJarFileName as String = '' 
     16     
     17    get javaMainClass from var 
     18    get fullJarFileName from var 
     19     
    1420 
    1521    # run prior to compilation to determine an output filename for the java source 
    16     # for testing for need for compilation. 
     22    # for testing, for need for compilation. 
    1723    # Filename used may be determined later post AST generation using the first classname in file 
    1824    # see 'fixup output filename' in compileJava below 
    1925    def computeOutNameJava as String 
     
    2531        if outName == '', outName = .defaultOutName to ! 
    2632        if outName.endsWith('.java') or outName.endsWith('.JAVA'), outName = outName[:-5] 
    2733        if outName.endsWith('.cobra') or outName.endsWith('.COBRA'), outName = outName[:-6] 
    28         _baseExeFileName = outName 
     34        _javaMainClass = outName 
    2935 
    30         outName = Utils.forceExtension(outName, '.class') 
    31         _fullExeFileName = outName 
     36        #outName = Utils.forceExtension(outName, '.class') #v1 
     37        outName = Utils.forceExtension(outName, '.jar')      
     38        _fullJarFileName = outName 
    3239        return outName 
    3340         
    3441    def writeJava 
    3542        Node.setCompiler(this) 
    3643        try 
    37             #pass 
    3844            _moduleFileName_to_curlyToCobraLineNum = Dictionary<of String, Dictionary<of int, int>>() 
    3945            for _curModule in _modules 
    4046                if not _curModule.fileName.endsWith('SystemInterfaces.cobra') 
    4147                    javaToCobraLineNum = _curModule.writeJavaDef 
    42                     _moduleFileName_to_curlyToCobraLineNum[_curModule.fileName] = javaToCobraLineNum 
    43                     _moduleFileName_to_curlyToCobraLineNum[Path.getFullPath(_curModule.fileName)] = javaToCobraLineNum 
     48                    .storeJavaFileInfo(_curModule.fileName, javaToCobraLineNum) 
    4449        finally 
    4550            Node.setCompiler(nil) 
    46  
     51             
     52    def storeJavaFileInfo(fileName as String, javaToCobraLineNum as Dictionary<of int, int>?) 
     53        # called above and fm Namespace.writeJavaDef after new class file 
     54        _moduleFileName_to_curlyToCobraLineNum[fileName] = javaToCobraLineNum 
     55        _moduleFileName_to_curlyToCobraLineNum[Path.getFullPath(fileName)] = javaToCobraLineNum 
     56             
    4757    def writeJavaTestInvocation 
    4858        # TODO 
    4959        print 'TODO: writeJavaTestInvocation' 
     
    5969            options = .options 
    6070 
    6171            # locate the Java compiler 
    62             # TODO: This just does files compilation, augment to also generate manifest and executable jarfile 
    6372            # TODO: augment to use Java library compiler when Java native 
    6473            compilerPath = options.getDefault('native-compiler', 'auto') to String 
    6574            if compilerPath <> 'auto' 
     
    7180                    print 'Cannot find compiler specified by -native-compiler argument: [compilerPath]' 
    7281                    throw StopCompilation(this) 
    7382            else 
    74                 # TODO use a javatool - wrap javac and jarfile gen for multiple files        
    75                 # javaTool -m MainClass -o outFile file-list 
    7683                compilerPath = if(Utils.isRunningOnUnix, 'javac', 'javac.exe') 
    7784                 
    7885            optChar = '-' 
     
    110117                    nativeArgs = nativeArgs[1:-1] 
    111118                backEndOptions += ' ' + nativeArgs 
    112119 
    113             #sep = if(CobraCore.isRunningOnUnix, ':', ';') # after next snapshot 
    114             cpSep = ';' 
    115             if (Environment.osVersion.platform to int) in [4, 6, 128]  # http://www.mono-project.com/FAQ:_Technical 
    116                 cpSep = ':' 
     120            cpSep = if(CobraCore.isRunningOnUnix, ':', ';') 
    117121 
    118122            libclassPath = '' 
    119123            for libPath in .options.getStringList('library-directory') 
     
    123127            # explicitly add reference to it until we get setup to install a copy in extdirs 
    124128            ccPath = Path.getDirectoryName(CobraCore.exePath) to ! 
    125129            ccPath = Path.combine(ccPath, 'CobraCore.jar') 
    126             backEndOptions += ' -classpath "[ccPath][libclassPath]"' 
     130            classPath = '[ccPath][libclassPath]' 
     131            backEndOptions += ' -classpath "[classPath]"' 
    127132                 
    128             # TODO: add libPaths/classpaths for referenced namespaces 
     133            # TODO: add libPaths/classpaths for any referenced namespaces 
    129134             
    130135            # .java files fm first class name in each module 
    131             javaFileNameList = List<of String>() 
     136            javaFileNameSet = Set<of String>() 
    132137            for module in _modules[1:] 
    133138                if module.javaFileName.length 
    134                     javaFileNameList.add(module.javaFileName) 
    135  
     139                    javaFileNameSet.add(module.javaFileName) 
     140            #javaFileNameList = _intermediateFileNames       
     141            for name in _intermediateFileNames       
     142                if name.length, javaFileNameSet.add(name) 
     143            javaFileNameList = List<of String>(javaFileNameSet) 
     144                     
     145            #trace javaFileNameList, _intermediateFileNames      
    136146            # fixup javaMain pkg path all lower case 
    137147            # fixup output filename (classfile) to be in sync with Java filename 
    138148            javaMain = _getFixedMainType 
    139149            if outName <> javaFileNameList[0] 
    140150                outName = javaFileNameList[0] 
    141                 #_baseExeFileName = Path.changeExtension(outName, nil) to ! # remove any extn 
    142                 _baseExeFileName = javaMain 
     151                _javaMainClass = javaMain 
    143152                outName = Utils.forceExtension(outName, '.class') 
    144                 _fullExeFileName = outName 
     153 
     154            if .defaultOutName 
     155                _fullJarFileName = Utils.forceExtension(.defaultOutName to !, '.jar') 
    145156                         
    146157            # compilation command 
    147158            if _verbosity 
     
    150161                print 'compiler =', compilerPath 
    151162                print '    compilerOptions =', backEndOptions 
    152163                print '    nativeArgs =', nativeArgs 
    153                 print '    outName =', .fullExeFileName 
     164                print '    fullExeFileName=',  _fullJarFileName 
    154165                print '    baseExeFileName =', .baseExeFileName 
    155166                print '    javaFileNameList =', javaFileNameList 
    156  
    157             output = _execAndCapture(compilerPath, backEndOptions, javaFileNameList) 
    158             _parseJavaCompilerOutput(output) 
    159  
     167                print '    defaultOutName =', .defaultOutName 
     168                 
     169            #output = _execAndCapture(compilerPath, backEndOptions, javaFileNameList) 
     170            #_parseJavaCompilerOutput(output) 
     171            _javaTool(compilerPath, backEndOptions, classPath, javaFileNameList) 
     172                 
    160173            if .errors.count 
    161174                _exitFromErrors 
    162175 
    163176            _deleteIntermediateFiles 
     177            _copyCoreJvm(outName) 
     178 
     179    def _copyCoreJvm(outName as String) 
     180        if not .options.boolValue('copy-core'), return 
     181        sourceDir = Path.getDirectoryName(Assembly.getEntryAssembly.location) ? '' 
     182            # above should be something non platform dependent (COBRA_HOME ?)  
     183        outDir = Path.getDirectoryName(outName) ? '' 
     184        _copyFile(sourceDir, outDir, 'Cobra.Core.jar') 
     185        #if .options.getDefault('debug', '') not in ['', '-', '0', 0, false] 
     186        # copy any debug files 
     187        # TODO: probably also need to adjust Manifest File Class-Path for cobra RTL 
    164188             
    165     # Better would be options as optionList as List<of String> but ftm we're stuck with space joined strings 
    166     # coming into caller 
     189    def _javaTool(javacPath as String, options as String, classPath as String, fileNameList as List<of String>) 
     190        """ 
     191        javatool - wrap executing javac and executable jarfile generation for single or  
     192            multiple files       
     193        """ 
     194        # clean directory for java compiled cobraClasses 
     195        classDir = 'cobraClasses' 
     196        if Directory.exists(classDir) 
     197            Directory.delete(classDir, true) 
     198        Directory.createDirectory(classDir) 
     199             
     200        # put compiled classes in own subdir 
     201        options = options.replace('-d .', '-d [classDir]') 
     202 
     203        # run java compiler on java srcfiles 
     204        output = _execAndCapture(javacPath, options, fileNameList) 
     205        _parseJavaCompilerOutput(output) 
     206        if _errors.count, return 
     207         
     208        # Make manifest file - entry for Class-Path: <classPath> 
     209        cpSep = if(CobraCore.isRunningOnUnix, ':', ';') 
     210        manFileName = 'in.mf'    
     211        mfClassPath = classPath.replace(cpSep, ' ') # classpath entries space separated 
     212        mfClassPath = mfClassPath.replace('C:',' ') # cant handle drive designators 
     213        mfText = 'Class-Path: [mfClassPath] [Environment.newLine]' 
     214        try 
     215            File.writeAllLines(manFileName, [mfText].toArray) 
     216        catch exc as Exception 
     217            .warning(CobraWarning(manFileName, nil, 'Cannot write manifest file due to: [exc.message] ([exc.getType.name]).')) 
     218         
     219        # make jarfile 
     220        _intermediateFileNames.add(manFileName) 
     221        #outName = Utils.forceExtension(.defaultOutName to !, '.jar') 
     222        outName = _fullJarFileName 
     223        mainClass = _javaMainClass 
     224        #print  'jar cfem [outName] [.baseExeFileName] [manFileName] -C [classDir] .' 
     225        output = _execAndCapture('jar', 'cfem [outName] [mainClass] [manFileName] -C [classDir]', ['.']) 
     226        #print output # TODO parse this for errs 
     227         
     228    # Better would be options as optionList as List<of String> but ftm we're stuck with space  
     229    # joined strings coming into caller 
    167230    # TODO: move this to CobraCore to avoid .NET dependency 
    168231    def _execAndCapture(binaryPath as String, options as String, fileNameList as List<of String>) as String 
    169232        p = System.Diagnostics.Process() 
     
    173236        p.startInfo.arguments = '[options] [fileNames]' 
    174237        if _verbosity >= 2 
    175238            print '[p.startInfo.fileName] [p.startInfo.arguments]' 
    176         output = p.runAndCaptureAllOutput 
     239        try 
     240            output = p.runAndCaptureAllOutput 
     241        catch exc as System.ComponentModel.Win32Exception 
     242            print 'Error: Compiler file [p.startInfo.fileName] not found in path' 
     243            throw exc 
    177244        # TODO: check p.exitCode, especially if output is empty 
    178245        return output        
    179246             
     
    204271            javaMain = '[javaPkg].[exeParts.last]'   
    205272        #trace .mainMethodTypeName, javaMain 
    206273        return javaMain  
     274     
     275    #hoisted from CobraModule 
     276    def newJavaFile( cobraModuleName as String, javaFileName as String) as CurlyWriter 
     277        #print 'newJavaFile [javaFileName]' 
     278        file = File.createText(javaFileName) 
     279        sw = CurlyWriter(file, CurlyLineNumberTreatment.Comment) 
     280        .addIntermediateFile(javaFileName) 
     281        sw.start(cobraModuleName) 
     282        sw.write('// [javaFileName] from [cobraModuleName]\n') 
     283        sw.write('// Generated by Cobra-[CommandLine.versionString]\n') 
     284        sw.write('// on [DateTime.now]\n') 
     285        # TODO: list op sys 
     286        sw.write('\n') 
     287        return sw 
     288     
     289         
    207290## 
    208291## Node 
    209292## 
     
    328411        return classFileName 
    329412                         
    330413    def writeJavaDef as Dictionary<of int, int>? is override 
    331         file = File.createText(.javaFileName) 
    332         using sw = CurlyWriter(file, CurlyLineNumberTreatment.Comment) 
    333             .compiler.addIntermediateFile(_javaFileName) 
    334             sw.start(.fileName) 
    335             sw.write('// [_javaFileName] from [_fileName]\n') 
    336             sw.write('// Generated by Cobra-[CommandLine.versionString]\n') 
    337             sw.write('// on [DateTime.now]\n') 
    338             # TODO: list op sys 
    339             sw.write('\n') 
    340  
     414        using sw = (.compiler to Compiler).newJavaFile(.fileName, .javaFileName) 
    341415            if .isMainWrapper    
    342416                .synthesizeJavaMainWrapper(sw) 
    343417            else 
    344418                .topNameSpace.writeJavaDef(sw) 
    345419            d = sw.curlyToCobraLineNum 
    346420        return d 
    347  
     421         
    348422    def writeJavaTestInvocation(sw as CurlyWriter) is override 
    349423        # TODO: .topNameSpace.writeJavaTestInvocation(sw) 
    350424        pass 
     
    422496        return .javaRef 
    423497 
    424498    get _computeJavaRef as String 
    425         if .parent 
    426             s = .parent.javaRef 
     499        if .parent  
     500            if .parent.javaRef <> 'global' # no global (default) namespace in java 
     501                s = .parent.javaRef # ?'global'? 
     502                s += '.' 
    427503            s += .javaName 
    428504        else 
    429505            s = .javaName 
     
    480556 
    481557    def writeJavaDef(sw as CurlyWriter) 
    482558        """ 
    483         Write the Java code for this member declaration to the given CurlyWriter. 
     559        Write the Java code for this member definition/declaration to the given CurlyWriter. 
    484560        """ 
    485561 
    486562    def writeJavaTestInvocation(sw as CurlyWriter) 
     
    523599    def writeJavaDef(sw as CurlyWriter) is override 
    524600        assert not .isUnified 
    525601        base.writeJavaDef(sw) 
     602        _writeNameSpaceHeading(sw) 
     603         
     604        # Top Level classes + Interfaces in a Namespace 
     605        d0 = _declsInOrder[0] 
     606        firstclass = true 
     607        for decl in _declsInOrder 
     608            isNames = List<of String>() 
     609            if decl inherits Box  
     610                .compiler.boxStack.push(decl) 
     611                isNames.add(decl.defaultAccessLevel) 
     612            isNames.addRange(decl.isNames) 
     613            if firstclass or decl == d0 or not 'public' in isNames 
     614                decl.writeJavaDef(sw) 
     615                firstclass = if(decl inherits EnumDecl, true, false) 
     616            else  
     617                # java - compile error if public classes are not in their own file - a pox on whoever made this unsuppressable 
     618                javaFileName = decl.name + '.java' 
     619                # make it look like this class was made in its own cobra module  
     620                # TODO insert Module name in new Java file header 
     621                cobraModName = decl.name + '.cobra'  
     622                #trace javaFileName, cobraModName 
     623 
     624                compiler =(.compiler to Compiler) 
     625                using sw = compiler.newJavaFile(cobraModName, javaFileName) 
     626                    _writeNameSpaceHeading(sw) 
     627                    decl.writeJavaDef(sw) 
     628                    javaToCobraLineNum = sw.curlyToCobraLineNum 
     629                    compiler.storeJavaFileInfo(cobraModName, javaToCobraLineNum) 
     630             
     631            if decl inherits Box, .compiler.boxStack.pop 
     632 
     633    def _writeNameSpaceHeading(sw as CurlyWriter) 
    526634        if not .isRoot 
    527             javaName = .name.toLower # java idiom is all lowercase for packages 
    528             sw.write('package [javaName][.javaSuffix]; \n\n') 
     635            pkgName = .name.toLower # java idiom is all lowercase for packages 
     636            sw.write('package [pkgName][.javaSuffix]; \n\n') 
    529637        for ud in _useDirectives  # imports 
    530638            ud.writeJavaDef(sw) 
    531         for decl in _declsInOrder 
    532             if decl inherits Box 
    533                 .compiler.boxStack.push(decl) 
    534             decl.writeJavaDef(sw) 
    535             if decl inherits Box 
    536                 .compiler.boxStack.pop 
    537         # TODO 
    538         #if not .isRoot 
    539         #   sw.dedentAndWrite('} // namespace [.name]\n') 
    540  
     639         
    541640    def writeJavaTestInvocation(sw as CurlyWriter) is override 
    542641        for decl in _declsInOrder 
    543642            decl.writeJavaTestInvocation(sw) 
     
    590689    get javaNameComponent as String 
    591690        """ 
    592691        Returns a string that refers to this type and is suitable for embedding in a larger 
    593         identifier (meaning there will be no punction, spaces or Java comments). 
     692        identifier (meaning there will be no punctuation, spaces or Java comments). 
    594693        """ 
    595694 
    596695    get javaRef as String 
     
    778877## Enums 
    779878## 
    780879 
    781 # TODO 
     880class EnumDecl is partial 
     881     
     882    get javaRef as String is override 
     883        if .isUsedAsSet, return 'java.util.EnumSet<[base.javaRef]>'  
     884        return  base.javaRef 
     885             
     886    get javaRefBase as String 
     887        return base.javaRef 
    782888 
     889    get javaInit as String is override 
     890        if .isUsedAsSet, return 'java.util.EnumSet.noneOf([base.javaRef].class)'  
     891        return base.javaRef + '.' + _declsInOrder[0].name 
    783892 
     893    def writeJavaDef(sw as CurlyWriter) is override 
     894        base.writeJavaDef(sw) 
     895        if 'extern' in _isNames 
     896            return 
     897        for attr in .attributes 
     898            attr.writeJavaDef(sw) 
     899        if not _isNames.count, _isNames.add('internal') # to get nothing 
     900        .writeJavaIsNames(sw) 
     901        sw.write('enum [_name]') 
     902        /# we dont support storageType in java  
     903        storageType = _storageType ... 
     904        #/ 
     905        sw.write(' {\n') 
     906        sw.indent 
     907        sep = '' 
     908        i = 0 
     909        for em as EnumMember in .declsInOrder 
     910            sw.write(sep) 
     911            sw.write(em.name) 
     912            if em.value 
     913                sw.write('([em.value])') 
     914                complex = true 
     915            i += 1 
     916            sep = ',\n' 
     917        sw.write(';\n') 
     918        if complex # Make something like .Net enum - specific int values  
     919            sw.writeLine 
     920            sw.writeLine('private static int defValue = 0;') 
     921            sw.writeLine('private static void setDef(int b){defValue = b;}') 
     922            sw.writeLine('private static int  getDef(){return defValue;}') 
     923            sw.writeLine('private int value;') 
     924            sw.writeLine('private [_name](int value) {this.value = value; setDef(value+1);}') 
     925            sw.writeLine('private [_name]() {this.value = getDef(); setDef(this.value+1);}') 
     926            sw.writeLine('public int getValue() {return this.value;}') 
     927        sw.dedent 
     928        sw.write('}\n\n') 
     929 
     930 
     931class EnumMember is partial 
     932 
     933    def writeJavaTestInvocation(sw as CurlyWriter) 
     934        pass 
     935 
     936    get javaRef as String is override 
     937        return _enumDecl.javaRefBase + '.' + .javaName 
     938 
     939 
    784940## 
    785941## Boxes 
    786942## 
     
    831987            #trace .parentNameSpace.javaQualifier 
    832988            javaRef = .parentNameSpace.javaQualifier + javaRef 
    833989        else if .parentBox 
    834             trace .parentBox.javaRef 
     990            #trace .parentBox.javaRef 
    835991            javaRef = .parentBox.javaRef + '.' + javaRef 
    836         #trace java 
     992        #trace javaRef 
    837993        return javaRef 
    838994 
    839995    def writeJavaDef(sw as CurlyWriter) 
     
    10771233 
    10781234    def writeJavaConstraint(sw as CurlyWriter) 
    10791235        if _constraints.count 
    1080             # TODO 
     1236            # TODO map cobra constraints to Java syntax 
     1237            # TODO before that try and comprehend java syntax for generic constraints 
    10811238            sw.write(' where [_name] :  ') 
    10821239            sep = '' 
    10831240            for constraint in _constraints 
     
    11131270 
    11141271class Extension is partial 
    11151272 
    1116     pass # TODO STUB 
     1273    pass # TODO STUB: Implement extensions 
    11171274 
    11181275 
    11191276## 
     
    15211678        if .isCompilerGenerated, return 
    15221679        if param.type.isReference and not param.type inherits NilableType and not param.isOut 
    15231680            if .compiler.options.boolValue('include-nil-checks') 
    1524                 sw.write('if (cobra.core.CobraCore._willCheckNil && [param.javaName]==null) throw new System.ArgumentNullException("[param.name]");\n') 
     1681                sw.write('if (cobra.core.CobraCore._willCheckNil)\n') 
     1682                sw.indent 
     1683                sw.write('cobra.core.CobraImp.checkNotNull([param.javaName], "ParameterGuard Fail: non nilable parameter `[param.javaName]` is null" );\n') 
     1684                sw.dedent 
     1685                #sw.write('if (cobra.core.CobraCore._willCheckNil && [param.javaName]==null) throw new NullPointerException("Non nilable parameter named [param.name] is Null");\n') 
    15251686 
    15261687    def writeJavaDSTHead(sw as CurlyWriter) 
    15271688        if not .canHaveDetailedStackTrace 
     
    18021963        #.writeJavaIsNames(sw) 
    18031964        sw.write(' [_returnType.javaRef] this') 
    18041965        .writeJavaParams(sw, r'[]') 
    1805         #.writeSharpBody(sw) 
     1966        #.writeJavaBody(sw) 
    18061967        # TODO: finish correctly (method with special attribute??? 
    18071968 
    18081969class MemberOverload is partial 
     
    19392100        sw.write(');\n') 
    19402101        sw.dedent 
    19412102         
    1942         /# 
    1943         # use java asserts initially at least 
    1944         sw.write(')\n') 
    1945         sw.indent 
    1946         sw.write('assert ') 
    1947         _expr.writeJavaDef(sw) 
    1948         #_expr.writeJavaBreakdown(sw) 
    1949         #sw.write('[.javaThis]') 
    1950         sw.write(' : ') 
    1951         sw.write('String.format("assertion %s FAILED", ') 
    1952         #sw.write('"assertion " + ') 
    1953         tmpsw = CurlyWriter(StringWriter(), sw.curlyLineNumberTreatment) 
    1954         _expr.writeJavaDef(tmpsw) 
    1955         #sw.write(tmpsw.toString.replace('"', r'\"')) 
    1956         sw.write(Utils.javaStringLiteralFor(tmpsw.toString)) 
    1957         #sw.write('" + " FAILED "') 
    1958         sw.write(')') 
    1959         if _info 
    1960             sw.write(' + "info="+') 
    1961             _info.writeJavaDef(sw) 
    1962         sw.write(';\n') 
    1963         sw.dedent 
    1964         #/ 
    1965  
    19662103class BlockStmt is partial 
    19672104 
    19682105    def writeJavaDef(sw as CurlyWriter) 
     
    20742211        sw.write('boolean [gotRightExceptionVarName] = false;\n') 
    20752212        sw.write('try ') 
    20762213        _block.writeJavaDef(sw) 
    2077         sw.writeAndIndent('catch ([exceptionTypeRef]) {\n') 
     2214        sw.writeAndIndent('catch ([exceptionTypeRef] _expExc ) {\n') 
    20782215        sw.write('// exactly what is expected\n') 
    20792216        sw.write('[gotRightExceptionVarName] = true;\n') 
    20802217        sw.dedentAndWrite('}\n') 
    20812218        if _exceptionType inherits Box and not (_exceptionType to Box).isJvmSystemExceptionClass 
    2082             wrongExceptionVarName = '_lh_expect_[_varNumber]' 
    2083             assert gotRightExceptionVarName <> wrongExceptionVarName 
    2084             sw.writeAndIndent('catch (java.lang.Throwable [wrongExceptionVarName]) {\n') 
    2085             sw.write('throw new cobra.core.ExpectException(typeof([exceptionTypeRef]), [wrongExceptionVarName]);\n') 
     2219            gotWrongExceptionVarName = '_lh_expect_[_varNumber]' 
     2220            assert gotRightExceptionVarName <> gotWrongExceptionVarName 
     2221            sw.writeAndIndent('catch (java.lang.Throwable [gotWrongExceptionVarName]) {\n') 
     2222            sw.write('throw new cobra.core.ExpectException([gotWrongExceptionVarName].getClass(), [gotWrongExceptionVarName]);\n') 
    20862223            sw.dedentAndWrite('}\n') 
    2087         sw.write('if (![gotRightExceptionVarName]) throw new cobra.core.ExpectException([exceptionTypeRef].getClass(), null);\n') 
     2224        sw.write('if (![gotRightExceptionVarName]) throw new cobra.core.ExpectException([exceptionTypeRef].class, null);\n') 
    20882225 
    20892226 
    20902227class ForStmt is partial 
     
    22512388        # in a C# foreach, (1) you must declare a new variable, (2) you cannot reassign it and (3) you cannot access it after the loop 
    22522389        # these constraints don't exist in Cobra 
    22532390        helperName = '_lh_for_[_var.name]_[_varNumber]' 
    2254         sw.write('foreach ([_var.type.javaRef] [helperName] in ') 
     2391        sw.write('for/*each*/ ([_var.type.javaRef] [helperName] : ') 
    22552392 
    22562393        # TODO: the javaRef of a type is not qualified and there is no using System.Collections; 
    22572394        # _what.writeJavaDefInContext(sw, false) 
     
    23202457 
    23212458    def writeJavaDef(sw as CurlyWriter) 
    23222459        base.writeJavaDef(sw) 
    2323         if _destination # java Writer 
     2460        if _destination # java Writer or PrintStream 
     2461            methodName = if(_stop, 'printStop', 'printLine') 
     2462            sw.write('cobra.core.CobraImp.[methodName](') 
    23242463            _destination.writeJavaDef(sw, true) 
    2325             methodName = if(_stop, 'write', 'writeLine') 
    2326             sw.write('.[methodName](') 
     2464            sw.write(', ') 
     2465             
     2466            # if destination as one type or with constant available methods in each type 
     2467            # i.e if we only allowed destination to be a PrintStream or a PrintWriter... 
     2468            #_destination.writeJavaDef(sw, true) 
     2469            #methodName = if(_stop, 'print', 'println') 
     2470            #sw.write('.[methodName](') 
    23272471        else 
    23282472            methodName = if(_stop, 'printStop', 'printLine') 
    23292473            sw.write('cobra.core.CobraImp.[methodName](') 
     
    24292573                sw.write('[sep][Utils.javaStringLiteralFor(expr.toCobraSource)][sep]') 
    24302574                expr.writeJavaDef(sw, false) 
    24312575            sw.write(');\n') 
     2576         
     2577 
     2578class TryStmt 
     2579    is partial 
     2580 
     2581    def writeJavaDef(sw as CurlyWriter) 
     2582        base.writeJavaDef(sw) 
     2583        if _successBlock 
     2584            # Java also has no "success" (or in Python, "else") block for the "try" statement 
     2585            # so it has to be simulated 
     2586            helperName = '_lh_success_[_varNumber]' 
     2587            sw.write('boolean [helperName] = true;\n') 
     2588            if _finallyBlock 
     2589                sw.write('try {\n') 
     2590                sw.indent 
     2591            sw.write('try') 
     2592            _tryBlock.writeJavaDef(sw, false) 
     2593            sw.dedent 
     2594            sw.write('}\n') 
     2595            if _catchBlocks.count 
     2596                .writeCatchBlocksJava(sw, helperName) 
     2597 
     2598            sw.write('finally /*Success block*/{ if ([helperName])') 
     2599            _successBlock.writeJavaDef(sw) 
     2600            sw.write('}\n') 
     2601 
     2602            if _finallyBlock 
     2603                sw.dedent 
     2604                sw.write('}\n') 
     2605                sw.write('finally') 
     2606                _finallyBlock.writeJavaDef(sw) 
     2607        else 
     2608            sw.write('try') 
     2609            _tryBlock.writeJavaDef(sw) 
     2610            .writeCatchBlocksJava(sw, nil) 
     2611            if _finallyBlock 
     2612                sw.write('finally') 
     2613                _finallyBlock.writeJavaDef(sw) 
     2614     
     2615    # All exceptions caught will be either 
     2616    # checked exceptions wrapped in a (RuntimeException based) CheckedExceptionWrapper 
     2617    # or other RunTimeException based Exceptions. 
     2618    # Generate code to rethrow these and can then just use the original cobra catchblocks as-is 
     2619    def writeCatchBlocksJava(sw as CurlyWriter, localSuccessVar as String?) 
     2620        sw.write('catch (Exception _exc0) {\n') 
     2621        sw.indent 
     2622        sw.write('try {\n') 
     2623        sw.write('    if (_exc0 instanceof cobra.core.CheckedExceptionWrapper) {\n') 
     2624        sw.write('        cobra.core.CheckedExceptionWrapper cxw = (cobra.core.CheckedExceptionWrapper)_exc0;\n') 
     2625        sw.write('        throw cxw.getCause();   //:codeGen: catchblock Header\n') 
     2626        sw.write('    }\n') 
     2627        sw.write('    else throw (RuntimeException)_exc0;}\n')     
     2628         
     2629        sw.write('//-- cobra-code catch blocks\n') 
     2630        _writeCatchBlocksJava(sw, localSuccessVar) # cobra code catch blocks 
     2631        sw.write('//-- end cobra-code catch blocks\n') 
     2632         
     2633        # fallthrough catch clause to propagate uncaught exceptions 
     2634        hasException = false 
     2635        for cb in _catchBlocks 
     2636            if not cb.type or (cb.type and cb.type == .compiler.exceptionType) 
     2637                hasException = true 
     2638                break 
     2639        if not hasException 
     2640            sw.write('catch (Exception _exc1){\n')     
     2641            sw.write('    if (_exc1 instanceof cobra.core.CheckedExceptionWrapper){\n') 
     2642            sw.write('        throw (cobra.core.CheckedExceptionWrapper) _exc0; //rethrow as uncaught\n') 
     2643            sw.write('    }\n') 
     2644            sw.write('    else throw (RuntimeException)_exc1;\n}\n')     
     2645 
     2646        sw.dedent 
     2647        sw.write('}\n')     
     2648         
     2649    # This is original writeCatchBlocksJava code generating the cobra catch blocks. 
     2650    # It originally worked alongside writeJavaDef0 in ThrowStmt below 
     2651    def _writeCatchBlocksJava(sw as CurlyWriter, localSuccessVar as String?) 
     2652        for cb in _catchBlocks 
     2653            sw.write('catch (') 
     2654            if cb.type 
     2655                sw.write(' [cb.type.javaRef]') 
     2656            else # no catchAll in Java language so catch Exception 
     2657                sw.write(' java.lang.Exception') 
     2658            helperName = if(cb.varName, cb.sharpHelperName, '_anyExc' ) 
     2659            # using sharpHelperName above is correct(same as sharp) - value set in Statements:CatchBlock.bindInt 
     2660            # should be renamed to be non BE specific tho' 
     2661            cb.javaRethrowName = helperName to ! # stash name for possible rethrow 
     2662            sw.write(' [helperName]') 
     2663            sw.write(')') 
     2664            sb = StringBuilder() 
     2665            if localSuccessVar, sb.append('[localSuccessVar] = false;\n') 
     2666            if .compiler.hasDetailedStackTraceOption, sb.append('CobraCoreInternal.CobraImp.HandledException();\n') 
     2667            if cb.varName 
     2668                trackLocals = .compiler.willTrackLocals 
     2669                if trackLocals, sb.append('CobraCoreInternal.CobraImp.SetLocal("[cb.varName]", ') 
     2670                sb.append('[cb.javaVarName] = [helperName]') 
     2671                if trackLocals, sb.append(')') 
     2672                sb.append(';\n') 
     2673            cb.block.writeJavaDef(sw, sb.toString) 
     2674 
     2675         
     2676         
     2677class CatchBlock 
     2678    is partial 
     2679 
     2680    get javaVarName as String 
     2681        return _var.javaName 
     2682 
     2683    var javaRethrowName as String? 
     2684     
     2685class ThrowStmt is partial 
     2686    /#  
     2687    # This version tries to hide cobra mapping of Exceptions to RuntimeExceptions from coders 
     2688    # We allow checked Exceptions (cobra coded) but when thrown codegen encloses them in a runtimeException:  
     2689    # In catch clauses we internally unwrap, examine and handle exceptions as coded 
     2690    # 
     2691    # Feeds into and is related to the (YTBD) determination of  what to do with calls to methods throwing  
     2692    # checked exceptions - 
     2693    # Ideally codegen would automatically wrap calls generating a checked exception in a RuntimeException  
     2694    # generating wrapper and remainder handling would be treated  as above.  
     2695    # There is the possibility of a lotta repeated duplicated line by line code generated for cases  
     2696    # that a coder writing same would put together in a block using same try-catch... 
     2697    # e.g. multiple sequential file io calls all generating IOException say.. 
     2698    # so as a convenience we may also want to provide some level of user specification  
     2699    # (compiler directive maybe) that specifies the enclosure limits of what should be wrapped in the  
     2700    # automatic code generated exception trapping boilerplate.. 
     2701    #   @jvm-wcx <ExceptionTYPE> 
     2702    #       .. lines of code that may gen such an Exception 
     2703    # 
     2704    # As currently implemented we could just detect such calls (not explicitly wrapped) and emit a  
     2705    # diagnostic (actually rely on javac to do it for us) suggesting that the cobra code must explicitly  
     2706    # trap/handle/declare checked exceptions and generate RuntimeException from it. 
     2707    # 
     2708    # This explicitly pushes all the necessary handling onto the user but cruelly exposes our non support for  
     2709    # conveniently remapping checked exceptions...  
     2710    # also it makes for ugly code (and obscures the code control flow).  
     2711    #  (see comment below) 
     2712    #/ 
     2713    def writeJavaDef(sw as CurlyWriter) 
     2714        base.writeJavaDef(sw) 
     2715        sw.write('throw ') 
     2716        if _expr 
     2717            if _expr.type.isDynamic,  sw.write('((java.lang.RuntimeException)') 
     2718            #throw Runtime Exceptions as is 
     2719            if _expr.type.isDescendantOf(.compiler.libraryType('Java.Lang.RuntimeException') to Box) 
     2720                _expr.writeJavaDef(sw, false) 
     2721            else 
     2722                # Checked exceptions get wrapped in a runtime Exc wrapper 
     2723                sw.write('new cobra.core.CheckedExceptionWrapper("Wrapping checked exception [_expr.type.name]",') 
     2724                _expr.writeJavaDef(sw, false) 
     2725                sw.write(')') 
     2726            if _expr.type.isDynamic, sw.write(')') 
     2727        else # rethrow 
     2728            # Java doesnt support an unadorned throw in a CatchBlock (rethrow), needs an explicit exception 
     2729            # look in enclosing block and if its a catchblock use its stored name (javaVarName) 
     2730            if .superNode and .superNode.superNode inherits CatchBlock 
     2731                rethrow = (.superNode.superNode to CatchBlock).javaRethrowName 
     2732                sw.write('cobra.core.CobraImp.reThrow(') 
     2733                sw.write(rethrow) 
     2734                sw.write(')') 
     2735                 
     2736        sw.write(';\n') 
     2737 
     2738    /#  
     2739    # This original version of throws codegen was used along with JvmBackEnd 
     2740    #   _tagToTypeName = {...  
     2741    #       'Exception' :       'Java.Lang.RuntimeException', 
     2742    #       ...} 
     2743    # makes the base Exceptionclass (compiler.ExceptionType) to be set to RuntimeException. 
     2744    # 
     2745    # This forces dvlpr written code to use only RuntimeException based exceptions explicitly        
     2746    # and also forces the explicit trapping of all methods throwing checked exceptions 
     2747    #(to handle or convert to a wrapping RuntimeException) which does obscure the code layout - just like java...  
     2748    # We dont provide a way to specify Exceptions thrown on a method definition so the net effect is to  
     2749    # force cobra code to be written to conform (explicitly) to our forcing of all java Exceptions to  
     2750    # unchecked which may or may not be seen to be a good thing..... 
     2751    # but its extra boilerplate cruft in cobra code, ugly and inconvenient to do 
     2752    #/ 
     2753    def writeJavaDef0(sw as CurlyWriter) 
     2754        base.writeJavaDef(sw) 
     2755        sw.write('throw ') 
     2756        if _expr 
     2757            if _expr.type.isDynamic 
     2758                sw.write('((java.lang.RuntimeException)') 
     2759            _expr.writeJavaDef(sw, false) 
     2760            if _expr.type.isDynamic 
     2761                sw.write(')') 
     2762        else # rethrow 
     2763            # Java doesnt support an unadorned throw in a CatchBlock (rethrow), needs an explicit exception 
     2764            # look in enclosing block and if catchblock use its stored name ( javaVarName) 
     2765            if .superNode and .superNode.superNode inherits CatchBlock 
     2766                rethrow = (.superNode.superNode to CatchBlock).javaRethrowName 
     2767                sw.write(rethrow) 
     2768                 
     2769        sw.write(';\n') 
     2770 
    24322771             
    24332772 
    24342773class UsingStmt 
     
    26983037class EnumCallExpr is partial 
    26993038 
    27003039    def writeJavaDef(sw as CurlyWriter, parens as bool) is override 
    2701         if parens, sw.write('(') 
     3040        isSet = _args.count > 1 
     3041        if parens 
     3042            if isSet, sw.write('java.util.EnumSet.of') 
     3043            sw.write('(')    
    27023044        if _args.count == 0 
    2703             sw.write('default([_definition.javaRef])') 
     3045            sw.write('/*default([_definition.javaRef])*/') 
    27043046        else 
    27053047            sep = '' 
    27063048            for member in _members 
    27073049                sw.write(sep) 
    27083050                sw.write(member.javaRef) 
    2709                 sep = '|' 
     3051                sep = ','  
    27103052        if parens, sw.write(')') 
    27113053 
    27123054    def writeJavaBreakdownItems(sw as CurlyWriter) 
     
    28873229                handled = true 
    28883230        if not handled 
    28893231            _target.writeJavaDef(sw) 
    2890  
    2891         ntIdxr = _lookupReadIndexer 
     3232        ntIdxr = _lookupIndexer 
    28923233        if ntIdxr 
    2893             assert ntIdxr.getter 
    2894             assert ntIdxr.getter.name.length 
     3234            assert ntIdxr.getter             # getter for indexer exists  
     3235            assert ntIdxr.getter.name.length # and has a name set 
    28953236            _writeIndexer(sw, '.[ntIdxr.getter.name](', ')') 
    28963237        else 
     3238            # fallback to treat as Array Type 
    28973239            _writeIndexer(sw, r'[', r']') 
    28983240         
    28993241        if parens, sw.write(')') 
    29003242 
    2901     def _lookupReadIndexer as JavaFieldInfo? 
     3243    def _lookupIndexer as JavaFieldInfo? 
    29023244        nt = JvmNativeType.nativeType(_target.type.nonNil) 
    2903         #print 'dbg', _target.type.nonNil, nt 
     3245        if not nt # _target.type is genericInstance 
     3246            tt = _target.type.nonNil to Box 
     3247            genDef = tt.genericDef 
     3248            nt = JvmNativeType.nativeType(genDef) 
     3249        #print 'dbg-ReadIdxr', _target.type.nonNil,'\n+ ', nt 
    29043250        if nt  
    2905             ntIdxr = nt.indexer 
     3251            ntIdxr = nt.lookupIndexer 
     3252            #print 'ntIdxr: [ntIdxr]' 
    29063253            if ntIdxr and ntIdxr.isReadable 
    29073254                return ntIdxr 
    29083255        #TODO: check for Indexer attribute for a Cobra user written version 
    29093256        return nil       
    2910                  
     3257     
    29113258    def _writeIndexer(sw as CurlyWriter, prefix as String, suffix as String) 
    29123259        sw.write(prefix)  
    29133260        _writeIdxArgs(sw) 
     
    32373584 
    32383585    get asJava as String is override 
    32393586        return _name 
     3587         
     3588class SequenceLit 
     3589    is partial 
    32403590 
     3591    def writeJavaBreakdownItems(sw as CurlyWriter) is override  # CC: axe is override 
     3592        base.writeJavaBreakdownItems(sw) 
     3593        sw.write(', +1') 
     3594        for expr in _exprs 
     3595            expr.writeJavaBreakdownItems(sw) 
     3596        sw.write(', -1') 
     3597     
     3598class ListLit 
     3599    is partial 
     3600 
     3601    def writeJavaDef(sw as CurlyWriter, parens as bool) is override 
     3602        innerType = (_type to Box).genericParams[0] 
     3603        sw.write('cobra.core.CobraImp.makeList/*<[innerType.javaRef]>*/(') 
     3604        if _exprs.count 
     3605            sep = '' 
     3606            for expr in _exprs 
     3607                sw.write(sep) 
     3608                expr.writeJavaDef(sw, false) 
     3609                sep = ', ' 
     3610        sw.write(')') 
     3611 
     3612 
     3613class ArrayLit 
     3614    is partial 
     3615 
     3616    def writeJavaDef(sw as CurlyWriter, parens as bool) is override 
     3617        sw.write('new [_type.javaRef] { ') 
     3618        if _exprs.count 
     3619            sep = '' 
     3620            for expr in _exprs 
     3621                sw.write(sep) 
     3622                expr.writeJavaDef(sw, false) 
     3623                sep = ', ' 
     3624        sw.write(' }') 
     3625 
     3626 
     3627class SetLit 
     3628    is partial 
     3629 
     3630    def writeJavaDef(sw as CurlyWriter, parens as bool) is override 
     3631        innerType = (_type to Box).genericParams[0] 
     3632        sw.write('cobra.core.CobraImp.makeSet/*<[innerType.javaRef]>*/(') 
     3633        if _exprs.count 
     3634            sw.write(', ') 
     3635            sep = '' 
     3636            for expr in _exprs 
     3637                sw.write(sep) 
     3638                expr.writeJavaDef(sw, false) 
     3639                sep = ', ' 
     3640        sw.write(')') 
     3641     
     3642         
    32413643class ToNilableOrNotExpr 
    32423644    is partial 
    32433645 
     
    33383740        if _right inherits TryCatchExpr # expand trycatch and do assignment within the expansion 
    33393741            _right.writeJavaDef(sw)  
    33403742            return   
     3743        #trace _right 
    33413744 
    33423745        # TODO: 
    33433746        # if trackLocal: 
     
    38674270        _left.writeJavaBreakdownItems(sw) 
    38684271 
    38694272 
     4273class InExpr is partial 
     4274    """ v in container""" 
     4275     
     4276    def writeJavaDef(sw as CurlyWriter, parens as bool) is override 
     4277        v = true 
     4278        branch _op 
     4279            on 'NOTIN', sw.write('!') 
     4280            on 'IN', pass 
     4281            else, throw FallThroughException(_op) 
     4282        if .needsNilCheck 
     4283            # Constraints: 
     4284            # (1) Don't evaluate the left expression twice 
     4285            # (2) Don't evaluate the right expression if the left is nil 
     4286            # In general, don't cause side effects from extra evaluation. 
     4287            if .left inherits IdentifierExpr and (.left to IdentifierExpr).definition inherits IVar 
     4288                # If the left is a var, there is no danger in double evaluation causing side effects. 
     4289                # java: (someVar==null ? false : <check>) 
     4290                sw.write('(') 
     4291                .left.writeJavaDef(sw, false) 
     4292                sw.write('==null ? false : ') 
     4293                # may have to cast int? to int, for example 
     4294                if  .left.type inherits NilableType _ 
     4295                    and not .left.type.nonNil.isReference _ 
     4296                    and .containsExpr 
     4297                    # well this last condition doesn't work so well due to generic args like List.Contains(T item) 
     4298                    # and not (((.containsExpr to DotExpr).dotRight.memberDefinition to Method).params[0].type inherits NilableType) 
     4299                     
     4300                    # then need cast 
     4301                    assert .left.type inherits NilableType 
     4302                    .right.writeJavaDef(sw, false) 
     4303                    sw.write('.contains(([.left.type.nonNil.javaRef])') 
     4304                    .left.writeJavaDef(sw) 
     4305                    sw.write(')') 
     4306                    if v, sw.write('/* A */') 
     4307                else 
     4308                    .writeInCheckJava(sw, false, v) 
     4309                    if v, sw.write('/* B */') 
     4310                sw.write(')') 
     4311            else 
     4312                # cobra: left in right 
     4313                # java: cobra.core.CobraImp.inWithNullCheck(<left>, delegate(__lh_value) { return <right>.Contains(__lh_value); }) 
     4314                # use:   static public bool inWithNullCheck<T>(T a, Predicate<T> predicate) 
     4315 
     4316                # cobra: left in right 
     4317                # java: cobra.core.CobraImp.in(left, delegate() { return right; }) 
     4318                sw.write('cobra.core/*CobraCoreInternal*/.CobraImp.inWithNullCheck(') 
     4319                .left.writeJavaDef(sw, false) 
     4320                sw.write(', delegate([.left.type.javaRef] __lh_value_[.serialNum]) { return ') 
     4321                if .containsExpr 
     4322                    # Do not write the .containsExpr here because it's argument to .contains in the 
     4323                    # full left expression. Instead, we want to pass the argument passed to the delegate. 
     4324                    .right.writeJavaDef(sw, false) 
     4325                    sw.write('.contains(([.left.type.nonNil.javaRef]) __lh_value_[.serialNum]);') 
     4326                    if v, sw.write('/* C */') 
     4327                else 
     4328                    # No .containsExpr means we use CobraImp.In(a, b) for the delegate 
     4329                    sw.write('cobra.core/*CobraCoreInternal*/.CobraImp.in(__lh_value_[.serialNum], ') 
     4330                    .right.writeJavaDef(sw, false) 
     4331                    if v, sw.write('/* D */') 
     4332                sw.write(' })') 
     4333        else 
     4334            .writeInCheckJava(sw, parens, v) 
     4335            if v, sw.write('/* E */') 
     4336 
     4337    def writeInCheckJava(sw as CurlyWriter, parens as bool, v as bool) 
     4338        if .containsExpr 
     4339            .containsExpr.writeJavaDef(sw, parens) 
     4340            if v, sw.write('/* F */') 
     4341        else 
     4342            sw.write('cobra.core/*CobraCoreInternal*/.CobraImp.in(') 
     4343            .left.writeJavaDef(sw, false) 
     4344            sw.write(',') 
     4345            .right.writeJavaDef(sw, false) 
     4346            sw.write(')') 
     4347            if v, sw.write('/* G */') 
     4348 
    38704349class InheritsExpr 
    38714350    is partial 
    38724351                 
     
    38754354        #sw.write(' instanceof ') 
    38764355        #_right.writeJavaDef(sw, false) 
    38774356 
     4357        # have to handle primitives ( bool, char, byte,short, int, long,float, double) specially 
     4358         
    38784359        # Compiler generates error with instanceof for non intersecting instance and Type so 
    38794360        # instead use runtime instance lookup 
    38804361        #  instead of 'e instanceof T'  becomes 'T.class.isInstance(e)' 
    3881         _right.writeJavaDef(sw, false) 
    3882         #sw.write('.class.isInstance(') 
     4362        # Have to map primitives to wrappers cos autoboxing makes it wrong 
     4363        if _right inherits TypeExpr # primitives 
     4364            ctjref = _right.containedType.javaRef 
     4365            if  ctjref == 'int',    ctjref = 'Integer.class' 
     4366            if  ctjref == 'char',   ctjref = 'Character.class' 
     4367            if  ctjref == 'short',  ctjref = 'Short.class' 
     4368            if  ctjref == 'long',   ctjref = 'Long.class' 
     4369            if  ctjref == 'boolean', ctjref = 'Boolean.class' 
     4370            if  ctjref == 'byte',   ctjref = 'Byte.class' 
     4371            if  ctjref == 'float',  ctjref = 'Float.class' 
     4372            if  ctjref == 'double', ctjref = 'Double.class' 
     4373            sw.write(ctjref)     
     4374            isPrim = true 
     4375 
     4376        if not isPrim 
     4377            _right.writeJavaDef(sw, false) 
    38834378        sw.write('.isInstance(') 
    38844379        _left.writeJavaDef(sw) 
    38854380        sw.write(' )') 
    38864381 
    3887  
    38884382class ToExpr is partial 
    38894383 
    38904384/#  def needsContextCast as bool is override 
     
    41684662    def writeJavaTestInvocation(sw as CurlyWriter) 
    41694663        pass 
    41704664 
    4171 class EnumMember is partial 
    4172  
    4173     def writeJavaTestInvocation(sw as CurlyWriter) 
    4174         pass 
    4175  
    41764665class Extension is partial 
    41774666 
    41784667    get javaKeyWord as String is override 
  • Source/TestifyRunner.cobra

     
    281281        if baseName.startsWith('.') or baseName.startsWith('_') 
    282282            # examples: .svn, _svn. Also, _ is a nice way to temporarily exclude a directory if possible 
    283283            return 
     284        if _useConfigPath(dirName), return       
     285        _testifyDir(dirName, nil) 
     286         
     287    def _useConfigPath(dirName as String) as bool 
    284288        configPath = Path.combine(dirName, 'testify.kv') 
    285289        if File.exists(configPath) 
    286290            dirOptions = Utils.readKeyValues(configPath) 
     
    288292                argSets = dirOptions['args'].split(@[c'|']) 
    289293                for argSet in argSets 
    290294                    _testifyDir(dirName, argSet.trim) 
    291                 return 
    292         _testifyDir(dirName, nil) 
    293  
     295                return true 
     296        return false 
     297             
    294298    def _testifyDir(dirName as String, args as String?) 
    295         willCopyLibs = true  # not CobraCore.isRunningMono  # On Mono, we use MONO_PATH instead of coping the Cobra.Core.dll around 
    296         libNames = ['Cobra.Core.dll'] 
    297299        _extraTestifyArgs = args 
    298300        _statusWriter.writeLine(if(args, '[dirName] with args: [args]', '[dirName]')) 
    299301        _statusWriter.indent 
    300302        try 
    301303            print 'Running tests in [dirName]' 
    302304            saveDir = Environment.currentDirectory 
    303             if willCopyLibs 
    304                 # _cleanDir will remove these later 
    305                 for libName in libNames 
    306                     # Ideally, the file operations below would not be wrapped with try...catch and a 
    307                     # 'warning' because if the current library files cannot be copied into the test 
    308                     # directory, then the test is not valid. However, Windows has problems with 
    309                     # deleting and/or copying over Cobra.Core.dll right after it has been used. This 
    310                     # comes up on Tests\340-contracts whose testify.kv config file specifies two 
    311                     # runs of the directory. 
    312                     targetPath = Path.combine(dirName, libName) 
    313                     if File.exists(targetPath) 
    314                         if Utils.isRunningOnUnix 
    315                             File.delete(targetPath) 
    316                         else 
    317                             # Windows is just too damn picky... 
    318                             try 
    319                                 File.delete(targetPath) 
    320                             catch UnauthorizedAccessException 
    321                                 print 'testify warning: cannot delete:', targetPath 
    322                     if libName == 'Cobra.Core.dll' or File.exists(libName) 
    323                         if Utils.isRunningOnUnix 
    324                             File.copy(Path.combine(saveDir, libName), targetPath) 
    325                         else 
    326                             try 
    327                                 File.copy(Path.combine(saveDir, libName), targetPath, true) 
    328                             catch IOException 
    329                                 print 'testify warning: cannot copy or copy over:', targetPath 
     305            _copyLibs(saveDir, dirName) 
    330306            Directory.setCurrentDirectory(dirName) 
    331307            try 
    332                 setUpScript = if(Utils.isRunningOnUnix, 'testify-set-up', 'testify-set-up.bat') 
    333                 if File.exists(setUpScript) 
    334                     print '* * * * Running [setUpScript]' 
    335                     process = Process() 
    336                     process.startInfo.fileName = setUpScript 
    337                     setUpOutput = process.runAndCaptureAllOutput 
    338                     if process.exitCode <> 0 
    339                         print 'TESTIFY WARNING: Script [setUpScript] exited with error code [process.exitCode]' 
    340                         print 'begin output' 
    341                         print setUpOutput.trim 
    342                         print 'end output' 
     308                _runSetupScript 
    343309                paths = List<of String>(Directory.getFiles('.')) 
    344310                paths.addRange(Directory.getDirectories('.') to passthrough) 
    345311                paths.sort 
     
    359325        finally 
    360326            _statusWriter.dedent 
    361327        _testifyFlush 
     328         
     329    def _copyLibs(saveDir as String, dirName as String) 
     330        """ 
     331        Copy Cobra Libs etc to sources directory if platform needs it. 
     332        _cleanDir will remove these later 
     333        """ 
     334        if _runtimePlatform(nil) <> 'clr'  # other platforms use a Path instead of dll in cwd 
     335            return 
     336        # if CobraCore.isRunningMono  # On Mono, we use MONO_PATH instead of copying the Cobra.Core.dll around 
     337        #   return   
     338        libNames = ['Cobra.Core.dll'] 
     339        for libName in libNames 
     340            # Ideally, the file operations below would not be wrapped with try...catch and a 
     341            # 'warning' because if the current library files cannot be copied into the test 
     342            # directory, then the test is not valid. However, Windows has problems with 
     343            # deleting and/or copying over Cobra.Core.dll right after it has been used. This 
     344            # comes up on Tests\340-contracts whose testify.kv config file specifies two 
     345            # runs of the directory. 
     346            targetPath = Path.combine(dirName, libName) 
     347            if File.exists(targetPath) 
     348                if Utils.isRunningOnUnix 
     349                    File.delete(targetPath) 
     350                else 
     351                    # Windows is just too damn picky... 
     352                    try 
     353                        File.delete(targetPath) 
     354                    catch UnauthorizedAccessException 
     355                        print 'testify warning: cannot delete:', targetPath 
     356            if libName == 'Cobra.Core.dll' or File.exists(libName) 
     357                if Utils.isRunningOnUnix 
     358                    File.copy(Path.combine(saveDir, libName), targetPath) 
     359                else 
     360                    try 
     361                        File.copy(Path.combine(saveDir, libName), targetPath, true) 
     362                    catch IOException 
     363                        print 'testify warning: cannot copy or copy over:', targetPath 
    362364 
     365    def _runtimePlatform(options as OptionValues?) as String 
     366        if not options, options = .testifyOptions 
     367        rtp = if(options['back-end']=='none', CobraCore.runtimePlatform, options['back-end'] to String) 
     368        return rtp 
     369         
     370    def _runSetupScript 
     371        setUpScript = if(Utils.isRunningOnUnix, 'testify-set-up', 'testify-set-up.bat') 
     372        if File.exists(setUpScript) 
     373            print '* * * * Running [setUpScript]' 
     374            process = Process() 
     375            process.startInfo.fileName = setUpScript 
     376            setUpOutput = process.runAndCaptureAllOutput 
     377            if process.exitCode <> 0 
     378                print 'TESTIFY WARNING: Script [setUpScript] exited with error code [process.exitCode]' 
     379                print 'begin output' 
     380                print setUpOutput.trim 
     381                print 'end output' 
     382 
    363383    def testifyFile(baseName as String) as int 
    364384        save = _failureCount 
    365385        _firstAttempt = true 
     
    423443        if inLineMessages.count > 0 #  hasInlineMessages 
    424444            return _testifyInlineMessages(inLineMessages, expectingError,  
    425445                                            compilerVerbosity, [baseName], options, verbose ) 
    426  
     446                                             
    427447        if firstLineInsensitive.startsWith('#.error.') 
    428448            # deprecated: put errors on the lines where they occur. the "hasInlineMessages" code above will detect them. 
    429449            # Note that errors that are only detected by the backend C# compiler are not detectable by testify 
     
    471491                continue 
    472492 
    473493            if firstLineInsensitive.startsWith('#.require.') 
    474                 rtPlatform  = if(options['back-end']=='none', CobraCore.runtimePlatform, options['back-end']) 
     494                rtPlatform  = _runtimePlatform(options) 
    475495                what = firstLineInsensitive[10:] 
    476496                branch what 
    477497                    on 'mono' 
     
    714734             
    715735    def _testifyStd(compilerVerbosity as int, fileNames as IList<of String>, 
    716736                            options as OptionValues, verbose as bool) as int         
    717         c = Compiler(compilerVerbosity, _cachedTestifyModules, commandLineArgParser=_cl.argParser) 
    718737        try 
     738            c = Compiler(compilerVerbosity, _cachedTestifyModules, commandLineArgParser=_cl.argParser) 
    719739            c.testifyFilesNamed(fileNames, options, _resultsWriter to !, verbose) 
    720740        catch StopCompilation 
    721741            .failed 
     
    768788 
    769789        print .bar 
    770790 
    771         _cachedTestifyModules = for mod in c.modules where mod inherits AssemblyModule get mod 
     791        #_cachedTestifyModules = for mod in c.modules where mod inherits AssemblyModule get mod 
    772792 
    773793        return 1 
    774794 
  • Source/Enums.cobra

     
    1010    var _storageType as IType? 
    1111    var _nativeType as NativeType? 
    1212    var _needScanNativeType as bool 
    13  
     13    var _isUsedAsSet = false    # flag if multi member initted 
     14     
    1415    cue init(parent as IParentSpace?, token as IToken, idToken as IToken, name as String, isNames as List<of String>, attribs as AttributeList, storageTypeNode as ITypeProxy?, docString as String, enumMembers as List<of EnumMember>) 
    1516        base.init(parent, token, name, isNames, docString) 
    1617        _idToken = idToken 
     
    2728        _nativeType = nativeType 
    2829        assert .compiler 
    2930        .backEnd.setUnderlyingType(this)  # sets _storageTypeNode 
    30         #_storageTypeNode = ClrTypeProxy(Enum.getUnderlyingType((_nativeType to ClrNativeType).backEndType))  # TODO: fix native 
    3131        _needScanNativeType = true 
    3232         
    3333    def _scanNativeType 
     
    8484    get typeForReceiver as IType is override 
    8585        return this 
    8686 
     87    pro isUsedAsSet from var 
     88         
    8789    def _bindInt is override 
    8890        base._bindInt 
    8991        for attrib in .attributes, attrib.bindInt 
     
    119121        body 
    120122            _parentBox = newBox 
    121123 
    122  
     124     
    123125class EnumMember 
    124126    is partial 
    125127    inherits NamedNode 
    126128    implements IMember 
    127129    """ 
    128130    Holds the name and (optionally) value of a member of an EnumDecl. 
     131    One of these is what an enum instance will be set to. 
    129132    """ 
    130133 
    131134    var _value as int? 
  • Source/Statements.cobra

     
    667667        if isStreamOfKeyValue, kvRHS = ['key', 'value'] 
    668668        if _var.type inherits Box 
    669669            if not isStreamOfKeyValue 
    670                 isStreamOfKeyValue = (_var.type to Box).isIndirectConstructionOf(.compiler.libraryType('System.Collections.Generic.KeyValuePair<of,>') to Box) 
     670                #isStreamOfKeyValue = (_var.type to Box).isIndirectConstructionOf(.compiler.libraryType('System.Collections.Generic.KeyValuePair<of,>') to Box)  # compiler.KeyValuePairOfType 
     671                isStreamOfKeyValue = (_var.type to Box).isIndirectConstructionOf(.compiler.keyValuePairOfType) 
    671672                if isStreamOfKeyValue, kvRHS = ['key', 'value'] 
    672673            if not isStreamOfKeyValue 
    673674                isStreamOfKeyValue = (_var.type to Box).isDescendantOf(.compiler.libraryType('Cobra.Core.BasePair') to Box) 
     
    976977        base._bindImp 
    977978        if .destination 
    978979            .destination.bindImp 
    979             t = .destination.type 
    980             if not t.isDynamicOrPassThrough and not t.isDescendantOf(.compiler.libraryType('System.IO.TextWriter'))  # TODO-BACKEND 
    981                 .recordError('Invalid destination of type "[t.name]" for "print". Use a TextWriter or subclass thereof instead.') 
     980            t = .destination.type  
     981            if not t.isDynamicOrPassThrough 
     982                .compiler.backEnd.validatePrintDestType(t to !, this)  
    982983 
    983984 
    984985class PrintStmt inherits AbstractPrintStmt is partial 
     
    14031404        else if _typeNode 
    14041405            _typeNode.bindImp 
    14051406            _type = _typeNode.realType  # CC: combine with above: _type = _typeNode.bindImp.realType 
    1406         _sharpHelperName = '_lh_catch_[.compiler.curBox.makeNextPrivateSerialNumber]' 
     1407        _sharpHelperName = '_lh_catch_[.compiler.curBox.makeNextPrivateSerialNumber]' # TODO rename to HelperName or beHelperName - common to both backends 
    14071408        _block.bindImp 
    14081409 
    14091410 
     
    14551456                    if _expr.definition inherits BoxEvent 
    14561457                        reason = 'it is an event, not an exception. Either throw an exception or use "raise" on the event instead of "throw"' 
    14571458                    else 
    1458                         reason = 'it does not inherit from Exception' 
     1459                        reason = 'it does not inherit from [exceptionType.name] (Exception)' 
    14591460                    .throwError('Cannot throw "[_expr.type.name]" because [reason].') 
    14601461        .compiler.curCodeMember.hasThrowStmt = true 
    14611462 
     
    18071808            if isPair 
    18081809                memberNames = ['a', 'b'] 
    18091810            else if not isPair 
    1810                 isPair = (_source.type to Box).isIndirectConstructionOf(.compiler.libraryType('System.Collections.Generic.KeyValuePair<of,>') to Box) 
     1811                isPair = (_source.type to Box).isIndirectConstructionOf(.compiler.keyValuePairOfType) 
    18111812                if isPair, memberNames = ['key', 'value'] 
    18121813 
    18131814        if isPair 
  • Source/Cobra.Core/Java/CobraCore.java

     
    1818 
    1919public class CobraCore { 
    2020     
     21    // Release info 
     22    public static final String versionDescription = "svn-post-0.8.0(jvm)"; 
     23        //textual description of the version such as "X.Y.Z" or "svn-post-X.Y.Z". 
     24    public static final boolean isOfficialRelease = false; 
     25    public static final int version = CobraCore.toVersionNumber(0,8,0); // Version# - Version(0, 8, 0) 
     26    public static final int releaseNum = 25; 
     27 
    2128    public static Boolean _willCheckInvariant = true; 
    2229    public static Boolean _willCheckRequire = true; 
    2330    public static Boolean _willCheckEnsure = true; 
     
    4754    System.exit(exitCode); 
    4855    } 
    4956             
    50     public static String newLine =  System.getProperty("line.separator"); 
     57    public static final String newLine =  System.getProperty("line.separator"); 
    5158    //""" Returns the newline for the current environment/platform. """ 
    5259 
    5360    // Property StringMaker techStringMaker 
     
    7683    """ 
    7784    Run all Cobra `test` sections in all assemblies using reflection to locate them. 
    7885    """ 
    79     if CobraImp.showTestProgress, listener = Cobra.Lang.Test.TextWriterListener(Console.out) 
    80     else, listener = Cobra.Lang.Test.TextWriterOnlyOnFailureListener(Console.out) 
    81     tr = Cobra.Lang.Test.TestRunner(listener) 
     86    if CobraImp.showTestProgress, listener = Cobra.Core.Test.TextWriterListener(Console.out) 
     87    else, listener = Cobra.Core.Test.TextWriterOnlyOnFailureListener(Console.out) 
     88    tr = Cobra.Core.Test.TestRunner(listener) 
    8289    tr.runAllTests 
    8390    if listener.testFailures, CobraCore.exit(1) 
    8491     */ 
     
    103110        // TODO: CobraImp.printLine( "    See also: http://cobra-language.com/docs/debugging"); 
    104111        CobraImp.printLine(); 
    105112    } 
     113     
     114    public static final int toVersionNumber( int major, int minor, int rev) { 
     115        assert major >= 0 && major < 100 : "Major between 0 and 99"; 
     116        assert minor >= 0 && minor < 100 : "Minor between 0 and 99"; 
     117        assert rev >= 0 && rev < 100 :   "Rev between 0 and 99"; 
     118        return major*1000 + minor*100 + rev; 
     119    } 
    106120} 
    107121 
  • Source/Cobra.Core/Java/CheckedExceptionWrapper.java

     
     1/*  
     2 * A Runtime Exception wrapping a Checked Exception. 
     3 * Compiler generates internally in 'throws' codegen and unwrapped in catch clauses codegen 
     4 * See JavaGenerator.cobra ThrowStmt.writeJavaDef and tryStmt.writeCatchBlocksJava 
     5 *  
     6 * Basically a Marker class for RuntimeException. 
     7 */ 
     8 
     9package cobra.core; 
     10 
     11 
     12public class CheckedExceptionWrapper extends RuntimeException { 
     13     
     14    public CheckedExceptionWrapper(String msg, Exception cause) { 
     15        super(msg, cause); 
     16    } 
     17 
     18    public Exception getCause() { 
     19        return (Exception) super.getCause(); 
     20    } 
     21} 
     22 
     23 
  • Source/Cobra.Core/Java/Delegate.java

     
    88 *  call      myDelegate(arg0,...)                                -> d.invoke(arg0,...)   
    99 *            (or no args:  myDelegate()                          -> d.invoke()  ) 
    1010 * 
    11  * Doesnt support Generics. 
    1211 * This Implementation genericised on ReturnType 
    1312 */ 
    1413 
     
    1918import java.util.*; 
    2019import java.lang.reflect.*; 
    2120 
     21 
    2222public class Delegate<T> { 
    2323    Class<T> returnType;  //  method returnType 
    2424    Class<?>[] paramTypes;  // Types of the method parameters  
     
    209209    } 
    210210}     
    211211 
    212  
    213212class TestDelegate { 
    214213 
    215214    public static void main(String... args) {  
  • Source/Cobra.Core/Java/mkjarAll

     
    88 
    99[ -d classes ] || mkdir classes 
    1010[ -d classes/cobra/lang ] && rm -rf classes/cobra/lang/* 
    11 EXCEPTIONS="AssertException.java InvariantException.java RequireException.java EnsureException.java NonNilCastException.java DynamicOperationException.java" 
    12 javac -d classes SourceSite.java CobraImp.java CobraCore.java Delegate.java ${EXCEPTIONS} CobraDirectString.java 
     11EXCEPTIONS="AssertException.java InvariantException.java RequireException.java EnsureException.java NonNilCastException.java DynamicOperationException.java ExpectException.java CheckedExceptionWrapper.java" 
     12javac -d classes SourceSite.java CobraImp.java CobraCore.java Delegate.java DelegateO.java Pair.java ${EXCEPTIONS} CobraDirectString.java 
    1313[ $? == 0 ] || exit 
    1414 
    1515jar cvf ${CORE_RTL} -C classes . 
    1616[ $? == 0 ] || exit 
    1717cp ${CORE_RTL} ../.. 
    1818 
    19 if [ ! -e PkgSig.class ]; then  
     19#if [ ! -e PkgSig.class ]; then  
     20#    echo 'building PkgSig app' 
     21#    javac -cp . PkgSig.java 
     22if [ ! -e PkgSig.jar ]; then  
    2023    echo 'building PkgSig app' 
    21     javac -cp . PkgSig.java 
     24    PSIGDIR=pkgsig-classes 
     25    [ -d $PSIGDIR ] || mkdir $PSIGDIR 
     26    javac -cp . -d $PSIGDIR PkgSig.java 
     27    jar cvfe PkgSig.jar PkgSig -C $PSIGDIR . 
    2228fi 
    2329 
     30export CLASSPATH='PkgSig.jar' 
    2431echo 'Gen pkgSig file for '${CORE_RTL} 
    25 java -cp ".;${CORE_RTL}"  PkgSig -j ${CORE_RTL} > ${CORE_RTL}.sig 
     32java -cp "${CLASSPATH};${CORE_RTL}"  PkgSig -j ${CORE_RTL} > ${CORE_RTL}.sig 
    2633 
    2734echo 'Gen pkgSig file for rt.jar' 
    28 java -cp .  PkgSig -jrt > rt.jar.sig 
     35java   PkgSig -jrt > rt.jar.sig 
    2936echo 'Gen pkgSig file for java.lang' 
    30 java -cp .  PkgSig  java.lang > java.lang.sig 
     37java   PkgSig  java.lang > java.lang.sig 
    3138echo 'Gen pkgSig file for java.util' 
    32 java -cp . PkgSig  java.util > java.util.sig 
     39java PkgSig  java.util > java.util.sig 
    3340 
    34 echo 'copy .sig files to compiler directory' 
    35 cp ${CORE_RTL}.sig rt.jar.sig java.util.sig java.lang.sig ../.. 
     41#echo 'copy .sig files to compiler directory' 
     42#cp ${CORE_RTL}.sig rt.jar.sig java.util.sig java.lang.sig ../.. 
  • Source/Cobra.Core/Java/read-me.text

     
    1414compiler. Then we'll have a cobra to java compiler on the jvm. 
    1515 
    1616Supporting that is this directory which provides  
    17     - stub code for a java cobra RTL (CobraLang.jar) 
    18     - A tool to generate descriptions for java library classes (Pkgsig files) 
    19     - a script to build and copy the various supporting pieces the java backend needs to the compiler Source directory. 
     17    - stub code for a java cobra RTL (CobraCore.jar) 
     18    - A tool (PkgSig.jar) to generate descriptions for java library classes (Pkgsig files) 
     19    - a script (mkjarAll) to build and copy the various supporting pieces the java backend needs to  
     20        the compiler Source directory.  
    2021     
    2122 
    22 The base java version supported is currently 1.6. 
     23The base java version supported is currently 1.7.  
     24(I started with 1.6, It may still work to some extent with that java version ) 
    2325This one specifically 
    24 java version "1.6.0_24" 
    25 Java(TM) SE Runtime Environment (build 1.6.0_24-b07) 
    26 Java HotSpot(TM) Client VM (build 19.1-b02, mixed mode, sharing) 
     26java version "1.7.0_04" 
     27Java(TM) SE Runtime Environment (build 1.7.0_04-b22) 
     28Java HotSpot(TM) Client VM (build 23.0-b21, mixed mode, sharing) 
    2729 
    28 This will move up to at least 1.7 as the port/code generation progresses 
    29 and will probably stabilise on 1.8 eventually. 
     30This will move up to 1.8 as the port/code generation progresses and 1.8 gets more useful 
     31capabilities. I will probably stabilise on 1.8 as the base eventually. 
    3032  
    3133= Caveats = 
    3234 
    3335This backend  is under development  
    34  - for any but the most trivial uses it is effectively broken. 
    35  There are large amounts of (java) code generation missing. 
    36  It spews debug/diagnostic/conversational/status info all the time. 
     36 - for any but the most trivial uses it is broken. 
     37 There are amounts of (java) code generation missing. 
     38 It will possibly spew debug/diagnostic/conversational/status info all the time. 
    3739 There are bugs. 
    3840 There are missing capabilities.   
    3941 There are horrible bits  
     
    4850This is a source modification - no installation support.  
    4951Run the compiler out of the cobra source directory 
    5052 
    51 I'm developing this in Windows (XP) - it may or may not be working under mono  
    52 on any other platform. 
     53I'm now developing this in Windows 7 (I started in Windows XP but a System crash caused a  
     54correction of that) 
     55 - it may or may not be working under mono on any other platform. 
    5356 
    54 This is (the/an) initial devt snapshot.  
    55 Its working(ish) but rough,  
     57This is a devt snapshot. Its working(ish) but rough,  
    5658Things will change 
    5759 
    5860- What To Do - 
     
    6163 happens, good for you. 
    6264 This is what you will need to do 
    6365 
    64     1) If necessary  
    65        Apply the patches providing the java backend to the Source tree 
    66         - get the modified compiler subsequently rebuilt 
    67  
    68        As on Nov 2011 (and touch wood) all the jvm patches posted have been  
    69        applied to the code repository source so it should be enough to pull down the latest compiler source and build it. 
     66    1) As of Nov 2011 (and touch wood) all the jvm patches posted have been  
     67       applied to the code repository source so it should be enough to pull down the latest 
     68       compiler source and build it. 
    7069        
    71         Patch should be on the cobra website http://cobra-language.com 
     70        Any unapplied patches should be on the cobra website http://cobra-language.com 
    7271        Ticket 275: More support for java backend 
    7372            http://cobra-language.com/trac/cobra/ticket/275 
    7473         
    75     2) Ensure java and javac executables (1.6 or later) are in your PATH and work. 
     74    2) Ensure jar, java and javac executables (1.7 or later) are in your PATH and work. 
    7675     
    77     3) Go into this dir and run ./mkJarAll  
     76    3) Go into this dir and run ./mkjarAll  
    7877        ( its a bash script - you will need bash or to port the script,  
    79             or run the command contents manually) - this builds the cobra RTL and   
    80         package signature tool and uses that to create the class description info and copies  
    81         them to the  right places in the  cobra source tree) 
     78            or run the command contents manually) - this compiles the (minimal) cobra RTL  
     79            ( java source to a jarfile) and builds the package signature tool and uses that  
     80            to create the class description info (.sig files) and copies  
     81                them to the  right places (Source dir alongside cobra.exe) 
     82                in the cobra source tree) 
     83                 
    8284    4) find a test cobra script and compile it 
     85        a good smoke test is Tests/100-basic/001.cobra 
    8386 
    8487- How to build and run a cobra program generating a java app - 
    8588 
     
    103106 
    104107 
    105108- Status -  
     109 
     110    Theres a more complete timeline tracking the uploaded patches on the ticket 
     111        Ticket 275: More support for java backend 
     112        http://cobra-language.com/trac/cobra/ticket/275 
     113     
    106114 Jul-2011 - We can build and run simple (single file) programs.  
    107115    (Up to about 032-* of the files in Tests/100-*.  
    108116        Maybe more but thats what I've tried so far) 
     
    112120    (Up to about 080-* of the files in Tests/100-*) 
    113121    Have properties and Indexers, Casting, reading Commandline ...      
    114122 
     123 Mar-2012 - Minor cleanups and Testify compiler testcase harness working 
     124 
     125 10-Aug-2012 try and sync a major push for a cobra release. 
     126   Fix testcase regressions noted from testify runs 
     127      - fix issue with multiple internal Testify invocations 
     128      - fix incorrect conversion cobra to java name forms in multiple namespace generics  
     129      - get indexing ([]) working mapping to indexer getters/setters 
     130      - type inference in foreach and support iterators (java vs enumerators in CLR) 
     131      - In pkgSig correct some bad name handling for nested classes in rt.jar 
     132      - extend java RTL some more support codegen 
     133   Support try/catch and throw  (minimally) 
     134       Modify this to correctly autowrap thrown checked Exceptions into runtimeExceptions and 
     135       unwrap, catch or propagate correctly in catch clauses. 
     136       Methods throwing checked Exceptions still need to be explicitly wrapped  
     137 
     138   Pull backEnd Abstract base classs out to its own file 
     139    
     140   Start enum support    
     141    (Up to about 301-* of the files in Tests/100-*) 
     142 
     143   Sep-2012 
     144   Complete enum support (Tests/100-*/302-334a*). 
     145   setup so enums built using the ctor syntax with multiple initial values( Enum(val1,val2) internally 
     146   use ( in java) and EnumSet. Codegen will break if you try and treat this as a sigle enum value and a multivalue set. 
     147     
     148   Add inbuilt support for testing for an enum value membership in a enum variable (set) using  
     149   'value in enumSet' 
     150   see test  (310j-* and 334a-*) 
     151    
     152   Modify build so creates an executable jar file from classfiles(and runs it). 
     153   This means all classpath dependencies must be listed correctly in the manifest file  
     154   (see JavaGenerator.cobra _javaTool)  
     155   Correct handling for multiple cobra files and cobra file containing multiple classes.  
     156 
     157   Fixup codegen for print and print redirection. Add necessary RTL support  
     158 
     159   BackEnd KeyValuePair Type, support Pair (since compiler now uses it)  
     160 
     161 
     162TODO 
     163    load annotations into .sig files and use for things like nullability 
     164        PkgSig.java and handling in JarSig.cobra 
     165         
     166    Support native static methods on box instances 
     167        installJvmNativeMethods in ScanJvmType     
     168         
     169    Handling ( wrap and convert) calls to methods in java libs generating 
     170    non RuntimeExceptions. 
     171      
    115172- Other - 
    116173 
    117174Post issues/questions/comments/etc if you have them to Cobra Programming Language Forum 
  • Source/Cobra.Core/Java/DynamicOperationException.java

     
    2929    protected Object _obj; 
    3030    protected String _name; 
    3131    Class _type; 
     32    Exception _exc; 
    3233 
    3334    public UnknownMemberException(Object obj, String name, Class type) {  this(obj, name, type, null);} 
    3435    public UnknownMemberException(Object obj, String name, Class type, Exception cause)  
     
    3940        this._obj = obj; 
    4041        this._name = name; 
    4142        this._type = type; 
     43        this._exc = cause; 
    4244    } 
    4345} 
    4446 
     47 
     48class CannotInTypeException extends UnknownMemberException 
     49{ 
     50    public CannotInTypeException(Object obj, String name, Class type) 
     51    { 
     52        super(obj, name, type, null); 
     53    } 
     54 
     55    public CannotInTypeException(Object obj, String name, Class type, Exception innerExc) 
     56    { 
     57    super(obj, name, type, innerExc); 
     58    } 
     59} 
     60 
     61 
    4562/* 
    4663 * Missing, unreadable Member Exceptions 
    4764 * /  
     
    7895            base.init(obj, name, type, innerExc) 
    7996 
    8097 
    81     class CannotInTypeException inherits UnknownMemberException 
    8298 
    83         # CC: axe init()s 
    84  
    85         cue init(obj as Object, name as String, type as Type) 
    86             .init(obj, name, type, nil) 
    87  
    88         cue init(obj as Object, name as String, type as Type, innerExc as Exception?) 
    89             base.init(obj, name, type, innerExc) 
    90  
    91  
    9299class CannotCompareException inherits DynamicOperationException 
    93100 
    94101        var _a 
  • Source/Cobra.Core/Java/PkgSig.java

     
    799799        String tStr = t.toString(); 
    800800        //System.out.printf("### %-50s \n", tStr); 
    801801        tStr = tStr.replace("java.util.Map.java.util.Map", "java.util.Map");  // EntrySets screwed for some reason 
    802          
     802        tStr = tStr.replace("java.nio.file.WatchEvent.java.nio.file.WatchEvent", "java.nio.file.WatchEvent");  
     803        tStr = tStr.replace("java.nio.file.DirectoryStream.java.nio.file.DirectoryStream", "java.nio.file.DirectoryStream"); 
     804 
    803805        tStr = tStr.replace("class ", "");  // sometimes puts on leading 'type ' 
    804806        tStr = tStr.replace("interface ", "");   
    805807        // possibly others - enum ? 
  • Source/Cobra.Core/Java/mkjar

     
    77 
    88[ -d classes ] || mkdir classes 
    99[ -d classes/cobra/lang ] && rm -rf classes/cobra/lang/* 
    10 EXCEPTIONS="AssertException.java InvariantException.java RequireException.java EnsureException.java NonNilCastException.java DynamicOperationException.java" 
    11 javac -d classes SourceSite.java CobraImp.java CobraCore.java Delegate.java ${EXCEPTIONS} CobraDirectString.java 
     10EXCEPTIONS="AssertException.java InvariantException.java RequireException.java EnsureException.java NonNilCastException.java DynamicOperationException.java ExpectException.java CheckedExceptionWrapper.java" 
     11javac -Xlint:unchecked -d classes SourceSite.java CobraImp.java CobraCore.java Delegate.java DelegateO.java BasePair.java Pair.java ${EXCEPTIONS} CobraDirectString.java 
    1212[ $? == 0 ] || exit 
    1313 
    1414jar cvf ${CORE_RTL} -C classes . 
    1515[ $? == 0 ] || exit 
    1616cp ${CORE_RTL} ../.. 
    1717 
    18 #echo 'pkgSig for CobraCore.jar' 
    19 java -cp ".;${CORE_RTL}"  PkgSig -j ${CORE_RTL} >  ${CORE_RTL}.sig 
     18echo 'Gen pkgSig for CobraCore.jar' 
     19java -cp "PkgSig.jar;${CORE_RTL}"  PkgSig -j ${CORE_RTL} >  ${CORE_RTL}.sig 
    2020cp  ${CORE_RTL}.sig  ../.. 
    2121 
    2222 
  • Source/Cobra.Core/Java/SourceSite.java

     
    88 
    99public class SourceSite { 
    1010    /*  
    11      * See Cobra.Lang.SourceSite.cobra 
     11     * See Cobra.Core.SourceSite.cobra 
    1212     *  
    1313     *  A SourceSite instance captures the source location of a statement or expression, as in the 
    1414     *  filename, line number, declaring class name, method name and run-time object. The Cobra 
  • Source/Cobra.Core/Java/mkjarOnly

     
    77 
    88[ -d classes ] || mkdir classes 
    99[ -d classes/cobra/lang ] && rm -rf classes/cobra/lang/* 
    10 EXCEPTIONS="AssertException.java InvariantException.java RequireException.java EnsureException.java NonNilCastException.java DynamicOperationException.java" 
    11 javac -d classes SourceSite.java CobraImp.java CobraCore.java Delegate.java ${EXCEPTIONS} CobraDirectString.java 
     10EXCEPTIONS="AssertException.java InvariantException.java RequireException.java EnsureException.java NonNilCastException.java DynamicOperationException.java ExpectException.java CheckedExceptionWrapper.java" 
     11javac -Xlint:unchecked -d classes SourceSite.java CobraImp.java CobraCore.java Delegate.java DelegateO.java Pair.java ${EXCEPTIONS} CobraDirectString.java 
    1212[ $? == 0 ] || exit 
    1313jar cvf ${CORE_RTL} -C classes . 
    1414[ $? == 0 ] || exit 
    1515cp ${CORE_RTL} ../.. 
    1616 
    1717#echo 'pkgSig for '${CORE_RTL} 
    18 #java -cp ".;${CORE_RTL}"  PkgSig -j ${CORE_RTL} > ${CORE_RTL}.sig 
     18#java -cp "PkgSig.jar;${CORE_RTL}"  PkgSig -j ${CORE_RTL} > ${CORE_RTL}.sig 
    1919#cp ${CORE_RTL}.sig  ../.. 
    2020 
    2121 
  • Source/Cobra.Core/Java/NonNilCastException.java

     
    3939 
    4040/*  ## Misc exceptions 
    4141 
    42     class ExpectException inherits Exception 
     42    class ExpectException extends RuntimeException  
    4343 
    4444        cue init(expectedExceptionType as Type, actualException as Exception?) 
    4545            base.init 
  • Source/Cobra.Core/Java/DelegateO.java

     
     1/* 
     2 * Non genericised Delegate Impl (to be same as .Net) 
     3 * Uses genericised version code  
     4 * Will need codegen support for casting invoke type 
     5 */ 
     6 
     7package cobra.core; 
     8 
     9import java.lang.*; 
     10import java.io.*; 
     11import java.util.*; 
     12 
     13 
     14// Tmp placeholder 
     15public class DelegateO { 
     16    Delegate<Object> delTgt; 
     17    Class returnType; 
     18     
     19    public DelegateO(Class returnType, Class... paramTypes) { 
     20    //public Delegate(Class... paramTypes) { 
     21        this.delTgt = new Delegate<Object>(Object.class, paramTypes); 
     22        this.returnType = returnType; 
     23    } 
     24 
     25    public void init(Object target, String methodName){ 
     26        this.delTgt.init(target, methodName); 
     27    } 
     28 
     29    public void init(Class targetClass, String methodName){ 
     30        this.delTgt.init(targetClass, methodName); 
     31    } 
     32    public Object invoke(Object... arguments) { 
     33        return delTgt.invoke(arguments); 
     34    } 
     35 
     36    public void add(Object target, String methodName){ 
     37        delTgt.add(target, methodName); 
     38    } 
     39     
     40    public void remove(Object target, String methodName){ 
     41        delTgt.remove(target, methodName); 
     42    } 
     43     
     44    public void clear(){ 
     45        delTgt.clear(); 
     46    } 
     47} 
     48 
  • Source/Cobra.Core/Java/Pair.java

     
     1/*  
     2 *  Pair.java 
     3 * 
     4 *  Provides simple pair class hierarchy to satisfy RT requirements for compiler 
     5 *  Rough translation based on cobra implementation for .Net 
     6 */  
     7 
     8package cobra.core; 
     9import java.util.*; 
     10 
     11public class Pair<TA extends Comparable<Object>, TB extends Comparable<Object>>  extends BasePair  
     12implements Iterable, Comparator<Pair<TA, TB>> {  
     13 
     14    final TA a; 
     15    final TB b; 
     16 
     17/*  test 
     18        p = Pair<of int, int>(1, 2) 
     19        assert p.a == 1 and p.b == 2 
     20        assert p[0] == 1 and p[1] == 2 
     21        assert p.toString == 'Pair(1, 2)' 
     22        expect IndexOutOfRangeException, assert p[2] == 3 
     23*/ 
     24    public Pair(TA a, TB b) { 
     25        super(); 
     26        this.a = a; 
     27        this.b = b; 
     28    }  
     29     
     30    public Object getItem(int i) { 
     31        if (i == 0) 
     32            return a; 
     33    if (i == 1) 
     34            return b; 
     35    throw new IndexOutOfBoundsException("Index " + i + " is out of range for a pair (0-1)."); 
     36    } 
     37 
     38    @Override 
     39    public String toString() { 
     40        String sa = CobraCore.toTechString(this.a); 
     41        String sb = CobraCore.toTechString(this.b); 
     42        return String.format("%s (%s, %s)", this.getClass().getName(), sa, sb); 
     43    } 
     44 
     45    //Comparator 
     46    public int compare(Pair<TA, TB> t, Pair<TA, TB> p) { 
     47        /* test 
     48        assert Pair<of String, int>('a', 3) < Pair<of String, int>('b', 4) 
     49        assert Pair<of int, int>(2, 3) > Pair<of int, int>(2, 2) 
     50        assert Pair<of String?, String?>(nil, 'a') < Pair<of String?, String?>(nil, 'b') 
     51        assert Pair<of int?, int?>(nil, nil) < Pair<of int?, int?>(1, 1) 
     52         */  
     53        int cmp = _compare(t.a, p.a); 
     54    if (cmp == 0) 
     55            cmp = _compare(t.b, p.b); 
     56    return cmp; 
     57    } 
     58     
     59    public boolean equals(Pair<TA, TB> p) { 
     60    /*test 
     61        assert Pair<String>('a', 'b').equals(Pair<String>('a', 'b')) 
     62        assert Pair<String>('a', 'b').equals(Pair<String>('a', 'c')) 
     63         */  
     64    return p.a == this.a && p.b == this.b; 
     65    } 
     66     
     67    //Comparable 
     68    public int compareTo(Pair<TA, TB> obj) { 
     69        if (obj == null) return 1; 
     70        if (obj == this) return 0; 
     71        if (obj.getClass() == this.getClass() ) { 
     72            int comp_a = this.a.compareTo(obj.a); 
     73            if (comp_a ==0 ) 
     74                return this.b.compareTo(obj.b); 
     75            return comp_a; 
     76        } 
     77        return -1; 
     78    } 
     79 
     80    @Override 
     81    @SuppressWarnings("unchecked")  
     82    public boolean equals(Object obj) { 
     83        /*test 
     84            assert Pair<String, String>('a', 'b') == Pair<String, String>('a', 'b') 
     85        assert Pair<String, String>('a', 'b') <> Pair<String, String>('a', 'c') 
     86         */ 
     87        if (obj == null)  return false; 
     88        if (obj == this)  return true; 
     89        if (obj.getClass() == this.getClass() ) { 
     90            Pair<TA,TB> p = (Pair<TA,TB>) obj; 
     91            return p.a == this.a && p.b == this.b; 
     92        } 
     93        return false; 
     94    } 
     95     
     96     
     97    @Override 
     98    public int hashCode() { 
     99    /*test 
     100        d = { 
     101            Pair<of int, String>(1, 'one'): 'one', 
     102            Pair<of int, int>(2, 2): 'two', 
     103            Pair<of dynamic, dynamic>(nil, nil): 'nil', 
     104        } 
     105        assert d[Pair<of int, String>(1, 'one')] == 'one' 
     106        assert d[Pair<of int, int>(2, 2)] == 'two' 
     107        assert d[Pair<of dynamic, dynamic>(nil, nil)] == 'nil' 
     108         */ 
     109    // same as CobraCore.HashCodeUtils.combine( (this.a == null) ? 0  : this.a.hashCode(), 
     110        //                          (this.b == null) ? 0  : this.b.hashCode() ); 
     111        int ha = ((this.a == null) ? 0  : this.a.hashCode()); 
     112        int hb = ((this.b == null) ? 0  : this.b.hashCode()); 
     113        int hc = 37; 
     114        hc = hc * 23 + ha; 
     115        hc = hc * 23 + hb; 
     116        return hc; 
     117    } 
     118     
     119    public Iterator<?> iterator() { 
     120    /*test 
     121        assert (for s in Pair<of String, String>('a', 'b') get s) == ['a', 'b'] 
     122         */  
     123        return new PairIterator(); 
     124      } 
     125     
     126    class PairIterator implements Iterator { 
     127        private int ofst =0; 
     128 
     129        public boolean hasNext() {  
     130            return (this.ofst < 2);  
     131        } 
     132 
     133        public Object next() { 
     134            Object ret; 
     135            switch (this.ofst) { 
     136                case 0: ret= a; break; 
     137                case 1: ret = b; break; 
     138                default: 
     139                    throw new NoSuchElementException("Pair: no element after .a and .b") ; 
     140            } 
     141            this.ofst++; 
     142            return ret; 
     143             
     144        } 
     145         
     146        public void remove() { 
     147            throw new UnsupportedOperationException("Pair: no remove operation"); 
     148        } 
     149         
     150    }         
     151     
     152} 
  • Source/Cobra.Core/Java/CobraImp.java

     
    22 * CobraImp.java 
    33 * Native java support code RTL 
    44 * 
    5  *  Currently only stubbed, minimal native code impl. 
    6  * see also Cobra.Lang/NativeExtern.cobra 
     5 *  Currently only stubbed, minimal native code implementation. 
    76 */ 
    87package cobra.core; 
    98 
    109import java.lang.*; 
    1110import java.util.*; 
     11import java.io.PrintStream; 
     12import java.io.PrintWriter; 
     13import java.io.Writer; 
     14import java.io.IOException; 
    1215 
    1316  
    1417public class CobraImp { 
     
    2225            return sb.toString(); 
    2326        } 
    2427        public String makeString(String s) { 
     28            if (s == null) return "nil"; 
    2529            return s; 
    2630        } 
    2731        public String makeString(int i) { 
     
    3438         
    3539        public String makeString(Object... args) { 
    3640            StringBuilder sb = new StringBuilder(); 
    37             for (Object arg : args) 
    38                 sb.append(arg.toString()); 
     41            for (Object arg : args) { 
     42                if (arg == null)  
     43                    sb.append("nil"); 
     44                else 
     45                    sb.append(arg.toString()); 
     46            } 
    3947            return sb.toString(); 
    4048        } 
    4149    } 
     50 
    4251    //static public StringMaker _printStringMaker = new StringMaker(); 
    4352    static public SimpleStringMaker _printStringMaker = new SimpleStringMaker(); 
    44     static public SimpleStringMaker _techStringMaker = new SimpleStringMaker(); 
    45      
     53    static public SimpleStringMaker _techStringMaker  = new SimpleStringMaker(); 
     54 
     55    //static private Deque<PrintStream> _printToStack;  
     56    // above is Stack<PrintStream> TODO: support as PrintWriter 
     57    static private Deque<Writer> _printToStack;  
     58    static private PrintWriter outPW; 
     59    static private boolean _prtErr = false; 
    4660    static { 
    47         //  _printToStack = new Stack<TextWriter>(); 
    48         //  PushPrintTo(Console.Out); 
     61        _printToStack = new ArrayDeque<Writer>();  
     62        outPW = new PrintWriter(System.out, true);   
     63        pushPrintTo(outPW); 
    4964        //  _printStringMaker = new PrintStringMaker(); 
    5065        //  _techStringMaker = new TechStringMaker(); 
    5166        //  PromoteNumerics = NumericTypeInfo.PromoteNumerics; 
    5267        } 
    5368     
     69    static public Writer getPrintDestination() { 
     70        return _printToStack.peek(); 
     71    } 
     72 
     73    // Convenience for System.out 
     74    static public void pushPrintTo(PrintStream ps) { 
     75        Writer wr = new PrintWriter(ps, true); 
     76        pushPrintTo(wr);       
     77    } 
     78     
     79    static public void pushPrintTo(Writer wr) { 
     80    _printToStack.push(wr); 
     81    } 
     82 
     83    static public void popPrintTo() { 
     84    _printToStack.pop(); 
     85    } 
     86 
     87    static public void reset() { 
     88    _printToStack.clear(); 
     89    pushPrintTo(outPW); 
     90    } 
     91     
     92    static public void reset(Writer printDestination) { 
     93    _printToStack.clear(); 
     94    pushPrintTo(printDestination); 
     95    } 
     96     
    5497    static public void printLine() { 
    55         //_printToStack.Peek().WriteLine(); 
    56         System.out.println(); 
     98        printLine(""); 
     99        //_printToStack.peek().println(); 
     100        //System.out.println(); 
    57101    } 
    58  
     102         
     103    // Convenience for System.out 
     104    static public void printLine(PrintStream ps, String s) { 
     105        Writer wr = new PrintWriter(ps, true); 
     106        printLine(wr, s); 
     107    } 
     108     
    59109    static public void printLine(String s) { 
    60         //      _printToStack.Peek().WriteLine(s); 
    61         System.out.println(s); 
     110        printLine(_printToStack.peek(), s); 
    62111    } 
     112    static public void printLine(Writer wr, String s) { 
     113        // We do this manually rather than using a PrintWriter so we can support use of a StringWriter 
     114        // which (of course) doesnt support the useful things like print and println... 
     115        try { 
     116            wr.write(s); 
     117            wr.write(System.lineSeparator()); 
     118            wr.flush(); 
     119        } 
     120        catch(IOException ioe) { 
     121            _prtErr = true; 
     122        } 
     123        //_printToStack.peek().println(s); 
     124        //System.out.println(s); 
     125    } 
    63126 
    64127    static public void printStop() { 
    65128    } 
    66129 
     130    // Convenience for System.out 
     131    static public void printStop(PrintStream ps, String s) { 
     132        Writer wr = new PrintWriter(ps, true); 
     133        printStop(wr, s); 
     134    } 
     135     
    67136    static public void printStop(String s) { 
    68         //      _printToStack.Peek().Write(s); 
    69         System.out.print(s); 
     137        printStop(_printToStack.peek(), s); 
    70138    } 
     139     
     140    static public void printStop(Writer wr, String s) { 
     141        try { 
     142            wr.write(s); 
     143        } 
     144        catch(IOException ioe) { 
     145            _prtErr = true; 
     146        } 
     147             
     148        //_printToStack.peek().print(s); 
     149        //System.out.print(s); 
     150    } 
     151    /* 
     152     * Check if a Print error (IOException on print) has ocurred. 
     153     */ 
     154    static public boolean chkPrtError() { 
     155        return _prtErr; 
     156    } 
    71157 
     158    /* 
     159     * Clear any print error setting, return the value that was set. 
     160     */ 
     161    static public boolean clearPrtError() { 
     162        boolean r = _prtErr; 
     163        _prtErr = false; 
     164        return r; 
     165    } 
     166 
     167 
     168 
    72169    static public String makeString(String[] args) { 
    73170        return _printStringMaker.makeString(args); 
    74171    } 
     
    77174        return s; 
    78175    } 
    79176     
     177    /* 
     178     * Convenience ctors for literal Lists, Sets and Dicts 
     179     */ 
    80180    @SuppressWarnings("unchecked") 
    81181    static public <T> List<T> makeList(T... args) { 
    82     return new ArrayList<T>(Arrays.asList(args)); 
     182    //return new ArrayList<T>(Arrays.asList(args)); 
     183    return Arrays.asList(args); 
    83184    } 
    84185     
    85186    /* 
     
    106207    return d; 
    107208    } 
    108209     
     210     
     211     
     212    static public <T> T checkNotNull(T value, String msg) 
     213    { 
     214        // used for generic checking for non null arg, throws NPE if fails 
     215        // msg expected to contain info and name of item being checked. 
     216        if (/*cobra.core.CobraCore._willCheckNil && */ value == null) { 
     217           throw new NullPointerException(msg); 
     218        } 
     219        return value; 
     220    } 
    109221 
    110222    static public <T> T checkNonNil(Object obj, String sourceCode, T value, /* SourceSite */ String fileName, int lineNum, String className, String memberName, Object thiss)  
    111223    { 
     
    285397     
    286398    static public boolean referenceEquals(Object o, Object o1) { return o == o1; } 
    287399     
     400    static public boolean is(Object a, Object b) { 
     401    return CobraImp.referenceEquals(a, b); 
     402    } 
     403 
     404    static public boolean isNot(Object a, Object b) { 
     405            return !CobraImp.referenceEquals(a, b); 
     406    } 
     407 
     408    static public boolean is(Enum a, Enum b) { 
     409        if (a==null) return b==null; 
     410        if (b==null) return false; 
     411        //   return a.equals(b); 
     412        return a == b;   
     413    } 
    288414     
     415    /* 
     416     * ........ Support for inclusion testing: 'a in b' ...................  
     417     */ 
     418 
     419    static public boolean in(char a, String b) { 
     420    return b.indexOf(a) != -1; 
     421    } 
     422 
     423    static public boolean in(String a, String b) { 
     424    return b.indexOf(a) != -1; 
     425    } 
     426/*    static public boolean in(Object a, Collection b) { 
     427    return b.contains(a); 
     428    } 
     429 
     430    static public boolean in(Object a, Iterable b) { 
     431    for (Object item : b) { 
     432            if (item.equals(a) /*equals(item, a)* /) 
     433        return true; 
     434    } 
     435    return false; 
     436    } 
     437 */ 
     438     
     439    // Covers collections including enums via EnumSet 
     440    static public <T> boolean in(T a, Collection<T> b) { 
     441    return b.contains(a); 
     442    } 
     443 
     444    static public <T> boolean in(T a, Iterable<T> b) { 
     445    for (T item : b) { 
     446        if (item.equals(a) /* Equals(item, a) */) 
     447        return true; 
     448    } 
     449    return false; 
     450    } 
     451 
     452    static public <T> boolean in(T a, Map<T,?> b) { 
     453    return b.containsKey(a); 
     454    } 
     455 
     456/* LATER    static public <T> bool InWithNullCheck(T a, Predicate<T> predicate) { 
     457        if (a == null) return false; 
     458        return predicate(a); 
     459    } 
     460*/ 
     461    static private boolean _noNestedIn = false; 
     462 
     463    static public boolean in(Object a, Object b) { 
     464        if (_noNestedIn) 
     465            throw new RuntimeException(String.format("_noNestedIn a=0$s, a.getClass=%1$s, b=%2$s, b.getClass=%3$s",  
     466                a, a==null ? "" : a.getClass().getName(),  
     467                b, b==null ? "" : b.getClass().getName() ) ); 
     468    _noNestedIn = true; 
     469    try { 
     470        if (b instanceof Collection) {   // probably the most common case, check has to come before Iterable 
     471            return in(a, (Collection<?>)b); 
     472        } else if (b instanceof String) { 
     473            if (a instanceof String) { 
     474            return in((String)a, (String)b); 
     475            } if (a instanceof Character) { 
     476            return in( ((Character)a).charValue(), (String)b); 
     477            } else { 
     478            throw new CannotInTypeException(a, "a of `a in b`", a.getClass()); 
     479            } 
     480        } else if (b instanceof Iterable) { 
     481            return in(a, (Iterable<?>)b); 
     482        } else if (b instanceof Map) { 
     483            return in(a, (Map<?,?>)b); 
     484        } else if (b instanceof Enum) { 
     485                    throw new RuntimeException("Cannnot test inclusion in an Enum, should be using EnumSet");  
     486        } else { 
     487            throw new CannotInTypeException(b, "b of `a in b`", b.getClass()); 
     488        } 
     489    } finally { 
     490        _noNestedIn = false; 
     491    } 
     492    } 
     493     
     494    // support for throws codegen and checked exception wrapping 
     495    static public RuntimeException reThrow(Exception e) { 
     496        if (e instanceof RuntimeException) return (RuntimeException)e; 
     497        return new CheckedExceptionWrapper("Wrapping rethrown exception", e); 
     498    } 
    289499} 
    290500 
  • Source/Cobra.Core/Native.cs

     
    326326        return false; 
    327327    } 
    328328 
     329        static bool In(Object a, Enum b) { 
     330            Type et = b.GetType(); 
     331            if (! Enum.IsDefined(et, a)) 
     332                return false; 
     333            int v = Convert.ToInt32((Enum)a); 
     334            int v1 = Convert.ToInt32(b); 
     335            return (v1 & v) == v; 
     336        } 
     337         
    329338    static public bool In<T>(T a, IList<T> b) { 
    330339        return b.Contains(a); 
    331340    } 
     
    364373                return In(a, (IEnumerable)b); 
    365374            } else if (b is IDictionary) { 
    366375                return In(a, (IDictionary)b); 
     376            } else if (b is Enum) { 
     377                return In(a, (Enum)b); 
    367378            } else { 
    368379                throw new CannotInTypeException(b, "b of `a in b`", b.GetType()); 
    369380            } 
  • Tests/100-basics/307-enums.cobra

     
     1#.require. clr 
    12enum Medium 
    23    of uint64 
    34    air 
  • Tests/100-basics/902-access.cobra

     
     1#.require. clr 
     2@platform clr 
     3 
    14# access System 
    25class Test 
    36 
  • Tests/100-basics/900j-access.cobra

     
     1#.require. jvm 
     2@platform jvm 
     3class Test 
     4 
     5    def main is shared 
     6        assert CobraCore.releaseNum > 0 
     7        assert CobraCore.version > CobraCore.toVersionNumber(0,0,0) 
     8        #print CobraCore.commandLineArgs.size 
     9        assert CobraCore.commandLineArgs.size == 1 
  • Tests/100-basics/902j-access.cobra

     
     1#.require. jvm 
     2@platform jvm 
     3 
     4# access System 
     5class Test 
     6 
     7    def main 
     8        is shared 
     9         
     10        s as Java.Lang.Object = 'aoeu' 
     11        s = 'qwerty' 
     12        CobraCore.noOp(s) 
  • Tests/100-basics/140-break.cobra

     
     1#.require. clr 
     2 
    13namespace Test 
    24     
    35    class Test 
  • Tests/100-basics/140j-break.cobra

     
     1#.require. jvm 
     2@platform jvm 
     3 
     4namespace Test 
     5     
     6    class Test 
     7         
     8        def main 
     9            is shared 
     10                 
     11            x as int = 0 
     12            while x<10 
     13                if true, break 
     14                x += 1 # without 'if true' above java unreachable statement 
     15            assert x==0 
     16             
     17            x = 1 
     18            for i as int in 1 : 10 
     19                if true, break 
     20                x = i 
     21            assert x==1 
     22             
     23            s as String = 'a' 
     24            #l =['a', 'b']  # works 
     25            l = ArrayList<of String>() 
     26            l.add('a') 
     27            l.add('done') 
     28            for arg as String in l 
     29                CobraCore.noOp(arg) 
     30                if true, break 
     31                s = 'b' 
     32            assert s=='a' 
  • Tests/100-basics/150j-continue.cobra

     
     1#.require. jvm 
     2# Modified for jvm for use of commandLine args and unreachable statement errors fm BackEnd  
     3# on statements after a unconditional continue. 
     4 
     5namespace Test 
     6     
     7    class Test 
     8         
     9        def main 
     10            is shared 
     11                 
     12            count as int = 0 
     13            x as int = 0 
     14            while x<10 
     15                x += 1 
     16                if true, continue 
     17                count += 1 #unreachable statement 
     18            assert x==10 
     19            assert count==0 
     20             
     21            x = 1 
     22            for i as int in 1 : 10 
     23                x = i 
     24                if true, continue 
     25                count += 1 #unreachable statement 
     26            assert x==9 
     27            assert count==0 
     28             
     29            s as String = 'a' 
     30            for arg as String in CobraCore.commandLineArgs 
     31                CobraCore.noOp(arg) 
     32                s = 'b' 
     33                if true, continue 
     34                count += 1 #unreachable statement 
     35            assert s=='b' 
     36            assert count==0 
  • Tests/100-basics/700j-print.cobra

     
     1#.require. jvm 
     2@platform jvm 
     3 
     4class Test 
     5 
     6    def main 
     7        is shared 
     8        print 1 
     9        print 1, 2, 3 
     10        print 1 stop 
     11        print 2 stop 
     12        print 3 
     13        print to System.out, 1, 2, 3 
     14        print to System.out 
     15            print 123 
     16 
     17        sw as StringWriter = StringWriter() 
     18        print to sw, 1, 2 
     19        assert sw.toString.trim == '1 2' 
     20        assert sw.toString <> sw.toString.trim 
     21 
     22        sw = StringWriter() 
     23        print to sw, 1, 2 stop 
     24        assert sw.toString == '1 2' 
     25        assert sw.toString == sw.toString.trim 
     26         
     27        sw = StringWriter() 
     28        print to sw      
     29            print 'sw.' 
     30        assert sw.toString == 'sw.'+ System.lineSeparator 
  • Tests/100-basics/200-try-raise.cobra

     
    88            x as int = 0 
    99 
    1010            try 
    11                 throw Exception('') 
    12                 x = 1 
    13             catch 
    14                 pass 
    15             finally 
    16                 x = 2 
    17             assert x==2 
    18  
    19             try 
    20                 x = 1 
    21                 throw Exception('') 
    22                 x = 2 
    23             catch 
    24                 pass 
    25             assert x==1 
    26  
    27  
    28             try 
    29                 x = 0 
    30                 throw Exception('') 
    31             catch e as Exception 
    32                 x = 1 
    33             assert x==1 
    34  
    35  
    36             try 
    37                 x = 0 
    38                 throw Exception('') 
    39             catch nre as NullReferenceException 
     11                if true 
     12                    throw Exception('') 
     13                x = 1 # unreachable code without if above (jvm) 
     14            catch nre as NonNilCastException # Platform independent exception 
    4015                CobraCore.noOp(nre) 
    4116                x = 1 
    4217            catch 
  • Tests/100-basics/150-continue.cobra

     
     1#.require. clr 
    12namespace Test 
    23     
    34    class Test 
  • Tests/100-basics/080j-inherits.cobra

     
    1717            assert 5 inherits int  # .warning. always 
    1818            assert 5 inherits Object  # .warning. always 
    1919            assert not (5 inherits String)  # .warning. never 
    20             assert 5 inherits Integer  # .warning. always 
     20            assert 5 inherits Integer  # .warning. never 
    2121             
    2222            assert true inherits bool  # .warning. always 
    2323            assert true inherits Object  # .warning. always 
  • Tests/100-basics/900-access.cobra

     
     1#.require. clr 
     2@platform clr 
    13class Test 
    24 
    35    def main is shared 
  • Tests/100-basics/310j-enums-combine.cobra

     
     1enum Color 
     2    Red = 1 
     3    Green = 2 
     4    Blue = 4 
     5 
     6class Test 
     7 
     8    def main is shared 
     9        c = Color(Red, Green) # in java BE c is actually an EnumSet 
     10        print c 
     11        .takeColor(Color(Red, Green)) 
     12        # TODO: Should have a qualified example. System.SomeEnum(MemberA, MemberB) and System.Something.SomeEnum(MemberA, MemberB) 
     13         
     14        # testing enum membership with 'in' operator 
     15        assert Color.Red in c 
     16        assert Color.Green in c 
     17        assert Color.Blue not in c 
     18        assert not Color.Blue in c 
     19 
     20    def takeColor(c as Color) is shared 
     21        pass 
  • Tests/100-basics/901j-access.cobra

     
     1#.require. jvm 
     2@platform jvm 
     3# access System 
     4class Test 
     5 
     6    def main 
     7        is shared 
     8         
     9        nl as String = System.lineSeparator 
     10        CobraCore.noOp(nl) 
     11         
     12        nl1 = CobraCore.newLine 
     13        assert nl1 == System.lineSeparator 
  • Tests/100-basics/903j-use-structs.cobra

     
     1#.require. jvm 
     2@platform jvm 
     3 
     4namespace Test 
     5    class Test 
     6        def main 
     7            is shared 
     8             
     9            #now as Date = Date() #DateTime.now 
     10            #assert now.year > 2000 
     11            now as Calendar = Calendar.getInstance 
     12            assert now.get(Calendar.YEAR) > 2000 
  • Tests/100-basics/200j-try-raise.cobra

     
     1#.require. jvm 
     2# mods for unreachable code and using RuntimeException rather than Exception 
     3namespace Test 
     4 
     5    class Test 
     6 
     7        def main 
     8            is shared 
     9 
     10            x as int = 0 
     11 
     12            try 
     13                if true, throw RuntimeException('') 
     14                x = 1 # unreachable code 
     15            catch nre as NonNilCastException # Cobra RTL exception 
     16                CobraCore.noOp(nre) 
     17                x = 1 
     18            catch 
     19                x = 2 
     20            assert x==2 
     21 
     22            try 
     23                x = 0 
     24                throw RuntimeException('') 
     25            catch RuntimeException  # no variable 
     26                x = 1 
     27            assert x==1 
     28 
     29            loc as int 
     30            count as int 
     31 
     32            # rethrow an exception 
     33            try 
     34                loc = 1 
     35                try 
     36                    loc = 2 
     37                    throw RuntimeException('aoeu') 
     38                catch 
     39                    loc = 3 
     40                    throw 
     41            catch e as RuntimeException 
     42                loc = 4 
     43                assert e.message=='aoeu' 
     44            assert loc==4 
     45 
     46            # test that the "success" block executes when no exception is thrown 
     47            try 
     48                loc = 1 
     49                count = 1 
     50            catch 
     51                loc = 2 
     52                count += 1 
     53            success 
     54                loc = 3 
     55                count += 1 
     56            assert loc==3 
     57            assert count==2 
     58 
     59 
     60            # test the the "catch" block does not execute when no exception is throwd 
     61            try 
     62                loc = 1 
     63                count = 1 
     64            catch 
     65                loc = 2 
     66                count += 1 
     67            success 
     68                loc = 3 
     69                count += 1 
     70            finally 
     71                loc = 4 
     72                count += 1 
     73            assert loc==4 
     74            assert count==3 
     75 
     76 
     77            # test that the "success" block does not execute when an exception is throwd 
     78            try 
     79                loc = 1 
     80                count = 1 
     81                throw RuntimeException('') 
     82            catch 
     83                loc = 2 
     84                count += 1 
     85            success 
     86                loc = 3 
     87                count += 1 
     88            assert loc==2 
     89            assert count==2 
     90 
     91 
     92            # just for the heck of it 
     93            try 
     94                pass 
     95            catch 
     96                pass 
     97            success 
     98                pass 
     99            finally 
     100                pass 
     101 
     102            # this actually failed code gen at one point 
     103            try 
     104                pass 
     105            success 
     106                pass 
     107            finally 
     108                pass 
     109 
     110            # test that `success` and `finally` execute even with a `return` in the try part 
     111            .reset 
     112            .returnInTry 
     113            assert not _didCatch and _didSuccess and _didFinally 
     114 
     115            # test that `finally` executes even with an exception 
     116            .reset 
     117            expect RuntimeException, .throwInSuccess 
     118            assert not _didCatch and _didSuccess and _didFinally 
     119 
     120        var _didCatch as bool is shared 
     121        var _didSuccess as bool is shared 
     122        var _didFinally as bool is shared 
     123         
     124        def reset is shared 
     125            _didCatch = _didSuccess = _didFinally = false 
     126 
     127        def returnInTry is shared 
     128            try 
     129                return  # test that a return statement here doesn't side-step `success` or `finally` 
     130            catch 
     131                _didCatch = true 
     132                throw 
     133            success 
     134                _didSuccess = true 
     135            finally 
     136                _didFinally = true 
     137 
     138        def throwInSuccess is shared 
     139            try 
     140                pass 
     141            success 
     142                _didSuccess = true 
     143                throw Exception('aoeu')  # test that an exception here doesn't side-step `finally` 
     144            finally 
     145                _didFinally = true 
  • Tests/100-basics/334a-enums-comma.cobra

     
     1# comma sep syntax, platform independant inclusion test 
     2enum Color 
     3    Red = 1, Green = 2, 
     4    Blue = 4 
     5 
     6class Test 
     7 
     8    def main is shared 
     9        c = Color(Red, Green) 
     10        print c 
     11        .takeColor(Color(Red, Green)) 
     12        # TODO: Should have a qualified example. System.SomeEnum(MemberA, MemberB) and System.Something.SomeEnum(MemberA, MemberB) 
     13         
     14        # testing enum membership with 'in' 
     15        assert Color.Red in c 
     16        assert Color.Green in c 
     17        assert Color.Blue not in c 
     18        assert not Color.Blue in c 
     19 
     20    def takeColor(c as Color) is shared 
     21        pass 
  • Tests/100-basics/700-print.cobra

     
     1#.require. clr 
     2@platform clr 
     3 
    14class Test 
    25 
    36    def main 
  • Tests/100-basics/310-enums-combine.cobra

     
     1#.require. clr 
     2@platform clr 
     3 
    14enum Color 
    25    Red = 1 
    36    Green = 2 
  • Tests/100-basics/901-access.cobra

     
     1#.require. clr 
     2@platform clr 
    13# access System 
    24class Test 
    35 
  • Tests/100-basics/903-use-structs.cobra

     
     1#.require. clr 
     2@platform clr 
     3 
    14namespace Test 
    25    class Test 
    36        def main 
  • Tests/100-basics/300-enums.cobra

     
     1#.require. clr 
     2@platform clr 
     3# This test of enums as value types is specific to the .Net environment 
     4# other platforms treat enums differently. 
    15enum Colors 
    26    Red 
    37    Green 
  • Tests/100-basics/334-enums-comma.cobra

     
    1 # comma sep syntax 
     1#.require. clr 
     2@platform clr 
     3# comma seperated syntax 
    24enum Color 
    35    Red = 1, Green = 2, 
    46    Blue = 4 
     
    810    def main is shared 
    911        c = Color(Red, Green) 
    1012        print c 
    11         print c to int 
     13        print c to int # non portable - assumes Enums castable to int 
    1214        .takeColor(Color(Red, Green)) 
    1315        # TODO: Should have a qualified example. System.SomeEnum(MemberA, MemberB) and System.Something.SomeEnum(MemberA, MemberB) 
    1416         
    15         # testing enum membership with bitwise-and 
     17        # testing enum membership with bitwise-and (also int centric) 
    1618        assert c & Color.Red == Color.Red 
    1719        assert c & Color.Green == Color.Green 
    1820        assert c & Color.Blue <> Color.Blue