Wiki

Changes between Initial Version and Version 1 of PortingC

Show
Ignore:
Timestamp:
10/29/09 12:23:14 (15 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PortingC

    v1 v1  
     1= Porting C# code to Cobra = 
     2 
     3 
     4see [wiki:C#Diffs Cobra differences fm C#] 
     5 
     6Heres a process for C# to Cobra generally. 
     7 
     8 
     9 1. Take .cs file, rename to .cobra file. 
     10 
     11 
     12 2. Change multiline comments from multiple // or /* */ bounded to be /# #/ bounded.[[br]] 
     13     Change single line comments to use leading ''''#'''' 
     14 
     15     Remove extraneous separator line comments between method calls - if any   
     16        {{{ // ------------------------------ }}} 
     17         
     18 3. Remove trailing '''';'''' from each line they occur on 
     19 
     20 4.  Fixup blocking and indentation. 
     21  Remove all '{' and '}' indicating start and end blocks from source.[[br]] 
     22  Ensure remaining blocks are all correctly (equally) indented and that  
     23  indentation uses either all spaces or all Tabs and that the 
     24  indents are in multiples of either 4 spaces or 1 tab. 
     25 
     26      
     27 5. Fix 'using' statements: [[br]] 
     28    '''using''' -> '''use''' [[br]] 
     29  Remove any lines for  
     30  {{{ 
     31using System 
     32using System.Collections.Generic 
     33using System.IO 
     34using System.Text 
     35  }}} 
     36  as cobra includes these automagically 
     37 
     38  Change any other lines from  
     39        ''''using xxxx;'''' to ''''use xxxx''''                     
     40 
     41 
     42 6. Namespace decl 
     43    Namespaces in cobra need to start with an upcased letter. [[BR]] 
     44    Remove any lowercased  prefix from namespace line   
     45 
     46 
     47 7. Correct any Enum definitions. [[br]] 
     48    After applying the above its basically removing trailing ',' and ensuring indentation 
     49    of enum items are all the same. 
     50    e.g.  
     51    {{{ 
     52enum ID_CONTROLS  { 
     53    ID_RADIO_ORIENT = 5999, 
     54    ID_CHK_SHOWIMAGES, 
     55    ID_BTN_NEXT_PAGE, 
     56    ID_NOTEBOOK 
     57} 
     58    }}} 
     59    to 
     60    {{{ 
     61enum ID_CONTROLS 
     62    ID_RADIO_ORIENT = 5999 
     63    ID_CHK_SHOWIMAGES 
     64    ID_BTN_NEXT_PAGE 
     65    ID_NOTEBOOK 
     66    }}} 
     67 
     68 
     69 8. Fix class declaration syntax 
     70      Change lines like ''''class X : Y'''' to ''''class X inherits Y''''         
     71 
     72 
     73 9. Change any class instance attributes/fields to be prefixed with ''''var'''' 
     74    Change type and accessmodifier to cobra forms 
     75   {{{ 
     76<Type> name = <init> ; 
     77   }}} 
     78   to  
     79   {{{ 
     80var name as <Type> = <init> 
     81    }}}            
     82 
     8310. Fixup class constructor definitions 
     84     For initializer/constructor change lines like : 
     85        {{{ public ClassName(params,...) : base(baseParams,...) }}} 
     86      to 
     87{{{ 
     88    cue init(params, ...) is public 
     89        base.init(baseParams,...)  
     90}}}         
     91 
     92     Within the params list correct type declaration syntax from C# to cobra 
     93     {{{ 
     94[<accessModifier> ...] <Type> name,  
     95     }}} 
     96     to  
     97     {{{ 
     98name as <Type> [is <accessModifier>, ...], 
     99     }}} 
     100 
     101     public is default accessModifier so can leave that off[[br]]        
     102     e.g. 
     103    {{{ 
     104public GeoArea (string title, GeoPoint pos, Size size) : base(title, pos, size) 
     105    }}} 
     106   to  
     107    {{{ 
     108cue init(title as String, pos as GeoPoint, size as Size ) is public 
     109    base.init (title, pos, size) 
     110    }}} 
     111                
     11211. Remove any uses of ''''new'''' to construct a class instance,  
     113    instead just make a call to the class type passing any initializer args, i.e just remove 'new'[[br]] 
     114    e.g. 
     115    {{{ 
     116    new gFile = new GeoFile() -> gFile = GeoFile() 
     117    new GeoPoint(10, 100)     -> GeoPoint(10, 100) 
     118    new Size(650,340)         -> Size(650, 340) 
     119    }}} 
     120 
     121    For classes that construct delegates (take method names) prefix the methodname with '''ref''',  
     122    methodNames must start with a lowercase letter and are prefixed by ''''this.'''' or just ''''.'''' 
     123    so change the methodName accordingly 
     124        e.g. {{{new Listener( OnFileChange )  -> Listener(ref .onFileChange) }}} 
     125 
     126 
     12712. Methods: 
     128    Prefix methods with '''def'''.[[br]] 
     129    Change methodnames to start with a lowercase letter, [[br]] 
     130    Change param definitions to cobra form.[[br]] 
     131        {{{[<accessModifier> ...] <Type> name,}}} -> {{{name as <Type> [is <accessModifier>, ...],}}} 
     132    Cobra methods by default return void and are public so if the C# code specifies '''public void'''  
     133    you can just delete those words. 
     134 
     135    Also, if a method has no parameters then the empty trailing () can be left off. 
     136             
     137    Note builtin types: 
     138        C# string -> String (System.String) [[br]] 
     139        C# object -> Object (System.Object) [[br]] 
     140    {{{ 
     141public void OnFileChange( object sender, Event e ) 
     142    }}}  
     143   to  
     144    {{{ 
     145def onFileChange(sender as Object, e as Event)   
     146 
     147    # no param method 
     148    public override bool OnInit() 
     149    }}} 
     150     -> 
     151    {{{ 
     152def onInit as bool is override  
     153    # or def onInit as bool is public, override # explicit 
     154}}} 
     155             
     15613. Calls to methods, properties and attributes/fields. 
     157     Instance method names, properties and fields are called with a prefix of ''''this.'''' or just ''''.'''' [[br]] 
     158     and they all must be named starting with a lowercase letter so the calls and settings[[br]] 
     159     must be modified accordingly. 
     160 
     161     e.g. 
     162   {{{ 
     163    gFile.AppendPosn(...   ->   gFile.appendPosn( ... 
     164    CreateNewCity()       -> .createNewCity # or this.createNewCity 
     165   }}} 
     166    Similarly with properties and fields 
     167   {{{ 
     168    Street = new Street(this, ...   ->  .street = Street(this, ... 
     169    }}} 
     170 
     171    If a method takes no args then any empty trailing () on a method call can/should be left off. 
     172 
     17314.  Property definition. Depending on the property use, prefix properties with :[[br]]  
     174        * get - readable property only  
     175        * set - settable property only 
     176        * pro - gettable and settable 
     177 
     178    Change property names to start with a lowercase letter 
     179 
     180 
     18115. Local variables declared and assigned on first use can usually be changed to remove  
     182    the type definition (type inferred from the assignment) or can still be explicitly typed. 
     183    {{{ 
     184    DataItem info = new DataItem()  
     185      -> 
     186    info = DataItem()     # or    info as DataItem = DataItem() 
     187    }}} 
     188     
     189 
     190 
     19116. Fixup 'For' loops 
     192    {{{ 
     193for ( <Type> name = start ; name < stop; ++name )  
     194        -> 
     195for name as Type in start : stop : 1 # explicit or equally use shortcuts: 
     196for name in start : stop : 1  
     197for name  in start : stop  # if step is one (1) 
     198for name in stop           # if start is zero (0) and step is one (1) 
     199    # same as for name in 0 : stop : 1 
     200    }}} 
     201     
     202    e.g {{{ for ( int i = 0; i < 200; ++i ) -> for i in 0 : 200 }}}     
     203 
     204 
     20517. Casting 
     206    Casts of form '(Type)name' become 'name to Type' [[BR]] 
     207    e.g.  {{{ ... (object) cl.MyString ...  -->  ... cl.myString to Object ... }}} 
     208 
     209    Casts of form 'a as Type' can also become 'name to Type' or 'name to? Type' 
     210    e.g {{{ ListEvent le = event as ListEvent;   --->  le = event to ListEvent }}} 
     211         
     21218. String catenated with expressions (converted to string) can be modified to use string interpolation. 
     213    Put the expressions ( suitably cobra converted) into the string inside [] 
     214    e.g. {{{ "Value " + item.Value + " selected item: " + item.Selected  --->  "Value [item.value] selected item:[item.selected]" }}} 
     215              
     21619. Adding eventHandlers 
     217    Cobra uses the '''listen''' statement to register event handlers where C# uses an override on  
     218    the += operator.  [[br]] 
     219    Otherwise normal cobra mapping for method names and reference to methods (vs method calls) apply;[[br]] 
     220    Methodnames in Cobra must start with a lower case letter and calls to this instance names  
     221        are prefixed with ''''this.'''' or (easier) ''''.'''' 
     222     
     223    When translating from a 'use'd library namespace,  calls to[[br]]  
     224        Capcased methodnames are referenced in cobra with the first '''char''' lowercased,[[br]]  
     225        Upcased methodnames  are referenced with the first '''word''' lowercased 
     226    so  
     227    {{{ 
     228    EVENT +=  new Listener(MethodName)  in C# becomes 
     229    listen .EVENT,      Listener( ref .methodName )  
     230 
     231    EVENT += new Listener( EH_METHOD_NAME ) in C# becomes 
     232    listen .EVENT,      Listener( ref .eh_METHOD_NAME )  
     233    }}} 
     234    e.g. 
     235    {{{ 
     236    FileChange += new Listener( OnFileChange ) 
     237        -> 
     238    listen .fileChange,    Listener(ref .onFileChange)  
     239    }}} 
     240 
     241 
     24220. Main method 
     243    'public void' is default, static is an accessmodifier which becomes ''''shared'''' in cobra, 
     244    C# attributes (!MetaInformation) are denoted with ''''has'''' 
     245    e.g. 
     246    {{{ 
     247[STAThread] 
     248static public void main() 
     249    }}} 
     250    becomes  
     251    {{{ 
     252def main is shared has STAThread  
     253    }}}     
     254