This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc parseBool(s: string): bool = | |
case normalize(s) | |
of "y", "yes", "true", "1", "on": result = true | |
of "n", "no", "false", "0", "off": result = false | |
else: raise newException(EInvalidValue, "cannot interpret as a bool: " & s) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# | |
# Nimrod's Runtime Library | |
# (c) Copyright 2011 Andreas Rumpf | |
# | |
# See the file "copying.txt", included in this | |
# distribution, for details about the copyright. | |
# | |
## This module implements big integers for Nimrod. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# module A | |
when not defined(dolog): | |
template dolog(msg: string) = nil | |
proc whatever() = | |
dolog "start of whatever" | |
doSomeWork | |
dolog "end of whatever" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc allocCStringArray*(a: openArray[string]): cstringArray = | |
## creates a NULL terminated cstringArray from `a`. The result has to | |
## be freed with `deallocCStringArray` after it's not needed anymore. | |
result = cast[cstringArray](alloc0((a.len+1) * sizeof(cstring))) | |
for i in 0 .. a.high: | |
# XXX get rid of this string copy here: | |
var x = a[i] | |
result[i] = cast[cstring](alloc0(x.len+1)) | |
copyMem(result[i], addr(x[0]), x.len) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
type | |
TUtf16Char = distinct int16 | |
WideCString = ptr array[0.. 1_000_000, TUtf16Char] | |
const | |
utf8Encoding = 65001 | |
proc len(w: WideCString): int = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
discard """ | |
disabled: false | |
""" | |
import math, algorithm | |
proc sorted[T](a: openArray[T], order: TSortOrder): bool = | |
result = true | |
for i in 0 .. < a.high: | |
if cmp(a[i], a[i+1]) * order > 0: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc p = | |
var delta = 7 | |
proc accumulator(start: int): proc(): int = | |
var x = start-1 | |
result = proc (): int = | |
x = x + delta | |
inc delta | |
return x | |
var a = accumulator(3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var funcs: seq[proc (): int {.closure.}] = @[] | |
proc main() = | |
var i = 0 | |
while i <= 10: | |
funcs.add((proc (): int = | |
var x = i | |
return x * x)) | |
inc i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proc outer = | |
var x = 0 | |
proc innerB() = | |
capture x | |
while true: | |
var y = 0 | |
proc innerA = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Nimrod is a statically typed, imperative programming language that tries to | |
give the programmer ultimate power without compromises on runtime efficiency. | |
This means it focuses on compile-time mechanisms in all its | |
various forms. Beneath a nice infix/indentation based syntax with a | |
powerful (AST based, hygienic) macro system lies a semantic model that supports | |
a soft realtime GC on thread local heaps. Asynchronous message passing is used | |
between threads, so no "stop the world" mechanism is necessary. An unsafe | |
shared memory heap is also provided for the increased efficiency that results | |
from that model. |
OlderNewer