diff --git a/src/SUMMARY.md b/src/SUMMARY.md index adaba6f..92b3f4a 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -1,5 +1,13 @@ # Summary * [Hello World](hello_world.md) -* [Variables](variables.md) +* [Numbers](numbers.md) +* [Variables](variables/page.md) + * [Result](variables/result.md) +* [Types](types.md) +* [Control Flow](control_flow/page.md) + * [Case Statements](control_flow/case.md) + * [For Loops & Iterators](control_flow/for_iterators.md) + * [Exceptions](control_flow/exceptions.md) +* [Functions]() * [Varargs](varargs.md) \ No newline at end of file diff --git a/src/control_flow/case.md b/src/control_flow/case.md new file mode 100644 index 0000000..d8b272e --- /dev/null +++ b/src/control_flow/case.md @@ -0,0 +1 @@ +# Case Statements \ No newline at end of file diff --git a/src/control_flow/exceptions.md b/src/control_flow/exceptions.md new file mode 100644 index 0000000..9617c0d --- /dev/null +++ b/src/control_flow/exceptions.md @@ -0,0 +1 @@ +# Exceptions \ No newline at end of file diff --git a/src/control_flow/for_iterators.md b/src/control_flow/for_iterators.md new file mode 100644 index 0000000..0e23909 --- /dev/null +++ b/src/control_flow/for_iterators.md @@ -0,0 +1 @@ +# For Loops & Iterators \ No newline at end of file diff --git a/src/control_flow/page.md b/src/control_flow/page.md new file mode 100644 index 0000000..9879b3e --- /dev/null +++ b/src/control_flow/page.md @@ -0,0 +1,23 @@ +# Control Flow + +Nimrod many different control flow constructs, including the standard `if`s, `else`s, and `while`s. + +``` Nimrod +import strutils + +let + answer = 4 # Chosen by a fair dice roll, + # guaranteed to be random + +while true: + echo "I have a number from 1 to 10, what is it? " + let guess = parseInt(stdin.readLine) + + if guess < answer: + echo "Too low, try again" + elif guess > answer: + echo "Too high, try again" + else: + echo "Correct!" + break +``` \ No newline at end of file diff --git a/src/numbers.md b/src/numbers.md new file mode 100644 index 0000000..bd36131 --- /dev/null +++ b/src/numbers.md @@ -0,0 +1,23 @@ +# Numbers + +Nimrod has several primitive types: + +* signed integers: `int8`, `int16`, `int32`, `int64`, and `int`, where `int` is the same size as a pointer +* unsigned integers are similar with `u` prepended to the type +* floating points numbers: `float32`, `float64`, and `float`, where `float` is the processor's fastest type + +To indicate the size of an integer literal, append `u` or `i` and the size you'd like to the end. However, this is not typically necessary. + +Integers can also have `0[xX]`, `0o`, `0[Bb]` prepended to indicate a hex, octal, or binary literal, respectively. Underscores are also valid in literals, and can help with readability. + +``` nimrod +let + a: int8 = 0x7F # Works + b: uint8 = 0x1111_1111 # Works + d = 0xFF # type is int + c: uint8 = 256 # Compile time error +``` + +Precedence rules are the same as most other languages, but instead of `^`, `&`, `|`, `>>`, `<<`, the `xor`, `and`, `or`, `shr`, `shl` operators are used, respectively. + +Another difference that may be surprising is that the `/` operator returns a floating point result, even when the operands are integers. This means that `3/2` will return something like `0.33`. If integer division is needed, the `div` operator should be used. \ No newline at end of file diff --git a/src/types.md b/src/types.md new file mode 100644 index 0000000..3b30843 --- /dev/null +++ b/src/types.md @@ -0,0 +1 @@ +# Types \ No newline at end of file diff --git a/src/variables/page.md b/src/variables/page.md new file mode 100644 index 0000000..d29223e --- /dev/null +++ b/src/variables/page.md @@ -0,0 +1,40 @@ +# Variables + +Nimrod supports three different types of variables, `let`, `var`, and `const`. As with most things, multiple variables can be declared in the same section. + +``` nimrod +proc getLetterA(): string = + result = "" + for letter in 'a'..'z': + result.add(letter) + +const abcs = getLetterA() +var + foo = "bar" + bar = "foo" +# oops, I need to fix the names +let temp = foo +foo = bar +bar = temp + +echo "abcs is ", abcs +echo "foo is ", foo +echo "bar is ", bar +``` + +``` +$ nimrod c -r ./assignment.nim +abcs is abcdefghijklmnopqrstuvwxyz +foo is foo +bar is bar +``` + +A `const` variable is evaluated at compile-time, so if you inspect the C sources, you'll see the following line: + +``` C +STRING_LITERAL(TMP129, "abcdefghijklmnopqrstuvwxyz", 26); +``` + +The limitation with `const` is that it cannot interface with C because there is no compile-time foreign function interface at this time. + + `var` variables are the standard mutable variables, you can modify them after they've been assigned. `let` variables are also fairly standard immutable variables. This means you can assign to them at creation time and can't change them later on. \ No newline at end of file diff --git a/src/variables/result.md b/src/variables/result.md new file mode 100644 index 0000000..8ffb402 --- /dev/null +++ b/src/variables/result.md @@ -0,0 +1,20 @@ +# Result + +The `result` variable is a special variable that serves as an implicit return. This is useful because in most cases, the control flow semantics of the `return` statement are unneeded. `result` is initialized with the default value for a given return type, so the the following code it starts as `0`, before being initialized to `number` + +``` nimrod +proc expr(number, power: int): int = + result = number + for i in 1..power: + result *= number +``` + +A common gotcha is declaring a variable called result and expecting it to have the same semantics. + +``` nimrod +proc unexpected(): int = + var result = 5 + result += 5 + +echo unexpected() # Prints 0, not 10 +``` \ No newline at end of file