Merge branch 'master' of github.com:Araq/Nimrod

This commit is contained in:
Araq 2013-08-22 19:28:58 +02:00
commit a8c8a85135
27 changed files with 652 additions and 326 deletions

View file

@ -1,5 +1,5 @@
discard """
output: "1.0000000000000000e+00 10"
output: "1.1000000000000001e+00 11"
ccodecheck: "!@'ClEnv'"
"""
@ -8,5 +8,6 @@ proc p[T](a, b: T): T
echo p(0.9, 0.1), " ", p(9, 1)
proc p[T](a, b: T): T =
result = a + b
let c = b
result = a + b + c

View file

@ -0,0 +1,10 @@
type
TIDGen*[A: Ordinal] = object
next: A
free: seq[A]
proc newIDGen*[A]: TIDGen[A] =
newSeq result.free, 0
var x = newIDGen[int]()

View file

@ -0,0 +1,29 @@
type
TFoo[T, U, R = int] = object
x: T
y: U
z: R
TBar[T] = TFoo[T, array[4, T], T]
var x1: TFoo[int, float]
static:
assert type(x1.x) is int
assert type(x1.y) is float
assert type(x1.z) is int
var x2: TFoo[string, R = float, U = seq[int]]
static:
assert type(x2.x) is string
assert type(x2.y) is seq[int]
assert type(x2.z) is float
var x3: TBar[float]
static:
assert type(x3.x) is float
assert type(x3.y) is array[4, float]
assert type(x3.z) is float

View file

@ -1,52 +1,52 @@
type
TBase = object of TObject
x, y: int
TSubclassKind = enum ka, kb, kc, kd, ke, kf
TSubclass = object of TBase
case c: TSubclassKind
of ka, kb, kc, kd:
a, b: int
of ke:
d, e, f: char
else: nil
n: bool
type
TBase = object of TObject
x, y: int
TSubclassKind = enum ka, kb, kc, kd, ke, kf
TSubclass = object of TBase
case c: TSubclassKind
of ka, kb, kc, kd:
a, b: int
of ke:
d, e, f: char
else: nil
n: bool
type
TMyObject = object of TObject
case disp: range[0..4]:
case disp: range[0..4]
of 0: arg: char
of 1: s: string
else: wtf: bool
var
x: TMyObject
var
global: int
var
s: string
r: float = 0.0
i: int = 500 + 400
case i
of 500..999: write(stdout, "ha!\n")
of 1000..3000, 12: write(stdout, "ganz schön groß\n")
of 1, 2, 3: write(stdout, "1 2 oder 3\n")
else: write(stdout, "sollte nicht passieren\n")
case readLine(stdin)
of "Rumpf": write(stdout, "Hallo Meister!\n")
of "Andreas": write(stdout, "Hallo Meister!\n")
else: write(stdout, "Nicht mein Meister!\n")
global = global + 1
write(stdout, "Hallo wie heißt du? \n")
s = readLine(stdin)
i = 0
while i < len(s):
if s[i] == 'c': write(stdout, "'c' in deinem Namen gefunden\n")
i = i + 1
write(stdout, "Du heißt " & s)
var
global: int
var
s: string
r: float = 0.0
i: int = 500 + 400
case i
of 500..999: write(stdout, "ha!\n")
of 1000..3000, 12: write(stdout, "ganz schön groß\n")
of 1, 2, 3: write(stdout, "1 2 oder 3\n")
else: write(stdout, "sollte nicht passieren\n")
case readLine(stdin)
of "Rumpf": write(stdout, "Hallo Meister!\n")
of "Andreas": write(stdout, "Hallo Meister!\n")
else: write(stdout, "Nicht mein Meister!\n")
global = global + 1
write(stdout, "Hallo wie heißt du? \n")
s = readLine(stdin)
i = 0
while i < len(s):
if s[i] == 'c': write(stdout, "'c' in deinem Namen gefunden\n")
i = i + 1
write(stdout, "Du heißt " & s)

View file

