Changeset 1596

Show
Ignore:
Timestamp:
08/28/08 05:23:09 (3 months ago)
Author:
Chuck.Esterbrook
Message:

Test that the default type for vars, properties and method arguments is dynamic?

Location:
cobra/trunk
Files:
1 added
4 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/HowTo/290-TranslatePseudoCodeToCobra1.cobra

    r1454 r1596  
    55 
    66Note that were pseudo-code says "i = i+1", the Cobra code uses augmented assignment 
    7 operators such as "i += 1". However, you can still write "i = i+1" if you choose. 
     7operators such as "i += 1". However, you can still write "i = i+1" if you like. 
     8 
     9Note that the default type for an argument is dynamic? which means that the argument 
     10can be any object or nil. 
    811 
    912Authors: Max Grender-Jones, Chuck Esterbrook 
     
    2326                        assert sortMethod([1,1,1,1]) == [1,1,1,1] 
    2427 
    25                 def bubbleSort(list) as dynamic 
     28                def bubbleSort(list) as dynamic? 
    2629                        """ 
    2730                        From http://en.wikipedia.org/wiki/Bubble_sort#Pseudocode_implementation 
     
    4144                                .testSort(ref .bubbleSort) 
    4245                        body 
    43                                 swapped = false 
    44                                 post while swapped 
     46                                if list 
    4547                                        swapped = false 
    46                                         for i in list.count - 1 
    47                                                 if list[i] > list[i+1] 
    48                                                         list.swap(i, i+1) 
    49                                                         swapped = true 
     48                                        post while swapped 
     49                                                swapped = false 
     50                                                for i in list.count - 1 
     51                                                        if list[i] > list[i+1] 
     52                                                                list.swap(i, i+1) 
     53                                                                swapped = true 
    5054                                return list 
    5155 
    52                 def heapSort(list) as dynamic 
     56                def heapSort(list) as dynamic? 
    5357                        """ 
    5458                        From http://en.wikipedia.org/wiki/Heapsort#Pseudocode 
     
    7276                                .testSort(ref .heapSort) 
    7377                        body 
    74                                 .heapify(list) 
    75                                 last = list.count - 1 
    76                                 while last > 0 
    77                                         list.swap(0, last) 
    78                                         last -= 1 
    79                                         .siftDown(list, 0, last) 
     78                                if list 
     79                                        .heapify(list) 
     80                                        last = list.count - 1 
     81                                        while last > 0 
     82                                                list.swap(0, last) 
     83                                                last -= 1 
     84                                                .siftDown(list, 0, last) 
    8085                                return list 
    8186 
    82                 def heapify(list) 
     87                def heapify(list as dynamic) 
    8388                        """ 
    8489                        From http://en.wikipedia.org/wiki/Heapsort#Pseudocode 
     
    129134                                        return 
    130135 
    131                 def insertionSort(list) as dynamic  
     136                def insertionSort(list) as dynamic? 
    132137                        """ 
    133138                        From http://en.wikipedia.org/wiki/Insertion_sort 
     
    144149                                .testSort(ref .insertionSort) 
    145150                        body 
    146                                 for i in 1 : list.count 
    147                                         value = list[i] 
    148                                         j = i - 1 
    149                                         while j >= 0 and list[j] > value 
    150                                                 list[j+1] = list[j] 
    151                                                 j -= 1 
    152                                         list[j+1] = value 
     151                                if list 
     152                                        for i in 1 : list.count 
     153                                                value = list[i] 
     154                                                j = i - 1 
     155                                                while j >= 0 and list[j] > value 
     156                                                        list[j+1] = list[j] 
     157                                                        j -= 1 
     158                                                list[j+1] = value 
    153159                                return list 
    154160 
    155161 
    156                 def quickSort(list) as dynamic 
     162                def quickSort(list) as dynamic? 
    157163                        """ 
    158164                        From http://en.wikipedia.org/wiki/Quicksort 
     
    171177                                .testSort(ref .quickSort) 
    172178                        body 
    173                                 if list.count <= 1 
     179                                if list is nil or list.count <= 1 
    174180                                        return list 
    175181                                else 
  • cobra/trunk/Source/CobraParser.cobra

    r1593 r1596  
    11501150                        initExpr = nil 
    11511151                        if type is nil 
    1152                                 type = .typeProvider.dynamicType 
     1152                                type = .typeProvider.defaultType 
    11531153                docString = '' to ? 
    11541154                isNames = List<of String>(_isNamesStack) 
     
    13981398                        returnType = .typeId to ITypeProxy 
    13991399                else 
    1400                         returnType = _typeProvider.dynamicType 
     1400                        returnType = _typeProvider.defaultType 
    14011401                # TODO: implements? 
    14021402                hasIsNames = false 
     
    15281528                        returnType = .typeId to ITypeProxy 
    15291529                else 
    1530                         returnType = _typeProvider.dynamicType 
     1530                        returnType = _typeProvider.defaultType 
    15311531                # TODO: implements? 
    15321532                hasIsNames = false 
     
    16591659                        isMissingType = false 
    16601660                else 
    1661                         type = TypeProxy(_typeProvider.dynamicType)  # the default type for a parameter is dynamic 
     1661                        type = TypeProxy(_typeProvider.defaultType) 
    16621662                        isMissingType = true 
    16631663                # note: isTypeless is currently used to generate a warning in .paramDecls above, but in the future it may also be needed for when parameter types are inferred for anon methods 
  • cobra/trunk/Source/Compiler.cobra

    r1583 r1596  
    439439        var _voidType as VoidType? 
    440440        var _variTypes as Dictionary<of INode, VariType>? 
     441        var _defaultType as IType? 
    441442 
    442443        def typeForName(name as String) as IType 
     
    558559                _variTypes[type] = vt = VariType(type) 
    559560                return vt 
     561 
     562        def defaultType as IType 
     563                if _defaultType is nil 
     564                        _defaultType = .nilableType(.dynamicType) 
     565                return _defaultType to ! 
    560566 
    561567 
  • cobra/trunk/Source/Types.cobra

    r1583 r1596  
    5555 
    5656        def clrType(qualifiedName as String) as System.Type 
     57 
     58        def defaultType as IType 
    5759 
    5860 
     
    7779        var _voidType                   as VoidType? 
    7880        var _variTypes                  as Dictionary<of IType, VariType>? 
     81        var _defaultType                as IType? 
    7982 
    8083        def typeForName(name as String) as IType 
     
    184187        def clrType(qualifiedName as String) as System.Type 
    185188                return sharp'Type.GetType(qualifiedName)' to System.Type 
     189 
     190        def defaultType as IType 
     191                if _defaultType is nil 
     192                        _defaultType = NilableType(.dynamicType) 
     193                return _defaultType to ! 
    186194 
    187195