diff options
author | Araq <rumpf_a@web.de> | 2013-09-27 08:45:14 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2013-09-27 08:45:14 +0200 |
commit | 2f0671eb795192b079402e452a229e51a7ca990f (patch) | |
tree | c3021a0d31c99549c4241f6e439948aa29b2f349 | |
parent | a0b82db40229620a29726fa33a4358db2f5fcd44 (diff) | |
download | Nim-2f0671eb795192b079402e452a229e51a7ca990f.tar.gz |
examples from the talk part of test suite
-rw-r--r-- | examples/talk/dsl.nim | 33 | ||||
-rw-r--r-- | examples/talk/formatoptimizer.nim | 55 | ||||
-rw-r--r-- | examples/talk/hoisting.nim | 23 | ||||
-rw-r--r-- | examples/talk/lazyeval.nim | 12 | ||||
-rw-r--r-- | examples/talk/quasiquote.nim | 11 | ||||
-rw-r--r-- | examples/talk/tags.nim | 8 | ||||
-rw-r--r-- | tests/tester.nim | 1 | ||||
-rw-r--r-- | todo.txt | 8 | ||||
-rw-r--r-- | tools/niminst/niminst.nim | 5 |
9 files changed, 153 insertions, 3 deletions
diff --git a/examples/talk/dsl.nim b/examples/talk/dsl.nim new file mode 100644 index 000000000..4dfab5cd7 --- /dev/null +++ b/examples/talk/dsl.nim @@ -0,0 +1,33 @@ + +import strutils + +template html(name: expr, matter: stmt) {.immediate.} = + proc name(): string = + result = "<html>" + matter + result.add("</html>") + +template nestedTag(tag: expr) {.immediate.} = + template tag(matter: stmt) {.immediate.} = + result.add("<" & astToStr(tag) & ">") + matter + result.add("</" & astToStr(tag) & ">") + +template simpleTag(tag: expr) {.immediate.} = + template tag(matter: expr) {.immediate.} = + result.add("<$1>$2</$1>" % [astToStr(tag), matter]) + +nestedTag body +nestedTag head +nestedTag ul +simpleTag title +simpleTag li + +html mainPage: + head: + title "now look at this" + body: + ul: + li "Nimrod is quite capable" + +echo mainPage() diff --git a/examples/talk/formatoptimizer.nim b/examples/talk/formatoptimizer.nim new file mode 100644 index 000000000..db11d112d --- /dev/null +++ b/examples/talk/formatoptimizer.nim @@ -0,0 +1,55 @@ +## This is the example that optimizes a modified "hello world" + +import macros + +proc invalidFormatString() = + echo "invalidFormatString" + +template formatImpl(handleChar: expr) = + var i = 0 + while i < f.len: + if f[i] == '$': + case f[i+1] + of '1'..'9': + var j = 0 + i += 1 + while f[i] in {'0'..'9'}: + j = j * 10 + ord(f[i]) - ord('0') + i += 1 + result.add(a[j-1]) + else: + invalidFormatString() + else: + result.add(handleChar(f[i])) + i += 1 + +proc `%`*(f: string, a: openArray[string]): string = + template identity(x: expr): expr = x + result = "" + formatImpl(identity) + +macro optFormat{`%`(f, a)}(f: string{lit}, a: openArray[string]): expr = + result = newNimNode(nnkBracket) + #newCall("&") + let f = f.strVal + formatImpl(newLit) + result = nestList(!"&", result) + +template optAdd1{x = y; add(x, z)}(x, y, z: string) = + x = y & z + +#template optAdd2{x.add(y); x.add(z)}(x, y, z: string) = +# x.add(y & z) + +proc `/&` [T: object](x: T): string = + result = "(" + for name, value in fieldPairs(x): + result.add("$1: $2\n" % [name, $value]) + result.add(")") + +type + MyObject = object + a, b: int + s: string +let obj = MyObject(a: 3, b: 4, s: "abc") +echo(/&obj) diff --git a/examples/talk/hoisting.nim b/examples/talk/hoisting.nim new file mode 100644 index 000000000..df13ba2cb --- /dev/null +++ b/examples/talk/hoisting.nim @@ -0,0 +1,23 @@ +type + Regex = distinct string + +const maxSubpatterns = 10 + +proc re(x: string): Regex = + result = Regex(x) + +proc match(s: string, pattern: Regex, captures: var openArray[string]): bool = + true + +template optRe{re(x)}(x: string{lit}): Regex = + var g {.global.} = re(x) + g + +template `=~`(s: string, pattern: Regex): bool = + when not definedInScope(matches): + var matches {.inject.}: array[maxSubPatterns, string] + match(s, pattern, matches) + +for line in lines("input.txt"): + if line =~ re"(\w+)=(\w+)": + echo "key-value pair; key: ", matches[0], " value: ", matches[1] diff --git a/examples/talk/lazyeval.nim b/examples/talk/lazyeval.nim new file mode 100644 index 000000000..77d963834 --- /dev/null +++ b/examples/talk/lazyeval.nim @@ -0,0 +1,12 @@ + +const + debug = true + +template log(msg: string) = + if debug: + echo msg +var + x = 1 + y = 2 + +log("x: " & $x & ", y: " & $y) diff --git a/examples/talk/quasiquote.nim b/examples/talk/quasiquote.nim new file mode 100644 index 000000000..df4003e6e --- /dev/null +++ b/examples/talk/quasiquote.nim @@ -0,0 +1,11 @@ + +import macros + +macro check(ex: expr): stmt = + var info = ex.lineinfo + var expString = ex.toStrLit + result = quote do: + if not `ex`: + echo `info`, ": Check failed: ", `expString` + +check 1 < 2 diff --git a/examples/talk/tags.nim b/examples/talk/tags.nim new file mode 100644 index 000000000..d47b09e07 --- /dev/null +++ b/examples/talk/tags.nim @@ -0,0 +1,8 @@ + +template htmlTag(tag: expr) {.immediate.} = + proc tag(): string = "<" & astToStr(tag) & ">" + +htmlTag(br) +htmlTag(html) + +echo br() diff --git a/tests/tester.nim b/tests/tester.nim index e10b89761..fe21fc9ee 100644 --- a/tests/tester.nim +++ b/tests/tester.nim @@ -402,6 +402,7 @@ proc main() = compileExample(r, "lib/pure/*.nim", p.cmdLineRest.string) compileExample(r, "examples/*.nim", p.cmdLineRest.string) compileExample(r, "examples/gtk/*.nim", p.cmdLineRest.string) + compileExample(r, "examples/talk/*.nim", p.cmdLineRest.string) compileSpecialTests(r, p.cmdLineRest.string) writeResults(compileJson, r) of "run": diff --git a/todo.txt b/todo.txt index 302a5eeaf..4f36ba77f 100644 --- a/todo.txt +++ b/todo.txt @@ -1,13 +1,16 @@ version 0.9.4 ============= -- add the code from the talk to examples or run tests +- fails in release mode: + +nimrod compile -f --symbolFiles:off --compileOnly --gen_mapping --cc:gcc + --skipUserCfg --os:haiku --cpu:amd64 -d:relase compiler/nimrod.nim + - new VM: - implement the glue to replace evals.nim - implement missing magics - implement overflow checking - implement the FFI - - implement on the fly CSE - make 'bind' default for templates and introduce 'mixin' - special rule for ``[]=`` @@ -160,3 +163,4 @@ Optimizations even further write barrier specialization - inlining of first class functions - proc specialization in the code gen for write barrier specialization +- VM/optimizer: implement on the fly CSE diff --git a/tools/niminst/niminst.nim b/tools/niminst/niminst.nim index 0feac6de8..6ed8fe3fe 100644 --- a/tools/niminst/niminst.nim +++ b/tools/niminst/niminst.nim @@ -244,6 +244,7 @@ proc parseIniFile(c: var TConfigData) = var p: TCfgParser section = "" + hasCpuOs = false var input = newFileStream(c.infile, fmRead) if input != nil: open(p, input, c.infile) @@ -265,16 +266,18 @@ proc parseIniFile(c: var TConfigData) = of "version": c.version = v of "os": c.oses = split(v, {';'}) + hasCpuOs = true if c.explicitPlatforms: quit(errorStr(p, "you cannot have both 'platforms' and 'os'")) of "cpu": c.cpus = split(v, {';'}) + hasCpuOs = true if c.explicitPlatforms: quit(errorStr(p, "you cannot have both 'platforms' and 'cpu'")) of "platforms": platforms(c, v) c.explicitPlatforms = true - if c.cpus.len > 0 or c.oses.len > 0: + if hasCpuOs: quit(errorStr(p, "you cannot have both 'platforms' and 'os'")) of "authors": c.authors = split(v, {';'}) of "description": c.description = v |