Skip to content

Commit 3f03d9d

Browse files
committed
'assert' is now implemented without compiler magic
1 parent 140ad5d commit 3f03d9d

File tree

16 files changed

+66
-24
lines changed

16 files changed

+66
-24
lines changed

compiler/ast.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ type
370370
mConTArr, mConTT, mSlice,
371371
mFields, mFieldPairs,
372372
mAppendStrCh, mAppendStrStr, mAppendSeqElem,
373-
mInRange, mInSet, mRepr, mExit, mSetLengthStr, mSetLengthSeq, mAssert,
373+
mInRange, mInSet, mRepr, mExit, mSetLengthStr, mSetLengthSeq,
374+
mAssert, mAstToStr, mRand,
374375
mSwap, mIsNil, mArrToSeq, mCopyStr, mCopyStrLast,
375376
mNewString, mNewStringOfCap,
376377
mReset,

compiler/ccgexprs.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ proc genCheckedRecordField(p: BProc, e: PNode, d: var TLoc) =
607607
InternalError(e.info, "genCheckedRecordField") # generate the checks:
608608
for i in countup(1, sonsLen(e) - 1):
609609
it = e.sons[i]
610-
assert(it.kind == nkCall)
610+
assert(it.kind in nkCallKinds)
611611
assert(it.sons[0].kind == nkSym)
612612
op = it.sons[0].sym
613613
if op.magic == mNot: it = it.sons[1]
@@ -1403,7 +1403,7 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
14031403
of mIncl, mExcl, mCard, mLtSet, mLeSet, mEqSet, mMulSet, mPlusSet, mMinusSet,
14041404
mInSet:
14051405
genSetOp(p, e, d, op)
1406-
of mNewString, mNewStringOfCap, mCopyStr, mCopyStrLast, mExit:
1406+
of mNewString, mNewStringOfCap, mCopyStr, mCopyStrLast, mExit, mRand:
14071407
var opr = e.sons[0].sym
14081408
if lfNoDecl notin opr.loc.flags:
14091409
discard cgsym(p.module, opr.loc.r.ropeToStr)

compiler/evals.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,8 @@ proc evalAux(c: PEvalContext, n: PNode, flags: TEvalFlags): PNode =
11621162
of nkEmpty: result = n
11631163
of nkSym: result = evalSym(c, n, flags)
11641164
of nkType..nkNilLit: result = copyNode(n) # end of atoms
1165-
of nkCall, nkHiddenCallConv, nkMacroStmt, nkCommand, nkCallStrLit:
1165+
of nkCall, nkHiddenCallConv, nkMacroStmt, nkCommand, nkCallStrLit, nkInfix,
1166+
nkPrefix, nkPostfix:
11661167
result = evalMagicOrCall(c, n)
11671168
of nkCurly, nkBracket, nkRange:
11681169
# flags need to be passed here for mNAddMultiple :-(

compiler/msgs.nim

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ const
412412
PosErrorFormat* = "$1($2, $3) Error: $4"
413413
PosWarningFormat* = "$1($2, $3) Warning: $4"
414414
PosHintFormat* = "$1($2, $3) Hint: $4"
415+
PosContextFormat = "$1($2, $3) Info: $4"
415416
RawErrorFormat* = "Error: $1"
416417
RawWarningFormat* = "Warning: $1"
417418
RawHintFormat* = "Hint: $1"
@@ -536,10 +537,10 @@ proc writeContext(lastinfo: TLineInfo) =
536537
var info = lastInfo
537538
for i in countup(0, len(msgContext) - 1):
538539
if msgContext[i] != lastInfo and msgContext[i] != info:
539-
MsgWriteln(posErrorFormat % [toFilename(msgContext[i]),
540-
coordToStr(msgContext[i].line),
541-
coordToStr(msgContext[i].col),
542-
getMessageStr(errInstantiationFrom, "")])
540+
MsgWriteln(posContextFormat % [toFilename(msgContext[i]),
541+
coordToStr(msgContext[i].line),
542+
coordToStr(msgContext[i].col),
543+
getMessageStr(errInstantiationFrom, "")])
543544
info = msgContext[i]
544545

