Wiki

Ticket #122: const.patch

File const.patch, 7.8 KB (added by hopscc, 9 years ago)
  • Source/Members.cobra

     
    379379    inherits BoxMember 
    380380    implements IVar 
    381381    """ 
    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: 
    383383        var _x as int is shared 
     384        const PI = 3.14 
    384385 
    385386    Classes and structs can have vars, but interfaces cannot. 
    386387     
     
    392393    var _initExpr as Expr? 
    393394    var _ifInheritsStack as Stack<of IType> 
    394395    var _isAssignedTo as bool 
     396    var _isConst as bool 
    395397 
    396398    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) 
    397399        base.init(token, box, name, isNames, attribs ? AttributeList(), docString) 
     
    411413        .addField('initExpr', _initExpr) 
    412414 
    413415    get englishName as String is override 
    414         return 'variable member' 
     416        return if(_isConst, 'constant member',  'variable member') 
    415417 
    416418    pro isAssignedTo from var 
    417419 
     
    419421 
    420422    get requiresDotPrefix as bool 
    421423        return not _name.startsWith('_') 
     424         
     425    def makeConstant 
     426        _isConst = true  
     427         
     428    get isShared as bool 
     429        return _isConst or base.isShared 
    422430 
    423431    def attemptAssignmentOf(type as IType) as bool 
    424432        # TODO: dup'ed with AbstractLocalVar 
     
    454462                _type = _initExpr.type 
    455463            else if not _initExpr.canBeAssignedTo(_type to !) 
    456464                .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?     
    457468 
    458469    get resultType as IType is override 
    459470        """ 
  • Source/BackEndClr/SharpGenerator.cobra

     
    15181518        base.writeSharpDef(sw) 
    15191519        .writeSharpAttribs(sw) 
    15201520        .writeSharpIsNames(sw) 
     1521        if _isConst, sw.write('const ') 
    15211522        sw.write(_type.sharpRef) 
    15221523        sw.write(' [_backEndName]') 
    15231524        if _initExpr 
  • Source/KeywordSpecs.cobra

     
    8787                'set         | declarative: Declare property writer/mutator.', 
    8888                'pro         | declarative: Declare a property.', 
    8989                '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.', 
    9091                'from        | declarative: Specify the backing variable for a property. Also, may be used for future LINQ syntax.', 
    9192                'has         | declarative: Specify attributes to attach to the containing declaration.', 
    9293                'body        | declarative: Declare the body of a method, indexer or property after blocked contracts or unit tests.', 
  • Source/CobraParser.cobra

     
    991991                    on 'GET', .addDecl(box, .declareGetOnlyProperty) 
    992992                    on 'SET', .addDecl(box, .declareSetOnlyProperty) 
    993993                    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)) 
    995996                    on 'INVARIANT', .declareInvariant 
    996997                    on 'EOL', .endOfLine 
    997998                    on 'ENUM', .addDecl(box, .enumDecl) 
     
    10321033        .grab 
    10331034        .endOfLine 
    10341035 
    1035     def boxVarDecl as BoxVar 
    1036         .expect('VAR') 
     1036    def boxVarDecl(isVar as bool) as BoxVar 
     1037        .expect(if(isVar, 'VAR', 'CONST')) 
    10371038        tok = .expect('ID') 
    10381039        identifier = tok.text 
    10391040        other = .curBox.declForName(identifier) 
     
    10461047            numUnderscores += 1 
    10471048        if not s.length 
    10481049            .throwError('A class variable must be made of more than underscores. Try "[identifier]x" or "[identifier]1".') 
    1049         if not s[0].isLower 
     1050        if isVar and not s[0].isLower 
    10501051            sugg = String(c'_', numUnderscores) + s[0].toString.toLower + s[1:] 
    10511052            sugg = ' Try "[sugg]".' 
    10521053            .throwError('Class variables must start with lowercase letters (after the underscore(s)) to distinguish them from other types of identifiers.[sugg]') 
     
    10721073            attribs = .hasAttribs 
    10731074            docString = .docString 
    10741075            .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                 
    10771081    def declareInvariant 
    10781082        .expect('INVARIANT') 
    10791083        if .optional('COLON') 
  • Tests/820-errors/400-declarations/520-constant.cobra

     
     1# Test const - compile time constant 
     2class 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 
     2class 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 
     2class 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 
     2class 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

     
    99 
    1010* Improved error checking on `listen` and `ignore` statements. 
    1111 
     12* Support constant declarations on class instance variables: ticket:122 
     13 
    1214================================================================================ 
    1315Library 
    1416================================================================================