| 1 | = Program Structure = |
| 2 | |
| 3 | (This page needs expansion/completion) |
| 4 | |
| 5 | A cobra program is composed of a series of source modules - These modules can be either |
| 6 | cobra or source code for the back end compiler (C# currently, eventually a range of other |
| 7 | backend compilers (java, objective-C) is expected to be supported). |
| 8 | |
| 9 | Backend source modules are passed to the backend compiler without change. |
| 10 | Cobra modules are validated and translated to backend source code and also passed to the |
| 11 | backend compiler for compilation. |
| 12 | The result is assembled into an executable, library or other binary file for execution or use with an executable. |
| 13 | |
| 14 | == Cobra Modules == |
| 15 | |
| 16 | Within a cobra source module the code is a sequence of clauses or directives |
| 17 | optionally enclosed in a namespace definition. |
| 18 | Within these clauses are subparts specific to the type of clause |
| 19 | and within that cobra code and expressions. |
| 20 | |
| 21 | A cobra module can start with an optional !DocString and/or a comment block prior to |
| 22 | any code clauses. |
| 23 | |
| 24 | Generally 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 |
| 26 | code(Top of file, first lines of class/struct/interface/enum decls, |
| 27 | after method/var decls, ...) |
| 28 | |
| 29 | Each major clause is defined by subsequent code at an increased indentation level |
| 30 | except for 'use' and 'sig' which consume tokens to the end of the current line. |
| 31 | |
| 32 | Clauses 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 | |
| 43 | A namespace may contain any of the above clauses (except namespace). If a namespace is |
| 44 | not provided the module code is built as if it had been specified in the global namespace. |
| 45 | |
| 46 | In 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 | {{{ |
| 53 | use NAMESPACE_NAME |
| 54 | |
| 55 | namespace NAMESPACE_NAME |
| 56 | NAMESPACE_BLOCK |
| 57 | |
| 58 | class CLASS_NAME |
| 59 | [inherits CLASS_NAME] |
| 60 | [implements INTERFACE_NAME] |
| 61 | [adds MIXIN_NAME] |
| 62 | [is ISNAMES_LIST] |
| 63 | CLASS_BLOCK |
| 64 | |
| 65 | struct STRUCT_NAME |
| 66 | [inherits CLASS_NAME] |
| 67 | [implements INTERFACE_NAME] |
| 68 | [adds MIXIN_NAME] |
| 69 | [is ISNAMES_LIST] |
| 70 | CLASS_BLOCK |
| 71 | |
| 72 | interface INTERFACE_NAME |
| 73 | CLASS_BLOCK |
| 74 | |
| 75 | mixin MIXIN_NAME |
| 76 | CLASS_BLOCK |
| 77 | |
| 78 | enum ENUM_NAME [of TYPE] |
| 79 | ENUM_VALUE [ = IntValue] [, ENUM_VALUE [= IntValue]... ] |
| 80 | |
| 81 | extend 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 | |
| 89 | event NAME as TYPE |
| 90 | [ is ISNAMES_LIST] |
| 91 | [ has ATTRIBUTES] |
| 92 | |
| 93 | sig TYPENAME(ARGS) |
| 94 | sig TYPENAME as RETURNTYPE |
| 95 | sig TYPENAME(ARGS) as RETURNTYPE |
| 96 | |
| 97 | assembly has ATTRIBUTE_NAME {has ATTRIBUTE_NAME}... |
| 98 | |
| 99 | @COMPILER_DIRECTIVE_ID {DIRECTIVE_ARGS} |
| 100 | %%COMPILER_DIRECTIVE_ID {DIRECTIVE_ARGS} |
| 101 | }}} |
| 102 | |
| 103 | == Platform == |
| 104 | |
| 105 | Currently the translation is only to C# and therefore only C# code is the non cobra source |
| 106 | supported. |
| 107 | |
| 108 | This is available for Windows platforms supporting C# using the Microsoft csharp compiler |
| 109 | and on other platforms supported by [http://www.go-mono.org mono] and the mono compiler. |
| 110 | |
| 111 | == Examples == |
| 112 | {{{ |
| 113 | """ |
| 114 | Example DocString for this module |
| 115 | """ |
| 116 | assembly has SharedAttribute |
| 117 | |
| 118 | use System.Text.RegularExpressions |
| 119 | |
| 120 | %%number decimal |
| 121 | |
| 122 | namespace 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 | |
| 168 | See subsequent expansion pages for each of the above clauses/directives. |
| 169 | |
| 170 | [wiki:LanguageTopics Back to Language Topics] |