2013-12-19 09:28:28 +00:00
(Work in progress)
This is a guide for people with experience in C or a similar language. The general tutorials can be found here:
http://nimrod-lang.org/tut1.html
http://nimrod-lang.org/tut2.html
The manual provides a more or less complete overview of the language:
http://nimrod-lang.org/manual.html
2013-12-20 14:00:20 +00:00
### At a glance
< table >
< tr > < th > Feature< / th > < th > C< / th > < th > Nimrod< / th > < / tr >
< tr > < td > Memory Management< / td > < td > Manual (not counting libraries)< / td > < td > Garbage-collected and manual< / td > < / tr >
2013-12-20 14:03:48 +00:00
< tr > < td > Types< / td > < td > Static< / td > < td > Static< / td > < / tr >
2013-12-20 14:05:07 +00:00
< tr > < td > Compilation< / td > < td > Machine code< / td > < td > Machine code via C < br > (other backends in progress/planned)< / td > < / tr >
< tr > < td > Meta-programming< / td > < td > C Preprocessor< / td > < td > Nimrod (const/when/template/macro)< / td > < / tr >
2013-12-20 14:00:20 +00:00
< tr > < td > < / td > < td > < / td > < td > < / td > < / tr >
< / table >
2013-12-19 09:28:28 +00:00
### Structs - Tuples and Objects
Tuples and Objects in Nimrod are kind of like structs in C, but not really.
### Interfacing C and Nimrod
See [Foreign Function Interface ](http://nimrod-lang.org/manual.html#foreign-function-interface )
2013-12-19 09:32:27 +00:00
### Converting C code to Nimrod
See [c2nim ](http://nimrod-lang.org/c2nim.html )
2013-12-19 09:28:28 +00:00
### 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 >
< / tr >
2013-12-19 10:35:58 +00:00
2013-12-20 08:32:23 +00:00
< tr >
< td >
```C
int x;
int y = 2;
```
< / td >
< td >
```Nimrod
var x : int
var y1 : int = 2
var y2 = 2
let z = 2
```
< / td >
2013-12-20 13:57:57 +00:00
< 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 >
2013-12-20 08:32:23 +00:00
< / td >
< / tr >
2013-12-19 10:35:58 +00:00
< tr >
< td >
```C
2013-12-19 10:37:05 +00:00
9 % 8 // 1
-9 % 8 // -1
(unsigned)(-9) % (unsigned)(8) // 7
2013-12-19 10:35:58 +00:00
```
< / td >
< td >
```Nimrod
2013-12-19 10:37:05 +00:00
9 mod 8 # 1
-9 mod 8 # -1
-9 %% 8 # 7
2013-12-19 10:35:58 +00:00
```
< / td >
2013-12-19 11:13:10 +00:00
< 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 >
2013-12-19 10:35:58 +00:00
< / td >
< / tr >
2013-12-19 09:28:28 +00:00
< tr >
< td >
```C
int x = foobar() ? 42 : 0;
```
< / td >
< td >
```Nimrod
var x = if foobar(): 42 else: 0
```
< / 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 >
< / td >
< / tr >
2013-12-20 10:16:18 +00:00
< tr >
< td >
```C
void foo() {
printf("Hello World\n");
}
int bar() {
return 2;
}
int baz(int x) {
return x*2;
}
```
< / td >
< td >
```Nimrod
proc foo() =
echo "Hello World"
proc bar() : int =
2
proc baz(x : int) =
x*2
```
< / td >
< td > < b > Function/Procedure.< / b > < / td >
< / tr >
2013-12-19 09:28:28 +00:00
< tr >
< td >
```C
void foobar(person_t *a) {
person_t b;
b = *a;
b.name = "Bob";
*a = b;
}
```
< / td >
< td >
```Nimrod
proc foobar(a: ref TPerson) =
var b: TPerson
b = a[]
b.name = "Bob"
a[] = b
```
< / 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 >
2013-12-20 10:16:18 +00:00
2013-12-19 09:28:28 +00:00
< / table >