=====================
Nim Tutorial (Part I)
=====================
:Author: Andreas Rumpf
:Version: |nimversion|
.. contents::
Introduction
============
.. raw:: html
"Der Mensch ist doch ein Augentier -- schöne Dinge wünsch ich mir."
This document is a tutorial for the programming language *Nim*.
This tutorial assumes that you are familiar with basic programming concepts
like variables, types or statements but is kept very basic. The `manual
`_ contains many more examples of the advanced language features.
All code examples in this tutorial, as well as the ones found in the rest of
Nim's documentation, follow the `Nim style guide `_.
The first program
=================
We start the tour with a modified "hello world" program:
.. code-block:: Nim
:test: "nim c $1"
# This is a comment
echo "What's your name? "
var name: string = readLine(stdin)
echo "Hi, ", name, "!"
Save this code to the file "greetings.nim". Now compile and run it::
nim compile --run greetings.nim
With the ``--run`` `switch `_ Nim
executes the file automatically after compilation. You can give your program
command line arguments by appending them after the filename::
nim compile --run greetings.nim arg1 arg2
Commonly used commands and switches have abbreviations, so you can also use::
nim c -r greetings.nim
To compile a release version use::
nim c -d:release greetings.nim
By default the Nim compiler generates a large amount of runtime checks
aiming for your debugging pleasure. With ``-d:release`` these checks are
`turned off and optimizations are turned on
`_.
Though it should be pretty obvious what the program does, I will explain the
syntax: statements which are not indented are executed when the program
starts. Indentation is Nim's way of grouping statements. Indentation is
done with spaces only, tabulators are not allowed.
String literals are enclosed in double quotes. The ``var`` statement declares
a new variable named ``name`` of type ``string`` with the value that is
returned by the `readLine `_ procedure. Since the
compiler knows that `readLine `_ returns a string,
you can leave out the type in the declaration (this is called `local type
inference`:idx:). So this will work too:
.. code-block:: Nim
:test: "nim c $1"
var name = readLine(stdin)
Note that this is basically the only form of type inference that exists in
Nim: it is a good compromise between brevity and readability.
The "hello world" program contains several identifiers that are already known
to the compiler: ``echo``, `readLine `_, etc.
These built-ins are declared in the system_ module which is implicitly
imported by any other module.
Lexical elements
================
Let us look at Nim's lexical elements in more detail: like other
programming languages Nim consists of (string) literals, identifiers,
keywords, comments, operators, and other punctuation marks.
String and character literals
-----------------------------
String literals are enclosed in double quotes; character literals in single
quotes. Special characters are escaped with ``\``: ``\n`` means newline, ``\t``
means tabulator, etc. There are also *raw* string literals:
.. code-block:: Nim
r"C:\program files\nim"
In raw literals the backslash is not an escape character.
The third and last way to write string literals are *long string literals*.
They are written with three quotes: ``""" ... """``; they can span over
multiple lines and the ``\`` is not an escape character either. They are very
useful for embedding HTML code templates for example.
Comments
--------
Comments start anywhere outside a string or character literal with the
hash character ``#``. Documentation comments start with ``##``:
.. code-block:: nim
:test: "nim c $1"
# A comment.
var myVariable: int ## a documentation comment
Documentation comments are tokens; they are only allowed at certain places in
the input file as they belong to the syntax tree! This feature enables simpler
documentation generators.
Multiline comments are started with ``#[`` and terminated with ``]#``. Multiline
comments can also be nested.
.. code-block:: nim
:test: "nim c $1"
#[
You can have any Nim code text commented
out inside this with no indentation restrictions.
yes("May I ask a pointless question?")
#[
Note: these can be nested!!
]#
import os
proc findNodeJs*(): string =
result = findExe("nodejs")
if result == "":
result = findExe("node")
if result == "":
result = findExe("iojs")