Ticket #122: const.patch
File const.patch, 7.8 KB (added by hopscc, 16 years ago) |
---|
-
Source/Members.cobra
379 379 inherits BoxMember 380 380 implements IVar 381 381 """ 382 A BoxVar is a variable declared for a box, whether instance or shared. For example:382 A BoxVar is a variable or constant declared for a box, whether instance or shared. For example: 383 383 var _x as int is shared 384 const PI = 3.14 384 385 385 386 Classes and structs can have vars, but interfaces cannot. 386 387 … … 392 393 var _initExpr as Expr? 393 394 var _ifInheritsStack as Stack<of IType> 394 395 var _isAssignedTo as bool 396 var _isConst as bool 395 397 396 398 def init(token as IToken, box as Box, name as String, typeNode as ITypeProxy?, isNames as List<of String>, initExpr as Expr?, attribs as AttributeList?, docString as String) 397 399 base.init(token, box, name, isNames, attribs ? AttributeList(), docString) … … 411 413 .addField('initExpr', _initExpr) 412 414 413 415 get englishName as String is override 414 return 'variable member'416 return if(_isConst, 'constant member', 'variable member') 415 417 416 418 pro isAssignedTo from var 417 419 … … 419 421 420 422 get requiresDotPrefix as bool 421 423 return not _name.startsWith('_') 424 425 def makeConstant 426 _isConst = true 427 428 get isShared as bool 429 return _isConst or base.isShared 422 430 423 431 def attemptAssignmentOf(type as IType) as bool 424 432 # TODO: dup'ed with AbstractLocalVar … … 454 462 _type = _initExpr.type 455 463 else if not _initExpr.canBeAssignedTo(_type to !) 456 464 .throwError('Incompatible types. Cannot assign value of type [_initExpr.type.name] on the right to [_type.name] on the left.') 465 if _isConst and not _initExpr 466 .throwError('Cobra const must be explicitly initialised on declaration') 467 # enforce all upcase on const names? 457 468 458 469 get resultType as IType is override 459 470 """ -
Source/BackEndClr/SharpGenerator.cobra
1518 1518 base.writeSharpDef(sw) 1519 1519 .writeSharpAttribs(sw) 1520 1520 .writeSharpIsNames(sw) 1521 if _isConst, sw.write('const ') 1521 1522 sw.write(_type.sharpRef) 1522 1523 sw.write(' [_backEndName]') 1523 1524 if _initExpr -
Source/KeywordSpecs.cobra
87 87 'set | declarative: Declare property writer/mutator.', 88 88 'pro | declarative: Declare a property.', 89 89 'var | declarative: Declare a variable at the object or type level. Also, used as `from var` in property declarations. | expression: Refer to the variable that matches the containing property name.', 90 'const | declarative: Declare a constant at the object or type level.', 90 91 'from | declarative: Specify the backing variable for a property. Also, may be used for future LINQ syntax.', 91 92 'has | declarative: Specify attributes to attach to the containing declaration.', 92 93 'body | declarative: Declare the body of a method, indexer or property after blocked contracts or unit tests.', -
Source/CobraParser.cobra
991 991 on 'GET', .addDecl(box, .declareGetOnlyProperty) 992 992 on 'SET', .addDecl(box, .declareSetOnlyProperty) 993 993 on 'PRO', .addDecl(box, .declareProperty) 994 on 'VAR', .addDecl(box, .boxVarDecl) 994 on 'VAR', .addDecl(box, .boxVarDecl(true)) 995 on 'CONST', .addDecl(box, .boxVarDecl(false)) 995 996 on 'INVARIANT', .declareInvariant 996 997 on 'EOL', .endOfLine 997 998 on 'ENUM', .addDecl(box, .enumDecl) … … 1032 1033 .grab 1033 1034 .endOfLine 1034 1035 1035 def boxVarDecl as BoxVar1036 .expect( 'VAR')1036 def boxVarDecl(isVar as bool) as BoxVar 1037 .expect(if(isVar, 'VAR', 'CONST')) 1037 1038 tok = .expect('ID') 1038 1039 identifier = tok.text 1039 1040 other = .curBox.declForName(identifier) … … 1046 1047 numUnderscores += 1 1047 1048 if not s.length 1048 1049 .throwError('A class variable must be made of more than underscores. Try "[identifier]x" or "[identifier]1".') 1049 if not s[0].isLower1050 if isVar and not s[0].isLower 1050 1051 sugg = String(c'_', numUnderscores) + s[0].toString.toLower + s[1:] 1051 1052 sugg = ' Try "[sugg]".' 1052 1053 .throwError('Class variables must start with lowercase letters (after the underscore(s)) to distinguish them from other types of identifiers.[sugg]') … … 1072 1073 attribs = .hasAttribs 1073 1074 docString = .docString 1074 1075 .dedent 1075 return BoxVar(tok, .curBox, identifier, type, isNames, initExpr, attribs, docString) 1076 1076 bv = BoxVar(tok, .curBox, identifier, type, isNames, initExpr, attribs, docString) 1077 if not isVar 1078 bv.makeConstant 1079 return bv 1080 1077 1081 def declareInvariant 1078 1082 .expect('INVARIANT') 1079 1083 if .optional('COLON') -
Tests/820-errors/400-declarations/520-constant.cobra
1 # Test const - compile time constant 2 class AConst 3 const _uyear as int # .error. const must be explicitly initialised on declaration 4 5 def main is shared 6 pass -
Tests/820-errors/400-declarations/522-constant.cobra
1 # Test const - compile time constant 2 class AConst 3 const year = 1959 4 5 def init 6 # error currently generated by C# compiler, 7 # shld probaby be caught by cobra 8 .year = 1967 # .error. 9 10 def main is shared 11 pass 12 -
Tests/820-errors/400-declarations/521-constant.cobra
1 # Test const - compile time constant 2 class AConst 3 const YEAR as int = 1959 4 5 def main is shared 6 # error currently generated by C# compiler, 7 # shld probaby be caught by cobra 8 AConst.YEAR = 1967 # .error. 9 -
Tests/120-classes/224-fields-constant.cobra
1 # Test const - compile time constant 2 class AConst 3 const YEAR as int = 1959 4 const pcode = 'xyzzy' 5 const _lastYear = .YEAR -1 6 var _nyear 7 8 9 def init 10 assert AConst.YEAR == 1959 11 assert .YEAR == 1959 12 assert _lastYear == 1958 13 assert .pcode == 'xyzzy' 14 _nyear = AConst.YEAR +1 15 16 def main is shared 17 x = AConst() 18 19 assert AConst.YEAR == 1959 20 assert AConst._lastYear == 1958 21 assert x._nyear == 1960 22 -
Developer/IntermediateReleaseNotes.text
9 9 10 10 * Improved error checking on `listen` and `ignore` statements. 11 11 12 * Support constant declarations on class instance variables: ticket:122 13 12 14 ================================================================================ 13 15 Library 14 16 ================================================================================