Wiki

Changeset 2316

Show
Ignore:
Timestamp:
03/14/10 06:45:43 (2 years ago)
Author:
Chuck.Esterbrook
Message:

Arrays and lists can now be compared with == and <>.

Location:
cobra/trunk
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/Source/Boxes.cobra

    r2307 r2316  
    537537            r = true 
    538538        if not r and ('dynamic' in .name or 'dynamic' in t.name)  # TODO: hacky 
     539            r = true 
     540        if not r and t inherits ArrayType and (t to ArrayType).isEquatableTo(this)  # TODO: hacky 
    539541            r = true 
    540542        return r 
  • cobra/trunk/Source/Compiler.cobra

    r2315 r2316  
    486486    var _anyFloatType as AnyFloatType? 
    487487    var _anyIntType as AnyIntType? 
     488    var _arrayTypes as Dictionary<of IType, ArrayType>? 
    488489    var _boolType as BoolType? 
    489490    var _charType as CharType? 
     
    518519            _anyIntType = AnyIntType() 
    519520        return _anyIntType to ! 
     521 
     522    def arrayType(type as IType) as ArrayType 
     523        if _arrayTypes is nil, _arrayTypes = Dictionary<of IType, ArrayType>() 
     524        if _arrayTypes.containsKey(type) 
     525            return _arrayTypes[type] 
     526        else 
     527            _arrayTypes[type] = at = ArrayType(type) 
     528            at.bindInh 
     529            at.bindInt 
     530            return at 
    520531 
    521532    get boolType as BoolType 
  • cobra/trunk/Source/Expr.cobra

    r2304 r2316  
    12041204            if _target.isKindOf(.compiler.typeType) 
    12051205                if _target inherits IPotentialTypeExpr and (pt = (_target to IPotentialTypeExpr).potentialType) 
    1206                     _transformTo(TypeExpr(.token, ArrayType(pt))) 
    1207                     return                   
     1206                    _transformTo(TypeExpr(.token, .typeProvider.arrayType(pt to !))) 
     1207                    return 
    12081208                else 
    12091209                    .throwError('Unknown array type.') 
     
    28382838 
    28392839 
    2840 class ArrayLit 
    2841     is partial 
    2842     inherits SequenceLit 
     2840class ArrayLit inherits SequenceLit is partial 
    28432841 
    28442842    # CC: should just inherit this because no initializers are defined 
     
    28472845 
    28482846    def _makeTypeWith(type as IType) as IType is override 
    2849         return ArrayType(type) 
     2847        return .typeProvider.arrayType(type) 
    28502848 
    28512849    get brackets as List<of String> is override 
  • cobra/trunk/Source/TypeProxies.cobra

    r2304 r2316  
    185185        if clrType.isArray 
    186186            # assert clrType.name.endsWith(r'[]') # could be [,] so TODO: handle multidim arrays 
    187             return ArrayType(_realTypeWithCache(clrType.getElementType to !)) 
     187            return .typeProvider.arrayType(_realTypeWithCache(clrType.getElementType to !)) 
    188188        else if clrType.isNested and not clrType.isGenericParameter 
    189189            declaringType = _realTypeWithCache(clrType.declaringType to !) 
     
    462462 
    463463 
    464 class ArrayTypeIdentifier 
    465     inherits WrappedTypeIdentifier 
     464class ArrayTypeIdentifier inherits WrappedTypeIdentifier 
    466465 
    467466    cue init(token as IToken, typeId as AbstractTypeIdentifier) 
     
    472471 
    473472    def _resolveType as IType is override 
    474         t = ArrayType(_typeId.realType) 
    475         t.bindInh 
    476         t.bindInt 
    477         return t 
     473        return .typeProvider.arrayType(_typeId.realType) 
    478474 
    479475 
  • cobra/trunk/Source/Types.cobra

    r2313 r2316  
    7070    def typeOrNilForName(name as String) as IType? 
    7171 
     72    def arrayType(type as IType) as ArrayType 
     73 
    7274    get boolType as BoolType 
    7375 
     
    122124    var _anyFloatType       as AnyFloatType? 
    123125    var _anyIntType         as AnyIntType? 
     126    var _arrayTypes         as Dictionary<of IType, ArrayType>? 
    124127    var _boolType           as BoolType? 
    125128    var _charType           as CharType? 
     
    154157            _anyIntType = AnyIntType() 
    155158        return _anyIntType to ! 
     159 
     160    def arrayType(type as IType) as ArrayType 
     161        if _arrayTypes is nil, _arrayTypes = Dictionary<of IType, ArrayType>() 
     162        if _arrayTypes.containsKey(type) 
     163            return _arrayTypes[type] 
     164        else 
     165            _arrayTypes[type] = at = ArrayType(type) 
     166            at.bindInh 
     167            at.bindInt 
     168            return at 
    156169 
    157170    get boolType as BoolType 
     
    975988        return r 
    976989 
    977     # TODO? 
    978 #   def isComparableTo(t as IType) as bool 
    979 #       return this is t 
    980  
    981 #   def isEquatableTo(t as IType) as bool 
    982  
    983990    def isComparableTo(t as IType) as bool 
    984991        return this is t or base.isComparableTo(t) 
     992 
     993# TODO? 
     994#   def isEquatableTo(t as IType) as bool 
    985995 
    986996 
     
    16711681    """ 
    16721682    Represents an array. 
     1683 
    16731684    Only single dimension arrays have been tested and are officially supported. These are common in 
    16741685    the BCL as return types and sometimes as parameters. Multi-dim and jagged arrays are not popular. 
    16751686    Also, most projects can get by just fine with nested List<of>s. 
    1676     TODO: Nevertheless, this should be expanded. 
     1687 
     1688    Rather than instantiating this type directly, use: 
     1689     
     1690        .typeProvider.arrayType(t) 
     1691 
     1692    Or if the code block already belongs to a type provider: 
     1693     
     1694        .arrayType(t) 
     1695         
    16771696    TODO: Get this related to System.Array. 
    16781697    """ 
     
    16891708            Node.typeProvider = saveTP 
    16901709 
     1710    var _ilistOf as Interface? 
     1711     
    16911712    cue init(wrappedType as IType) 
    16921713        base.init(wrappedType) 
     
    17251746        return base.isDescendantOf(type) or .box.isDescendantOf(type) 
    17261747 
     1748    def isEquatableTo(t as IType) as bool 
     1749        assert .didBindInh 
     1750        return base.isEquatableTo(t) or t.isDescendantOf(_ilistOf to !) or _ilistOf.isDescendantOf(t) 
     1751 
    17271752    def memberForName(name as String) as IMember? is override 
    17281753        m = base.memberForName(name) 
     
    17361761        return m 
    17371762 
     1763    def secondaryConstructedTypeFor(box as Box, gpToType as Dictionary<of GenericParam, IType>) as IType is override 
     1764        at = ArrayType(_wrappedType.secondaryConstructedTypeFor(box, gpToType)) 
     1765        at.bindInh 
     1766        at.bindInt 
     1767        return at 
     1768 
    17381769    def suggestionsForBadMemberName(name as String) as List<of String> is override 
    17391770        return .box.suggestionsForBadMemberName(name) 
     
    17451776            icoll = (.compiler.libraryType('System.Collections.Generic.ICollection<of>') to Box).constructedTypeFor([.theWrappedType]) 
    17461777            ienum = .compiler.enumerableOfType.constructedTypeFor([.theWrappedType]) 
    1747             # TODO: ilist = .compiler.ilistOfType.constructedTypeFor([.theWrappedType]) 
    1748             interfaces = [iclone to ITypeProxy, icoll, ienum]  # TODO:, ilist]  -- problems with cobra -ert:yes hello and extension method String.split 
     1778            _ilistOf = .compiler.ilistOfType.constructedTypeFor([.theWrappedType]) to Interface 
     1779            interfaces = [iclone to ITypeProxy, icoll, ienum]  # TODO:, _ilistOf]  -- problems with cobra -ert:yes hello and extension method String.split 
    17491780            _box = Class(Token.empty, Token.empty, '[.getType.name]_[.serialNum]', List<of IType>(), List<of String>(), AttributeList(), nil, interfaces, List<of ITypeProxy>(), nil) 
    17501781            # TODO: make members based on System.Array 
     
    17651796 
    17661797 
    1767 class VariType 
    1768     is partial 
    1769     inherits ArrayType 
     1798class VariType inherits ArrayType is partial 
    17701799    """ 
    17711800    Represents the type for variable number of arguments: 
     
    17741803    """ 
    17751804 
    1776     var _ilistOf as Interface?  # TODO: does ArrayType need _ilistOf too? 
    1777  
    17781805    cue init(wrappedType as IType) 
    17791806        base.init(wrappedType) 
     
    17851812        return _wrappedType 
    17861813 
    1787     def isEquatableTo(t as IType) as bool is override 
    1788         # vari type inherits IList<of> 
    1789         assert .didBindInh 
    1790         return base.isEquatableTo(t) or t.isDescendantOf(_ilistOf to !) or _ilistOf.isDescendantOf(t) 
    1791  
    17921814    get name as String is override 
    17931815        return 'vari ' + _wrappedType.name 
     
    17951817    def _bindInh 
    17961818        base._bindInh 
    1797         if .compiler  # will be nil during unit tests 
    1798             _ilistOf = .compiler.ilistOfType.constructedTypeFor([.theWrappedType]) to Interface 
    1799             _ilistOf.bindInh 
    18001819 
    18011820    def _bindInt 
  • cobra/trunk/Tests/240-generics/100-use-generics-collections/100-use-list.cobra

    r1177 r2316  
    11class Test 
    22 
    3     def main is shared 
    4  
     3    def main 
     4        .testBasics 
     5        .compareWithArrays 
     6     
     7    def testBasics 
    58        t as List<of String> = List<of String>() 
    69        assert t.count == 0 
     
    2225#       assert v[0].equals('aoeu') 
    2326#       assert v[1].equals(1) 
     27 
     28    def compareWithArrays 
     29        a as int[] = @[1, 2, 3] 
     30        b as List<of int> = [1, 2, 3] 
     31        c as List<of int> = [1, 2, 3, 4] 
     32        assert a == b 
     33        assert b == a 
     34        assert a <> c 
     35        assert c <> a