Updated Nimrod for C programmers (markdown)
This commit is contained in:
parent
8d34828f25
commit
719c53fff4
|
@ -95,6 +95,7 @@ See [c2nim](http://nimrod-lang.org/c2nim.html)
|
||||||
|
|
||||||
### Cheat Sheet
|
### Cheat Sheet
|
||||||
Note: Code examples are not exactly one-to-one, there may be subtle differences in the semantics. See comments.
|
Note: Code examples are not exactly one-to-one, there may be subtle differences in the semantics. See comments.
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th>C</th><th>Nimrod</th><th>Comment</th>
|
<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>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
```C
|
<pre>
|
||||||
int x;
|
int x;
|
||||||
int y = 2;
|
int y = 2;
|
||||||
```
|
</pre>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
```Nimrod
|
<pre>
|
||||||
var x : int
|
var x : int
|
||||||
var y1 : int = 2
|
var y1 : int = 2
|
||||||
var y2 = 2
|
var y2 = 2
|
||||||
let z = 2
|
let z = 2
|
||||||
```
|
</pre>
|
||||||
</td>
|
</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><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>
|
</td>
|
||||||
|
@ -122,22 +123,22 @@ let z = 2
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
```C
|
<pre>
|
||||||
char* s = "Hello World.";
|
char* s = "Hello World.";
|
||||||
char s0 = s[0]; // 'H'
|
char s0 = s[0]; // 'H'
|
||||||
char *t = s; // Pointer to s
|
char *t = s; // Pointer to s
|
||||||
s[11] = '!';
|
s[11] = '!';
|
||||||
// s and t are both "Hello World."
|
// s and t are both "Hello World."
|
||||||
```
|
</pre>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
```Nimrod
|
<pre>
|
||||||
var s: string = "Hello World."
|
var s: string = "Hello World."
|
||||||
var s0: char = s[0] # 'H'
|
var s0: char = s[0] # 'H'
|
||||||
var t = s # Copy of s
|
var t = s # Copy of s
|
||||||
s[11] = '!'
|
s[11] = '!'
|
||||||
# s is "Hello World!", t is "Hello World."
|
# s is "Hello World!", t is "Hello World."
|
||||||
```
|
</pre>
|
||||||
</td>
|
</td>
|
||||||
<td><b>Strings and char</b>. Strings are pass-by-value (copied on assignment) and strictly bounds-checked on access.</i>
|
<td><b>Strings and char</b>. Strings are pass-by-value (copied on assignment) and strictly bounds-checked on access.</i>
|
||||||
</td>
|
</td>
|
||||||
|
@ -145,16 +146,16 @@ s[11] = '!'
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
```C
|
<pre>
|
||||||
char a = '\n';
|
char a = '\n';
|
||||||
printf("byte %d\nA%cB\n", a, a);
|
printf("byte %d\nA%cB\n", a, a);
|
||||||
```
|
</pre>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
```Nimrod
|
<pre>
|
||||||
let a = '\L'
|
let a = '\L'
|
||||||
echo "byte ", $int(a), "\nA" & $a & "B"
|
echo "byte ", $int(a), "\nA" & $a & "B"
|
||||||
```
|
</pre>
|
||||||
</td>
|
</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><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>
|
</td>
|
||||||
|
@ -162,18 +163,18 @@ echo "byte ", $int(a), "\nA" & $a & "B"
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
```C
|
<pre>
|
||||||
9 % 8 // 1
|
9 % 8 // 1
|
||||||
-9 % 8 // -1
|
-9 % 8 // -1
|
||||||
(unsigned)(-9) % (unsigned)(8) // 7
|
(unsigned)(-9) % (unsigned)(8) // 7
|
||||||
```
|
</pre>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
```Nimrod
|
<pre>
|
||||||
9 mod 8 # 1
|
9 mod 8 # 1
|
||||||
-9 mod 8 # -1
|
-9 mod 8 # -1
|
||||||
-9 %% 8 # 7
|
-9 %% 8 # 7
|
||||||
```
|
</pre>
|
||||||
</td>
|
</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><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>
|
</td>
|
||||||
|
@ -182,14 +183,14 @@ echo "byte ", $int(a), "\nA" & $a & "B"
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
```C
|
<pre>
|
||||||
int x = foobar() ? 42 : 0;
|
int x = foobar() ? 42 : 0;
|
||||||
```
|
</pre>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
```Nimrod
|
<pre>
|
||||||
var x = if foobar(): 42 else: 0
|
var x = if foobar(): 42 else: 0
|
||||||
```
|
</pre>
|
||||||
</td>
|
</td>
|
||||||
<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>
|
<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>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
```C
|
<pre>
|
||||||
void foo() {
|
void foo() {
|
||||||
printf("Hello World\n");
|
printf("Hello World\n");
|
||||||
}
|
}
|
||||||
|
@ -209,10 +210,10 @@ int bar() {
|
||||||
int baz(int x) {
|
int baz(int x) {
|
||||||
return x*2;
|
return x*2;
|
||||||
}
|
}
|
||||||
```
|
</pre>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
```Nimrod
|
<pre>
|
||||||
proc foo() =
|
proc foo() =
|
||||||
echo "Hello World"
|
echo "Hello World"
|
||||||
|
|
||||||
|
@ -221,30 +222,30 @@ proc bar() : int =
|
||||||
|
|
||||||
proc baz(x : int) =
|
proc baz(x : int) =
|
||||||
x*2
|
x*2
|
||||||
```
|
</pre>
|
||||||
</td>
|
</td>
|
||||||
<td><b>Function/Procedure.</b> </td>
|
<td><b>Function/Procedure.</b> </td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
```C
|
<pre>
|
||||||
void foobar(person_t *a) {
|
void foobar(person_t *a) {
|
||||||
person_t b;
|
person_t b;
|
||||||
b = *a;
|
b = *a;
|
||||||
b.name = "Bob";
|
b.name = "Bob";
|
||||||
*a = b;
|
*a = b;
|
||||||
}
|
}
|
||||||
```
|
</pre>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
```Nimrod
|
<pre>
|
||||||
proc foobar(a: ref TPerson) =
|
proc foobar(a: ref TPerson) =
|
||||||
var b: TPerson
|
var b: TPerson
|
||||||
b = a[]
|
b = a[]
|
||||||
b.name = "Bob"
|
b.name = "Bob"
|
||||||
a[] = b
|
a[] = b
|
||||||
```
|
</pre>
|
||||||
</td>
|
</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>
|
<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>
|
</tr>
|
||||||
|
|
Loading…
Reference in New Issue