Cleaned up the circular dependecies and remaining issues
Changed: The []= operator for strings and sequences is now capable of splicing
This commit is contained in:
parent
22546c44d1
commit
e3deb5b502
11 changed files with 105 additions and 100 deletions
|
|
@ -327,7 +327,7 @@ type
|
|||
TMagic* = enum # symbols that require compiler magic:
|
||||
mNone, mDefined, mDefinedInScope, mLow, mHigh, mSizeOf, mIs, mOf,
|
||||
mEcho, mShallowCopy, mSlurp,
|
||||
mAstToYaml, mParseExprToAst, mParseStmtToAst, mExpandMacroToAst,
|
||||
mAstToYaml, mParseExprToAst, mParseStmtToAst, mExpandToAst,
|
||||
mUnaryLt, mSucc,
|
||||
mPred, mInc, mDec, mOrd, mNew, mNewFinalize, mNewSeq, mLengthOpenArray,
|
||||
mLengthStr, mLengthArray, mLengthSeq, mIncl, mExcl, mCard, mChr, mGCref,
|
||||
|
|
|
|||
|
|
@ -780,7 +780,66 @@ proc evalParseStmt(c: PEvalContext, n: PNode): PNode =
|
|||
code.stringStartingLine)
|
||||
result.typ = newType(tyStmt, c.module)
|
||||
|
||||
proc evalTemplateAux*(templ, actual: PNode, sym: PSym): PNode =
|
||||
case templ.kind
|
||||
of nkSym:
|
||||
var p = templ.sym
|
||||
if (p.kind == skParam) and (p.owner.id == sym.id):
|
||||
result = copyTree(actual.sons[p.position])
|
||||
else:
|
||||
result = copyNode(templ)
|
||||
of nkNone..nkIdent, nkType..nkNilLit: # atom
|
||||
result = copyNode(templ)
|
||||
else:
|
||||
result = copyNode(templ)
|
||||
newSons(result, sonsLen(templ))
|
||||
for i in countup(0, sonsLen(templ) - 1):
|
||||
result.sons[i] = evalTemplateAux(templ.sons[i], actual, sym)
|
||||
|
||||
proc evalTemplateArgs(n: PNode, s: PSym): PNode =
|
||||
var
|
||||
f, a: int
|
||||
arg: PNode
|
||||
|
||||
f = sonsLen(s.typ)
|
||||
|
||||
# if the template has zero arguments, it can be called without ``()``
|
||||
# `n` is then a nkSym or something similar
|
||||
case n.kind
|
||||
of nkCall, nkInfix, nkPrefix, nkPostfix, nkCommand, nkCallStrLit:
|
||||
a = sonsLen(n)
|
||||
else: a = 0
|
||||
|
||||
if a > f: GlobalError(n.info, errWrongNumberOfArguments)
|
||||
|
||||
result = copyNode(n)
|
||||
for i in countup(1, f - 1):
|
||||
if i < a:
|
||||
arg = n.sons[i]
|
||||
else:
|
||||
arg = copyTree(s.typ.n.sons[i].sym.ast)
|
||||
|
||||
addSon(result, arg)
|
||||
|
||||
var evalTemplateCounter = 0
|
||||
# to prevend endless recursion in templates instantation
|
||||
|
||||
proc evalTemplate(n: PNode, sym: PSym): PNode =
|
||||
inc(evalTemplateCounter)
|
||||
if evalTemplateCounter > 100:
|
||||
GlobalError(n.info, errTemplateInstantiationTooNested)
|
||||
|
||||
# replace each param by the corresponding node:
|
||||
var args = evalTemplateArgs(n, sym)
|
||||
result = evalTemplateAux(sym.ast.sons[codePos], args, sym)
|
||||
|
||||
dec(evalTemplateCounter)
|
||||
|
||||
proc evalMacroCall*(c: PEvalContext, n: PNode, sym: PSym): PNode =
|
||||
inc(evalTemplateCounter)
|
||||
if evalTemplateCounter > 100:
|
||||
GlobalError(n.info, errTemplateInstantiationTooNested)
|
||||
|
||||
var s = newStackFrame()
|
||||
s.call = n
|
||||
setlen(s.params, 2)
|
||||
|
|
@ -792,37 +851,26 @@ proc evalMacroCall*(c: PEvalContext, n: PNode, sym: PSym): PNode =
|
|||
popStackFrame(c)
|
||||
if cyclicTree(result): GlobalError(n.info, errCyclicTree)
|
||||
|
||||
# XXX:
|
||||
# These imports could be removed when the template evaluation code is extracted in a
|
||||
# separate module. semdata is needed only for PContext (which is not wanted here, see below)
|
||||
import
|
||||
semdata, sem
|
||||
dec(evalTemplateCounter)
|
||||
|
||||
proc evalExpandToAst(c: PEvalContext, original: PNode): PNode =
|
||||
var
|
||||
n = original.copyTree
|
||||
macroCall = n.sons[1]
|
||||
expandedSym = macroCall.sons[0].sym
|
||||
|
||||
# XXX: It's unfortunate that evalTemplate requires a PContext,
|
||||
# although it's used only for very specific corner cases.
|
||||
#
|
||||
# Template expansion should be about AST manipulation only, so
|
||||
# maybe this requirement can be lifted.
|
||||
dummyContext : PContext
|
||||
|
||||
for i in countup(1, macroCall.sonsLen - 1):
|
||||
macroCall.sons[i] = evalAux(c, macroCall.sons[i], {})
|
||||
|
||||
case expandedSym.kind
|
||||
of skTemplate:
|
||||
result = evalTemplate(dummyContext, macroCall, expandedSym)
|
||||
result = evalTemplate(macroCall, expandedSym)
|
||||
of skMacro:
|
||||
# XXX:
|
||||
# At this point macroCall.sons[0] is nkSym node.
|
||||
# To be completely compatible with normal macro invocation,
|
||||
# we may want to replace it with nkIdent node featuring
|
||||
# we want to replace it with nkIdent node featuring
|
||||
# the original unmangled macro name.
|
||||
macroCall.sons[0] = newIdentNode(expandedSym.name, expandedSym.info)
|
||||
result = evalMacroCall(c, macroCall, expandedSym)
|
||||
else:
|
||||
InternalError(macroCall.info,
|
||||
|
|
@ -854,7 +902,7 @@ proc evalMagicOrCall(c: PEvalContext, n: PNode): PNode =
|
|||
of mAppendSeqElem: result = evalAppendSeqElem(c, n)
|
||||
of mParseExprToAst: result = evalParseExpr(c, n)
|
||||
of mParseStmtToAst: result = evalParseStmt(c, n)
|
||||
of mExpandMacroToAst: result = evalExpandToAst(c, n)
|
||||
of mExpandToAst: result = evalExpandToAst(c, n)
|
||||
of mNLen:
|
||||
result = evalAux(c, n.sons[1], {efLValue})
|
||||
if isSpecial(result): return
|
||||
|
|
|
|||
|
|
@ -106,14 +106,10 @@ proc semAfterMacroCall(c: PContext, n: PNode, s: PSym): PNode =
|
|||
|
||||
proc semMacroExpr(c: PContext, n: PNode, sym: PSym,
|
||||
semCheck: bool = true): PNode =
|
||||
inc(evalTemplateCounter)
|
||||
if evalTemplateCounter > 100:
|
||||
GlobalError(n.info, errTemplateInstantiationTooNested)
|
||||
markUsed(n, sym)
|
||||
var p = newEvalContext(c.module, "", false)
|
||||
result = evalMacroCall(p, n, sym)
|
||||
if semCheck: result = semAfterMacroCall(c, result, sym)
|
||||
dec(evalTemplateCounter)
|
||||
|
||||
proc forceBool(c: PContext, n: PNode): PNode =
|
||||
result = fitNode(c, getSysType(tyBool), n)
|
||||
|
|
|
|||
|
|
@ -907,7 +907,7 @@ proc expectStringArg(c: PContext, n: PNode, i: int): PNode =
|
|||
proc isAstValue(n: PNode): bool =
|
||||
result = n.typ.sym.name.s in [ "expr", "stmt", "PNimrodNode" ]
|
||||
|
||||
proc semExpandMacroToAst(c: PContext, n: PNode, magicSym: PSym, flags: TExprFlags): PNode =
|
||||
proc semExpandToAst(c: PContext, n: PNode, magicSym: PSym, flags: TExprFlags): PNode =
|
||||
if sonsLen(n) == 2:
|
||||
if not isCallExpr(n.sons[1]):
|
||||
GlobalError(n.info, errXisNoMacroOrTemplate, n.renderTree)
|
||||
|
|
@ -925,29 +925,14 @@ proc semExpandMacroToAst(c: PContext, n: PNode, magicSym: PSym, flags: TExprFlag
|
|||
macroCall.sons[0].sym = expandedSym
|
||||
markUsed(n, expandedSym)
|
||||
|
||||
# Any macro arguments that are already AST values are passed as such
|
||||
# All other expressions within the arguments are converted to AST as
|
||||
# in normal macro/template expansion.
|
||||
# The actual expansion does not happen here, but in evals.nim, where
|
||||
# the dynamic AST values will be known.
|
||||
for i in countup(1, macroCall.sonsLen - 1):
|
||||
var argAst = macroCall.sons[i]
|
||||
var typedArg = semExprWithType(c, argAst, {efAllowType})
|
||||
if isAstValue(typedArg):
|
||||
macroCall.sons[i] = typedArg
|
||||
else:
|
||||
macroCall.sons[i] = newMetaNodeIT(argAst, argAst.info, newTypeS(tyExpr, c))
|
||||
macroCall.sons[i] = semExprWithType(c, macroCall.sons[i], {efAllowType})
|
||||
|
||||
# Preserve the magic symbol in order to handled in evals.nim
|
||||
n.sons[0] = newNodeI(nkSym, n.info)
|
||||
n.sons[0].sym = magicSym
|
||||
|
||||
# XXX:
|
||||
# Hmm, expandedSym.typ is something like proc (e: expr): stmt
|
||||
# In theory, it should be better here to report the actual return type,
|
||||
# but the code is working fine so far with tyStmt, so I am leaving it
|
||||
# here for someone more knowledgable to see ;)
|
||||
n.typ = newTypeS(tyStmt, c) # expandedSym.typ
|
||||
|
||||
n.typ = expandedSym.getReturnType
|
||||
|
||||
result = n
|
||||
else:
|
||||
|
|
@ -989,7 +974,7 @@ 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 mExpandMacroToAst: result = semExpandMacroToAst(c, n, s, flags)
|
||||
of mExpandToAst: result = semExpandToAst(c, n, s, flags)
|
||||
else: result = semDirectOp(c, n, flags)
|
||||
|
||||
proc semIfExpr(c: PContext, n: PNode): PNode =
|
||||
|
|
|
|||
|
|
@ -206,7 +206,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, mExpandMacroToAst,
|
||||
mParseExprToAst, mParseStmtToAst, mExpandToAst,
|
||||
mNLen..mNError, mEqRef:
|
||||
nil
|
||||
else: InternalError(a.info, "evalOp(" & $m & ')')
|
||||
|
|
|
|||
|
|
@ -440,7 +440,7 @@ proc semTry(c: PContext, n: PNode): PNode =
|
|||
var length = sonsLen(a)
|
||||
if a.kind == nkExceptBranch:
|
||||
if length == 2 and a.sons[0].kind == nkBracket:
|
||||
a.sons.splice(0, 1, a.sons[0].sons)
|
||||
a.sons[0..0] = a.sons[0].sons
|
||||
length = a.sonsLen
|
||||
|
||||
for j in countup(0, length - 2):
|
||||
|
|
|
|||
|
|
@ -33,22 +33,6 @@ proc isTypeDesc(n: PNode): bool =
|
|||
result = true
|
||||
else: result = false
|
||||
|
||||
proc evalTemplateAux(templ, actual: PNode, sym: PSym): PNode =
|
||||
case templ.kind
|
||||
of nkSym:
|
||||
var p = templ.sym
|
||||
if (p.kind == skParam) and (p.owner.id == sym.id):
|
||||
result = copyTree(actual.sons[p.position])
|
||||
else:
|
||||
result = copyNode(templ)
|
||||
of nkNone..nkIdent, nkType..nkNilLit: # atom
|
||||
result = copyNode(templ)
|
||||
else:
|
||||
result = copyNode(templ)
|
||||
newSons(result, sonsLen(templ))
|
||||
for i in countup(0, sonsLen(templ) - 1):
|
||||
result.sons[i] = evalTemplateAux(templ.sons[i], actual, sym)
|
||||
|
||||
var evalTemplateCounter: int = 0
|
||||
# to prevend endless recursion in templates instantation
|
||||
|
||||
|
|
|
|||
|
|
@ -925,6 +925,11 @@ proc computeSize(typ: PType): biggestInt =
|
|||
var a: biggestInt = 1
|
||||
result = computeSizeAux(typ, a)
|
||||
|
||||
proc getReturnType*(s: PSym): PType =
|
||||
# Obtains the return type of a iterator/proc/macro/template
|
||||
assert s.kind in { skProc, skTemplate, skMacro, skIterator }
|
||||
result = s.typ.n.sons[0].typ
|
||||
|
||||
proc getSize(typ: PType): biggestInt =
|
||||
result = computeSize(typ)
|
||||
if result < 0: InternalError("getSize(" & $typ.kind & ')')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue