Nimrod
Implementation of programming language NimrodThe original compiler for the language, created in 2004. Initially it was written in Free Pascal and Python, but in 2008 a new version of the compiler was able to bootstrap, so now the compiler is written in Nimrod itself. Currently (up to version 1.0.0) the compiler is unstable and new versions might introduce incompatibility with older code.
The compiler is crossplatform and enables easy porting to new platforms. It compiles code to C, C++ or Objective C. Bindings to Python, Lua, TCL, MySQL and SQLite (among others) are included in the standard distribution. Nimrod is distributed under GNU GPL.
Links:
Examples:
Hello, World!:
Example for versions Nimrod 0.8.8echo "Hello, World!"
Factorial:
Example for versions Nimrod 0.8.8This example uses recursive factorial definition.
proc factorial(n: int): int64 =
if n == 0:
result = 1
else:
result = n * factorial(n - 1)
for i in countup(0,16):
echo i, "! = ", factorial(i)
Factorial:
Example for versions Nimrod 0.8.8This example uses iterative factorial calculation. Note that you can’t just omit int64
in variable definition, since its type will be deduced as integer, and it will overflow on 13!.
var f: int64 = 1
for i in countup(0,16):
echo i, "! = ", f
f = f * (i + 1)
Fibonacci numbers:
Example for versions Nimrod 0.8.8This example uses iterative calculation of Fibonacci numbers. A single var
keyword can declare several variables if they are indented to form a block. Note that variable type can be omitted from the declaration only if it is initialized immediately; Nimrod uses local type inference, not global one.
for i in 1..16
is an alternate form of writing countup
loop. &
is string concatenation operator, and $
is number-to-string conversion.
var
f1 = 1
f2 = 1
f3: int
res = ""
for i in 1..16:
res = res & $f1 & ", "
f3 = f1 + f2
f1 = f2
f2 = f3
echo res & "..."
Fibonacci numbers:
Example for versions Nimrod 0.8.8This example uses Binet’s formula to calculate the numbers. Addition of epsilon value before rounding is necessary to get exact integer values.
from math import sqrt, pow, round
proc fibonacci(n: int): int =
var phi: float64 = (1.0 + sqrt(5.0)) / 2.0
return round((pow(phi, float64(n)) - pow(-phi, -float64(n))) / sqrt(5.0) + 0.0001)
var res = ""
for i in 1..16:
res = res & $fibonacci(i) & ", "
echo res & "..."
Quadratic equation:
Example for versions Nimrod 0.8.8from math import sqrt
from strutils import parseFloat
var A = parseFloat(readLine(stdin))
if A == 0.0:
echo "Not a quadratic equation."
else:
var B = parseFloat(readLine(stdin))
var C = parseFloat(readLine(stdin))
var D = B * B - 4.0 * A * C
if D == 0.0:
echo "x = ", -0.5 * B / A
elif D > 0.0:
echo "x1 = ", -0.5 * (B - sqrt(D)) / A
echo "x2 = ", -0.5 * (B + sqrt(D)) / A
else:
echo "x1 = (", -0.5 * B / A, ", ", 0.5 * sqrt(-D) / A, ")"
echo "x2 = (", -0.5 * B / A, ", ", -0.5 * sqrt(-D) / A, ")"
CamelCase:
Example for versions Nimrod 0.8.8This example uses regular expressions. In Nimrod they are implemented using PRCE (Perl-Compatible Regular Expressions) library written in C.
from strutils import toLower, capitalize, join
import re
var text = toLower(readLine(stdin))
var words = split(text, re"[^a-z]+")
echo join(words.each(capitalize))
Comments
]]>blog comments powered by Disqus
]]>