@ -1,32 +1,32 @@
# Test overloading of procs when used as function pointers
import strutils
proc parseInt(x: float): int {.noSideEffect.} = nil
proc parseInt(x: bool): int {.noSideEffect.} = nil
proc parseInt(x: float32): int {.noSideEffect.} = nil
proc parseInt(x: int8): int {.noSideEffect.} = nil
proc parseInt(x: TFile): int {.noSideEffect.} = nil
proc parseInt(x: char): int {.noSideEffect.} = nil
proc parseInt(x: int16): int {.noSideEffect.} = nil
# Test overloading of procs when used as function pointers
import strutils
proc parseInt(x: float): int {.noSideEffect.} = nil
proc parseInt(x: bool): int {.noSideEffect.} = nil
proc parseInt(x: float32): int {.noSideEffect.} = nil
proc parseInt(x: int8): int {.noSideEffect.} = nil
proc parseInt(x: TFile): int {.noSideEffect.} = nil
proc parseInt(x: char): int {.noSideEffect.} = nil
proc parseInt(x: int16): int {.noSideEffect.} = nil
proc parseInt[T](x: T): int = echo x; 34
type
TParseInt = proc (x: string): int {.noSideEffect.}
var
q = TParseInt(parseInt)
p: TParseInt = parseInt
proc takeParseInt(x: proc (y: string): int {.noSideEffect.}): int =
result = x("123")
echo "Give a list of numbers (separated by spaces): "
var x = stdin.readline.split.map(parseInt).max
echo x, " is the maximum!"
echo "another number: ", takeParseInt(parseInt)
type
TParseInt = proc (x: string): int {.noSideEffect.}
var
q = TParseInt(parseInt)
p: TParseInt = parseInt
proc takeParseInt(x: proc (y: string): int {.noSideEffect.}): int =
result = x("123")
echo "Give a list of numbers (separated by spaces): "
var x = stdin.readline.split.map(parseInt).max
echo x, " is the maximum!"
echo "another number: ", takeParseInt(parseInt)
type
TFoo[a,b] = object

View file

@ -5,6 +5,8 @@ type
T1 = expr
T2 = expr
Numeric = int|float
proc takesExpr(x, y) =
echo x, y
@ -32,3 +34,22 @@ takesFoo(f, f)
takes2Types(1, 1, "string")
takes2Types[string, int]("test", "test", 1)
proc takesSeq(x: seq) =
echo "seq"
takesSeq(@[1, 2, 3])
takesSeq(@["x", "y", "z"])
proc takesSeqOfFoos(x: seq[TFoo]) =
echo "foo seq"
var sf = newSeq[TFoo[int]](3)
takesSeq(sf)
takesSeqOfFoos(sf)
proc takesFooOfNumeric(x: TFoo[Numeric]) =
echo "foo of numeric"
takesFooOfNumeric(sf[0])

View file

@ -0,0 +1,30 @@
discard """
file: "tgenconstraints.nim"
line: 25
errormsg: "cannot instantiate T2"
"""
type
T1[T: int|string] = object
x: T
T2[T: Ordinal] = object
x: T
var x1: T1[int]
var x2: T1[string]
var x3: T2[int]
proc foo[T](x: T): T2[T] {.discardable.} =
var o: T1[T]
foo(10)
proc bar(x: string|TNumber): T1[type(x)] {.discardable.} =
when type(x) is TNumber:
var o: T2[type(x)]
bar "test"
bar 100
bar 1.1

View file

@ -5,7 +5,7 @@ discard """
type
TMyTuple = tuple[a, b: int]
proc indexOf*(t: typedesc, name: string): int {.compiletime.} =
proc indexOf*(t: typedesc, name: string): int =
## takes a tuple and looks for the field by name.
## returs index of that field.
var

View file

@ -1,8 +1,6 @@
discard """
file: "tfinally.nim"
output: '''came
here
3'''
output: "came\nhere\n3"
"""
# Test return in try statement:
@ -14,10 +12,8 @@ proc main: int =
echo("came")
return 2
finally:
echo("here ")
echo("here")
return 3
echo main() #OUT came here 3

View file

@ -0,0 +1,17 @@
discard """
file: "tstaticparams.nim"
output: "abracadabra\ntest"
"""
type
TFoo[T; Val: expr[string]] = object
data: array[4, T]
proc takeFoo(x: TFoo) =
echo "abracadabra"
echo TFoo.Val
var x: TFoo[int, "test"]
takeFoo(x)