diff options
author | Araq <rumpf_a@web.de> | 2012-06-03 10:11:01 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2012-06-03 10:11:01 +0200 |
commit | 70c283ac6490907940510f643c6e7d9923e93aa7 (patch) | |
tree | 5da5d3252f00e7453d437a7c9d4245592f79f8cf /compiler/evals.nim | |
parent | 5f527769eed6aafec5fe2f59ec0af31d198cb5f2 (diff) | |
parent | ab18654e593085feb92e8f93f5c575d6ce62cda4 (diff) | |
download | Nim-70c283ac6490907940510f643c6e7d9923e93aa7.tar.gz |
Merge branch 'master' of github.com:Araq/Nimrod
Diffstat (limited to 'compiler/evals.nim')
-rwxr-xr-x | compiler/evals.nim | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/compiler/evals.nim b/compiler/evals.nim index b2fded071..5c77a4d94 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, - parser, ropes, rodread, idgen + parser, ropes, rodread, idgen, osproc, streams type PStackFrame* = ref TStackFrame @@ -910,6 +910,43 @@ proc evalTypeTrait*(n: PNode, context: PSym): PNode = else: internalAssert false +proc expectString(n: PNode) = + if n.kind notin {nkStrLit, nkRStrLit, nkTripleStrLit}: + GlobalError(n.info, errStringLiteralExpected) + +proc evalSlurp*(e: PNode, module: PSym): PNode = + expectString(e) + try: + var filename = e.strVal.FindFile + var content = readFile(filename) + result = newStrNode(nkStrLit, content) + result.typ = getSysType(tyString) + result.info = e.info + # we produce a fake include statement for every slurped filename, so that + # the module dependencies are accurate: + appendToModule(module, newNode(nkIncludeStmt, e.info, @[ + newStrNode(nkStrLit, filename)])) + except EIO: + GlobalError(e.info, errCannotOpenFile, e.strVal) + +proc readOutput(p: PProcess): string = + result = "" + var output = p.outputStream + discard p.waitForExit + while not output.atEnd: + result.add(output.readLine) + +proc evalStaticExec*(cmd, input: PNode): PNode = + expectString(cmd) + var p = startCmd(cmd.strVal) + if input != nil: + expectString(input) + p.inputStream.write(input.strVal) + p.inputStream.close() + result = newStrNode(nkStrLit, p.readOutput) + result.typ = getSysType(tyString) + result.info = cmd.info + proc evalExpandToAst(c: PEvalContext, original: PNode): PNode = var n = original.copyTree @@ -960,6 +997,11 @@ proc evalMagicOrCall(c: PEvalContext, n: PNode): PNode = of mParseStmtToAst: result = evalParseStmt(c, n) of mExpandToAst: result = evalExpandToAst(c, n) of mTypeTrait: result = evalTypeTrait(n, c.module) + of mSlurp: result = evalSlurp(evalAux(c, n.sons[1], {}), c.module) + of mStaticExec: + let cmd = evalAux(c, n.sons[1], {}) + let input = if n.sonsLen == 3: evalAux(c, n.sons[2], {}) else: nil + result = evalStaticExec(cmd, input) of mNLen: result = evalAux(c, n.sons[1], {efLValue}) if isSpecial(result): return |