diff options
Diffstat (limited to 'compiler/evals.nim')
-rwxr-xr-x | compiler/evals.nim | 29 |
1 files changed, 28 insertions, 1 deletions
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 |