Changeset 1752

Show
Ignore:
Timestamp:
11/09/08 02:01:13 (2 months ago)
Author:
Chuck.Esterbrook
Message:

Progress towards multiple back-ends.
Code refactoring, renaming, etc.

Location:
cobra/trunk/Source
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • cobra/trunk/Source/BackEndClr/SharpGenerator.cobra

    r1751 r1752  
    1515        var _fullExeFileName as String = '' 
    1616        var _moduleFileName_to_sharpToCobraLineNum as Dictionary<of String, Dictionary<of int, int>?>? 
     17 
     18        def generateCode(writeTestInvocation as bool) 
     19                .writeSharp 
     20                if writeTestInvocation 
     21                        .writeSharpTestInvocation 
     22                        .compileSharp('/main:RunTests') 
     23                else 
     24                        .compileSharp 
    1725 
    1826        def writeSharp 
     
    350358 
    351359 
    352  
    353360## 
    354361## Node 
     
    12411248                return 'class'  # because the extension is always housed in a C# class 
    12421249 
    1243         var _sharpName as String? 
     1250        var _backEndName as String? 
    12441251         
    12451252        get sharpName as String is override 
    1246                 if not _sharpName 
    1247                         _sharpName = if(.nativeType, .nativeType.name, 'Extend_[.extendedBox.sharpNameComponent]_[Utils.toIdentifier(Path.getFileNameWithoutExtension(.compiler.curModule.fileName) to !)]') 
    1248                 return _sharpName to ! 
     1253                if not _backEndName 
     1254                        _backEndName = if(.nativeType, .nativeType.name, 'Extend_[.extendedBox.sharpNameComponent]_[Utils.toIdentifier(Path.getFileNameWithoutExtension(.compiler.curModule.fileName) to !)]') 
     1255                return _backEndName to ! 
    12491256 
    12501257        get sharpThis as String is override 
     
    12711278        is partial 
    12721279 
    1273         get useSharpNameStack as Stack<of String> 
     1280        get useBackEndNameStack as Stack<of String> 
    12741281 
    12751282        get sharpAssignmentNames as List<of String>? 
     
    12841291 
    12851292        get sharpAssignmentNames as List<of String> 
    1286                 return [_sharpName] 
    1287                 # return List<of String>(_useSharpNameStack) 
     1293                return [_backEndName] 
     1294                # return List<of String>(_useBackEndNameStack) 
    12881295 
    12891296        get sharpName as String is override 
    12901297                if _ifInheritsStack.count 
    12911298                        if _type.isReference 
    1292                                 return '(([_ifInheritsStack.peek.sharpRef])[_sharpName])' 
     1299                                return '(([_ifInheritsStack.peek.sharpRef])[_backEndName])' 
    12931300                        else 
    12941301                                # could it be a subclass? no. value types are structs and cannot be subclassed so this must be `if i` where `i` is nullable struct 
    12951302                                return '[.name].Value'  # Nullable<of T>.Value 
    12961303                else 
    1297                         return _sharpName 
    1298                 # return _useSharpNameStack.peek to ! 
     1304                        return _backEndName 
     1305                # return _useBackEndNameStack.peek to ! 
    12991306 
    13001307 
     
    13311338 
    13321339        get sharpName as String is override 
    1333                 return _sharpName 
     1340                return _backEndName 
    13341341 
    13351342 
     
    14331440        is partial 
    14341441 
    1435         var _useSharpNameStack as Stack<of String> 
    1436  
    1437         get useSharpNameStack from var 
     1442        var _useBackEndNameStack as Stack<of String> 
     1443 
     1444        get useBackEndNameStack from var 
    14381445 
    14391446        get sharpAssignmentNames as List<of String> 
    1440                 return [_sharpName] 
    1441                 # return List<of String>(_useSharpNameStack) 
     1447                return [_backEndName] 
     1448                # return List<of String>(_useBackEndNameStack) 
    14421449 
    14431450        get sharpName as String is override 
    14441451                if _ifInheritsStack.count 
    14451452                        if _type.isReference 
    1446                                 return '(([_ifInheritsStack.peek.sharpRef])[_sharpName])' 
     1453                                return '(([_ifInheritsStack.peek.sharpRef])[_backEndName])' 
    14471454                        else 
    14481455                                # could it be a subclass? no. value types are structs and cannot be subclassed so this must be `if i` where `i` is nullable struct 
    1449                                 return '[_sharpName].Value'  # Nullable<of T>.Value 
     1456                                return '[_backEndName].Value'  # Nullable<of T>.Value 
    14501457                else 
    14511458                        return .name 
    1452                 # return _useSharpNameStack.peek to ! 
    1453  
    1454         get _sharpName as String 
     1459                # return _useBackEndNameStack.peek to ! 
     1460 
     1461        get _backEndName as String 
    14551462                name = .name 
    14561463                if not name.startsWith('_') 
     
    14631470                .writeSharpIsNames(sw) 
    14641471                sw.write(_type.sharpRef) 
    1465                 sw.write(' [_sharpName]') 
     1472                sw.write(' [_backEndName]') 
    14661473                if _initExpr 
    14671474                        sw.write(' = ') 
     
    14731480        is partial 
    14741481 
    1475         var _sharpResultVarName = '' 
     1482        var _backEndResultVarName = '' 
    14761483 
    14771484        get sharpGenericParams as String 
     
    14821489                return '' 
    14831490 
    1484         get sharpResultVarName from var 
     1491        get backEndResultVarName from var 
    14851492 
    14861493        def writeSharpDef(sw as SharpWriter) 
     
    15201527                                                        sharpInit = 'DateTime.Today' 
    15211528                                                sw.write('[param.sharpName] = [sharpInit];') 
    1522                                 if _sharpResultVarName.length  # set in _bindImp 
    1523                                         sw.write('[_returnType.sharpRef] [_sharpResultVarName]') 
     1529                                if _backEndResultVarName.length  # set in _bindImp 
     1530                                        sw.write('[_returnType.sharpRef] [_backEndResultVarName]') 
    15241531                                        if _returnType.sharpInit.length 
    15251532                                                sw.write('= [_returnType.sharpInit]') 
     
    16061613 
    16071614        def writeSharpEnsureArgs(sw as SharpWriter) 
    1608                 if .sharpResultVarName.length 
    1609                         sw.write('[.sharpResultVarName]') 
     1615                if .backEndResultVarName.length 
     1616                        sw.write('[.backEndResultVarName]') 
    16101617                        sep = [','] 
    16111618                else 
     
    16261633 
    16271634        def writeSharpEnsureParamDecls(sw as SharpWriter) 
    1628                 if .sharpResultVarName.length 
    1629                         sw.write('[.resultType.sharpRef] [.sharpResultVarName]') 
     1635                if .backEndResultVarName.length 
     1636                        sw.write('[.resultType.sharpRef] [.backEndResultVarName]') 
    16301637                        sep = [','] 
    16311638                else 
     
    22992306                        #newName = '_lh_' + _ifInheritsVar.name + '_' + _ifInheritsType.sharpNameComponent 
    23002307                        #sw.write('[typeName] [newName] = ([typeName])[_ifInheritsVar.sharpName];\n') 
    2301                         #_ifInheritsVar.useSharpNameStack.push(newName) 
     2308                        #_ifInheritsVar.useBackEndNameStack.push(newName) 
    23022309                        ifInheritsCount = _ifInheritsVar.ifInheritsStack.count  # AssignExpr could pop our _ifInheritsType. We detect that with the count. 
    23032310                        _ifInheritsVar.ifInheritsStack.push(_ifInheritsType) 
     
    23052312                        stmt.writeSharpStmt(sw) 
    23062313                if _ifInheritsVar and _ifInheritsVar.ifInheritsStack.count > ifInheritsCount 
    2307                         #_ifInheritsVar.useSharpNameStack.pop 
     2314                        #_ifInheritsVar.useBackEndNameStack.pop 
    23082315                        _ifInheritsVar.ifInheritsStack.pop 
    23092316                if close 
     
    26442651        is partial 
    26452652 
    2646         var _sharpResultVarName as String? 
    2647  
    2648         pro sharpResultVarName from var 
     2653        var _backEndResultVarName as String? 
     2654 
     2655        pro backEndResultVarName from var 
    26492656 
    26502657        def writeSharpDef(sw as SharpWriter) 
     
    26552662                        sw.write('_lh_canEnsure = true;\n') 
    26562663                if _expr 
    2657                         if willEnsure and _sharpResultVarName and _sharpResultVarName.length 
    2658                                 sw.write('return [_sharpResultVarName]=') 
     2664                        if willEnsure and _backEndResultVarName and _backEndResultVarName.length 
     2665                                sw.write('return [_backEndResultVarName]=') 
    26592666                                _expr.writeSharpDefInContext(sw) 
    26602667                                sw.write(';\n') 
     
    28642871                        sw.write('_lh_canEnsure = true;\n') 
    28652872                if _expr 
    2866                         sharpResultVarName = .compiler.curCodeMember.sharpResultVarName 
    2867                         if willEnsure and sharpResultVarName.length 
     2873                        backEndResultVarName = .compiler.curCodeMember.backEndResultVarName 
     2874                        if willEnsure and backEndResultVarName.length 
    28682875                                # TODO: resolve yield return with ensure 
    2869                                 # sw.write('yield return [sharpResultVarName]=') 
     2876                                # sw.write('yield return [backEndResultVarName]=') 
    28702877                                # so for now: 
    28712878                                sw.write('yield return ') 
     
    45164523        def writeSharpTestInvocation(sw as SharpWriter) 
    45174524                pass 
     4525 
     4526 
     4527## 
     4528## Misc items 
     4529## 
     4530 
     4531class BackEndUtils 
     4532 
     4533        shared 
     4534 
     4535                var _backEndKeyWordList = 'abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using virtual void volatile while'.split 
     4536 
     4537                # CC: should be a Set, not a Dictionary: 
     4538                var _backEndKeyWordSet = Set<of String>() 
     4539 
     4540                def isBackEndKeyWord(word as String) as bool 
     4541                        """ 
     4542                        Returns true if the given word is a keyword in C# 2.0. 
     4543                        """ 
     4544                        require 
     4545                                word.length 
     4546                        test 
     4547                                assert .isBackEndKeyWord('object') 
     4548                                assert .isBackEndKeyWord('if') 
     4549                                assert not .isBackEndKeyWord('total') 
     4550                        body 
     4551                                if _backEndKeyWordSet.count == 0 
     4552                                        for word in _backEndKeyWordList 
     4553                                                _backEndKeyWordSet.add(word) 
     4554                                return word in _backEndKeyWordSet 
     4555 
     4556                def backEndNameForLocalVarName(name as String) as String 
     4557                        if .isBackEndKeyWord(name) 
     4558                                return '@' + name  # C# supports @ as a prefix to escape identifiers 
     4559                        else 
     4560                                return name 
  • cobra/trunk/Source/Compiler.cobra

    r1746 r1752  
    286286                .bindImp 
    287287                .writeSourceCodeCorrections 
    288                 .writeSharp 
    289                 if writeTestInvocation 
    290                         .writeSharpTestInvocation 
    291                         .compileSharp('/main:RunTests') 
    292                 else 
    293                         .compileSharp 
    294  
     288                .generateCode(writeTestInvocation) 
     289         
    295290        def testifyFilesNamed(fileNames as IList<of String>, options as OptionValues, resultsWriter as IndentedWriter, verbose as bool) 
    296291                """ 
  • cobra/trunk/Source/Members.cobra

    r1745 r1752  
    401401                _initExpr = initExpr 
    402402                _ifInheritsStack = Stack<of IType>() 
    403                 _useSharpNameStack = Stack<of String>()  # for if-inherits 
    404                 _useSharpNameStack.push(name) 
     403                _useBackEndNameStack = Stack<of String>()  # for if-inherits 
     404                _useBackEndNameStack.push(name) 
    405405 
    406406        def addRefFields is override 
     
    710710                        _requirePart.bindImp 
    711711                if _ensurePart and _returnType and _returnType is not .compiler.voidType 
    712                         _sharpResultVarName = '_lh_result' 
     712                        _backEndResultVarName = '_lh_result' 
    713713                else 
    714                         _sharpResultVarName = '' 
     714                        _backEndResultVarName = '' 
    715715                _curStmtIndex = 0 
    716716                for stmt in _stmts.toArray 
  • cobra/trunk/Source/Statements.cobra

    r1728 r1752  
    10041004                                .throwError('Return statement must return a [curCodeMember.resultType.name], or [curCodeMember.name] must have its return type removed.') 
    10051005                curCodeMember.hasReturnStmt = true 
    1006                 _sharpResultVarName = curCodeMember.sharpResultVarName 
     1006                _backEndResultVarName = curCodeMember.backEndResultVarName 
    10071007 
    10081008 
  • cobra/trunk/Source/Utils.cobra

    r1742 r1752  
    239239                                                        return name[:upperCount].toLower + name[upperCount:]  # utf8 
    240240 
    241  
    242                 ## Code gen 
    243  
    244                 var _sharpKeyWordList = 'abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using virtual void volatile while'.split 
    245  
    246                 # CC: should be a Set, not a Dictionary: 
    247                 var _sharpKeyWordSet = Dictionary<of String, bool>() 
    248  
    249                 def isSharpKeyWord(word as String) as bool 
    250                         """ 
    251                         Returns true if the given word is a keyword in C# 2.0. 
    252                         """ 
    253                         require 
    254                                 word.length 
    255                         test 
    256                                 assert .isSharpKeyWord('object') 
    257                                 assert .isSharpKeyWord('if') 
    258                                 assert not .isSharpKeyWord('total') 
    259                         body 
    260                                 if _sharpKeyWordSet.count == 0 
    261                                         for word in _sharpKeyWordList 
    262                                                 _sharpKeyWordSet[word] = true 
    263                                 return _sharpKeyWordSet.containsKey(word) 
    264  
    265241                def sharpStringLiteralFor(s as String) as String 
    266242                        #print '<> csStringLitFor' 
  • cobra/trunk/Source/Vars.cobra

    r1730 r1752  
    3939        var _ifInheritsStack = Stack<of IType>() 
    4040        var _isAssignedTo as bool 
    41         var _sharpName as String 
    42         var _useSharpNameStack = Stack<of String>()  # for if-inherits 
     41        var _backEndName as String 
     42        var _useBackEndNameStack = Stack<of String>()  # for if-inherits 
    4343 
    4444        def init(token as IToken, type as IType) 
     
    6161                _isTracked = false 
    6262                if .isImplicit 
    63                         sharpPrefix = '' 
    64                 else if Utils.isSharpKeyWord(.name) 
    65                         sharpPrefix = '@'  # C# supports @ as a valid first character for identifiers to escape those that are keywords 
     63                        _backEndName = .name 
    6664                else 
    67                         sharpPrefix = '' 
    68                 _sharpName = sharpPrefix + .name 
    69                 _useSharpNameStack.push(_sharpName) 
     65                        _backEndName = BackEndUtils.backEndNameForLocalVarName(.name) 
     66                _useBackEndNameStack.push(_backEndName) 
    7067 
    7168        get englishName as String is abstract 
     
    7774        pro isTracked from var 
    7875 
    79         get useSharpNameStack from var 
     76        get useBackEndNameStack from var 
    8077 
    8178        def addMinFields 
     
    232229                base.init(_makeToken(token, codePart), codePart.resultType) 
    233230                .isImplicit = true 
    234                 _sharpName = codePart.sharpResultVarName 
     231                _backEndName = codePart.backEndResultVarName 
    235232 
    236233        def _makeToken(token as IToken, codePart as AbstractMethod) as IToken is shared