Add two chapters, finally get things working.

This commit is contained in:
flaviut 2014-05-29 12:23:33 -04:00
commit 395f3d0849
8 changed files with 141 additions and 6 deletions

3
.gitignore vendored
View file

@ -1 +1,2 @@
build/
src/_book/
!*.md

View file

View file

@ -1,4 +1,5 @@
Nimrod is a powerful staticly typed language that allows the programmer expressivness without comprimizing run-time performance.
Nimrod is a powerful statically typed language that allows the programmer expressiveness without compromising run-time performance.
``` nimrod
import tables, strutils
@ -17,3 +18,37 @@ for word, frequency in wordFrequencies:
echo "The most frequent word is '", word, "'"
```
# Getting Started
When installing Nimrod, you have two options: downloading a copy directly from git, or downloading a prepackaged distribution from the website
## Downloading a Nimrod Distribution
To get started with Nimrod, head over to the [downloads page](http://nimrod-lang.org/download.html) and download the version you'd like.
The Windows distribution comes with the MinGW compiler, which is recommended, but if you'd like to use your own compiler, download the version without MinGW.
On Mac and Linux, it is recommended to use either GCC or Clang.
| | i386 | amd64 |
| -- | ---- | ----- |
| Windows | [Download](http://nimrod-lang.org/download/nimrod_0.9.4_windows_i386_full.zip) ([w/o MinGW](http://nimrod-lang.org/download/nimrod_0.9.4_windows_i386_slim.zip))| [Download](http://nimrod-lang.org/download/nimrod_0.9.4_windows_amd64_full.zip) ([w/o MinGW](http://nimrod-lang.org/download/nimrod_0.9.4_windows_amd64_slim.zip)) |
| Mac | NA | [Download](http://nimrod-lang.org/download/nimrod_0.9.4_macosx_amd64.zip) |
| Linux | [Download](http://nimrod-lang.org/download/nimrod_0.9.4_linux_i386.tar.gz) | [Download](http://nimrod-lang.org/download/nimrod_0.9.4_linux_amd64.tar.gz) |
Once you have downloaded the appropriate distribution and extracted the files somewhere convenient, feel free to place the `bin` directory in the path for easier access.
## Downloading from Git
To install Nimrod from git, ensure that you have gcc installed on Windows, Linux, and BSDs and clang on Mac. Afterwards, run the following series of commands to download and bootstrap Nimrod:
```
git clone -b master git://github.com/Araq/Nimrod.git
cd Nimrod
git clone --depth 1 git://github.com/nimrod-code/csources
cd csources
sh build.sh # Replace with build.bat on windows
cd ..
bin/nimrod c koch
./koch boot -d:release
```
Once compiled, you may want to add the `bin` directory to your path for easy access.

View file

@ -1,5 +1,5 @@
# Summary
Nimrod is a statically typed, imperative programming language that gives the
programmer powerful compile-time tools, allowing for expressiveness without
compromising run-time efficiency.
* [Hello World](hello_world.md)
* [Variables](variables.md)
* [Varargs](varargs.md)

View file

@ -1,5 +1,4 @@
{
"output": "../build/",
"title": "Nimrod by Example",
"description": "A book of short examples of Nimrod features",
"github": null,

20
src/hello_world.md Normal file
View file

@ -0,0 +1,20 @@
# Hello World
The code for a simple hello world program is as follows:
``` nimrod
echo "Hello World"
```
To compile and execute the program, the following command should be run
```
$ nimrod c -r ./helloworld.nim
Hello World
```
The command has several elements:
* `c` is an alias for `compile`, which compiles the Nimrod sources into C and then invokes the C compiler on them
* `-r` is an alias for `--run`, which runs the program
* `./helloworld.nim` is the path to the source you want to compile

42
src/varargs.md Normal file
View file

@ -0,0 +1,42 @@
# Varargs
Standard varargs simply allows you to pass multiple parameters to your function.
``` nimrod
proc printThings(things: varargs[string]) =
for thing in things:
echo thing
printThings "words", "to", "print"
```
```
$ nimrod c -r ./varargs1.nim
words
to
print
```
However, trying to run
``` nimrod
printThings 1, "string", @[1, 2, 3]
```
will fail to compile because the compiler won't coerce anything into strings. Luckily enough, there is a tool to fix this, the coercing varargs:
``` nimrod
proc printThings(things: varargs[string, `$`]) =
for thing in things:
echo thing
printThings "thing 1", 2, @[4, 5, 6]
```
```
$ nimrod c -r ./varargs2.nim
thing 1
2
@[4, 5, 6]
```
It works by finding a function `` `$` `` that returns string for each argument type, and applying it to each argument.

38
src/variables.md Normal file
View file

@ -0,0 +1,38 @@
# Assignment
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);
```
`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.