From 719c53fff4da8f788f50a54b3e7a7f68e01ad156 Mon Sep 17 00:00:00 2001 From: Audun Wilhelmsen Date: Mon, 28 Apr 2014 13:44:17 -0700 Subject: [PATCH] Updated Nimrod for C programmers (markdown) --- Nimrod-for-C-programmers.md | 57 +++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/Nimrod-for-C-programmers.md b/Nimrod-for-C-programmers.md index 5c7386d..63110e7 100644 --- a/Nimrod-for-C-programmers.md +++ b/Nimrod-for-C-programmers.md @@ -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. + @@ -103,18 +104,18 @@ Note: Code examples are not exactly one-to-one, there may be subtle differences @@ -122,22 +123,22 @@ let z = 2 @@ -145,16 +146,16 @@ s[11] = '!' @@ -162,18 +163,18 @@ echo "byte ", $int(a), "\nA" & $a & "B" @@ -182,14 +183,14 @@ echo "byte ", $int(a), "\nA" & $a & "B"
CNimrodComment
-```C +
 int x;
 int y = 2;
-```
+
-```Nimrod +
 var x : int
 var y1 : int = 2
 var y2 = 2
 let z = 2
-```
+
Define variable. y2 uses type inference. z is single-assignment. In nimrod, uninitialized variables is initialized to 0/nil or similar defaults.
-```C +
 char* s = "Hello World.";
 char s0 = s[0]; // 'H'
 char *t = s; // Pointer to s
 s[11] = '!';
 // s and t are both "Hello World."
-```
+
-```Nimrod +
 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."
-```
+
Strings and char. Strings are pass-by-value (copied on assignment) and strictly bounds-checked on access.
-```C +
 char a = '\n';
 printf("byte %d\nA%cB\n", a, a);
-```
+
-```Nimrod +
 let a = '\L'
 echo "byte ", $int(a), "\nA" & $a & "B"
-```
+
Newlines and chars. 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.
-```C +
 9 % 8  // 1
 -9 % 8 // -1
 (unsigned)(-9) % (unsigned)(8) // 7
-```
+
-```Nimrod +
 9 mod 8  # 1
 -9 mod 8 # -1
 -9 %% 8  # 7
-```
+
Modulo operator. %% treats its argument as unsigned numbers. See
-```C +
 int x = foobar() ? 42 : 0;
-```
+
-```Nimrod +
 var x = if foobar(): 42 else: 0
-```
+
If-statements return the value of the expression they evaluate to, so Nimrod doesn't need a ternary operator. @@ -199,7 +200,7 @@ var x = if foobar(): 42 else: 0
-```C +
 void foo() {
   printf("Hello World\n");
 }
@@ -209,10 +210,10 @@ int bar() {
 int baz(int x) {
   return x*2;
 }
-```
+
-```Nimrod +
 proc foo() =
   echo "Hello World"
 
@@ -221,30 +222,30 @@ proc bar() : int =
 
 proc baz(x : int) =
   x*2
-```
+
Function/Procedure.
-```C +
 void foobar(person_t *a) {
   person_t b;
   b = *a;
   b.name = "Bob";
   *a = b;
 }
-```
+
-```Nimrod +
 proc foobar(a: ref TPerson) =
   var b: TPerson
   b = a[]
   b.name = "Bob"
   a[] = b
-```
+
Dereference. 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.