version 0.8.2

This commit is contained in:
rumpf_a@web.de 2009-10-21 10:20:15 +02:00
commit 053309e60a
125 changed files with 6564 additions and 1308 deletions

View file

@ -1,3 +1,4 @@
#! stdtmpl
#proc sunsetTemplate*(current, ticker, content: string,
# tabs: openarray[array[0..1, string]]): string =
# result = ""

16
tests/tadrdisc.nim Executable file
View file

@ -0,0 +1,16 @@
# Test that the address of a dicriminants cannot be taken
type
TKind = enum ka, kb, kc
TA = object
case k: TKind
of ka: x, y: int
of kb: a, b: string
of kc: c, d: float
proc setKind(k: var TKind) =
k = kc
var a: TA
setKind(a.k) #ERROR_MSG for a 'var' type a variable needs to be passed

14
tests/tbind1.nim Executable file
View file

@ -0,0 +1,14 @@
# Test the new ``bind`` keyword for templates
proc p1(x: int8, y: int): int = return x + y
template tempBind(x, y: expr): expr =
bind p1(x, y)
proc p1(x: int, y: int8): int = return x - y
# This is tricky: the call to ``p1(1'i8, 2'i8)`` should not fail in line 6,
# because it is not ambiguous there. But it is ambiguous after line 8.
echo tempBind(1'i8, 2'i8) #OUT 3

10
tests/tbind2.nim Executable file
View file

@ -0,0 +1,10 @@
# Test the new ``bind`` keyword for templates
proc p1(x: int8, y: int): int = return x + y
proc p1(x: int, y: int8): int = return x - y
template tempBind(x, y: expr): expr =
bind p1(x, y) #ERROR_MSG ambiguous call
echo tempBind(1'i8, 2'i8)

25
tests/tbintre2.nim Executable file
View file

@ -0,0 +1,25 @@
# Same test, but check module boundaries
import tbintree
var
root: PBinaryTree[string]
x = newNode("hallo")
add(root, x)
add(root, "world")
if find(root, "world"):
for str in items(root):
stdout.write(str)
else:
stdout.writeln("BUG")
var
r2: PBinaryTree[int]
add(r2, newNode(110))
add(r2, 223)
add(r2, 99)
for y in items(r2):
stdout.write(y)
#OUT halloworld99110223

View file

@ -4,29 +4,29 @@ type
tenum = enum eA, eB, eC
var
x: string
x: string = "yyy"
y: Tenum = eA
i: int
case y
of eA: write(stdout, "a\n")
of eB, eC: write(stdout, "b oder c\n")
of eA: write(stdout, "a")
of eB, eC: write(stdout, "b or c")
x = readLine(stdin)
case x
of "Andreas", "Rumpf": write(stdout, "Hallo Meister!\n")
of "aa", "bb": write(stdout, "Du bist nicht mein Meister\n")
of "Andreas", "Rumpf": write(stdout, "Hallo Meister!")
of "aa", "bb": write(stdout, "Du bist nicht mein Meister")
of "cc", "hash", "when": nil
of "will", "it", "finally", "be", "generated": nil
else: write(stdout, "das sollte nicht passieren!\N")
case i
of 0..5, 8, 9: nil
of 1..5, 8, 9: nil
of 6, 7: nil
elif x == "Ha":
nil
elif x == "Ho":
nil
elif x == "yyy":
write(stdout, x)
else:
nil
#OUT ayyy

11
tests/tcnstseq.nim Executable file
View file

@ -0,0 +1,11 @@
# Test the new implicit conversion from sequences to arrays in a constant
# context.
import strutils
const
myWords = "Angelika Anne Anna Anka Anja".split()
for x in items(myWords):
write(stdout, x) #OUT AngelikaAnneAnnaAnkaAnja

View file

@ -9,7 +9,7 @@ proc main() =
var
a, b: string
p: int
p = findSubStr("=", example)
p = find(example, "=")
a = copy(example, 0, p-1)
b = copy(example, p+1)
writeln(stdout, a & '=' & b)

