diff options
author | Araq <rumpf_a@web.de> | 2012-11-11 22:05:04 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2012-11-11 22:05:04 +0100 |
commit | 7ba0a7e324e41a9817dfd39022263f56763682b8 (patch) | |
tree | 66b7e80a87ec05ebd3cc9223c0830426ed19d53a /lib | |
parent | 48a62af3b13015cc7eda194b9f6ec7e194456e74 (diff) | |
parent | 1b691d87479880c9a7fad9696e92dccb600d5f96 (diff) | |
download | Nim-7ba0a7e324e41a9817dfd39022263f56763682b8.tar.gz |
Merge branch 'master' of github.com:Araq/Nimrod
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/core/macros.nim | 29 | ||||
-rwxr-xr-x | lib/system.nim | 4 |
2 files changed, 33 insertions, 0 deletions
diff --git a/lib/core/macros.nim b/lib/core/macros.nim index be6aecab5..b80de27fa 100755 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -234,6 +234,35 @@ proc getAst*(macroOrTemplate: expr): PNimrodNode {.magic: "ExpandToAst".} ## macro FooMacro() = ## var ast = getAst(BarTemplate()) +proc quote*(bl: stmt, op = "``"): PNimrodNode {.magic: "QuoteAst".} + ## Quasi-quoting operator. + ## Accepts an expression or a block and returns the AST that represents it. + ## Within the quoted AST, you are able to interpolate PNimrodNode expressions + ## from the surrounding scope. If no operator is given, quoting is done using + ## backticks. Otherwise, the given operator must be used as a prefix operator + ## for any interpolated expression. The original meaning of the interpolation + ## operator may be obtained by escaping it (by prefixing it with itself): + ## e.g. `@` is escaped as `@@`, `@@` is escaped as `@@@` and so on. + ## + ## Example: + ## + ## macro check(ex: expr): stmt = + ## # this is a simplified version of the check macro from the + ## # unittest module. + ## + ## # If there is a failed check, we want to make it easy for + ## # the user to jump to the faulty line in the code, so we + ## # get the line info here: + ## var info = ex.lineinfo + ## + ## # We will also display the code string of the failed check: + ## var expString = ex.toStrLit + ## + ## # Finally we compose the code to implement the check: + ## result = quote do: + ## if not `ex`: + ## echo `info` & ": Check failed: " & `expString` + template emit*(e: expr[string]): stmt = ## accepts a single string argument and treats it as nimrod code ## that should be inserted verbatim in the program diff --git a/lib/system.nim b/lib/system.nim index 5a4c491e1..b681ed51e 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -364,6 +364,10 @@ proc newSeq*[T](s: var seq[T], len: int) {.magic: "NewSeq", noSideEffect.} ## This is equivalent to ``s = @[]; setlen(s, len)``, but more ## efficient since no reallocation is needed. +proc newSeq*[T](len = 0): seq[T] = + ## creates a new sequence of type ``seq[T]`` with length ``len``. + newSeq(result, len) + proc len*[TOpenArray: openArray|varargs](x: TOpenArray): int {. magic: "LengthOpenArray", noSideEffect.} proc len*(x: string): int {.magic: "LengthStr", noSideEffect.} |