Changeset 1781

Show
Ignore:
Timestamp:
11/28/08 15:11:01 (16 months ago)
Author:
Chuck.Esterbrook
Message:

Added streams to the language. For example, int* or String*

Location:
cobra/trunk
Files:
1 added
8 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/Source/BackEndClr/SharpGenerator.cobra

    r1768 r1781  
    789789                return '0' 
    790790 
     791 
    791792class PassThroughType 
    792793        is partial 
     
    797798        get sharpRef as String is override 
    798799                return '/*passthrough*/object' 
     800 
     801 
     802class StreamType 
     803        is partial 
     804 
     805        get sharpInit as String is override 
     806                return 'new EmptyStream<[.theWrappedType.sharpRef]>()' 
     807 
     808        get sharpRef as String is override 
     809                assert .didBindInh and .didBindInt 
     810                return '/*[.name]*/[.box.sharpRef]' 
    799811 
    800812 
     
    14881500                        sw.write(' = ') 
    14891501                        _initExpr.writeSharpDef(sw) 
     1502                else if .type.isReference and not .type.nonNil inherits GenericParam 
     1503                        sharpInit = .type.sharpInit 
     1504                        if sharpInit.length 
     1505                                sw.write(' = ') 
     1506                                sw.write(sharpInit) 
    14901507                sw.write(';\n') 
    14911508 
  • cobra/trunk/Source/Boxes.cobra

    r1768 r1781  
    700700                                else if type.isGenericDef and .isGeneric and type is .genericDef 
    701701                                        r = type.isAssignableTo(this) 
     702                if not r 
     703                        if type inherits StreamType 
     704                                r = .isAssignableTo(type.box to !) 
    702705                return r 
    703706 
  • cobra/trunk/Source/Cobra.Lang/Misc.cobra

    r1773 r1781  
    11namespace Cobra.Lang 
    22 
     3        class EmptyStream<of T> 
     4                implements IEnumerable<of T> 
     5         
     6                def getEnumerator as IEnumerator<of T> 
     7                        yield break 
     8         
     9                def getEnumerator as System.Collections.IEnumerator 
     10                        implements System.Collections.IEnumerable 
     11                        return .getEnumerator  
     12         
    313        interface ITreeBuilder 
    414                def indent 
  • cobra/trunk/Source/CobraParser.cobra

    r1761 r1781  
    32523252                        t = ArrayTypeIdentifier(bracket, t) 
    32533253 
    3254                 # check for 'optional' aka 'can be nil' 
    3255                 question = .optional('QUESTION') 
    3256                 if question 
    3257                         t = NilableTypeIdentifier(question, t) 
     3254                while true 
     3255                        # check for 'optional' aka 'can be nil' 
     3256                        if .optional('QUESTION') 
     3257                                t = NilableTypeIdentifier(.last, t) 
     3258         
     3259                        # check for 'stream' aka 'multiple' aka 'zero or more' aka 'enumerable' 
     3260                        else if .optional('STAR') 
     3261                                t = StreamTypeIdentifier(.last, t) 
     3262 
     3263                        else 
     3264                                break 
    32583265 
    32593266                return t to ! 
  • cobra/trunk/Source/Container.cobra

    r1768 r1781  
    300300                return false 
    301301 
     302        get isUninitializedForLocalVars as bool 
     303                return .isReference 
     304 
    302305        get innerType as IType? 
    303306                return nil 
  • cobra/trunk/Source/Expr.cobra

    r1769 r1781  
    352352        def afterStatementBindImp is override 
    353353                base.afterStatementBindImp 
    354                 if not .type inherits NilableType 
    355                         if .type.isReference 
    356                                 .throwError('Must initialize this non-nil object type, or change the type to nilable (suffix a ?).') 
     354                if .type.isUninitializedForLocalVars 
     355                        .throwError('Must initialize this non-nil object type, or change the type to nilable (suffix with ?).') 
    357356 
    358357 
  • cobra/trunk/Source/TypeProxies.cobra

    r1759 r1781  
    357357                        return container.memberForName(.name) 
    358358                Generics have to do more work, though. 
     359                So do types with funky names. 
    359360                """ 
    360361                return container.memberForName(.name) 
     
    446447class NilableTypeIdentifier 
    447448        inherits WrappedTypeIdentifier 
     449        """ 
     450        Foo? 
     451        """ 
    448452 
    449453        def init(token as IToken, typeId as AbstractTypeIdentifier) 
     
    462466                else 
    463467                        return m 
     468 
     469 
     470class StreamTypeIdentifier 
     471        inherits WrappedTypeIdentifier 
     472        """ 
     473        Foo* 
     474        """ 
     475 
     476        def init(token as IToken, typeId as AbstractTypeIdentifier) 
     477                base.init(token, typeId) 
     478         
     479        def _resolveType as IType is override 
     480                t = StreamType(_typeId.realType) 
     481                t.bindInh 
     482                t.bindInt 
     483                return t 
     484 
     485        get name as String is override 
     486                return _typeId.name + '*' 
    464487 
    465488 
  • cobra/trunk/Source/Types.cobra

    r1768 r1781  
    306306 
    307307        def isAssignableTo(t as IType) as bool 
    308                 return this is t or t.isSystemObjectClass 
     308                return this == t or t.isSystemObjectClass 
    309309 
    310310        def isComparableTo(t as IType) as bool 
    311                 return this is t 
     311                return this == t 
    312312 
    313313        def isEquatableTo(t as IType) as bool 
    314                 return this is t 
     314                return this == t 
    315315 
    316316        def isDescendantOf(t as IType) as bool 
     
    320320                return not _nativeType.isValueType 
    321321 
     322        get isUninitializedForLocalVars as bool 
     323                return .isReference 
     324 
    322325        def isStrictDescendantOf(t as IType) as bool 
    323326                return this is not t and t.isSystemObjectClass 
     
    329332 
    330333        def greatestCommonDenominatorWith(type as IType) as IType 
    331                 if type is this 
     334                if type == this 
    332335                        return this 
    333336                else 
     
    505508                """ 
    506509                Return true if this type represents/is System.Type. 
     510                """ 
     511 
     512        get isUninitializedForLocalVars as bool 
     513                """ 
     514                Return true if a local var of this type is uninitialized without an explicit assignment. 
     515                The default implementation is typically: return .isReference 
     516                because non-nil reference types need an initial value. 
    507517                """ 
    508518 
     
    624634                return false 
    625635 
     636        get isUninitializedForLocalVars as bool 
     637                return .isReference 
     638 
    626639        def isComparableTo(t as IType) as bool 
    627640                t = t.nonNil 
     
    636649        def isEquatableTo(t as IType) as bool 
    637650                t = t.nonNil 
     651                if this == t 
     652                        return true 
    638653                if .isAssignableTo(t) or t.isAssignableTo(this) 
    639654                        return true 
     
    667682 
    668683        def greatestCommonDenominatorWith(type as IType) as IType 
    669                 if this is type 
     684                if this == type 
    670685                        return this 
    671686                if type inherits NilableType 
     
    689704                        .typeProvider 
    690705                body 
    691                         if type is .typeProvider.passThroughType 
    692                                 return true 
    693                         if type is .typeProvider.dynamicType 
    694                                 return true 
    695                         if type is .typeProvider.objectType 
    696                                 return true 
    697                         # TODO: remove this: 
    698                         if type inherits GenericParam 
    699                                 return true 
     706                        if this == type, return true 
     707                        if type is .typeProvider.passThroughType, return true 
     708                        if type is .typeProvider.dynamicType, return true 
     709                        if type is .typeProvider.objectType, return true 
     710                        if type inherits GenericParam, return true  # TODO: remove this: 
    700711                        # TODO: would this be needed if not for qualified types? 
    701                         if type inherits WrappedType 
     712                        if type inherits WrappedType  # TODO: I think this is wrong as it covers VariType and StreamType. It should only be non-nil 
    702713                                return .isAssignableTo(type.theWrappedType to passthrough) # CC: bug with if-inherits changes the parameter type 
    703714                        return .isDescendantOf(type) 
     
    707718                Returns true if this type is a subtype (direct or indirect) of type. 
    708719                """ 
    709                 if not .didBindInh 
    710                         .bindInh 
    711                 if this is type 
    712                         return true 
     720                if not .didBindInh, .bindInh 
     721                if this == type, return true 
    713722                curType = this to IType? 
    714723                counter = 0 
    715724                while true 
    716                         if curType is type 
    717                                 return true 
     725                        if curType == type, return true 
    718726                        curType = curType.superType 
    719                         if curType is nil 
    720                                 break 
     727                        if curType is nil, break 
    721728                        counter += 1 
    722729                        assert counter < 1000 
     
    724731 
    725732        def isStrictDescendantOf(type as IType) as bool 
    726                 if type is this 
     733                if type == this 
    727734                        return false 
    728735                else 
     
    10411048 
    10421049        def greatestCommonDenominatorWith(type as IType) as IType is override 
    1043                 if this is type 
     1050                if this == type 
    10441051                        return this 
    10451052                if type inherits NilableType 
     
    11371144 
    11381145        def isAssignableTo(type as IType) as bool 
    1139                 if type is this  # common case 
     1146                if type == this  # common case 
    11401147                        return true 
    11411148                if type inherits FloatType 
     
    12701277 
    12711278        def isAssignableTo(type as IType) as bool 
    1272                 if type is this  # common case 
     1279                if type == this  # common case 
    12731280                        return true 
    12741281                if type is .typeProvider.decimalType 
     
    13221329                return _wrappedType.isReference 
    13231330 
     1331        get isUninitializedForLocalVars as bool is override 
     1332                return false  # because nil is fine for the default value 
     1333 
    13241334        # TODO: I think the following is appropriate, but come up with a test case for it first. 
    13251335        #get innerType 
     
    13541364 
    13551365        def greatestCommonDenominatorWith(type as IType) as IType is override 
    1356                 if this is type 
     1366                if this == type 
    13571367                        return this 
    13581368                if type inherits NilType 
    13591369                        return this 
    13601370                if type inherits NilableType 
    1361                         if _wrappedType is type.nonNil 
     1371                        if _wrappedType == type.nonNil 
    13621372                                return this 
    13631373                        else 
     
    13801390 
    13811391        def isAssignableTo(type as IType) as bool is override 
    1382                 if this is type, return true 
     1392                if this == type, return true 
    13831393                if type is .compiler.passThroughType, return true 
    13841394                if type inherits NilableType, return true 
     
    13861396 
    13871397        def greatestCommonDenominatorWith(type as IType) as IType is override 
    1388                 if this is type 
     1398                if this == type 
    13891399                        return this 
    13901400                if type inherits NilableType 
     
    14491459 
    14501460 
     1461class StreamType 
     1462        is partial 
     1463        inherits WrappedType 
     1464        """ 
     1465        - Streams are portable between backends, whereas .NET's IEnumerable<of> and JVM's Iterable<of> are not. 
     1466        - Streams are two-way compatible with IEnumerable/Iterable: 
     1467                - Streams can be used whereever a compatible IEnumerable/Iterable is expected. 
     1468                - An IEnumerable/Iterable can be used whereever a compatible stream type is expected. 
     1469                - The term "compatible" means the same inner type: int* is compatible with IEnumerable<of int>/Iterable<of int>, but not IEnumerable<of String>/Iterable<of String> 
     1470        - Streams are an abstract type, so you cannot create them directly with a call on the type such as `int*()`. Instead, use a concrete class such as `List<of>` or `yield` results out of a method. 
     1471        - Streams are defaulted to be empty rather than nil. 
     1472        """ 
     1473 
     1474        var _box as Box? 
     1475 
     1476        def init(t as IType) 
     1477                base.init(t) 
     1478 
     1479        get englishName as String is override 
     1480                return 'stream of ' + _wrappedType.englishName 
     1481 
     1482        get box from var 
     1483 
     1484        get name as String is override 
     1485                return _wrappedType.name + '*' 
     1486 
     1487        get isReference as bool is override 
     1488                return true 
     1489 
     1490        get isUninitializedForLocalVars as bool is override 
     1491                return false  # because streams get initialized to EmptyStream<of T> for convenience 
     1492 
     1493        get innerType as IType? is override 
     1494                return .theWrappedType 
     1495 
     1496        def isAssignableTo(type as IType) as bool is override 
     1497                return base.isAssignableTo(type) 
     1498 
     1499        def isComparableTo(b as IType) as bool is override 
     1500                return base.isComparableTo(b) 
     1501 
     1502        def isDescendantOf(type as IType) as bool 
     1503                return base.isDescendantOf(type) or .box.isDescendantOf(type) 
     1504 
     1505        def isEquatableTo(b as IType) as bool is override 
     1506                return base.isEquatableTo(b) 
     1507 
     1508        def memberForName(name as String) as IMember? is override 
     1509                return _box.memberForName(name) 
     1510 
     1511        def greatestCommonDenominatorWith(type as IType) as IType is override 
     1512                return base.greatestCommonDenominatorWith(type) 
     1513 
     1514        def _bindInh 
     1515                base._bindInh 
     1516                if .compiler  # will be nil during unit tests 
     1517                        _box = .compiler.enumerableOfType.constructedTypeFor([.theWrappedType]) 
     1518                        _box.bindInh 
     1519                        _superType = _box 
     1520                else if _superType is nil  # TODO: is this really needed? 
     1521                        _superType = .compiler.objectType 
     1522 
     1523        def _bindInt 
     1524                base._bindInt 
     1525                # TODO: why do we have to do this? 
     1526                if not .didBindInh 
     1527                        .bindInh 
     1528 
     1529 
    14511530class VoidType 
    14521531        is partial 
     
    15151594 
    15161595        get innerType as IType? is override 
    1517                 return _wrappedType.innerType 
     1596                return _wrappedType.innerType  # TODO: 2008-11-22, I think this should just be _wrappedType 
    15181597 
    15191598        def secondaryConstructedTypeFor(box as Box, gpToType as Dictionary<of GenericParam, IType>) as IType is override 
     
    15601639 
    15611640        get name as String is override 
    1562                 return _wrappedType.name+r'[]' 
     1641                return _wrappedType.name + r'[]' 
    15631642 
    15641643        def isAssignableTo(type as IType) as bool is override 
     
    15681647                        if _wrappedType == type.theWrappedType 
    15691648                                return true 
     1649                if type inherits StreamType 
     1650                        return .isAssignableTo(type.box to !) 
    15701651                return false 
    15711652 
    15721653        def _bindInh 
    15731654                base._bindInh 
    1574                 ienum = .compiler.enumerableOfType 
    1575                 _superType = ienum.constructedTypeFor([.theWrappedType]) 
     1655                _superType = .compiler.enumerableOfType.constructedTypeFor([.theWrappedType]) 
    15761656 
    15771657        def memberForName(name as String) as IMember?