View file

@ -157,7 +157,7 @@ proc main(options: string) =
var comp = callCompiler(filename, options)
if sameResults(filename, spec, comp): inc(passed)
inc(total)
# ensure that the examples at least compiles
# ensure that the examples at least compile
for filename in os.walkFiles("examples/*.nim"):
var comp = callCompiler(filename, options)
var shortfile = os.extractFilename(filename)

3
tests/titer4.nim Executable file
View file

@ -0,0 +1,3 @@
for x in {'a'..'z'}: #ERROR_MSG iterator within for loop context expected
nil

23
tests/tmultim1.nim Executable file
View file

@ -0,0 +1,23 @@
# Test multi methods
type
TExpr = object
TLiteral = object of TExpr
x: int
TPlusExpr = object of TExpr
a, b: ref TExpr
method eval(e: ref TExpr): int = quit "to override!"
method eval(e: ref TLiteral): int = return e.x
method eval(e: ref TPlusExpr): int = return eval(e.a) + eval(e.b)
proc newLit(x: int): ref TLiteral =
new(result)
result.x = x
proc newPlus(a, b: ref TExpr): ref TPlusExpr =
new(result)
result.a = a
result.b = b
echo eval(newPlus(newPlus(newLit(1), newLit(2)), newLit(4))) #OUT 7

30
tests/tmultim2.nim Executable file
View file

@ -0,0 +1,30 @@
# Test multi methods
type
TThing = object
TUnit = object of TThing
x: int
TParticle = object of TThing
a, b: int
method collide(a, b: TThing) {.inline.} =
quit "to override!"
method collide(a: TThing, b: TUnit) {.inline.} =
write stdout, "collide: thing, unit "
method collide(a: TUnit, b: TThing) {.inline.} =
write stdout, "collide: unit, thing "
proc test(a, b: TThing) {.inline.} =
collide(a, b)
var
a: TThing
b, c: TUnit
collide(b, c) # ambiguous unit, thing or thing, unit? -> prefer unit, thing!
test(b, c)
collide(a, b)
#OUT collide: unit, thing collide: unit, thing collide: thing, unit

17
tests/tobjcov.nim Executable file
View file

@ -0,0 +1,17 @@
# Covariance is not type safe:
type
TA = object
a: int
TB = object of TA
b: array[0..5000_000, int]
proc ap(x: var TA) = x.a = -1
proc bp(x: var TB) = x.b[high(x.b)] = -1
# in Nimrod proc (x: TB) is compatible to proc (x: TA),
# but this is not type safe:
var f: proc (x: var TA) = bp
var a: TA
f(a) # bp expects a TB, but gets a TA

21
tests/toverl2.nim Executable file
View file

@ -0,0 +1,21 @@
# Test new overloading resolution rules
import strutils
proc toverl2(x: int): string = return $x
proc toverl2(x: bool): string = return $x
iterator toverl2(x: int): int =
var res = 0
while res < x:
yield res
inc(res)
var
pp: proc (x: bool): string = toverl2
stdout.write(pp(true))
for x in toverl2(3):
stdout.write(toverl2(x))
stdout.write("\n")
#OUT true012

25
tests/toverprc.nim Executable file
View file

@ -0,0 +1,25 @@
# Test overloading of procs when used as function pointers
import strutils
proc parseInt(x: float): int = nil
proc parseInt(x: bool): int = nil
proc parseInt(x: float32): int = nil
proc parseInt(x: int8): int = nil
proc parseInt(x: TFile): int = nil
proc parseInt(x: char): int = nil
proc parseInt(x: int16): int = nil
type
TParseInt = proc (x: string): int
var
q = TParseInt(parseInt)
p: TParseInt = parseInt
proc takeParseInt(x: proc (y: string): int): int =
result = x("123")
echo "Give a list of numbers (separated by spaces): "
var x = stdin.readline.split.each(parseInt).max echo x, " is the maximum!" echo "another number: ", takeParseInt(parseInt)

