Ticket #253: min-multi-dim-array.patch
File min-multi-dim-array.patch, 10.3 KB (added by hopscc, 14 years ago) |
---|
-
Source/TypeProxies.cobra
460 460 461 461 462 462 class ArrayTypeIdentifier inherits WrappedTypeIdentifier 463 463 var multiDimens as int = 0 464 464 465 cue init(token as IToken, typeId as AbstractTypeIdentifier) 465 466 base.init(token, typeId) 466 467 … … 468 469 return container.memberForName(.theWrappedTypeIdentifier.name) 469 470 470 471 def _resolveType as IType is override 471 return .typeProvider.arrayType(_typeId.realType) 472 if not .multiDimens 473 return .typeProvider.arrayType(_typeId.realType) 474 # uncached multidim arrayTypes 475 at = ArrayType(_typeId.realType) 476 at.multiDimens = .multiDimens 477 at.bindInh 478 at.bindInt 479 return at 472 480 473 474 481 class NilableTypeIdentifier 475 482 inherits WrappedTypeIdentifier 476 483 """ -
Source/BackEndClr/SharpGenerator.cobra
837 837 is partial 838 838 839 839 get sharpRef as String is override 840 return '[_wrappedType.sharpRef]' + r'[]' 841 840 suffix = r'[]' 841 # special form for multidimensional arrays # TYPE[,] or TYPE[,,] 842 if .multiDimens == 2, suffix = r'[,]' 843 else if .multiDimens == 3, suffix = r'[,,]' 844 wtsr = '[_wrappedType.sharpRef]' + suffix 845 return wtsr 846 842 847 get sharpInit as String is override 843 848 return 'null' 844 849 -
Source/Types.cobra
1708 1708 get suffix as String is override 1709 1709 return r'[]' 1710 1710 1711 var _multiDimens as int = 0 # 2=2D (matrix), 3=3D 1712 pro multiDimens from var 1713 # flag multidimension ArrayTypes specially TYPE[,] vs TYPE[][] 1714 1711 1715 def isAssignableTo(type as IType) as bool is override 1712 1716 if base.isAssignableTo(type), return true 1713 1717 if type inherits ArrayType … … 1758 1762 interfaces = [iclone to ITypeProxy, icoll, ienum] # TODO:, _ilistOf] -- problems with cobra -ert:yes hello and extension method String.split 1759 1763 _box = Class(Token.empty, Token.empty, '[.getType.name]_[.serialNum]', List<of IType>(), List<of String>(), AttributeList(), nil, interfaces, List<of ITypeProxy>(), nil) 1760 1764 # TODO: make members based on System.Array 1761 indexer = Indexer(Token.empty, Token.empty, _box, r'[]', [Param(Token('', 1, 1, 1, 'ID', 'index', nil), .compiler.intType)], _wrappedType, List<of String>(), AttributeList(), '') 1765 paramList = [Param(Token('', 1, 1, 1, 'ID', 'index', nil), .compiler.intType)] 1766 if _multiDimens >= 2 1767 paramList.add(Param(Token('', 1, 1, 1, 'ID', 'index2', nil), .compiler.intType)) 1768 if _multiDimens >= 3 1769 paramList.add(Param(Token('', 1, 1, 1, 'ID', 'index3', nil), .compiler.intType)) 1770 indexer = Indexer(Token.empty, Token.empty, _box, r'[]', paramList, _wrappedType, List<of String>(), AttributeList(), '') 1762 1771 _box.addDecl(indexer) 1763 1772 lengthProp = Property(Token.empty, Token.empty, _box, 'length', .compiler.intType, List<of String>(), AttributeList(), '') 1764 1773 _box.addDecl(lengthProp) -
Source/CobraParser.cobra
3447 3447 if bracket 3448 3448 if .peek.which=='INTEGER_LIT' 3449 3449 .throwError('The size of the array is not part of its type. Specify the size when creating the array such as: [t.name]\[]([.peek.text]).') 3450 .expect('RBRACKET') 3451 t = ArrayTypeIdentifier(bracket, t) 3450 # allow 1D array type only 3451 #.expect('RBRACKET') 3452 #t = ArrayTypeIdentifier(bracket, t) 3453 # 1D plus 2D and 3D (only) multidimensional and jagged 3454 t = _extendedArrayTypeId(bracket, t to !) 3452 3455 3453 3456 while true 3454 3457 # check for 'optional' aka 'can be nil' … … 3464 3467 3465 3468 return t to ! 3466 3469 3470 def _extendedArrayTypeId(bracket as IToken?, t as AbstractTypeIdentifier) as AbstractTypeIdentifier 3471 """ 3472 Scan for 1D array TYPE[] as above but also for 3473 2D or 3D (only) multidimensional array type 'TYPE[,]' or 'TYPE[,,]' or 3474 a jagged 2D or 3D array 'TYPE[][]' or 'TYPE[][][]' and return the resulting ArrayTypeIdentifier 3475 The latter two provide some minimal typing support for the additional array forms, 3476 initialisation of such types still has to be done by a fallthrough to csharp. 3477 e.g. arrj as int[][] = sharp'new int[2][2]' 3478 arrj[0], arrj[1] = int[](4), int[](2) 3479 3480 arr2D as int[,] = sharp'new int[4,2]' 3481 """ 3482 dimens = 0 3483 if .optional('COMMA'), dimens += 1 # 2D multiDimension TYPE[,] 3484 if .optional('COMMA'), dimens += 1 # 3D multiDimension TYPE[,,] 3485 .expect('RBRACKET') 3486 t = ArrayTypeIdentifier(bracket, t) 3487 if dimens > 0 3488 ta = t to ArrayTypeIdentifier 3489 ta.multiDimens = 1 + dimens # specially tagged with dimensions 3490 else 3491 bracket = .optional('LBRACKET') 3492 if bracket # 2D jagged array : TYPE[][] 3493 .expect('RBRACKET') 3494 t = ArrayTypeIdentifier(bracket, t) 3495 bracket = .optional('LBRACKET') 3496 if bracket # 3D jagged array : TYPE[][][] 3497 .expect('RBRACKET') 3498 t = ArrayTypeIdentifier(bracket, t) 3499 return t 3500 3467 3501 def genericTypeId(openGenericToken as IToken) as AbstractTypeIdentifier 3468 3502 require openGenericToken.text.trim.endsWith('<of') 3469 3503 fullName = openGenericToken.text.trim + ' ' -
Tests/110-basics-two/800-arrays/400-multi-dim-array.cobra
1 # Test for crude minimal support of jagged and mutidimensional arrays 2 # The support 3 # Recognises a type only, 4 # - jagged: TYPE[][] or TYPE[][][] 5 # - multidim TYPE[,] or TYPE[,,] 6 # 2D and 3D arrays only (no more) 7 # no constructor/initialiser call ( drop to sharp'' for that) 8 9 # The type names used here are specific to the .Net family 10 11 class MultiDimArray 12 const jaggedInt2DTypeStr = r'System.Int32[][]' 13 const intType2DStr = r'System.Int32[,]' 14 const intType3DStr = r'System.Int32[,,]' 15 const stringType2DStr = r'System.String[,]' 16 const stringType3DStr = r'System.String[,,]' 17 18 def main 19 .jagged 20 .multiDimen 21 .multiDimenString 22 .usingLit 23 .crtInst 24 25 def jagged 26 arr as int[][] = sharp'new int[2][]' 27 arr[0], arr[1] = int[](10), int[](6) 28 assert arr.typeOf.toString == .jaggedInt2DTypeStr 29 30 arr[0][0] = 9 31 assert arr[0][0] == 9 32 assert arr[0][1] == 0 33 arr[1][0] = 10 34 assert arr[1][0] == 10 35 arr[0][9] = 09 36 assert arr[0][9] == 9 37 arr[1][5] = 15 38 assert arr[1][5] == 15 39 #print arr 40 expect System.IndexOutOfRangeException 41 arr[0][10] = 404 42 expect System.IndexOutOfRangeException 43 arr[1][6] = 404 44 45 def multiDimen 46 arr as int[,] = sharp'new int[2,10]' 47 assert arr.typeOf.toString == .intType2DStr 48 49 arr[0,0] = 9 50 assert arr[0,0] == 9 51 assert arr[0,1] == 0 52 arr[1,0] = 10 53 assert arr[1,0] == 10 54 arr[1,9] = 19 55 assert arr[1,9] == 19 56 expect System.IndexOutOfRangeException 57 arr[0,10] = 404 58 expect System.IndexOutOfRangeException 59 arr[1,10] = 404 60 #print arr 61 62 arr3D as int[,,] = sharp'new int[2,2,2]' 63 assert arr3D.typeOf.toString == .intType3DStr 64 arr3D[0,0,0] = 9 65 assert arr3D[0,0,0] == 9 66 assert arr3D[0,0,1] == 0 67 arr3D[0,1,0] = 10 68 assert arr3D[0,1,0] == 10 69 arr3D[1,1,1] = 111 70 assert arr3D[1,1,1] == 111 71 72 expect System.IndexOutOfRangeException 73 arr3D[0,0,2] = 404 74 expect System.IndexOutOfRangeException 75 arr3D[0, 2, 0] = 404 76 expect System.IndexOutOfRangeException 77 arr3D[2, 0, 0] = 404 78 79 using sw = StringWriter() 80 print to sw 81 print arr3D stop 82 assert sw.toString == r'[9, 0, 10, 0, 0, 0, 0, 111]' 83 84 def multiDimenString 85 arr as String[,] = sharp'new string[2,10]' # 2D Matrix 86 # TODO: should actually be arr as String?[,] = sharp'new string[2,10]' 87 # but should just fall out when supprted for 1D arrays 88 assert arr.typeOf.toString == .stringType2DStr 89 90 arr[0,0] = 'hello' 91 assert arr[0,0] == 'hello' 92 #assert arr[0,1] == nil 93 arr[1,0] = 'Noddie' 94 assert arr[1,0] == 'Noddie' 95 #print arr 96 97 arr3D as String[,,] = sharp'new string[2,2,2]' # 3D Cubix 98 assert arr3D.typeOf.toString == .stringType3DStr 99 arr3D[0,0,0] = 'hi' 100 assert arr3D[0,0,0] == 'hi' 101 #assert arr3D[0,0,1] == nil 102 arr3D[0,1,0] = '10' 103 assert arr3D[0,1,0] == '10' 104 arr3D[1,0,0] = '100' 105 assert arr3D[1,0,0] == '100' 106 arr3D[1,1,1] = '111' 107 assert arr3D[1,1,1] == '111' 108 #print arr3D 109 using sw = StringWriter() 110 print to sw 111 print arr3D stop 112 assert sw.toString == r"['hi', nil, '10', nil, '100', nil, nil, '111']" 113 114 115 def usingLit 116 # making a 2D multidim array before the multidim array type was available 117 #essentially hand created like jagged 118 a = int[](10) 119 b = int[](10) 120 arr = @[a,b] #OK 121 assert arr.typeOf.toString == .jaggedInt2DTypeStr 122 123 arr[0][0] = 9 124 assert arr[0][0] == 9 125 assert arr[0][1] == 0 126 assert arr[1][0] == 0 127 arr[1][1] = 1 128 assert arr[1][1] == 1 129 #print arr 130 131 def crtInst 132 # Introspectively 133 arr = Array.createInstance(int, 2, 10) 134 arr.setValue(9,0,0) 135 assert arr.getValue(0,0) == 9 136 assert arr.getValue(0,1) == 0 137 assert arr.getValue(0,2) == 0 138 -
Developer/IntermediateReleaseNotes.text
64 64 65 65 * Generate warning on use of deprecated `$sharp('...')` syntax in favor of newer alternate sharp-string-literal form: `sharp'...'` and `sharp"..."`. 66 66 67 * Provide very minimal support (types only) for 2D and 3D jagged and multidimensional arrays : ticket 253 67 68 69 68 70 ================================================================================ 69 71 Library 70 72 ================================================================================