| 1 | <h2>Comments</h2> |
| 2 | <pre><code>// Single line comment |
| 3 | /* Multi-line |
| 4 | comment */ |
| 5 | /+ Mutli-line |
| 6 | /+ nestable +/ |
| 7 | comment +/</code></pre> |
| 8 | |
| 9 | <h2>Numbers</h2> |
| 10 | <pre><code>0 .. 2_147_483_647 |
| 11 | 2_147_483_648 .. 9_223_372_036_854_775_807 |
| 12 | 0L .. 9_223_372_036_854_775_807L |
| 13 | 0U .. 4_294_967_296U |
| 14 | 4_294_967_296U .. 18_446_744_073_709_551_615U |
| 15 | 0UL .. 18_446_744_073_709_551_615UL |
| 16 | 0x0 .. 0x7FFF_FFFF |
| 17 | 0x8000_0000 .. 0xFFFF_FFFF |
| 18 | 0x1_0000_0000 .. 0x7FFF_FFFF_FFFF_FFFF |
| 19 | 0x8000_0000_0000_0000 .. 0xFFFF_FFFF_FFFF_FFFF |
| 20 | 0x0L .. 0x7FFF_FFFF_FFFF_FFFFL |
| 21 | 0x8000_0000_0000_0000L .. 0xFFFF_FFFF_FFFF_FFFFL |
| 22 | 0x0U .. 0xFFFF_FFFFU |
| 23 | 0x1_0000_0000U .. 0xFFFF_FFFF_FFFF_FFFFU |
| 24 | 0x0UL .. 0xFFFF_FFFF_FFFF_FFFFUL |
| 25 | |
| 26 | 123_456.567_8 // 123456.5678 |
| 27 | 1_2_3_4_5_6_.5_6_7_8 // 123456.5678 |
| 28 | 1_2_3_4_5_6_.5e-6_ // 123456.5e-6 |
| 29 | 0x1.FFFFFFFFFFFFFp1023 // double.max |
| 30 | 0x1p-52 // double.epsilon |
| 31 | 1.175494351e-38F // float.min |
| 32 | 6.3i // idouble 6.3 |
| 33 | 6.3fi // ifloat 6.3 |
| 34 | 6.3Li // ireal 6.3 |
| 35 | 4.5 + 6.2i // complex number (phased out)</code></pre> |
| 36 | |
| 37 | <h2>Strings</h2> |
| 38 | <pre><code>// WYSIWYG strings |
| 39 | r"hello" |
| 40 | r"c:\root\foo.exe" |
| 41 | r"ab\n" |
| 42 | `hello` |
| 43 | `c:\root\foo.exe` |
| 44 | `ab\n` |
| 45 | |
| 46 | // Double-quoted strings |
| 47 | "hello" |
| 48 | "c:\\root\\foo.exe" |
| 49 | "ab\n" |
| 50 | "ab |
| 51 | " |
| 52 | |
| 53 | // Hex strings |
| 54 | x"0A" |
| 55 | x"00 FBCD 32FD 0A" |
| 56 | |
| 57 | // String postfix characters |
| 58 | "hello"c // string |
| 59 | "hello"w // wstring |
| 60 | "hello"d // dstring |
| 61 | |
| 62 | // Delimited strings |
| 63 | q"(foo(xxx))" |
| 64 | q"[foo{]" |
| 65 | q"EOS |
| 66 | This |
| 67 | is a multi-line |
| 68 | heredoc string |
| 69 | EOS" |
| 70 | q"/foo]/" |
| 71 | |
| 72 | // Token strings |
| 73 | q{foo} |
| 74 | q{/*}*/ } |
| 75 | q{ foo(q{hello}); } |
| 76 | q{ __TIME__ } |
| 77 | |
| 78 | // Character literals |
| 79 | 'a' |
| 80 | '\u000A'</code></pre> |
| 81 | |
| 82 | <h2>Iasm registers</h2> |
| 83 | <pre><code>AL AH AX EAX |
| 84 | BL BH BX EBX |
| 85 | CL CH CX ECX |
| 86 | DL DH DX EDX |
| 87 | BP EBP |
| 88 | SP ESP |
| 89 | DI EDI |
| 90 | SI ESI |
| 91 | ES CS SS DS GS FS |
| 92 | CR0 CR2 CR3 CR4 |
| 93 | DR0 DR1 DR2 DR3 DR6 DR7 |
| 94 | TR3 TR4 TR5 TR6 TR7 |
| 95 | ST |
| 96 | ST(0) ST(1) ST(2) ST(3) ST(4) ST(5) ST(6) ST(7) |
| 97 | MM0 MM1 MM2 MM3 MM4 MM5 MM6 MM7 |
| 98 | XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 |
| 99 | |
| 100 | RAX RBX RCX RDX |
| 101 | BPL RBP |
| 102 | SPL RSP |
| 103 | DIL RDI |
| 104 | SIL RSI |
| 105 | R8B R8W R8D R8 |
| 106 | R9B R9W R9D R9 |
| 107 | R10B R10W R10D R10 |
| 108 | R11B R11W R11D R11 |
| 109 | R12B R12W R12D R12 |
| 110 | R13B R13W R13D R13 |
| 111 | R14B R14W R14D R14 |
| 112 | R15B R15W R15D R15 |
| 113 | XMM8 XMM9 XMM10 XMM11 XMM12 XMM13 XMM14 XMM15 |
| 114 | YMM0 YMM1 YMM2 YMM3 YMM4 YMM5 YMM6 YMM7 |
| 115 | YMM8 YMM9 YMM10 YMM11 YMM12 YMM13 YMM14 YMM15</code></pre> |
| 116 | |
| 117 | <h2>Full example</h2> |
| 118 | <pre><code>#!/usr/bin/dmd -run |
| 119 | /* sh style script syntax is supported! */ |
| 120 | /* Hello World in D |
| 121 | To compile: |
| 122 | dmd hello.d |
| 123 | or to optimize: |
| 124 | dmd -O -inline -release hello.d |
| 125 | or to get generated documentation: |
| 126 | dmd hello.d -D |
| 127 | */ |
| 128 | import std.stdio; // References to commonly used I/O routines. |
| 129 | void main(char[][] args) // 'void' here means return 0 by default. |
| 130 | { |
| 131 | // Write-Formatted-Line |
| 132 | writefln("Hello World, " // automatic concatenation of string literals |
| 133 | "Reloaded"); |
| 134 | // Strings are denoted as a dynamic array of chars 'char[]' |
| 135 | // auto type inference and built-in foreach |
| 136 | foreach(argc, argv; args) |
| 137 | { |
| 138 | // OOP is supported, of course! And automatic type inference. |
| 139 | auto cl = new CmdLin(argc, argv); |
| 140 | |
| 141 | // 'writefln' is the improved 'printf' !! |
| 142 | // user-defined class properties. |
| 143 | writefln(cl.argnum, cl.suffix, " arg: %s", cl.argv); |
| 144 | // Garbage Collection or explicit memory management - your choice!!! |
| 145 | delete cl; |
| 146 | } |
| 147 | // Nested structs, classes and functions! |
| 148 | struct specs |
| 149 | { |
| 150 | // all vars. automatically initialized |
| 151 | int count, allocated; |
| 152 | } |
| 153 | |
| 154 | // Note that declarations read right-to-left. |
| 155 | // So that 'char[][]' reads as an array of an array of chars. |
| 156 | |
| 157 | specs argspecs(char[][] args) |
| 158 | // Optional (built-in) function contracts. |
| 159 | in{ |
| 160 | assert (args.length > 0); // assert built in |
| 161 | } |
| 162 | out(result){ |
| 163 | assert(result.count == CmdLin.total); |
| 164 | assert(result.allocated > 0); |
| 165 | } |
| 166 | body{ |
| 167 | specs* s = new specs; |
| 168 | // no need for '->' |
| 169 | s.count = args.length; // The 'length' property is number of elements. |
| 170 | s.allocated = typeof(args).sizeof; // built-in properties for native types |
| 171 | foreach(argv; args) |
| 172 | s.allocated += argv.length * typeof(argv[0]).sizeof; |
| 173 | return *s; |
| 174 | } |
| 175 | |
| 176 | // built-in string and common string operations, e.g. '~' is concatenate. |
| 177 | char[] argcmsg = "argc = %d"; |
| 178 | char[] allocmsg = "allocated = %d"; |
| 179 | writefln(argcmsg ~ ", " ~ allocmsg, |
| 180 | argspecs(args).count,argspecs(args).allocated); |
| 181 | } |
| 182 | /** |
| 183 | Stores a single command line argument. |
| 184 | */ |
| 185 | class CmdLin |
| 186 | { |
| 187 | private { |
| 188 | int _argc; |
| 189 | char[] _argv; |
| 190 | static uint _totalc; |
| 191 | } |
| 192 | |
| 193 | public: |
| 194 | /************ |
| 195 | Object constructor. |
| 196 | params: |
| 197 | argc = ordinal count of this argument. |
| 198 | argv = text of the parameter |
| 199 | *********/ |
| 200 | this(int argc, char[] argv) |
| 201 | { |
| 202 | _argc = argc + 1; |
| 203 | _argv = argv; |
| 204 | _totalc++; |
| 205 | } |
| 206 | |
| 207 | ~this() /// Object destructor |
| 208 | { |
| 209 | // Doesn't actually do anything for this example. |
| 210 | } |
| 211 | |
| 212 | int argnum() /// A property that returns arg number |
| 213 | { |
| 214 | return _argc; |
| 215 | } |
| 216 | char[] argv() /// A property that returns arg text |
| 217 | { |
| 218 | return _argv; |
| 219 | } |
| 220 | wchar[] suffix() /// A property that returns ordinal suffix |
| 221 | { |
| 222 | wchar[] suffix; // Built in Unicode strings (utf8,utf16, utf32) |
| 223 | switch(_argc) |
| 224 | { |
| 225 | case 1: |
| 226 | suffix = "st"; |
| 227 | break; |
| 228 | case 2: |
| 229 | suffix = "nd"; |
| 230 | break; |
| 231 | case 3: |
| 232 | suffix = "rd"; |
| 233 | break; |
| 234 | default: // 'default' is mandatory with "-w" compile switch. |
| 235 | suffix = "th"; |
| 236 | } |
| 237 | return suffix; |
| 238 | } |
| 239 | |
| 240 | /* ************** |
| 241 | * A property of the whole class, not just an instance. |
| 242 | * returns: The total number of commandline args added. |
| 243 | *************/ |
| 244 | static typeof(_totalc) total() |
| 245 | { |
| 246 | return _totalc; |
| 247 | } |
| 248 | // Class invariant, things that must be true after any method is run. |
| 249 | invariant |
| 250 | { |
| 251 | assert(_argc > 0); |
| 252 | assert(_totalc >= _argc); |
| 253 | } |
| 254 | }</code></pre> |