Nim Programming Language was formerly known as Nimrod. It was developed as well as designed by Andreas Rumpf. It is a multi-paradigm, imperative, and compiled programming language. Nim Programming language has the following features.
- Efficient and Expressive – Nim programming language is designed to be very efficient, expressive, and elegant. It offers serval features such as compile time code generation, algebraic data types, functional, message passing, procedural, etc.
- Functional Programming – It is a well designed programming language that supports Iterators which can be used as first class entities in the language for functional programming. It promotes a coding style to enable developers to write short, fast, and maintainable code.
- Metaprogramming Support – Nim programming language supports compile-time metaprogramming features such as term rewriting macros and syntactic macros. Term rewriting macros allow library applications of common data structures such as bignums and matrices to be applied with an effectiveness as if they were built-in programming language facilities.
- object-oriented programming styles – It supports Object oriented programming style through inheritance and multiple dispatch. It also supports both function as well as operator overloading i.e. polymorphism.
- simple syntax – It is typed statically with very simple syntax.
- Garbage Collection – Nim includes automatic garbage collection which is based on the deferred reference counting with cycle detection.
Nim programming language requires MingW C compiler for compilation which is present for both Windows and Unix operating systems. Given below is the link through which Nim can be downloaded depending on the operating system type.
Installing Elixir on Windows
As discussed earlier that Nim is available for both Windows and UNIX Operating Systems. In this article, we are going to use below link in order to download zip file [nim-0.18.0_x64.zip] for Nim setup installation on Windows operating system based on 32-bit or 64-bit operating system.
Nim Download for Windows
STEP 1: Unzip the downloaded file at a location on your PC say ‘C:\work\nim-0.18.0’. The unzipped file should have the following content as shown below.
STEP 2: Configure the Nim PATH Environment Variables as binaries from the downloaded zip file exist inside the bin directory (shown above). Usually, the Nim developers include two directories in their PATH environment variable as given below.
- The above-mentioned bin directory.
- %USERPROFILE%\.nimble\bin (where %USERPROFILE% is the home directory).
It should be noted that the zip file includes a simple application known as finish.exe that can help to add the first directory into your PATH as well as MingW C compiler PATH variable configuration. It also enables to check for the presence of a C compiler i.e. compatible version of MingW.
STEP 3: Open a Command line window and navigate to ‘C:\work\nim-0.18.0’ where finish.exe file is present. Type ‘finish.exe’. It will prompt you with the message ‘nim.exe is not in your PATH environment variable. Should it be added permanently? (y/n)’. Type ‘y’, Next, it will prompt with the message ‘No compatible MingW candidates found in the standard locations [Error]. Do you want to download MingW [64 bit – mingw64-126.96.36.199z] from Nim’s website? (y/n)’. Type ‘y’, to initiate the downloading of MingW as the Nim compiler needs a C compiler in order to compile software. You may notice the progress bar as shown below.
STEP 4: After, the download of MingW has completed. It will prompt you with the message as ‘C:\work\nim-0.18.0\dist\mingw64\bin, Would like to add Nim-0.18.0 to your start menu? (y/n)’. Type ‘y’ to finish configuration of PATH environment variable on your PC as well as adding Nim to the Start Menu. All series of commands which we have discussed here are shown below in the screenshot.
STEP 5: Open the control Panel and check if the following two paths (in yellow) are added to the PATH environment variable. If these two paths are not added, then Nim compiler won’t compile and run the program.
Example First Program in Nim Programming language
Let’s create a program in Nim that accepts a message i.e. World and print “Hello World!”. We can write the program and save it with file extension as. nim [say welcome.nim]. The program content is shown below.
# This is a comment
echo “What’s your message? “
var message: string = readLine (stdin)
echo “Hello, “, message, “!”
We can compile and run the above program with the help of the following command (in bold).
C:\work\nim-0.18.0>nim compile –run welcome.nim
Hint: used config file ‘C:\work\nim-0.18.0\config\nim.cfg’ [Conf]
Hint: system [Processing]
Hint: welcome [Processing]
Hint: operation successful (11719 lines compiled; 7.590 sec total; 22.164MiB peakmem; Debug Build) [SuccessX]
Hint: C:\work\nim-0.18.0\welcome.exe [Exec]
What’s your message?
Nim – Basic Syntax and rules
1. Lexical elements: Like any other programming languages Nim programming language also consists of string i.e. literals, identifiers, comments, keywords, operators, and other punctuation marks.
- String and character literals: In Nim programming language, String literals are surrounded with in double quotes; character literals are enclosed with in single quotes. Special characters are escaped with \: \n i.e. newline, \t means tabulator, etc. There are also raw string literals where the backslash is not an escape character [e.g. r”C:\work\nim”]. Another way to specify string literals are long string literals. They are specified with three quotes: “”” … “””; where they can span over multiple lines and the \ is no longer an escape character either.
- Comments: When the developer wants to enter texts in the program and don’t want that part of code to compile and execute such lines of code are known as comments. Single line comments in Nim programming language start with a ‘#’ symbol. For example,
Single Line Comment
#This is how we provide comment in Nim
Nim programming language supports Multiline comments which are started with #[ and ended with ]#. Multiline comments can also be nested in Nim Programming language. Examples of multiline comments are shown below.
Multi Line Comment
This is multiline comment.
Yes (“May I ask a pointless question?”)
This is nested multiline comment!
- Numbers: In Nim programming language, the numerical literals are written in a special way, underscores are allowed for better readability: 1_000_000 (one million). A number which contains a dot (or ‘e’ or ‘E’) is regarded as floating point literal: 1.0e9 (one billion). Hexadecimal literals are preceded with 0x, binary literals are preceded with 0b and octal literals are preceded with 0o. A leading zero alone does not produce an octal in Nim.
- Constants: In Nim programming language, constants are symbols that are bound to a value. The value of constants cannot be changed. The compiler must be able to evaluate the expression in a constant declared at the compile time. An example is shown below.
const x = “xyz” # the constant x contains the string “xyz”
- Operators: Nim programming language has built-in keyword operators such as and, or, not. Here, operators always consist of these characters: + – * \ / < > = @ $ ~ & % ! ? ^ . |
- Reserved Words or Keywords: The following are the reserved words in Nim programming language which cannot be used as variables, module or function names.
In this article, we discussed about Nim programming language and its dependency on MingW C compiler for the software compilation. We have demonstrated various steps to install Nim and MingW on windows machine and further discussed on Nim basic syntax and rules along with suitable examples.