Wiki

Changes between Initial Version and Version 1 of Program

Show
Ignore:
Timestamp:
02/15/10 00:46:01 (15 years ago)
Author:
hopscc
Comment:

first stab at describibg cobra program

Legend:

Unmodified
Added
Removed
Modified
  • Program

    v1 v1  
     1= Program Structure =  
     2 
     3(This page needs expansion/completion) 
     4 
     5A cobra program is composed of a series of source modules - These modules can be either 
     6cobra or source code for the back end compiler (C# currently, eventually a range of other  
     7backend compilers (java, objective-C) is expected to be supported). 
     8 
     9Backend source modules are passed to the backend compiler without change.  
     10Cobra modules are validated and translated to backend source code and also passed to the  
     11backend compiler for compilation.  
     12The result is assembled into an executable, library or other binary file for execution or use with an executable. 
     13 
     14== Cobra Modules == 
     15 
     16Within a cobra source module the code is a sequence of clauses or directives  
     17optionally enclosed in a namespace definition.  
     18Within these clauses are subparts specific to the type of clause  
     19and within that cobra code and expressions.  
     20 
     21A cobra module can start with an optional !DocString and/or a comment block prior to  
     22any code clauses. 
     23 
     24Generally comments are like whitespace - they can be placed anywhere in a source module. 
     25!DocStrings are more specific, being recognised only in a few natural positions within the 
     26code(Top of file, first lines of  class/struct/interface/enum decls,  
     27after method/var decls, ...) 
     28 
     29Each major clause is defined by subsequent code at an increased indentation level  
     30except for 'use' and 'sig' which consume tokens to the end of the current line. 
     31 
     32Clauses are: 
     33    * use       - specify a namespace and its contents to be used by this module. 
     34    * namespace - specify the namespace the enclosed code resides in. 
     35    * class     - specify a custom (reference) type grouping together variables of other types, methods and events. 
     36    * struct    - specify a (lightweight) custom value type (similar though more limited than a class).   
     37    * interface - provide an interface definition that other classes and structs must conform to. 
     38    * mixin     - specify a definition for a piece of capability that can be injected (mixed-in) to classes across any class hierarchy.  
     39    * enum      - specify an enumerated type that other items can reference. 
     40    * extend    - specify an extension to an existing type. 
     41    * sig (signature) - describe a type for a method signature (C# calls these delegates). 
     42 
     43A namespace may contain any of the above clauses (except namespace). If a namespace is 
     44not provided the module code is built as if it had been specified in the global namespace. 
     45 
     46In addition there are two keys  
     47    * assembly - specify Attributes for this assembly  
     48    * '%%' ( deprecated) or '@' - specify a directive to the cobra compiler. 
     49      See [wiki:CompilerDirectives compiler directives] 
     50 
     51== Grammar == 
     52{{{ 
     53use NAMESPACE_NAME 
     54 
     55namespace NAMESPACE_NAME 
     56    NAMESPACE_BLOCK 
     57     
     58class CLASS_NAME  
     59    [inherits CLASS_NAME]  
     60    [implements INTERFACE_NAME] 
     61    [adds MIXIN_NAME] 
     62    [is ISNAMES_LIST] 
     63    CLASS_BLOCK 
     64         
     65struct STRUCT_NAME 
     66    [inherits CLASS_NAME]  
     67    [implements INTERFACE_NAME] 
     68    [adds MIXIN_NAME] 
     69    [is ISNAMES_LIST] 
     70    CLASS_BLOCK 
     71 
     72interface INTERFACE_NAME 
     73    CLASS_BLOCK 
     74     
     75mixin MIXIN_NAME 
     76    CLASS_BLOCK 
     77 
     78enum ENUM_NAME [of TYPE] 
     79    ENUM_VALUE [ = IntValue] [, ENUM_VALUE [= IntValue]... ] 
     80     
     81extend CLASS_NAME 
     82    [ is ISNAMES_LIST] 
     83    [ has ATTRIBUTESLIST] 
     84    [ where GENERICCONSTRAINTS] 
     85    [ inherits CLASS_NAME] 
     86    [ implements INTERFACE_NAME] 
     87    CLASS_EXTENSION_BLOCK 
     88 
     89event NAME as TYPE  
     90    [ is ISNAMES_LIST] 
     91    [ has ATTRIBUTES] 
     92 
     93sig TYPENAME(ARGS) 
     94sig TYPENAME as RETURNTYPE     
     95sig TYPENAME(ARGS) as RETURNTYPE     
     96 
     97assembly has ATTRIBUTE_NAME {has ATTRIBUTE_NAME}... 
     98 
     99@COMPILER_DIRECTIVE_ID {DIRECTIVE_ARGS} 
     100%%COMPILER_DIRECTIVE_ID {DIRECTIVE_ARGS} 
     101}}} 
     102 
     103== Platform == 
     104 
     105Currently the translation is only to C# and therefore only C# code is the non cobra source 
     106supported. 
     107 
     108This is available for Windows platforms supporting C# using the Microsoft csharp compiler 
     109and on other platforms supported by [http://www.go-mono.org mono]  and the mono compiler. 
     110 
     111== Examples == 
     112{{{ 
     113""" 
     114Example DocString for this module 
     115""" 
     116assembly has SharedAttribute 
     117 
     118use System.Text.RegularExpressions 
     119 
     120%%number decimal 
     121 
     122namespace Hops 
     123    class Example 
     124        var counter = 0 
     125         
     126        def incCount(i as int) 
     127            .counter += i 
     128         
     129        def main is shared 
     130            e = Example() 
     131            e.incCount(10) 
     132            assert e.counter == 10 
     133             
     134    class AnotherExample 
     135        pass 
     136         
     137    struct Point 
     138        var x = 0 
     139        var y = 0 
     140     
     141    enum ColorPart 
     142        """What is the color of magic""" 
     143        Red 
     144        Green 
     145        Blue 
     146        Octarine 
     147    
     148    sig VoidDelegate   # method taking no args and having no return type 
     149    sig NullStringDelegate(s as String) as String? # method taking String and returning String or null 
     150 
     151    extend String 
     152        def fmt(args as vari Object) as String 
     153            """ 
     154            Returns the string with any given args applied to it via String.Format 
     155            """ 
     156            test 
     157                s = '{0}is{1}' 
     158                assert s.fmt('0', '1') =='0is1' 
     159                assert s.fmt(2, 1) == '2is1' 
     160                assert s.fmt('One', 'NotTwo') == 'OneisNotTwo' 
     161                assert s.fmt(nil, nil) == 'is' 
     162            body 
     163                return String.format(this, args) to ! 
     164}}}             
     165 
     166== Notes == 
     167 
     168See subsequent expansion pages for each of the above clauses/directives. 
     169 
     170[wiki:LanguageTopics Back to Language Topics]