Changeset 1596
- Timestamp:
- 08/28/08 05:23:09 (3 months ago)
- Location:
- cobra/trunk
- Files:
-
- 1 added
- 4 modified
-
HowTo/290-TranslatePseudoCodeToCobra1.cobra (modified) (7 diffs)
-
Source/CobraParser.cobra (modified) (4 diffs)
-
Source/Compiler.cobra (modified) (2 diffs)
-
Source/Types.cobra (modified) (3 diffs)
-
Tests/320-misc-two/360-default-type.cobra (added)
Legend:
- Unmodified
- Added
- Removed
-
cobra/trunk/HowTo/290-TranslatePseudoCodeToCobra1.cobra
r1454 r1596 5 5 6 6 Note 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. 7 operators such as "i += 1". However, you can still write "i = i+1" if you like. 8 9 Note that the default type for an argument is dynamic? which means that the argument 10 can be any object or nil. 8 11 9 12 Authors: Max Grender-Jones, Chuck Esterbrook … … 23 26 assert sortMethod([1,1,1,1]) == [1,1,1,1] 24 27 25 def bubbleSort(list) as dynamic 28 def bubbleSort(list) as dynamic? 26 29 """ 27 30 From http://en.wikipedia.org/wiki/Bubble_sort#Pseudocode_implementation … … 41 44 .testSort(ref .bubbleSort) 42 45 body 43 swapped = false 44 post while swapped 46 if list 45 47 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 50 54 return list 51 55 52 def heapSort(list) as dynamic 56 def heapSort(list) as dynamic? 53 57 """ 54 58 From http://en.wikipedia.org/wiki/Heapsort#Pseudocode … … 72 76 .testSort(ref .heapSort) 73 77 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) 80 85 return list 81 86 82 def heapify(list )87 def heapify(list as dynamic) 83 88 """ 84 89 From http://en.wikipedia.org/wiki/Heapsort#Pseudocode … … 129 134 return 130 135 131 def insertionSort(list) as dynamic 136 def insertionSort(list) as dynamic? 132 137 """ 133 138 From http://en.wikipedia.org/wiki/Insertion_sort … … 144 149 .testSort(ref .insertionSort) 145 150 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 153 159 return list 154 160 155 161 156 def quickSort(list) as dynamic 162 def quickSort(list) as dynamic? 157 163 """ 158 164 From http://en.wikipedia.org/wiki/Quicksort … … 171 177 .testSort(ref .quickSort) 172 178 body 173 if list .count <= 1179 if list is nil or list.count <= 1 174 180 return list 175 181 else -
cobra/trunk/Source/CobraParser.cobra
r1593 r1596 1150 1150 initExpr = nil 1151 1151 if type is nil 1152 type = .typeProvider.d ynamicType1152 type = .typeProvider.defaultType 1153 1153 docString = '' to ? 1154 1154 isNames = List<of String>(_isNamesStack) … … 1398 1398 returnType = .typeId to ITypeProxy 1399 1399 else 1400 returnType = _typeProvider.d ynamicType1400 returnType = _typeProvider.defaultType 1401 1401 # TODO: implements? 1402 1402 hasIsNames = false … … 1528 1528 returnType = .typeId to ITypeProxy 1529 1529 else 1530 returnType = _typeProvider.d ynamicType1530 returnType = _typeProvider.defaultType 1531 1531 # TODO: implements? 1532 1532 hasIsNames = false … … 1659 1659 isMissingType = false 1660 1660 else 1661 type = TypeProxy(_typeProvider.d ynamicType) # the default type for a parameter is dynamic1661 type = TypeProxy(_typeProvider.defaultType) 1662 1662 isMissingType = true 1663 1663 # 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 439 439 var _voidType as VoidType? 440 440 var _variTypes as Dictionary<of INode, VariType>? 441 var _defaultType as IType? 441 442 442 443 def typeForName(name as String) as IType … … 558 559 _variTypes[type] = vt = VariType(type) 559 560 return vt 561 562 def defaultType as IType 563 if _defaultType is nil 564 _defaultType = .nilableType(.dynamicType) 565 return _defaultType to ! 560 566 561 567 -
cobra/trunk/Source/Types.cobra
r1583 r1596 55 55 56 56 def clrType(qualifiedName as String) as System.Type 57 58 def defaultType as IType 57 59 58 60 … … 77 79 var _voidType as VoidType? 78 80 var _variTypes as Dictionary<of IType, VariType>? 81 var _defaultType as IType? 79 82 80 83 def typeForName(name as String) as IType … … 184 187 def clrType(qualifiedName as String) as System.Type 185 188 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 ! 186 194 187 195
