From 9b95ca407d38fecb23a39cf0f6d92ed838e57623 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov <zahary@gmail.comy> Date: Wed, 7 Sep 2011 15:47:03 +0300 Subject: Some very small steps towards an uuid generation module. I did some basic research on cross-platform UUID generation and didn't want to lose any information, so this code only represents what I learned (uuidMacTime should work tho). --- lib/pure/uuid.nim | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lib/pure/uuid.nim (limited to 'lib/pure') diff --git a/lib/pure/uuid.nim b/lib/pure/uuid.nim new file mode 100644 index 000000000..36fa9e445 --- /dev/null +++ b/lib/pure/uuid.nim @@ -0,0 +1,30 @@ +# This module implements the RFC 4122 specification for generating universally unique identifiers +# http://en.wikipedia.org/wiki/Universally_unique_identifier + +# This module is a work-in-progress +# If you want to help with the implementation, take a loot at: +# http://dsource.org/projects/tango/docs/current/tango.util.uuid.Uuid.html + +type TUuid* = array[0..15, char] + +when defined(windows): + # This is actually available only on Windows 2000+ + type PUuid* {.importc: "UUID __RPC_FAR *", header: "<Rpc.h>".} = ptr TUuid + proc uuid1Sys*(uuid: PUuid) {.importc: "UuidCreateSequential", header: "<Rpc.h>".} + +else: + type PUuid {.importc: "uuid_t", header: "<uuid/uuid.h>".} = ptr TUuid + proc uuid1Sys*(uuid: PUuid) {.importc: "uuid_generate_time", header: "<uuid/uuid.h>".} + +# v1 UUIDs include the MAC address of the machine generating the ID and a timestamp +# This scheme has the strongest guaranty of uniqueness, but discloses when the ID was generated +proc uuidMacTime* : TUuid = uuid1Sys(addr(result)) + +# v4 UUID are created entirely using a random number generator. +# Some bits have fixed value in order to indicate the UUID type +proc uuidRandom*[RandomGenerator](rand: RandomGenerator) : TUuid = nil + +# v3 and v5 UUIDs are derived from given namespace and name using a secure hashing algorithm. +# v3 uses MD5, v5 uses SHA1. +proc uuidByName*[Hash](namespace: TUuid, name: string, hasher: Hash, v: int) : TUuid = nil + -- cgit 1.4.1-2-gfad0 From dbcca9b3b994e3339c9a03c3846b7162147679fd Mon Sep 17 00:00:00 2001 From: Zahary Karadjov <zahary@gmail.comy> Date: Wed, 7 Sep 2011 22:05:21 +0300 Subject: Moved the parseAST magics to evals.nim Added string interpolation helper routines in parserutils Added a proof-of-concept string interpolation user-land macros (currently, only as a test case): the interpolated expression could either be transformed to concat("literal string ", $(interpolated), " end") or "literal string $1 end" % [$(interpolated)] Added a very initial definition of Optional[T] generic type A new overload of ParseIdent was added in hope to get around the fact that the old one doesn't work correctly in macros, but the problem persists. --- compiler/evals.nim | 29 +++++++++++- compiler/semexprs.nim | 42 +++--------------- compiler/semfold.nim | 1 + lib/pure/parseutils.nim | 91 ++++++++++++++++++++++++++++++++++++++ lib/system.nim | 9 +++- tests/accept/run/tstringinterp.nim | 82 ++++++++++++++++++++++++++++++++++ 6 files changed, 215 insertions(+), 39 deletions(-) create mode 100644 tests/accept/run/tstringinterp.nim (limited to 'lib/pure') diff --git a/compiler/evals.nim b/compiler/evals.nim index a61bd24cf..65c64f4d3 100755 --- a/compiler/evals.nim +++ b/compiler/evals.nim @@ -16,7 +16,7 @@ import strutils, magicsys, lists, options, ast, astalgo, trees, treetab, nimsets, msgs, os, condsyms, idents, renderer, types, passes, semfold, transf, - ropes + parser, ropes type PStackFrame* = ref TStackFrame @@ -752,6 +752,31 @@ proc evalRepr(c: PEvalContext, n: PNode): PNode = proc isEmpty(n: PNode): bool = result = (n != nil) and (n.kind == nkEmpty) +# The lexer marks multi-line strings as residing at the line where they are closed +# This function returns the line where the string begins +# Maybe the lexer should mark both the beginning and the end of expressions, then +# this function could be removed +proc stringStartingLine(s: PNode): int = + var totalLines = 0 + for ln in splitLines(s.strVal): inc totalLines + + result = s.info.line - totalLines + +proc evalParseExpr(c: PEvalContext, n: Pnode): Pnode = + var code = evalAux(c, n.sons[1], {}) + var ast = parseString(code.getStrValue, code.info.toFilename, code.stringStartingLine) + + if sonsLen(ast) != 1: + GlobalError(code.info, errExprExpected, "multiple statements") + + result = ast.sons[0] + result.typ = newType(tyExpr, c.module) + +proc evalParseStmt(c: PEvalContext, n: Pnode): Pnode = + var code = evalAux(c, n.sons[1], {}) + result = parseString(code.getStrValue, code.info.toFilename, code.stringStartingLine) + result.typ = newType(tyStmt, c.module) + proc evalMagicOrCall(c: PEvalContext, n: PNode): PNode = var m = getMagic(n) case m @@ -776,6 +801,8 @@ proc evalMagicOrCall(c: PEvalContext, n: PNode): PNode = of mAppendStrCh: result = evalAppendStrCh(c, n) of mAppendStrStr: result = evalAppendStrStr(c, n) of mAppendSeqElem: result = evalAppendSeqElem(c, n) + of mParseExprToAst: result = evalParseExpr(c, n) + of mParseStmtToAst: result = evalParseStmt(c, n) of mNLen: result = evalAux(c, n.sons[1], {efLValue}) if isSpecial(result): return diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 5f4c7749a..af4d4b6bc 100755 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -889,44 +889,16 @@ proc setMs(n: PNode, s: PSym): PNode = n.sons[0].info = n.info proc expectStringArg(c: PContext, n: PNode, i: int): PNode = - result = c.semConstExpr(c, n.sons[i+1]) + result = c.semAndEvalConstExpr(n.sons[i+1]) if result.kind notin {nkStrLit, nkRStrLit, nkTripleStrLit}: - GlobalError(result.info, errStringLiteralExpected) + GlobalError(result.info, errStringLiteralExpected) -# The lexer marks multi-line strings as residing at the line where they are closed -# This function returns the line where the string begins -# Maybe the lexer should mark both the beginning and the end of expressions, then -# this function could be removed -proc stringStartingLine(s: PNode): int = - var totalLines = 0 - for ln in splitLines(s.strVal): inc totalLines - - result = s.info.line - totalLines - -proc semParseExprToAst(c: PContext, n: PNode, flags: TExprFlags): PNode = - if sonsLen(n) == 2: - var code = expectStringArg(c, n, 0) - var ast = parseString(code.strVal, code.info.toFilename, code.stringStartingLine) - - if sonsLen(ast) != 1: - GlobalError(code.info, errExprExpected, "multiple statements") - - result = newMetaNodeIT(ast.sons[0], code.info, newTypeS(tyExpr, c)) - else: - result = semDirectOp(c, n, flags) - -proc semParseStmtToAst(c: PContext, n: PNode, flags: TExprFlags): PNode = +proc semExpandMacroToAst(c: PContext, n: PNode, flags: TExprFlags): PNode = if sonsLen(n) == 2: - var code = expectStringArg(c, n, 0) - var ast = parseString(code.strVal, code.info.toFilename, code.stringStartingLine) + if not isCallExpr(n.sons[1]): + GlobalError(n.info, errXisNoMacroOrTemplate, n.renderTree) - result = newMetaNodeIT(ast, code.info, newTypeS(tyStmt, c)) - else: - result = semDirectOp(c, n, flags) - -proc semExpandMacroToAst(c: PContext, n: PNode, flags: TExprFlags): PNode = - if sonsLen(n) == 2 and isCallExpr(n.sons[1]): var macroCall = n.sons[1] var s = qualifiedLookup(c, macroCall.sons[0], {checkUndeclared}) @@ -943,7 +915,7 @@ proc semExpandMacroToAst(c: PContext, n: PNode, flags: TExprFlags): PNode = var macroRetType = newTypeS(s.typ.sons[0].kind, c) result = newMetaNodeIT(expanded, n.info, macroRetType) else: - GlobalError(n.info, errXisNoMacroOrTemplate, n.renderTree) + result = semDirectOp(c, n, flags) proc semSlurp(c: PContext, n: PNode, flags: TExprFlags): PNode = if sonsLen(n) == 2: @@ -981,8 +953,6 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode = else: result = semDirectOp(c, n, flags) of mSlurp: result = semSlurp(c, n, flags) - of mParseExprToAst: result = semParseExprToAst(c, n, flags) - of mParseStmtToAst: result = semParseStmtToAst(c, n, flags) of mExpandMacroToAst: result = semExpandMacroToAst(c, n, flags) else: result = semDirectOp(c, n, flags) diff --git a/compiler/semfold.nim b/compiler/semfold.nim index 61e63a69f..570656a39 100755 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -208,6 +208,7 @@ proc evalOp(m: TMagic, n, a, b, c: PNode): PNode = of mNewString, mNewStringOfCap, mExit, mInc, ast.mDec, mEcho, mAssert, mSwap, mAppendStrCh, mAppendStrStr, mAppendSeqElem, mSetLengthStr, mSetLengthSeq, + mParseExprToAst, mParseStmtToAst, mNLen..mNError, mEqRef: nil else: InternalError(a.info, "evalOp(" & $m & ')') diff --git a/lib/pure/parseutils.nim b/lib/pure/parseutils.nim index d3346ecde..a046aff36 100755 --- a/lib/pure/parseutils.nim +++ b/lib/pure/parseutils.nim @@ -76,6 +76,19 @@ proc parseIdent*(s: string, ident: var string, start = 0): int = ident = substr(s, start, i-1) result = i-start +proc parseIdent*(s: string, start = 0): TOptional[string] = + ## parses an identifier and stores it in ``ident``. Returns + ## the number of the parsed characters or 0 in case of an error. + result.hasValue = false + var i = start + + if s[i] in IdentStartChars: + inc(i) + while s[i] in IdentChars: inc(i) + + result.hasValue = true + result.value = substr(s, start, i-1) + proc parseToken*(s: string, token: var string, validChars: set[char], start = 0): int {.inline, deprecated.} = ## parses a token and stores it in ``token``. Returns @@ -254,4 +267,82 @@ proc parseFloat*(s: string, number: var float, start = 0): int {. result = parseBiggestFloat(s, bf, start) number = bf +proc isEscaped*(s: string, pos: int) : bool = + assert pos >= 0 and pos < s.len + + var + backslashes = 0 + j = pos - 1 + + while j >= 0: + if s[j] == '\\': + inc backslashes + dec j + else: + break + + return backslashes mod 2 != 0 + +type + TInterpStrFragment* = tuple[interpStart, interpEnd, exprStart, exprEnd: int] + +iterator interpolatedFragments*(s: string): TInterpStrFragment = + var i = 0 + while i < s.len: + # The $ sign marks the start of an interpolation. + # + # It's followed either by a varialbe name or an opening bracket + # (so it should be before the end of the string) + # if the dollar sign is escaped, don't trigger interpolation + if s[i] == '$' and i < (s.len - 1) and not isEscaped(s, i): + var next = s[i+1] + + if next == '{': + inc i + + var + brackets = {'{', '}'} + nestingCount = 1 + start = i + 1 + + # find closing braket, while respecting any nested brackets + while i < s.len: + inc i, skipUntil(s, brackets, i+1) + 1 + + if not isEscaped(s, i): + if s[i] == '}': + dec nestingCount + if nestingCount == 0: break + else: + inc nestingCount + + var t : TInterpStrFragment + t.interpStart = start - 2 + t.interpEnd = i + t.exprStart = start + t.exprEnd = i - 1 + + yield t + + else: + var + start = i + 1 + identifier = parseIdent(s, i+1) + + if identifier.hasValue: + inc i, identifier.value.len + + var t : TInterpStrFragment + t.interpStart = start - 1 + t.interpEnd = i + t.exprStart = start + t.exprEnd = i + + yield t + + else: + raise newException(EInvalidValue, "Unable to parse a varible name at " & s[i..s.len]) + + inc i + {.pop.} diff --git a/lib/system.nim b/lib/system.nim index 2e754ece7..3fc4733b2 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -710,7 +710,7 @@ proc `&` * (x, y: string): string {. proc `&` * (x: char, y: string): string {. magic: "ConStrStr", noSideEffect, merge.} ## is the `concatenation operator`. It concatenates `x` and `y`. - + # implementation note: These must all have the same magic value "ConStrStr" so # that the merge optimization works properly. @@ -895,7 +895,12 @@ type # these work for most platforms: PFloat64* = ptr Float64 ## an alias for ``ptr float64`` PInt64* = ptr Int64 ## an alias for ``ptr int64`` PInt32* = ptr Int32 ## an alias for ``ptr int32`` - + +type TOptional*[T] = object + case hasValue* : bool + of true: value*: T + of false: nil + proc toFloat*(i: int): float {. magic: "ToFloat", noSideEffect, importc: "toFloat".} ## converts an integer `i` into a ``float``. If the conversion diff --git a/tests/accept/run/tstringinterp.nim b/tests/accept/run/tstringinterp.nim new file mode 100644 index 000000000..83bd37709 --- /dev/null +++ b/tests/accept/run/tstringinterp.nim @@ -0,0 +1,82 @@ +discard """ + file: "tstringinterp.nim" + output: "Hello Alice \$ 64 | Hello Bob, 10" +""" + +import macros, parseutils, strutils + +proc concat(strings: openarray[string]) : string = + result = newString(0) + for s in items(strings): result.add(s) + +# This will run though the intee +template ProcessInterpolations(e: expr) = + var + s = e[1].strVal + stringStart = 0 + + for i in interpolatedFragments(s): + var leadingString = s[stringStart..i.interpStart-1] + var interpolatedExpr = s[i.exprStart..i.exprEnd] + + addString(leadingString) + + var interpTargetAst = parseExpr("$(x)") + interpTargetAst[1][0] = parseExpr(interpolatedExpr) + addExpr(interpTargetAst) + + stringStart = i.interpEnd + 1 + + if stringStart != s.len: + var endingString = s[stringStart..s.len] + addString(endingString) + +macro formatStyleInterpolation(e: expr): expr = + var + formatString = "" + arrayNode = newNimNode(nnkBracket) + idx = 1 + + proc addString(s: string) = + formatString.add(s) + + proc addExpr(e: expr) = + arrayNode.add(e) + formatString.add("$" & $(idx)) + inc idx + + ProcessInterpolations(e) + + result = parseExpr("\"x\" % [y]") + result[1].strVal = formatString + result[2] = arrayNode + +macro concatStyleInterpolation(e: expr): expr = + var args : seq[PNimrodNode] + newSeq(args, 0) + + proc addString(s: string) = args.add(newStrLitNode(s)) + proc addExpr(e: expr) = args.add(e) + + ProcessInterpolations(e) + + result = newCall("concat", args) + +### + +proc sum(a, b, c: int): int = + return (a + b + c) + +var + alice = "Alice" + bob = "Bob" + a = 10 + b = 20 + c = 34 + +var + s1 = concatStyleInterpolation"Hello ${alice} \$ ${sum (a, b, c)}" + s2 = formatStyleInterpolation"Hello ${bob}, ${sum (alice.len, bob.len, 2)}" + +write(stdout, s1 & " | " & s2) + -- cgit 1.4.1-2-gfad0 From 9acfc43119d42d7265497fd6ae60eda3903d9938 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov <zahary@gmail.comy> Date: Sun, 11 Sep 2011 21:43:12 +0300 Subject: using statement (ala C#) implemented as macro (added as test). simplified the usage of the interpolatedFragments iterator. --- compiler/parser.nim | 3 +- compiler/semexprs.nim | 2 +- lib/pure/parseutils.nim | 69 ++++++++++++++---------- tests/accept/run/tstringinterp.nim | 25 +++------ tests/accept/run/tusingstatement.nim | 102 +++++++++++++++++++++++++++++++++++ 5 files changed, 152 insertions(+), 49 deletions(-) create mode 100644 tests/accept/run/tusingstatement.nim (limited to 'lib/pure') diff --git a/compiler/parser.nim b/compiler/parser.nim index c371e09cb..df21c9916 100755 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -1381,6 +1381,7 @@ proc parseString(s: string, filename: string = "", line: int = 0): PNode = var parser : TParser OpenParser(parser, filename, stream) - + result = parser.parseAll + CloseParser(parser) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index af4d4b6bc..6dbbba7b8 100755 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -903,7 +903,7 @@ proc semExpandMacroToAst(c: PContext, n: PNode, flags: TExprFlags): PNode = var s = qualifiedLookup(c, macroCall.sons[0], {checkUndeclared}) if s == nil: - GlobalError(n.info, errUndeclaredIdentifier, macroCall.sons[0].strVal) + GlobalError(n.info, errUndeclaredIdentifier, macroCall.sons[0].renderTree) var expanded : Pnode diff --git a/lib/pure/parseutils.nim b/lib/pure/parseutils.nim index a046aff36..1f4e54b96 100755 --- a/lib/pure/parseutils.nim +++ b/lib/pure/parseutils.nim @@ -76,18 +76,17 @@ proc parseIdent*(s: string, ident: var string, start = 0): int = ident = substr(s, start, i-1) result = i-start -proc parseIdent*(s: string, start = 0): TOptional[string] = - ## parses an identifier and stores it in ``ident``. Returns - ## the number of the parsed characters or 0 in case of an error. - result.hasValue = false +proc parseIdent*(s: string, start = 0): string = + ## parses an identifier and stores it in ``ident``. + ## Returns the parsed identifier or an empty string in case of an error. + result = "" var i = start if s[i] in IdentStartChars: inc(i) while s[i] in IdentChars: inc(i) - result.hasValue = true - result.value = substr(s, start, i-1) + result = substr(s, start, i-1) proc parseToken*(s: string, token: var string, validChars: set[char], start = 0): int {.inline, deprecated.} = @@ -284,10 +283,20 @@ proc isEscaped*(s: string, pos: int) : bool = return backslashes mod 2 != 0 type - TInterpStrFragment* = tuple[interpStart, interpEnd, exprStart, exprEnd: int] + TInterpolatedKind* = enum + ikString, ikExpr + + TInterpStrFragment* = tuple[kind: TInterpolatedKind, value: string] iterator interpolatedFragments*(s: string): TInterpStrFragment = - var i = 0 + var + i = 0 + tokenStart = 0 + + proc token(kind: TInterpolatedKind, value: string): TInterpStrFragment = + result.kind = kind + result.value = value + while i < s.len: # The $ sign marks the start of an interpolation. # @@ -295,17 +304,22 @@ iterator interpolatedFragments*(s: string): TInterpStrFragment = # (so it should be before the end of the string) # if the dollar sign is escaped, don't trigger interpolation if s[i] == '$' and i < (s.len - 1) and not isEscaped(s, i): + # Interpolation starts here. + # Return any string that we've ran over so far. + if i != tokenStart: + yield token(ikString, s[tokenStart..i-1]) + var next = s[i+1] - if next == '{': + # Complex expression: ${foo(bar) in {1..100}} + # Find closing braket, while respecting any nested brackets inc i + tokenStart = i + 1 var brackets = {'{', '}'} nestingCount = 1 - start = i + 1 - - # find closing braket, while respecting any nested brackets + while i < s.len: inc i, skipUntil(s, brackets, i+1) + 1 @@ -316,33 +330,30 @@ iterator interpolatedFragments*(s: string): TInterpStrFragment = else: inc nestingCount - var t : TInterpStrFragment - t.interpStart = start - 2 - t.interpEnd = i - t.exprStart = start - t.exprEnd = i - 1 + yield token(ikExpr, s[tokenStart..(i-1)]) - yield t + tokenStart = i + 1 else: - var - start = i + 1 - identifier = parseIdent(s, i+1) + tokenStart = i + 1 + var identifier = parseIdent(s, i+1) - if identifier.hasValue: - inc i, identifier.value.len + if identifier.len > 0: + inc i, identifier.len - var t : TInterpStrFragment - t.interpStart = start - 1 - t.interpEnd = i - t.exprStart = start - t.exprEnd = i + yield token(ikExpr, s[tokenStart..i]) - yield t + tokenStart = i + 1 else: raise newException(EInvalidValue, "Unable to parse a varible name at " & s[i..s.len]) inc i + #end while + + # We've reached the end of the string without finding a new interpolation. + # Return the last fragment at string. + if i != tokenStart: + yield token(ikString, s[tokenStart..i]) {.pop.} diff --git a/tests/accept/run/tstringinterp.nim b/tests/accept/run/tstringinterp.nim index 83bd37709..55baae7ec 100644 --- a/tests/accept/run/tstringinterp.nim +++ b/tests/accept/run/tstringinterp.nim @@ -1,6 +1,6 @@ discard """ file: "tstringinterp.nim" - output: "Hello Alice \$ 64 | Hello Bob, 10" + output: "Hello Alice, 64 | Hello Bob, 10" """ import macros, parseutils, strutils @@ -13,23 +13,12 @@ proc concat(strings: openarray[string]) : string = template ProcessInterpolations(e: expr) = var s = e[1].strVal - stringStart = 0 - - for i in interpolatedFragments(s): - var leadingString = s[stringStart..i.interpStart-1] - var interpolatedExpr = s[i.exprStart..i.exprEnd] - - addString(leadingString) - - var interpTargetAst = parseExpr("$(x)") - interpTargetAst[1][0] = parseExpr(interpolatedExpr) - addExpr(interpTargetAst) - stringStart = i.interpEnd + 1 - - if stringStart != s.len: - var endingString = s[stringStart..s.len] - addString(endingString) + for f in interpolatedFragments(s): + if f.kind == ikString: + addString(f.value) + else: + addExpr(f.value) macro formatStyleInterpolation(e: expr): expr = var @@ -75,7 +64,7 @@ var c = 34 var - s1 = concatStyleInterpolation"Hello ${alice} \$ ${sum (a, b, c)}" + s1 = concatStyleInterpolation"Hello ${alice}, ${sum (a, b, c)}}" s2 = formatStyleInterpolation"Hello ${bob}, ${sum (alice.len, bob.len, 2)}" write(stdout, s1 & " | " & s2) diff --git a/tests/accept/run/tusingstatement.nim b/tests/accept/run/tusingstatement.nim new file mode 100644 index 000000000..0017af556 --- /dev/null +++ b/tests/accept/run/tusingstatement.nim @@ -0,0 +1,102 @@ +discard """ + file: "tusingstatement.nim" + output: "Using test.Closing test." +""" + +import + macros + +# This macro mimics the using statement from C# +# +# XXX: +# It doen't match the C# version exactly yet. +# In particular, it's not recursive, which prevents it from dealing +# with exceptions thrown from the variable initializers when multiple. +# variables are used. +# +# Also, since nimrod relies less on exceptions in general, a more +# idiomatic definition could be: +# var x = init() +# if opened(x): +# try: +# body +# finally: +# close(x) +# +# `opened` here could be an overloaded proc which any type can define. +# A common practice can be returing an Optional[Resource] obj for which +# `opened` is defined to `optional.hasValue` +macro using(e: expr) : stmt = + if e.len != 2: + error "Using statement: unexpected number of arguments. Got " & + $e.len & ", expected: 1 or more variable assignments and a block" + + var args = e[0] + var body = e[1] + + var + variables : seq[PNimrodNode] + closingCalls : seq[PNimrodNode] + + newSeq(variables, 0) + newSeq(closingCalls, 0) + + for i in countup(1, args.len-1): + if args[i].kind == nnkExprEqExpr: + var varName = args[i][0] + var varValue = args[i][1] + + var varAssignment = newNimNode(nnkIdentDefs) + varAssignment.add(varName) + varAssignment.add(newNimNode(nnkEmpty)) # empty means no type + varAssignment.add(varValue) + variables.add(varAssignment) + + closingCalls.add(newCall(!"close", varName)) + else: + error "Using statement: Unexpected expression. Got " & + $args[i].kind & " instead of assignment." + + var varSection = newNimNode(nnkVarSection) + varSection.add(variables) + + var finallyBlock = newNimNode(nnkStmtList) + finallyBlock.add(closingCalls) + + # XXX: Use a template here once getAst is working properly + var targetAst = parseStmt"""block: + var + x = foo() + y = bar() + + try: + body() + + finally: + close x + close y + """ + + targetAst[0][1][0] = varSection + targetAst[0][1][1][0] = body + targetAst[0][1][1][1][0] = finallyBlock + + return targetAst + +type + TResource* = object + field*: string + +proc openResource(param: string): TResource = + result.field = param + +proc close(r: var TResource) = + write(stdout, "Closing " & r.field & ".") + +proc use(r: var TResource) = + write(stdout, "Using " & r.field & ".") + +using(r = openResource("test")): + use r + + -- cgit 1.4.1-2-gfad0