| 1 | = Ver Directive = |
| 2 | This does not describe an existing feature. |
| 3 | |
| 4 | A partial (and ongoing) implementation is available as a patch on ticket:312 |
| 5 | |
| 6 | Here is a a place to record notes and Doc for conditional compile directive and related features. |
| 7 | |
| 8 | == Motivation == |
| 9 | |
| 10 | Conditional inclusion of source code (aka "conditional compilation") is needed in many circumstances to accommodate |
| 11 | different code versions for different hardware and OS platforms and (wrt cobra) different target backends/target Libraries and |
| 12 | added coding for augmentation wrt different compiler options (tests, asserts on, contracts etc) |
| 13 | |
| 14 | Other languages have various levels of support for conditional compilation. Progressing canonically from that implemented as a preprocessor by C, modified for Std C and C++. Adjusted in later languages C# (feature restricted (wrt C/C++) #if, #define), D (version and debug conditions, static-if ) and java (supported (or not) as a language false branching code removal optimisation) |
| 15 | |
| 16 | This concept and syntax is based loosely on that of D (v2) without (currently) support for anything similar to static-if. |
| 17 | see [http://dlang.org/version.html D Conditional Compilation]. |
| 18 | |
| 19 | This directive describes and presents a setup for generating multiple versions of executable code from a single source code base[[BR]] |
| 20 | Hence the directive '''@ver''' is a shortened form indicating version or versioning. [[BR]] |
| 21 | (This is presented as a goal (versions) not a mechanism (utilising platform-chk, hardware, backend detection, ...) |
| 22 | |
| 23 | |
| 24 | == Design Drivers == |
| 25 | |
| 26 | Problems with preprocessor like constructs for conditional compilation are well known and should be avoided. |
| 27 | |
| 28 | Optionally compiled blocks in cobra should look like (tagged) code blocks not code in branch conditions in some |
| 29 | overlay variant language to cobra. |
| 30 | |
| 31 | We can, should and must do better than the solution provided circa 1970/1980 for C/C++ |
| 32 | |
| 33 | KISS. Favor clarity and simplicity for normal/common cases rather than providing capability for every possible conceivable case. |
| 34 | |
| 35 | == Concept == |
| 36 | |
| 37 | Code to be conditionally compiled should be presented in an inline separate indented block prefixed by a compiler directive tag |
| 38 | used to match the situation that triggers the inclusion of the conditionally compiled code. |
| 39 | |
| 40 | This tag is not a general purpose boolean condition.[[BR]] |
| 41 | Its a match to a 'situation' presented as a specifically named symbol possibly narrowed by additional specifications. |
| 42 | |
| 43 | Each conditionally compiled clause stands alone, unrelated to condCompile constructs before or after. |
| 44 | |
| 45 | |
| 46 | == Syntax == |
| 47 | {{{ |
| 48 | @ver.<tag>[.<tagSpec>] cobra-statement |
| 49 | |
| 50 | @ver.<tag>[.<tagSpec>] |
| 51 | <indented code-block> |
| 52 | }}} |
| 53 | |
| 54 | Version directives are only recognised in select places: |
| 55 | i.e start of line in |
| 56 | * a method |
| 57 | * a Box declaration (class, struct, interface, etc) |
| 58 | * a nameSpace declaration |
| 59 | |
| 60 | A code block can be any of: |
| 61 | * an indented block of statements |
| 62 | * an indented block of Box member declarations |
| 63 | * an indented block of Namespace members |
| 64 | Corresponding to the places a conditional compile (ver) directive is recognised.[[BR]] |
| 65 | (statements in a method, decls in a Box ( class-like declaration), decls in a NameSpace) |
| 66 | |
| 67 | e.g. |
| 68 | {{{ |
| 69 | #!cobra |
| 70 | @ver.never |
| 71 | a = 1234 |
| 72 | # this code is never included |
| 73 | |
| 74 | @ver.jvm l as List = java.util.ArrayList() |
| 75 | @ver.clr l = List() |
| 76 | |
| 77 | @ver.clr.35 |
| 78 | # do domething requiring CLR ver 3.5 |
| 79 | |
| 80 | |
| 81 | }}} |
| 82 | |
| 83 | == Tags == |
| 84 | |
| 85 | These are predefined tags that the compiler provides to give matches for:. |
| 86 | |
| 87 | * '''clr''' - backend is CLR |
| 88 | * '''jvm''' - backed is jvm |
| 89 | * '''objc''' - backend is objc |
| 90 | * '''windows''' - platform is Windows |
| 91 | * '''mac''' - platform is Apple Mac |
| 92 | * '''unix''' - platform is unix |
| 93 | * '''dotnet''' - is running on .Net |
| 94 | * '''mono''' - is running on mono |
| 95 | * '''asserts''' - asserts are enabled |
| 96 | * '''contracts''' - contracts are enabled |
| 97 | * '''tests''' - tests are enabled |
| 98 | * '''always''' - always true - always include code block |
| 99 | * '''never''' - never included - suppress code block |
| 100 | * '''cobra''' - match to cobra version number |
| 101 | |
| 102 | * '''otherThan''' - define to include code if the following tag/tags are NOT in effect |
| 103 | |
| 104 | Arbitrary user defined tags can be set by passing a name (or name+value) to the compiler using the compiler -define option |
| 105 | {{{ |
| 106 | cobra -define:tagName ... |
| 107 | cobra -define:tagName=value ... ( perhaps eventually) |
| 108 | |
| 109 | e.g. |
| 110 | cobra -define:color ..... |
| 111 | }}} |
| 112 | |
| 113 | |
| 114 | Some tags may have additional specifications to narrow or better define the match. |
| 115 | These follow the tag separated by a '.' |
| 116 | * Version Numbers - where a version number or range is being checked for, |
| 117 | the spec following the tag is an integer or an integer range ( two integers separated by '-') |
| 118 | This matches if the version number is >= the version number given or version number is within the range specification given. |
| 119 | (dotted version numbers (like '2.1.0') are converted to an integer by dropping internal dots ( so '2.1.0' becomes 210). |
| 120 | e.g. |
| 121 | {{{ |
| 122 | running on clr - @ver.clr |
| 123 | clr > 2.0 - @ver.clr.20 |
| 124 | clr 2.0-3.1 inclusive - @ver.clr.20-31 |
| 125 | }}} |
| 126 | |
| 127 | * Where a specific value is being tested for - exact value to match follows tag separated by '.' |
| 128 | the value may be quoted if need be. |
| 129 | e.g. chk for tag named 'color' - @ver.color |
| 130 | tag named color with value 'red' - @ver.color.red, @ver.color.'red' |
| 131 | |
| 132 | |
| 133 | |
| 134 | == Additionally (Different but the same) == |
| 135 | |
| 136 | @debug and @env below could conceivably be implemented using @ver (@ver.debug and @ver.env.<envVarName>) tags but its |
| 137 | perhaps clearer, convenient and more concise to have separate directives. |
| 138 | |
| 139 | 1. @debug for code to be included when debug (-d) is on |
| 140 | {{{ |
| 141 | #!cobra |
| 142 | v = .doCalc |
| 143 | @debug print v |
| 144 | .postProcess(v) |
| 145 | |
| 146 | |
| 147 | val = .doBigCalc |
| 148 | @debug |
| 149 | vv = .prettyPrint(v) |
| 150 | print 'BigCalc val [v.summary]' |
| 151 | print ' -', vv |
| 152 | v1 = .postProcess(v) |
| 153 | |
| 154 | }}} |
| 155 | |
| 156 | 2. @env (optionally) for testing existence or value of on environment variable that can be used to include code.[[BR]] |
| 157 | Syntax is @env.<envVarName> or @env.<envVarName>.<value> |
| 158 | |
| 159 | {{{ |
| 160 | #!cobra |
| 161 | @ver.env.IS_COBRA_DEV |
| 162 | print 'running on cobra Development system' |
| 163 | |
| 164 | @ver.env.OSVer.'Windows_NT' # or @ver.env.OSVer.Windows_NT |
| 165 | print 'running on Windows NT' |
| 166 | }}} |
| 167 | |
| 168 | |
| 169 | 3. Use of the 'cobra' tag can be elided for the following version-spec and the match will devolve to that cobra |
| 170 | version spec( version Number or range). |
| 171 | i.e |
| 172 | {{{ |
| 173 | #!cobra |
| 174 | @ver.cobra.092 |
| 175 | # do something specific to cobra 0.92 or greater |
| 176 | |
| 177 | # is same as |
| 178 | @ver.092 |
| 179 | # also code specific to cobra 0.92 or greater. |
| 180 | }}} |
| 181 | |
| 182 | |
| 183 | == Notes re 'other' cases == |
| 184 | |
| 185 | Often you want to do something for a particular case (or cases) and something else for everything else not already mentioned |
| 186 | even if its only to gen a diagnostic. |
| 187 | |
| 188 | This is addressed for by the 'otherThan' tag.[[BR]] |
| 189 | It matches/succeeds when the test situation does not match to any of the |
| 190 | appended tags (this is the only tag that may have a list of tags following).[[BR]] |
| 191 | |
| 192 | Envisaged use is similar to the following |
| 193 | {{{ |
| 194 | #!cobra |
| 195 | |
| 196 | @ver.mono |
| 197 | #something specific to mono |
| 198 | @ver.otherThan.mono |
| 199 | # everything else not including mono |
| 200 | |
| 201 | |
| 202 | #perhaps more commonly for everything not explicitly already specified |
| 203 | |
| 204 | @ver.clr |
| 205 | #something specific to clr backend |
| 206 | @ver.jvm |
| 207 | #something specific to jvm |
| 208 | # many somethings |
| 209 | @ver.mac |
| 210 | #something specific to mac |
| 211 | @ver.otherThan.clr.jvm.mac # something for not clr,jvm or mac |
| 212 | #Any other backend TBD |
| 213 | @ct_trace 'otherThan clr, jvm, mac - Other backends not yet implemented' |
| 214 | }}} |
| 215 | |
| 216 | Note here that this is the same form as all the others ( a matching tag + code, entirely standalone and complete in itself) |
| 217 | but the sense of the match is reversed. |
| 218 | |
| 219 | The syntax for matching the tags may yet change (@ver.otherThan.clr|mac|jvm, @ver.otherThan.clr.3.5|jvm.7, ...) |
| 220 | |
| 221 | == Questions == |
| 222 | |
| 223 | Is this set rich enough/sufficient for common uses ? |
| 224 | |
| 225 | Is env var access necessary given -define on cobra commandLine ? |
| 226 | |
| 227 | Any other predefined tags needed? |
| 228 | |
| 229 | |
| 230 | == See Also == |
| 231 | |
| 232 | Discussion at [http://cobra-language.com/forums/viewtopic.php?f=4&t=966] |
| 233 | |
| 234 | IfDirective |
| 235 | |
| 236 | CompilerDirectives |
| 237 | |
| 238 | LanguageTopics |