Changeset 1752
- Timestamp:
- 11/09/08 02:01:13 (2 months ago)
- Location:
- cobra/trunk/Source
- Files:
-
- 6 modified
-
BackEndClr/SharpGenerator.cobra (modified) (19 diffs)
-
Compiler.cobra (modified) (1 diff)
-
Members.cobra (modified) (2 diffs)
-
Statements.cobra (modified) (1 diff)
-
Utils.cobra (modified) (1 diff)
-
Vars.cobra (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
cobra/trunk/Source/BackEndClr/SharpGenerator.cobra
r1751 r1752 15 15 var _fullExeFileName as String = '' 16 16 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 17 25 18 26 def writeSharp … … 350 358 351 359 352 353 360 ## 354 361 ## Node … … 1241 1248 return 'class' # because the extension is always housed in a C# class 1242 1249 1243 var _ sharpName as String?1250 var _backEndName as String? 1244 1251 1245 1252 get sharpName as String is override 1246 if not _ sharpName1247 _ 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 ! 1249 1256 1250 1257 get sharpThis as String is override … … 1271 1278 is partial 1272 1279 1273 get use SharpNameStack as Stack<of String>1280 get useBackEndNameStack as Stack<of String> 1274 1281 1275 1282 get sharpAssignmentNames as List<of String>? … … 1284 1291 1285 1292 get sharpAssignmentNames as List<of String> 1286 return [_ sharpName]1287 # return List<of String>(_use SharpNameStack)1293 return [_backEndName] 1294 # return List<of String>(_useBackEndNameStack) 1288 1295 1289 1296 get sharpName as String is override 1290 1297 if _ifInheritsStack.count 1291 1298 if _type.isReference 1292 return '(([_ifInheritsStack.peek.sharpRef])[_ sharpName])'1299 return '(([_ifInheritsStack.peek.sharpRef])[_backEndName])' 1293 1300 else 1294 1301 # 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 1295 1302 return '[.name].Value' # Nullable<of T>.Value 1296 1303 else 1297 return _ sharpName1298 # return _use SharpNameStack.peek to !1304 return _backEndName 1305 # return _useBackEndNameStack.peek to ! 1299 1306 1300 1307 … … 1331 1338 1332 1339 get sharpName as String is override 1333 return _ sharpName1340 return _backEndName 1334 1341 1335 1342 … … 1433 1440 is partial 1434 1441 1435 var _use SharpNameStack as Stack<of String>1436 1437 get use SharpNameStack from var1442 var _useBackEndNameStack as Stack<of String> 1443 1444 get useBackEndNameStack from var 1438 1445 1439 1446 get sharpAssignmentNames as List<of String> 1440 return [_ sharpName]1441 # return List<of String>(_use SharpNameStack)1447 return [_backEndName] 1448 # return List<of String>(_useBackEndNameStack) 1442 1449 1443 1450 get sharpName as String is override 1444 1451 if _ifInheritsStack.count 1445 1452 if _type.isReference 1446 return '(([_ifInheritsStack.peek.sharpRef])[_ sharpName])'1453 return '(([_ifInheritsStack.peek.sharpRef])[_backEndName])' 1447 1454 else 1448 1455 # 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>.Value1456 return '[_backEndName].Value' # Nullable<of T>.Value 1450 1457 else 1451 1458 return .name 1452 # return _use SharpNameStack.peek to !1453 1454 get _ sharpName as String1459 # return _useBackEndNameStack.peek to ! 1460 1461 get _backEndName as String 1455 1462 name = .name 1456 1463 if not name.startsWith('_') … … 1463 1470 .writeSharpIsNames(sw) 1464 1471 sw.write(_type.sharpRef) 1465 sw.write(' [_ sharpName]')1472 sw.write(' [_backEndName]') 1466 1473 if _initExpr 1467 1474 sw.write(' = ') … … 1473 1480 is partial 1474 1481 1475 var _ sharpResultVarName = ''1482 var _backEndResultVarName = '' 1476 1483 1477 1484 get sharpGenericParams as String … … 1482 1489 return '' 1483 1490 1484 get sharpResultVarName from var1491 get backEndResultVarName from var 1485 1492 1486 1493 def writeSharpDef(sw as SharpWriter) … … 1520 1527 sharpInit = 'DateTime.Today' 1521 1528 sw.write('[param.sharpName] = [sharpInit];') 1522 if _ sharpResultVarName.length # set in _bindImp1523 sw.write('[_returnType.sharpRef] [_ sharpResultVarName]')1529 if _backEndResultVarName.length # set in _bindImp 1530 sw.write('[_returnType.sharpRef] [_backEndResultVarName]') 1524 1531 if _returnType.sharpInit.length 1525 1532 sw.write('= [_returnType.sharpInit]') … … 1606 1613 1607 1614 def writeSharpEnsureArgs(sw as SharpWriter) 1608 if . sharpResultVarName.length1609 sw.write('[. sharpResultVarName]')1615 if .backEndResultVarName.length 1616 sw.write('[.backEndResultVarName]') 1610 1617 sep = [','] 1611 1618 else … … 1626 1633 1627 1634 def writeSharpEnsureParamDecls(sw as SharpWriter) 1628 if . sharpResultVarName.length1629 sw.write('[.resultType.sharpRef] [. sharpResultVarName]')1635 if .backEndResultVarName.length 1636 sw.write('[.resultType.sharpRef] [.backEndResultVarName]') 1630 1637 sep = [','] 1631 1638 else … … 2299 2306 #newName = '_lh_' + _ifInheritsVar.name + '_' + _ifInheritsType.sharpNameComponent 2300 2307 #sw.write('[typeName] [newName] = ([typeName])[_ifInheritsVar.sharpName];\n') 2301 #_ifInheritsVar.use SharpNameStack.push(newName)2308 #_ifInheritsVar.useBackEndNameStack.push(newName) 2302 2309 ifInheritsCount = _ifInheritsVar.ifInheritsStack.count # AssignExpr could pop our _ifInheritsType. We detect that with the count. 2303 2310 _ifInheritsVar.ifInheritsStack.push(_ifInheritsType) … … 2305 2312 stmt.writeSharpStmt(sw) 2306 2313 if _ifInheritsVar and _ifInheritsVar.ifInheritsStack.count > ifInheritsCount 2307 #_ifInheritsVar.use SharpNameStack.pop2314 #_ifInheritsVar.useBackEndNameStack.pop 2308 2315 _ifInheritsVar.ifInheritsStack.pop 2309 2316 if close … … 2644 2651 is partial 2645 2652 2646 var _ sharpResultVarName as String?2647 2648 pro sharpResultVarName from var2653 var _backEndResultVarName as String? 2654 2655 pro backEndResultVarName from var 2649 2656 2650 2657 def writeSharpDef(sw as SharpWriter) … … 2655 2662 sw.write('_lh_canEnsure = true;\n') 2656 2663 if _expr 2657 if willEnsure and _ sharpResultVarName and _sharpResultVarName.length2658 sw.write('return [_ sharpResultVarName]=')2664 if willEnsure and _backEndResultVarName and _backEndResultVarName.length 2665 sw.write('return [_backEndResultVarName]=') 2659 2666 _expr.writeSharpDefInContext(sw) 2660 2667 sw.write(';\n') … … 2864 2871 sw.write('_lh_canEnsure = true;\n') 2865 2872 if _expr 2866 sharpResultVarName = .compiler.curCodeMember.sharpResultVarName2867 if willEnsure and sharpResultVarName.length2873 backEndResultVarName = .compiler.curCodeMember.backEndResultVarName 2874 if willEnsure and backEndResultVarName.length 2868 2875 # TODO: resolve yield return with ensure 2869 # sw.write('yield return [ sharpResultVarName]=')2876 # sw.write('yield return [backEndResultVarName]=') 2870 2877 # so for now: 2871 2878 sw.write('yield return ') … … 4516 4523 def writeSharpTestInvocation(sw as SharpWriter) 4517 4524 pass 4525 4526 4527 ## 4528 ## Misc items 4529 ## 4530 4531 class 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 286 286 .bindImp 287 287 .writeSourceCodeCorrections 288 .writeSharp 289 if writeTestInvocation 290 .writeSharpTestInvocation 291 .compileSharp('/main:RunTests') 292 else 293 .compileSharp 294 288 .generateCode(writeTestInvocation) 289 295 290 def testifyFilesNamed(fileNames as IList<of String>, options as OptionValues, resultsWriter as IndentedWriter, verbose as bool) 296 291 """ -
cobra/trunk/Source/Members.cobra
r1745 r1752 401 401 _initExpr = initExpr 402 402 _ifInheritsStack = Stack<of IType>() 403 _use SharpNameStack = Stack<of String>() # for if-inherits404 _use SharpNameStack.push(name)403 _useBackEndNameStack = Stack<of String>() # for if-inherits 404 _useBackEndNameStack.push(name) 405 405 406 406 def addRefFields is override … … 710 710 _requirePart.bindImp 711 711 if _ensurePart and _returnType and _returnType is not .compiler.voidType 712 _ sharpResultVarName = '_lh_result'712 _backEndResultVarName = '_lh_result' 713 713 else 714 _ sharpResultVarName = ''714 _backEndResultVarName = '' 715 715 _curStmtIndex = 0 716 716 for stmt in _stmts.toArray -
cobra/trunk/Source/Statements.cobra
r1728 r1752 1004 1004 .throwError('Return statement must return a [curCodeMember.resultType.name], or [curCodeMember.name] must have its return type removed.') 1005 1005 curCodeMember.hasReturnStmt = true 1006 _ sharpResultVarName = curCodeMember.sharpResultVarName1006 _backEndResultVarName = curCodeMember.backEndResultVarName 1007 1007 1008 1008 -
cobra/trunk/Source/Utils.cobra
r1742 r1752 239 239 return name[:upperCount].toLower + name[upperCount:] # utf8 240 240 241 242 ## Code gen243 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'.split245 246 # CC: should be a Set, not a Dictionary:247 var _sharpKeyWordSet = Dictionary<of String, bool>()248 249 def isSharpKeyWord(word as String) as bool250 """251 Returns true if the given word is a keyword in C# 2.0.252 """253 require254 word.length255 test256 assert .isSharpKeyWord('object')257 assert .isSharpKeyWord('if')258 assert not .isSharpKeyWord('total')259 body260 if _sharpKeyWordSet.count == 0261 for word in _sharpKeyWordList262 _sharpKeyWordSet[word] = true263 return _sharpKeyWordSet.containsKey(word)264 265 241 def sharpStringLiteralFor(s as String) as String 266 242 #print '<> csStringLitFor' -
cobra/trunk/Source/Vars.cobra
r1730 r1752 39 39 var _ifInheritsStack = Stack<of IType>() 40 40 var _isAssignedTo as bool 41 var _ sharpName as String42 var _use SharpNameStack = Stack<of String>() # for if-inherits41 var _backEndName as String 42 var _useBackEndNameStack = Stack<of String>() # for if-inherits 43 43 44 44 def init(token as IToken, type as IType) … … 61 61 _isTracked = false 62 62 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 66 64 else 67 sharpPrefix = '' 68 _sharpName = sharpPrefix + .name 69 _useSharpNameStack.push(_sharpName) 65 _backEndName = BackEndUtils.backEndNameForLocalVarName(.name) 66 _useBackEndNameStack.push(_backEndName) 70 67 71 68 get englishName as String is abstract … … 77 74 pro isTracked from var 78 75 79 get use SharpNameStack from var76 get useBackEndNameStack from var 80 77 81 78 def addMinFields … … 232 229 base.init(_makeToken(token, codePart), codePart.resultType) 233 230 .isImplicit = true 234 _ sharpName = codePart.sharpResultVarName231 _backEndName = codePart.backEndResultVarName 235 232 236 233 def _makeToken(token as IToken, codePart as AbstractMethod) as IToken is shared