545546
proc rawMessage*(msg: TMsgKind, args: openarray[string]) =

compiler/semexprs.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,12 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
993993
result = semDirectOp(c, n, flags)
994994
of mSlurp: result = semSlurp(c, n, flags)
995995
of mExpandToAst: result = semExpandToAst(c, n, s, flags)
996+
of mAstToStr:
997+
if sonsLen(n) == 2:
998+
result = newStrNodeT(renderTree(n[1], {renderNoComments}), n)
999+
result.typ = getSysType(tyString)
1000+
else:
1001+
result = semDirectOp(c, n, flags)
9961002
else: result = semDirectOp(c, n, flags)
9971003

9981004
proc semIfExpr(c: PContext, n: PNode): PNode =

compiler/semfold.nim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ proc evalOp(m: TMagic, n, a, b, c: PNode): PNode =
209209
mParseExprToAst, mParseStmtToAst, mExpandToAst,
210210
mNLen..mNError, mEqRef:
211211
nil
212+
of mRand:
213+
result = newIntNodeT(math.random(a.getInt.int), n)
212214
else: InternalError(a.info, "evalOp(" & $m & ')')
213215

214216
proc getConstIfExpr(c: PSym, n: PNode): PNode =
@@ -440,6 +442,8 @@ proc getConstExpr(m: PSym, n: PNode): PNode =
440442
result = magicCall(m, n)
441443
of mIs:
442444
result = newIntNodeT(ord(sameType(n[1].typ, n[2].typ)), n)
445+
of mAstToStr:
446+
result = newStrNodeT(renderTree(n[1], {renderNoComments}), n)
443447
else:
444448
result = magicCall(m, n)
445449
except EOverflow:

compiler/semstmts.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ proc semFor(c: PContext, n: PNode): PNode =
404404
openScope(c.tab)
405405
n.sons[length-2] = semExprNoDeref(c, n.sons[length-2], {efWantIterator})
406406
var call = n.sons[length-2]
407-
if call.kind != nkCall or call.sons[0].kind != nkSym or
407+
if call.kind notin nkCallKinds or call.sons[0].kind != nkSym or
408408
call.sons[0].sym.kind != skIterator:
409409
GlobalError(n.sons[length - 2].info, errIteratorExpected)
410410
elif call.sons[0].sym.magic != mNone:

compiler/sigmatch.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ proc matchesAux*(c: PContext, n: PNode, m: var TCandidate,
597597
var f = 1 # iterates over formal parameters
598598
var a = 1 # iterates over the actual given arguments
599599
m.state = csMatch # until proven otherwise
600-
m.call = newNodeI(nkCall, n.info)
600+
m.call = newNodeI(n.kind, n.info)
601601
m.call.typ = base(m.callee) # may be nil
602602
var formalLen = sonsLen(m.callee.n)
603603
addSon(m.call, copyTree(n.sons[0]))

compiler/syntaxes.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ proc getParser(ident: PIdent): TParserKind =
117117
rawMessage(errInvalidDirectiveX, ident.s)
118118

119119
proc getCallee(n: PNode): PIdent =
120-
if (n.kind == nkCall) and (n.sons[0].kind == nkIdent):
120+
if n.kind in nkCallKinds and n.sons[0].kind == nkIdent:
121121
result = n.sons[0].ident
122122
elif n.kind == nkIdent:
123123
result = n.ident

compiler/transf.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ proc transformFor(c: PTransf, n: PNode): PTransNode =
468468
addVar(v, copyTree(n.sons[i])) # declare new vars
469469
add(result, v.ptransNode)
470470
var call = n.sons[length - 2]
471-
if call.kind != nkCall or call.sons[0].kind != nkSym:
471+
if call.kind notin nkCallKinds or call.sons[0].kind != nkSym:
472472
InternalError(call.info, "transformFor")
473473

474474
var newC = newTransCon(call.sons[0].sym)

0 commit comments

Comments
 (0)