Updated Nimrod for C programmers (markdown)

This commit is contained in:
Audun Wilhelmsen 2014-04-28 13:44:17 -07:00
parent 8d34828f25
commit 719c53fff4
1 changed files with 29 additions and 28 deletions

View File

@ -95,6 +95,7 @@ See [c2nim](http://nimrod-lang.org/c2nim.html)
### Cheat Sheet
Note: Code examples are not exactly one-to-one, there may be subtle differences in the semantics. See comments.
<table>
<tr>
<th>C</th><th>Nimrod</th><th>Comment</th>
@ -103,18 +104,18 @@ Note: Code examples are not exactly one-to-one, there may be subtle differences
<tr>
<td>
```C
<pre>
int x;
int y = 2;
```
</pre>
</td>
<td>
```Nimrod
<pre>
var x : int
var y1 : int = 2
var y2 = 2
let z = 2
```
</pre>
</td>
<td><b>Define variable</b>. y2 uses type inference. z is single-assignment. In nimrod, uninitialized variables is initialized to 0/nil or similar defaults.</i>
</td>
@ -122,22 +123,22 @@ let z = 2
<tr>
<td>
```C
<pre>
char* s = "Hello World.";
char s0 = s[0]; // 'H'
char *t = s; // Pointer to s
s[11] = '!';
// s and t are both "Hello World."
```
</pre>
</td>
<td>
```Nimrod
<pre>
var s: string = "Hello World."
var s0: char = s[0] # 'H'
var t = s # Copy of s
s[11] = '!'
# s is "Hello World!", t is "Hello World."
```
</pre>
</td>
<td><b>Strings and char</b>. Strings are pass-by-value (copied on assignment) and strictly bounds-checked on access.</i>
</td>
@ -145,16 +146,16 @@ s[11] = '!'
<tr>
<td>
```C
<pre>
char a = '\n';
printf("byte %d\nA%cB\n", a, a);
```
</pre>
</td>
<td>
```Nimrod
<pre>
let a = '\L'
echo "byte ", $int(a), "\nA" & $a & "B"
```
</pre>
</td>
<td><b>Newlines and chars</b>. In nimrod you can't use ``\n`` as a character literal, because on the Windows platform it expands to CR+LR. So you need to specify which char to use.</i>
</td>
@ -162,18 +163,18 @@ echo "byte ", $int(a), "\nA" & $a & "B"
<tr>
<td>
```C
<pre>
9 % 8 // 1
-9 % 8 // -1
(unsigned)(-9) % (unsigned)(8) // 7
```
</pre>
</td>
<td>
```Nimrod
<pre>
9 mod 8 # 1
-9 mod 8 # -1
-9 %% 8 # 7
```
</pre>
</td>
<td><b>Modulo operator</b>. <i>%% treats its argument as unsigned numbers. <a href="http://nimrod-lang.org/manual.html#pre-defined-integer-types">See</a></i></i>
</td>
@ -182,14 +183,14 @@ echo "byte ", $int(a), "\nA" & $a & "B"
<tr>
<td>
```C
<pre>
int x = foobar() ? 42 : 0;
```
</pre>
</td>
<td>
```Nimrod
<pre>
var x = if foobar(): 42 else: 0
```
</pre>
</td>
<td>
<i>If-statements return the value of the expression they evaluate to, so Nimrod doesn't need a </i><b>ternary operator</b>.</i>
@ -199,7 +200,7 @@ var x = if foobar(): 42 else: 0
<tr>
<td>
```C
<pre>
void foo() {
printf("Hello World\n");
}
@ -209,10 +210,10 @@ int bar() {
int baz(int x) {
return x*2;
}
```
</pre>
</td>
<td>
```Nimrod
<pre>
proc foo() =
echo "Hello World"
@ -221,30 +222,30 @@ proc bar() : int =
proc baz(x : int) =
x*2
```
</pre>
</td>
<td><b>Function/Procedure.</b> </td>
</tr>
<tr>
<td>
```C
<pre>
void foobar(person_t *a) {
person_t b;
b = *a;
b.name = "Bob";
*a = b;
}
```
</pre>
</td>
<td>
```Nimrod
<pre>
proc foobar(a: ref TPerson) =
var b: TPerson
b = a[]
b.name = "Bob"
a[] = b
```
</pre>
</td>
<td><b>Dereference.</b> <i>In C, only the pointer to the strings in the struct is copied. In Nimrod, the string is also copied, but refs are not deep-copied.</i> </td>
</tr>