More work

This commit is contained in:
flaviut 2014-05-29 15:08:46 -04:00
commit 2ea62f358e
9 changed files with 119 additions and 1 deletions

View file

@ -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)

1
src/control_flow/case.md Normal file
View file

@ -0,0 +1 @@
# Case Statements

View file

@ -0,0 +1 @@
# Exceptions

View file

@ -0,0 +1 @@
# For Loops & Iterators

23
src/control_flow/page.md Normal file
View file

@ -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
```

23
src/numbers.md Normal file
View file

@ -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.

1
src/types.md Normal file
View file

@ -0,0 +1 @@
# Types

40
src/variables/page.md Normal file
View file

@ -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.

20
src/variables/result.md Normal file
View file

@ -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
```