View file

@ -1,13 +1,16 @@
# Test Posix interface
import posix
when not defined(windows):
var
u: Tutsname
import posix
discard uname(u)
var
u: Tutsname
discard uname(u)
writeln(stdout, u.sysname)
writeln(stdout, u.nodename)
writeln(stdout, u.release)
writeln(stdout, u.machine)
writeln(stdout, u.sysname)
writeln(stdout, u.nodename)
writeln(stdout, u.release)
writeln(stdout, u.machine)

12
tests/treadx.nim Executable file
View file

@ -0,0 +1,12 @@
import posix
var inp = ""
var buf: array[0..10, char]
while true:
var r = read(0, addr(buf), sizeof(buf)-1)
add inp, $buf
if r != sizeof(buf)-1: break
echo inp
#dafkladskölklödsaf ölksdakölfölksfklwe4iojr389wr 89uweokf sdlkf jweklr jweflksdj fioewjfsdlfsd

5
tests/trecinca.nim Executable file
View file

@ -0,0 +1,5 @@
# Test recursive includes
include trecincb #ERROR_MSG recursive dependency: 'tests/trecincb.nim'
echo "trecina"

6
tests/trecincb.nim Executable file
View file

@ -0,0 +1,6 @@
# Test recursive includes
include trecincb #ERROR_MSG recursive dependency: 'tests/trecincb.nim'
echo "trecinb"

7
tests/treciter.nim Executable file
View file

@ -0,0 +1,7 @@
# Test that an error message occurs for a recursive iterator
iterator myrec(n: int): int =
for x in myrec(n-1): #ERROR_MSG recursive dependency: 'myrec'
yield x
for x in myrec(10): echo x

9
tests/tseqcon2.nim Executable file
View file

@ -0,0 +1,9 @@
import os
proc rec_dir(dir: string): seq[string] =
result = @[]
for kind, path in walk_dir(dir):
if kind == pcDirectory:
add(result, rec_dir(path))
else:
add(result, path)

11
tests/tsidee1.nim Executable file
View file

@ -0,0 +1,11 @@
var
global: int
proc dontcare(x: int): int = return x + global
proc SideEffectLyer(x, y: int): int {.noSideEffect.} = #ERROR_MSG 'SideEffectLyer' can have side effects
return x + y + dontcare(x)
echo SideEffectLyer(1, 3)

11
tests/tsidee2.nim Executable file
View file

@ -0,0 +1,11 @@
var
global: int
proc dontcare(x: int): int = return x
proc SideEffectLyer(x, y: int): int {.noSideEffect.} =
return x + y + dontcare(x)
echo SideEffectLyer(1, 3) #OUT 5

11
tests/tsidee3.nim Executable file
View file

@ -0,0 +1,11 @@
var
global: int
proc dontcare(x: int): int {.noSideEffect.} = return x
proc noSideEffect(x, y: int, p: proc (a: int): int {.noSideEffect.}): int {.noSideEffect.} =
return x + y + dontcare(x)
echo noSideEffect(1, 3, dontcare) #OUT 5

10
tests/tsidee4.nim Executable file
View file

@ -0,0 +1,10 @@
var
global: int
proc dontcare(x: int): int = return x
proc noSideEffect(x, y: int, p: proc (a: int): int {.noSideEffect.}): int {.noSideEffect.} =
return x + y + dontcare(x)
echo noSideEffect(1, 3, dontcare) #ERROR_MSG type mismatch

11
tests/tvartup.nim Executable file
View file

@ -0,0 +1,11 @@
# Test the new tuple unpacking
proc divmod(a, b: int): tuple[di, mo: int] =
return (a div b, a mod b)
var (x, y) = divmod(15, 6)
stdout.write(x)
stdout.write(" ")
stdout.write(y)
#OUT 2 3