Ticket #27: explicitArraySize.patch
File explicitArraySize.patch, 4.7 KB (added by hopscc, 16 years ago) |
---|
patch for sized array as dcl type |
-
Source/CobraParser.cobra
3008 3008 # check for array 3009 3009 bracket = .optional('LBRACKET') 3010 3010 if bracket 3011 sizeToken = nil 3011 3012 if .peek.which=='INTEGER_LIT' 3012 .throwError('Explicit array sizes are not currently supported.') 3013 #.throwError('Explicit array sizes are not currently supported.') 3014 #.throwError('Explicit array sizes are not specified this way for some obscure reason. Do as assignment to an arrayType with literal size in braces e.g. ia as int[] = int[](10)') 3015 sizeToken = .grab 3013 3016 .expect('RBRACKET') 3014 t = ArrayTypeIdentifier(bracket, t) 3015 3017 if sizeToken 3018 size = sizeToken.value to int 3019 t = SizedArrayTypeIdentifier(bracket, t, size) 3020 else 3021 t = ArrayTypeIdentifier(bracket, t) 3022 3016 3023 # check for 'optional' aka 'can be nil' 3017 3024 question = .optional('QUESTION') 3018 3025 if question -
Source/Types.cobra
1801 1801 1802 1802 get name as String is override 1803 1803 return _wrappedType.name+r'[]' 1804 1804 1805 get size as int 1806 return -1 1807 1805 1808 def isAssignableTo(type as IType) as bool is override 1806 1809 if base.isAssignableTo(type) 1807 1810 return true … … 1835 1838 get sharpInit as String is override 1836 1839 return 'null' 1837 1840 1841 class SizedArrayType 1842 inherits ArrayType 1843 1844 var _size as int 1845 get size from var is override 1846 1847 def init(wrappedType as IType, size as int) 1848 base.init(wrappedType) 1849 _size = size 1838 1850 1851 get isReference as bool is override 1852 return false 1853 1854 get sharpInit as String is override 1855 return 'new [_wrappedType.sharpRef]' + r'[' + '[_size]' + r']' 1856 1857 1839 1858 class VariType 1840 1859 inherits ArrayType 1841 1860 """ … … 2002 2021 2003 2022 def _resolveType as IType is override 2004 2023 return ArrayType(_typeId.realType) 2024 2025 class SizedArrayTypeIdentifier 2026 inherits ArrayTypeIdentifier 2027 2028 var _size as int 2029 2030 def init(token as IToken, typeId as AbstractTypeIdentifier, size as int) 2031 base.init(token, typeId) 2032 _size = size 2033 2034 def _resolveType as IType is override 2035 return SizedArrayType(_typeId.realType, _size) 2036 2005 2037 2006 2007 2038 class NilableTypeIdentifier 2008 2039 inherits WrappedTypeIdentifier 2009 2040 -
Tests/110-basics-two/800-arrays/130-sized-single-dim-array.cobra
1 class ExplicitSizedArrayTest 2 """ 3 Tests for new sized array type - as Type[lit-size] 4 """ 5 shared 6 7 def main 8 .sizedDcl 9 .slicedArr 10 11 def sizedDcl 12 numbers as int[10] 13 assert numbers.length==10 14 for i as int in numbers 15 assert i==0 16 17 for i = 0 .. numbers.length 18 assert numbers[i]==0 19 numbers[i] = i 20 assert numbers[i]==i 21 22 names as String[] = @['aoeu'] 23 assert names[0] == 'aoeu' 24 names[0] = 'Cobra' 25 assert names[0] == 'Co' + 'bra' 26 27 assert .names[0]=='aoeu' 28 assert .names[1]=='asdf' 29 30 #TODO: stuff as String?[1] 31 32 numbers = @[1, 2, 3] 33 assert numbers.length==3 34 assert numbers[0]==1 35 36 # test equality 37 num0 as int[] = int[](3) 38 num1 as int[3] 39 assert num0 == num1 40 41 numbers0 = @[1, 2, 3] 42 numbers1 as int[3] 43 numbers1[0] = 1 44 numbers1[1] = 2 45 numbers1[2] = 3 46 assert numbers0 == numbers1 47 assert numbers1 == numbers1 48 49 def names as String[2] 50 names as String[2] 51 names[0] = 'aoeu' 52 names[1] = 'asdf' 53 return names 54 55 def slicedArr 56 numbers as int[3] 57 assert numbers[:-1] == @[0, 0] 58 assert numbers[1:] == @[0, 0] 59 assert numbers[:-2] == @[0] 60 numbers[0] = 1 61 numbers[1] = 2 62 numbers[2] = 3 63 assert numbers[:-1] == @[1, 2] 64 assert numbers[1:] == @[2, 3] 65 assert numbers[:-2] == @[1] 66 -
Developer/IntermediateReleaseNotes.text
1 1 Post 0.8 2 2 3 * Add support for explicitly sized array as declaration type - ticket#27 4 3 5 * Added a new built-in doc tool accessible via "cobra -doc ...". The documentation is generated to local HTML files using your declarations, doc strings, contracts, etc. 4 6 5 7 * Add support for specifying unsigned integers as Hex literals