Merge ../Nim into devel

This commit is contained in:
Charles Blake
2015-08-03 08:06:20 -04:00
22 changed files with 875 additions and 657 deletions

View File

@@ -6,7 +6,7 @@ Name: "Nim"
Version: "$version"
Platforms: """
windows: i386;amd64
linux: i386;amd64;powerpc64;arm;sparc;mips;powerpc
linux: i386;amd64;powerpc64;arm;sparc;mips;mipsel;powerpc;powerpc64el;arm64
macosx: i386;amd64;powerpc64
solaris: i386;amd64;sparc
freebsd: i386;amd64

View File

@@ -729,14 +729,13 @@ proc genBreakStmt(p: PProc, n: PNode) =
p.blocks[idx].id = abs(p.blocks[idx].id) # label is used
addf(p.body, "break L$1;$n" | "goto ::L$1::;$n", [rope(p.blocks[idx].id)])
proc genAsmStmt(p: PProc, n: PNode) =
proc genAsmOrEmitStmt(p: PProc, n: PNode) =
genLineDir(p, n)
assert(n.kind == nkAsmStmt)
for i in countup(0, sonsLen(n) - 1):
case n.sons[i].kind
of nkStrLit..nkTripleStrLit: add(p.body, n.sons[i].strVal)
of nkSym: add(p.body, mangleName(n.sons[i].sym))
else: internalError(n.sons[i].info, "jsgen: genAsmStmt()")
else: internalError(n.sons[i].info, "jsgen: genAsmOrEmitStmt()")
proc genIf(p: PProc, n: PNode, r: var TCompRes) =
var cond, stmt: TCompRes
@@ -1578,6 +1577,12 @@ proc genStmt(p: PProc, n: PNode) =
gen(p, n, r)
if r.res != nil: addf(p.body, "$#;$n", [r.res])
proc genPragma(p: PProc, n: PNode) =
for it in n.sons:
case whichPragma(it)
of wEmit: genAsmOrEmitStmt(p, it.sons[1])
else: discard
proc gen(p: PProc, n: PNode, r: var TCompRes) =
r.typ = etyNone
r.kind = resNone
@@ -1677,12 +1682,13 @@ proc gen(p: PProc, n: PNode, r: var TCompRes) =
if n.sons[0].kind != nkEmpty:
genLineDir(p, n)
gen(p, n.sons[0], r)
of nkAsmStmt: genAsmStmt(p, n)
of nkAsmStmt: genAsmOrEmitStmt(p, n)
of nkTryStmt: genTry(p, n, r)
of nkRaiseStmt: genRaiseStmt(p, n)
of nkTypeSection, nkCommentStmt, nkIteratorDef, nkIncludeStmt,
nkImportStmt, nkImportExceptStmt, nkExportStmt, nkExportExceptStmt,
nkFromStmt, nkTemplateDef, nkMacroDef, nkPragma: discard
nkFromStmt, nkTemplateDef, nkMacroDef: discard
of nkPragma: genPragma(p, n)
of nkProcDef, nkMethodDef, nkConverterDef:
var s = n.sons[namePos].sym
if {sfExportc, sfCompilerProc} * s.flags == {sfExportc}:

View File

@@ -158,8 +158,8 @@ type
TSystemCPU* = enum # Also add CPU for in initialization section and
# alias conditionals to condsyms (end of module).
cpuNone, cpuI386, cpuM68k, cpuAlpha, cpuPowerpc, cpuPowerpc64,
cpuSparc, cpuVm, cpuIa64, cpuAmd64, cpuMips, cpuArm,
cpuJS, cpuNimrodVM, cpuAVR
cpuPowerpc64el, cpuSparc, cpuVm, cpuIa64, cpuAmd64, cpuMips, cpuMipsel,
cpuArm, cpuArm64, cpuJS, cpuNimrodVM, cpuAVR
type
TEndian* = enum
@@ -175,12 +175,15 @@ const
(name: "alpha", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
(name: "powerpc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
(name: "powerpc64", intSize: 64, endian: bigEndian, floatSize: 64,bit: 64),
(name: "powerpc64el", intSize: 64, endian: littleEndian, floatSize: 64,bit: 64),
(name: "sparc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
(name: "vm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
(name: "ia64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
(name: "amd64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
(name: "mips", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
(name: "mipsel", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
(name: "arm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
(name: "arm64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
(name: "js", intSize: 32, endian: bigEndian,floatSize: 64,bit: 32),
(name: "nimrodvm", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
(name: "avr", intSize: 16, endian: littleEndian, floatSize: 32, bit: 16)]

View File

@@ -591,280 +591,282 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: int,
validPragmas: TSpecialWords): bool =
var it = n.sons[i]
var key = if it.kind == nkExprColonExpr: it.sons[0] else: it
if key.kind == nkIdent:
var userPragma = strTableGet(c.userPragmas, key.ident)
if userPragma != nil:
inc c.instCounter
if c.instCounter > 100:
globalError(it.info, errRecursiveDependencyX, userPragma.name.s)
pragma(c, sym, userPragma.ast, validPragmas)
# ensure the pragma is also remember for generic instantiations in other
# modules:
n.sons[i] = userPragma.ast
dec c.instCounter
else:
var k = whichKeyword(key.ident)
if k in validPragmas:
case k
of wExportc:
makeExternExport(sym, getOptionalStr(c, it, "$1"), it.info)
incl(sym.flags, sfUsed) # avoid wrong hints
of wImportc: makeExternImport(sym, getOptionalStr(c, it, "$1"))
of wImportCompilerProc:
processImportCompilerProc(sym, getOptionalStr(c, it, "$1"))
of wExtern: setExternName(sym, expectStrLit(c, it))
of wImmediate:
if sym.kind in {skTemplate, skMacro}: incl(sym.flags, sfImmediate)
else: invalidPragma(it)
of wDirty:
if sym.kind == skTemplate: incl(sym.flags, sfDirty)
else: invalidPragma(it)
of wImportCpp:
processImportCpp(sym, getOptionalStr(c, it, "$1"))
of wImportObjC:
processImportObjC(sym, getOptionalStr(c, it, "$1"))
of wAlign:
if sym.typ == nil: invalidPragma(it)
var align = expectIntLit(c, it)
if (not isPowerOfTwo(align) and align != 0) or align >% high(int16):
localError(it.info, errPowerOfTwoExpected)
else:
sym.typ.align = align.int16
of wSize:
if sym.typ == nil: invalidPragma(it)
var size = expectIntLit(c, it)
if not isPowerOfTwo(size) or size <= 0 or size > 8:
localError(it.info, errPowerOfTwoExpected)
else:
sym.typ.size = size
of wNodecl:
noVal(it)
incl(sym.loc.flags, lfNoDecl)
of wPure, wAsmNoStackFrame:
noVal(it)
if sym != nil:
if k == wPure and sym.kind in routineKinds: invalidPragma(it)
else: incl(sym.flags, sfPure)
of wVolatile:
noVal(it)
incl(sym.flags, sfVolatile)
of wRegister:
noVal(it)
incl(sym.flags, sfRegister)
of wThreadVar:
noVal(it)
incl(sym.flags, sfThread)
of wDeadCodeElim: pragmaDeadCodeElim(c, it)
of wNoForward: pragmaNoForward(c, it)
of wMagic: processMagic(c, it, sym)
of wCompileTime:
noVal(it)
incl(sym.flags, sfCompileTime)
incl(sym.loc.flags, lfNoDecl)
of wGlobal:
noVal(it)
incl(sym.flags, sfGlobal)
incl(sym.flags, sfPure)
of wMerge:
# only supported for backwards compat, doesn't do anything anymore
noVal(it)
of wConstructor:
noVal(it)
incl(sym.flags, sfConstructor)
of wHeader:
var lib = getLib(c, libHeader, getStrLitNode(c, it))
addToLib(lib, sym)
incl(sym.flags, sfImportc)
incl(sym.loc.flags, lfHeader)
incl(sym.loc.flags, lfNoDecl)
# implies nodecl, because otherwise header would not make sense
if sym.loc.r == nil: sym.loc.r = rope(sym.name.s)
of wDestructor:
sym.flags.incl sfOverriden
if sym.name.s.normalize != "destroy":
localError(n.info, errGenerated, "destructor has to be named 'destroy'")
of wOverride:
sym.flags.incl sfOverriden
of wNosideeffect:
noVal(it)
incl(sym.flags, sfNoSideEffect)
if sym.typ != nil: incl(sym.typ.flags, tfNoSideEffect)
of wSideeffect:
noVal(it)
incl(sym.flags, sfSideEffect)
of wNoreturn:
noVal(it)
incl(sym.flags, sfNoReturn)
of wDynlib:
processDynLib(c, it, sym)
of wCompilerproc:
noVal(it) # compilerproc may not get a string!
if sfFromGeneric notin sym.flags: markCompilerProc(sym)
of wProcVar:
noVal(it)
incl(sym.flags, sfProcvar)
of wDeprecated:
if it.kind == nkExprColonExpr: deprecatedStmt(c, it)
elif sym != nil: incl(sym.flags, sfDeprecated)
else: incl(c.module.flags, sfDeprecated)
of wVarargs:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfVarargs)
of wBorrow:
if sym.kind == skType:
typeBorrow(sym, it)
else:
noVal(it)
incl(sym.flags, sfBorrow)
of wFinal:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfFinal)
of wInheritable:
noVal(it)
if sym.typ == nil or tfFinal in sym.typ.flags: invalidPragma(it)
else: incl(sym.typ.flags, tfInheritable)
of wAcyclic:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfAcyclic)
of wShallow:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfShallow)
of wThread:
noVal(it)
incl(sym.flags, sfThread)
incl(sym.flags, sfProcvar)
if sym.typ != nil: incl(sym.typ.flags, tfThread)
of wGcSafe:
noVal(it)
if sym.kind != skType: incl(sym.flags, sfThread)
if sym.typ != nil: incl(sym.typ.flags, tfGcSafe)
else: invalidPragma(it)
of wPacked:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfPacked)
of wHint: message(it.info, hintUser, expectStrLit(c, it))
of wWarning: message(it.info, warnUser, expectStrLit(c, it))
of wError:
if sym != nil and sym.isRoutine:
# This is subtle but correct: the error *statement* is only
# allowed for top level statements. Seems to be easier than
# distinguishing properly between
# ``proc p() {.error}`` and ``proc p() = {.error: "msg".}``
noVal(it)
incl(sym.flags, sfError)
else:
localError(it.info, errUser, expectStrLit(c, it))
of wFatal: fatal(it.info, errUser, expectStrLit(c, it))
of wDefine: processDefine(c, it)
of wUndef: processUndef(c, it)
of wCompile: processCompile(c, it)
of wLink: processCommonLink(c, it, linkNormal)
of wLinksys: processCommonLink(c, it, linkSys)
of wPassl: extccomp.addLinkOption(expectStrLit(c, it))
of wPassc: extccomp.addCompileOption(expectStrLit(c, it))
of wBreakpoint: pragmaBreakpoint(c, it)
of wWatchPoint: pragmaWatchpoint(c, it)
of wPush:
processPush(c, n, i + 1)
result = true
of wPop: processPop(c, it)
of wPragma:
processPragma(c, n, i)
result = true
of wDiscardable:
noVal(it)
if sym != nil: incl(sym.flags, sfDiscardable)
of wNoInit:
noVal(it)
if sym != nil: incl(sym.flags, sfNoInit)
of wCodegenDecl: processCodegenDecl(c, it, sym)
of wChecks, wObjChecks, wFieldChecks, wRangechecks, wBoundchecks,
wOverflowchecks, wNilchecks, wAssertions, wWarnings, wHints,
wLinedir, wStacktrace, wLinetrace, wOptimization,
wCallconv,
wDebugger, wProfiler, wFloatchecks, wNanChecks, wInfChecks,
wPatterns:
if processOption(c, it):
# calling conventions (boring...):
localError(it.info, errOptionExpected)
of FirstCallConv..LastCallConv:
assert(sym != nil)
if sym.typ == nil: invalidPragma(it)
else: sym.typ.callConv = wordToCallConv(k)
of wEmit: pragmaEmit(c, it)
of wUnroll: pragmaUnroll(c, it)
of wLinearScanEnd, wComputedGoto: noVal(it)
of wEffects:
# is later processed in effect analysis:
noVal(it)
of wIncompleteStruct:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfIncompleteStruct)
of wUnchecked:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfUncheckedArray)
of wUnion:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfUnion)
of wRequiresInit:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfNeedsInit)
of wByRef:
noVal(it)
if sym == nil or sym.typ == nil:
if processOption(c, it): localError(it.info, errOptionExpected)
else:
incl(sym.typ.flags, tfByRef)
of wByCopy:
noVal(it)
if sym.kind != skType or sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfByCopy)
of wInject, wGensym:
# We check for errors, but do nothing with these pragmas otherwise
# as they are handled directly in 'evalTemplate'.
noVal(it)
if sym == nil: invalidPragma(it)
of wLine: pragmaLine(c, it)
of wRaises, wTags: pragmaRaisesOrTags(c, it)
of wLocks:
if sym == nil: pragmaLockStmt(c, it)
elif sym.typ == nil: invalidPragma(it)
else: sym.typ.lockLevel = pragmaLocks(c, it)
of wGuard:
if sym == nil or sym.kind notin {skVar, skLet, skField}:
invalidPragma(it)
else:
sym.guard = pragmaGuard(c, it, sym.kind)
of wGoto:
if sym == nil or sym.kind notin {skVar, skLet}:
invalidPragma(it)
else:
sym.flags.incl sfGoto
of wInjectStmt:
if it.kind != nkExprColonExpr:
localError(it.info, errExprExpected)
else:
it.sons[1] = c.semExpr(c, it.sons[1])
of wExperimental:
noVal(it)
if isTopLevel(c):
c.module.flags.incl sfExperimental
else:
localError(it.info, "'experimental' pragma only valid as toplevel statement")
of wNoRewrite:
noVal(it)
if key.kind == nkBracketExpr:
processNote(c, it)
return
let ident = considerQuotedIdent(key)
var userPragma = strTableGet(c.userPragmas, ident)
if userPragma != nil:
inc c.instCounter
if c.instCounter > 100:
globalError(it.info, errRecursiveDependencyX, userPragma.name.s)
pragma(c, sym, userPragma.ast, validPragmas)
# ensure the pragma is also remember for generic instantiations in other
# modules:
n.sons[i] = userPragma.ast
dec c.instCounter
else:
var k = whichKeyword(ident)
if k in validPragmas:
case k
of wExportc:
makeExternExport(sym, getOptionalStr(c, it, "$1"), it.info)
incl(sym.flags, sfUsed) # avoid wrong hints
of wImportc: makeExternImport(sym, getOptionalStr(c, it, "$1"))
of wImportCompilerProc:
processImportCompilerProc(sym, getOptionalStr(c, it, "$1"))
of wExtern: setExternName(sym, expectStrLit(c, it))
of wImmediate:
if sym.kind in {skTemplate, skMacro}: incl(sym.flags, sfImmediate)
else: invalidPragma(it)
of wDirty:
if sym.kind == skTemplate: incl(sym.flags, sfDirty)
else: invalidPragma(it)
of wImportCpp:
processImportCpp(sym, getOptionalStr(c, it, "$1"))
of wImportObjC:
processImportObjC(sym, getOptionalStr(c, it, "$1"))
of wAlign:
if sym.typ == nil: invalidPragma(it)
var align = expectIntLit(c, it)
if (not isPowerOfTwo(align) and align != 0) or align >% high(int16):
localError(it.info, errPowerOfTwoExpected)
else:
sym.typ.align = align.int16
of wSize:
if sym.typ == nil: invalidPragma(it)
var size = expectIntLit(c, it)
if not isPowerOfTwo(size) or size <= 0 or size > 8:
localError(it.info, errPowerOfTwoExpected)
else:
sym.typ.size = size
of wNodecl:
noVal(it)
incl(sym.loc.flags, lfNoDecl)
of wPure, wAsmNoStackFrame:
noVal(it)
if sym != nil:
if k == wPure and sym.kind in routineKinds: invalidPragma(it)
else: incl(sym.flags, sfPure)
of wVolatile:
noVal(it)
incl(sym.flags, sfVolatile)
of wRegister:
noVal(it)
incl(sym.flags, sfRegister)
of wThreadVar:
noVal(it)
incl(sym.flags, sfThread)
of wDeadCodeElim: pragmaDeadCodeElim(c, it)
of wNoForward: pragmaNoForward(c, it)
of wMagic: processMagic(c, it, sym)
of wCompileTime:
noVal(it)
incl(sym.flags, sfCompileTime)
incl(sym.loc.flags, lfNoDecl)
of wGlobal:
noVal(it)
incl(sym.flags, sfGlobal)
incl(sym.flags, sfPure)
of wMerge:
# only supported for backwards compat, doesn't do anything anymore
noVal(it)
of wConstructor:
noVal(it)
incl(sym.flags, sfConstructor)
of wHeader:
var lib = getLib(c, libHeader, getStrLitNode(c, it))
addToLib(lib, sym)
incl(sym.flags, sfImportc)
incl(sym.loc.flags, lfHeader)
incl(sym.loc.flags, lfNoDecl)
# implies nodecl, because otherwise header would not make sense
if sym.loc.r == nil: sym.loc.r = rope(sym.name.s)
of wDestructor:
sym.flags.incl sfOverriden
if sym.name.s.normalize != "destroy":
localError(n.info, errGenerated, "destructor has to be named 'destroy'")
of wOverride:
sym.flags.incl sfOverriden
of wNosideeffect:
noVal(it)
incl(sym.flags, sfNoSideEffect)
if sym.typ != nil: incl(sym.typ.flags, tfNoSideEffect)
of wSideeffect:
noVal(it)
incl(sym.flags, sfSideEffect)
of wNoreturn:
noVal(it)
incl(sym.flags, sfNoReturn)
of wDynlib:
processDynLib(c, it, sym)
of wCompilerproc:
noVal(it) # compilerproc may not get a string!
if sfFromGeneric notin sym.flags: markCompilerProc(sym)
of wProcVar:
noVal(it)
incl(sym.flags, sfProcvar)
of wDeprecated:
if it.kind == nkExprColonExpr: deprecatedStmt(c, it)
elif sym != nil: incl(sym.flags, sfDeprecated)
else: incl(c.module.flags, sfDeprecated)
of wVarargs:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfVarargs)
of wBorrow:
if sym.kind == skType:
typeBorrow(sym, it)
else:
noVal(it)
incl(sym.flags, sfBorrow)
of wFinal:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfFinal)
of wInheritable:
noVal(it)
if sym.typ == nil or tfFinal in sym.typ.flags: invalidPragma(it)
else: incl(sym.typ.flags, tfInheritable)
of wAcyclic:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfAcyclic)
of wShallow:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfShallow)
of wThread:
noVal(it)
incl(sym.flags, sfThread)
incl(sym.flags, sfProcvar)
if sym.typ != nil: incl(sym.typ.flags, tfThread)
of wGcSafe:
noVal(it)
if sym.kind != skType: incl(sym.flags, sfThread)
if sym.typ != nil: incl(sym.typ.flags, tfGcSafe)
else: invalidPragma(it)
of wPacked:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfPacked)
of wHint: message(it.info, hintUser, expectStrLit(c, it))
of wWarning: message(it.info, warnUser, expectStrLit(c, it))
of wError:
if sym != nil and sym.isRoutine:
# This is subtle but correct: the error *statement* is only
# allowed for top level statements. Seems to be easier than
# distinguishing properly between
# ``proc p() {.error}`` and ``proc p() = {.error: "msg".}``
noVal(it)
incl(sym.flags, sfError)
else:
localError(it.info, errUser, expectStrLit(c, it))
of wFatal: fatal(it.info, errUser, expectStrLit(c, it))
of wDefine: processDefine(c, it)
of wUndef: processUndef(c, it)
of wCompile: processCompile(c, it)
of wLink: processCommonLink(c, it, linkNormal)
of wLinksys: processCommonLink(c, it, linkSys)
of wPassl: extccomp.addLinkOption(expectStrLit(c, it))
of wPassc: extccomp.addCompileOption(expectStrLit(c, it))
of wBreakpoint: pragmaBreakpoint(c, it)
of wWatchPoint: pragmaWatchpoint(c, it)
of wPush:
processPush(c, n, i + 1)
result = true
of wPop: processPop(c, it)
of wPragma:
processPragma(c, n, i)
result = true
of wDiscardable:
noVal(it)
if sym != nil: incl(sym.flags, sfDiscardable)
of wNoInit:
noVal(it)
if sym != nil: incl(sym.flags, sfNoInit)
of wCodegenDecl: processCodegenDecl(c, it, sym)
of wChecks, wObjChecks, wFieldChecks, wRangechecks, wBoundchecks,
wOverflowchecks, wNilchecks, wAssertions, wWarnings, wHints,
wLinedir, wStacktrace, wLinetrace, wOptimization,
wCallconv,
wDebugger, wProfiler, wFloatchecks, wNanChecks, wInfChecks,
wPatterns:
if processOption(c, it):
# calling conventions (boring...):
localError(it.info, errOptionExpected)
of FirstCallConv..LastCallConv:
assert(sym != nil)
if sym.typ == nil: invalidPragma(it)
else: sym.typ.callConv = wordToCallConv(k)
of wEmit: pragmaEmit(c, it)
of wUnroll: pragmaUnroll(c, it)
of wLinearScanEnd, wComputedGoto: noVal(it)
of wEffects:
# is later processed in effect analysis:
noVal(it)
of wIncompleteStruct:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfIncompleteStruct)
of wUnchecked:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfUncheckedArray)
of wUnion:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfUnion)
of wRequiresInit:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfNeedsInit)
of wByRef:
noVal(it)
if sym == nil or sym.typ == nil:
if processOption(c, it): localError(it.info, errOptionExpected)
else:
incl(sym.typ.flags, tfByRef)
of wByCopy:
noVal(it)
if sym.kind != skType or sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfByCopy)
of wInject, wGensym:
# We check for errors, but do nothing with these pragmas otherwise
# as they are handled directly in 'evalTemplate'.
noVal(it)
if sym == nil: invalidPragma(it)
of wLine: pragmaLine(c, it)
of wRaises, wTags: pragmaRaisesOrTags(c, it)
of wLocks:
if sym == nil: pragmaLockStmt(c, it)
elif sym.typ == nil: invalidPragma(it)
else: sym.typ.lockLevel = pragmaLocks(c, it)
of wGuard:
if sym == nil or sym.kind notin {skVar, skLet, skField}:
invalidPragma(it)
else:
sym.guard = pragmaGuard(c, it, sym.kind)
of wGoto:
if sym == nil or sym.kind notin {skVar, skLet}:
invalidPragma(it)
else:
sym.flags.incl sfGoto
of wInjectStmt:
if it.kind != nkExprColonExpr:
localError(it.info, errExprExpected)
else:
it.sons[1] = c.semExpr(c, it.sons[1])
of wExperimental:
noVal(it)
if isTopLevel(c):
c.module.flags.incl sfExperimental
else:
localError(it.info, "'experimental' pragma only valid as toplevel statement")
of wNoRewrite:
noVal(it)
else: invalidPragma(it)
else: processNote(c, it)
else: invalidPragma(it)
proc implicitPragmas*(c: PContext, sym: PSym, n: PNode,
validPragmas: TSpecialWords) =

View File

@@ -171,11 +171,15 @@ proc newSymS(kind: TSymKind, n: PNode, c: PContext): PSym =
result = newSym(kind, considerQuotedIdent(n), getCurrOwner(), n.info)
proc newSymG*(kind: TSymKind, n: PNode, c: PContext): PSym =
proc `$`(kind: TSymKind): string = substr(system.`$`(kind), 2).toLower
# like newSymS, but considers gensym'ed symbols
if n.kind == nkSym:
# and sfGenSym in n.sym.flags:
result = n.sym
internalAssert result.kind == kind
if result.kind != kind:
localError(n.info, "cannot use symbol of kind '" &
$result.kind & "' as a '" & $kind & "'")
# when there is a nested proc inside a template, semtmpl
# will assign a wrong owner during the first pass over the
# template; we must fix it here: see #909

View File

@@ -307,12 +307,12 @@ proc evalOp(m: TMagic, n, a, b, c: PNode): PNode =
of mToU8: result = newIntNodeT(getInt(a) and 0x000000FF, n)
of mToU16: result = newIntNodeT(getInt(a) and 0x0000FFFF, n)
of mToU32: result = newIntNodeT(getInt(a) and 0x00000000FFFFFFFF'i64, n)
of mUnaryLt: result = newIntNodeT(getOrdValue(a) - 1, n)
of mSucc: result = newIntNodeT(getOrdValue(a) + getInt(b), n)
of mPred: result = newIntNodeT(getOrdValue(a) - getInt(b), n)
of mAddI: result = newIntNodeT(getInt(a) + getInt(b), n)
of mSubI: result = newIntNodeT(getInt(a) - getInt(b), n)
of mMulI: result = newIntNodeT(getInt(a) * getInt(b), n)
of mUnaryLt: result = newIntNodeT(getOrdValue(a) |-| 1, n)
of mSucc: result = newIntNodeT(getOrdValue(a) |+| getInt(b), n)
of mPred: result = newIntNodeT(getOrdValue(a) |-| getInt(b), n)
of mAddI: result = newIntNodeT(getInt(a) |+| getInt(b), n)
of mSubI: result = newIntNodeT(getInt(a) |-| getInt(b), n)
of mMulI: result = newIntNodeT(getInt(a) |*| getInt(b), n)
of mMinI:
if getInt(a) > getInt(b): result = newIntNodeT(getInt(b), n)
else: result = newIntNodeT(getInt(a), n)
@@ -338,11 +338,11 @@ proc evalOp(m: TMagic, n, a, b, c: PNode): PNode =
of mDivI:
let y = getInt(b)
if y != 0:
result = newIntNodeT(getInt(a) div y, n)
result = newIntNodeT(`|div|`(getInt(a), y), n)
of mModI:
let y = getInt(b)
if y != 0:
result = newIntNodeT(getInt(a) mod y, n)
result = newIntNodeT(`|mod|`(getInt(a), y), n)
of mAddF64: result = newFloatNodeT(getFloat(a) + getFloat(b), n)
of mSubF64: result = newFloatNodeT(getFloat(a) - getFloat(b), n)
of mMulF64: result = newFloatNodeT(getFloat(a) * getFloat(b), n)

View File

@@ -23,6 +23,9 @@ proc newConstraint(c: PContext, k: TTypeKind): PType =
proc semEnum(c: PContext, n: PNode, prev: PType): PType =
if n.sonsLen == 0: return newConstraint(c, tyEnum)
elif n.sonsLen == 1:
# don't create an empty tyEnum; fixes #3052
return errorType(c)
var
counter, x: BiggestInt
e: PSym

View File

@@ -120,10 +120,10 @@ template decodeBx(k: expr) {.immediate, dirty.} =
template move(a, b: expr) {.immediate, dirty.} = system.shallowCopy(a, b)
# XXX fix minor 'shallowCopy' overloading bug in compiler
proc createStrKeepNode(x: var TFullReg) =
proc createStrKeepNode(x: var TFullReg; keepNode=true) =
if x.node.isNil:
x.node = newNode(nkStrLit)
elif x.node.kind == nkNilLit:
elif x.node.kind == nkNilLit and keepNode:
when defined(useNodeIds):
let id = x.node.id
system.reset(x.node[])
@@ -385,6 +385,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
#if c.traceActive:
# echo "PC ", pc, " ", c.code[pc].opcode, " ra ", ra
# message(c.debug[pc], warnUser, "Trace")
case instr.opcode
of opcEof: return regs[ra]
of opcRet:
@@ -407,8 +408,8 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
decodeB(rkInt)
regs[ra].intVal = regs[rb].intVal
of opcAsgnStr:
decodeB(rkNode)
createStrKeepNode regs[ra]
decodeBC(rkNode)
createStrKeepNode regs[ra], rc != 0
regs[ra].node.strVal = regs[rb].node.strVal
of opcAsgnFloat:
decodeB(rkFloat)
@@ -509,8 +510,10 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
of rkNode:
if regs[rb].node.kind == nkNilLit:
stackTrace(c, tos, pc, errNilAccess)
assert regs[rb].node.kind == nkRefTy
regs[ra].node = regs[rb].node.sons[0]
if regs[rb].node.kind == nkRefTy:
regs[ra].node = regs[rb].node.sons[0]
else:
stackTrace(c, tos, pc, errGenerated, "limited VM support for 'ref'")
else:
stackTrace(c, tos, pc, errNilAccess)
of opcWrDeref:

View File

@@ -74,8 +74,9 @@ proc codeListing(c: PCtx, result: var string, start=0; last = -1) =
result.addf("\t$#\tr$#, L$#", ($opc).substr(3), x.regA,
i+x.regBx-wordExcess)
elif opc in {opcLdConst, opcAsgnConst}:
result.addf("\t$#\tr$#, $#", ($opc).substr(3), x.regA,
c.constants[x.regBx-wordExcess].renderTree)
let idx = x.regBx-wordExcess
result.addf("\t$#\tr$#, $# ($#)", ($opc).substr(3), x.regA,
c.constants[idx].renderTree, $idx)
elif opc in {opcMarshalLoad, opcMarshalStore}:
let y = c.code[i+1]
result.addf("\t$#\tr$#, r$#, $#", ($opc).substr(3), x.regA, x.regB,
@@ -184,7 +185,7 @@ proc getTemp(cc: PCtx; typ: PType): TRegister =
return TRegister(i)
# if register pressure is high, we re-use more aggressively:
if c.maxSlots >= HighRegisterPressure:
if c.maxSlots >= HighRegisterPressure and false:
for i in 0 .. c.maxSlots-1:
if not c.slots[i].inUse:
c.slots[i] = (inUse: true, kind: k)
@@ -1073,6 +1074,7 @@ const
tyUInt, tyUInt8, tyUInt16, tyUInt32, tyUInt64}
proc fitsRegister*(t: PType): bool =
assert t != nil
t.skipTypes(abstractInst-{tyTypeDesc}).kind in {
tyRange, tyEnum, tyBool, tyInt..tyUInt64, tyChar}
@@ -1110,6 +1112,7 @@ proc genAddrDeref(c: PCtx; n: PNode; dest: var TDest; opc: TOpcode;
if dest < 0: dest = c.getTemp(n.typ)
if not isAddr:
gABC(c, n, opc, dest, tmp)
assert n.typ != nil
if gfAddrOf notin flags and fitsRegister(n.typ):
c.gABC(n, opcNodeToReg, dest, dest)
elif c.prc.slots[tmp].kind >= slotTempUnknown:
@@ -1143,7 +1146,7 @@ proc whichAsgnOpc(n: PNode; opc: TOpcode): TOpcode = opc
proc genAsgn(c: PCtx; dest: TDest; ri: PNode; requiresCopy: bool) =
let tmp = c.genx(ri)
assert dest >= 0
gABC(c, ri, whichAsgnOpc(ri), dest, tmp)
gABC(c, ri, whichAsgnOpc(ri), dest, tmp, 1-ord(requiresCopy))
c.freeTemp(tmp)
proc setSlot(c: PCtx; v: PSym) =
@@ -1194,9 +1197,10 @@ proc preventFalseAlias(c: PCtx; n: PNode; opc: TOpcode;
# opcLdObj et al really means "load address". We sometimes have to create a
# copy in order to not introduce false aliasing:
# mylocal = a.b # needs a copy of the data!
assert n.typ != nil
if needsAdditionalCopy(n):
var cc = c.getTemp(n.typ)
c.gABC(n, whichAsgnOpc(n), cc, value)
c.gABC(n, whichAsgnOpc(n), cc, value, 0)
c.gABC(n, opc, dest, idx, cc)
c.freeTemp(cc)
else:
@@ -1241,10 +1245,11 @@ proc genAsgn(c: PCtx; le, ri: PNode; requiresCopy: bool) =
internalAssert s.position > 0 or (s.position == 0 and
s.kind in {skParam,skResult})
var dest: TRegister = s.position + ord(s.kind == skParam)
assert le.typ != nil
if needsAdditionalCopy(le) and s.kind in {skResult, skVar, skParam}:
var cc = c.getTemp(le.typ)
gen(c, ri, cc)
c.gABC(le, whichAsgnOpc(le), dest, cc)
c.gABC(le, whichAsgnOpc(le), dest, cc, 1)
c.freeTemp(cc)
else:
gen(c, ri, dest)
@@ -1303,6 +1308,7 @@ proc genRdVar(c: PCtx; n: PNode; dest: var TDest; flags: TGenFlags) =
if sfImportc in s.flags: c.importcSym(n.info, s)
else: genGlobalInit(c, n, s)
if dest < 0: dest = c.getTemp(n.typ)
assert s.typ != nil
if gfAddrOf notin flags and fitsRegister(s.typ):
var cc = c.getTemp(n.typ)
c.gABx(n, opcLdGlobal, cc, s.position)
@@ -1426,6 +1432,7 @@ proc getNullValue(typ: PType, info: TLineInfo): PNode =
globalError(info, "cannot create null element for: " & $t.kind)
proc ldNullOpcode(t: PType): TOpcode =
assert t != nil
if fitsRegister(t): opcLdNullReg else: opcLdNull
proc genVarSection(c: PCtx; n: PNode) =
@@ -1453,7 +1460,7 @@ proc genVarSection(c: PCtx; n: PNode) =
if a.sons[2].kind != nkEmpty:
let tmp = c.genx(a.sons[0], {gfAddrOf})
let val = c.genx(a.sons[2])
c.preventFalseAlias(a, opcWrDeref, tmp, 0, val)
c.preventFalseAlias(a.sons[2], opcWrDeref, tmp, 0, val)
c.freeTemp(val)
c.freeTemp(tmp)
else:
@@ -1461,13 +1468,15 @@ proc genVarSection(c: PCtx; n: PNode) =
if a.sons[2].kind == nkEmpty:
c.gABx(a, ldNullOpcode(s.typ), s.position, c.genType(s.typ))
else:
assert s.typ != nil
if not fitsRegister(s.typ):
c.gABx(a, ldNullOpcode(s.typ), s.position, c.genType(s.typ))
let le = a.sons[0]
assert le.typ != nil
if not fitsRegister(le.typ) and s.kind in {skResult, skVar, skParam}:
var cc = c.getTemp(le.typ)
gen(c, a.sons[2], cc)
c.gABC(le, whichAsgnOpc(le), s.position.TRegister, cc)
c.gABC(le, whichAsgnOpc(le), s.position.TRegister, cc, 1)
c.freeTemp(cc)
else:
gen(c, a.sons[2], s.position.TRegister)
@@ -1694,7 +1703,7 @@ proc gen(c: PCtx; n: PNode; dest: var TDest; flags: TGenFlags = {}) =
c.freeTemp(tmp1)
c.freeTemp(tmp2)
if dest >= 0:
gABC(c, n, whichAsgnOpc(n), dest, tmp0)
gABC(c, n, whichAsgnOpc(n), dest, tmp0, 1)
c.freeTemp(tmp0)
else:
dest = tmp0

View File

@@ -142,7 +142,7 @@ When contributing new procedures, be sure to add documentation, especially if
the procedure is exported from the module. Documentation begins on the line
following the ``proc`` definition, and is prefixed by ``##`` on each line.
Code examples are also encouraged. The RestructuredText Nim uses has a special
Code examples are also encouraged. The RestructuredText Nim uses has a special
syntax for including examples.
.. code-block:: nim
@@ -155,8 +155,8 @@ syntax for including examples.
## echo someproc() # "something"
result = "something" # single-hash comments do not produce documentation
The ``.. code-block:: nim`` followed by a newline and an indentation instructs the
``nim doc`` and ``nim doc2`` commands to produce syntax-highlighted example code with
The ``.. code-block:: nim`` followed by a newline and an indentation instructs the
``nim doc`` and ``nim doc2`` commands to produce syntax-highlighted example code with
the documentation.
When forward declaration is used, the documentation should be included with the
@@ -186,7 +186,7 @@ or
proc hello*(): string =
# says hello
result = "hello"
the first is preferred.
The Git stuff
@@ -216,3 +216,5 @@ General commit rules
git diff --check --cached || exit $?
3. Describe your commit and use your common sense.
.. include:: styleguide.rst

View File

@@ -588,7 +588,8 @@ can also be defined with indentation instead of ``[]``:
Objects provide many features that tuples do not. Object provide inheritance
and information hiding. Objects have access to their type at runtime, so that
the ``of`` operator can be used to determine the object's type.
the ``of`` operator can be used to determine the object's type. The ``of`` operator
is similar to the ``instanceof`` operator in Java.
.. code-block:: nim
type

140
docstyle.rst Normal file
View File

@@ -0,0 +1,140 @@
Documentation Style
===================
General Guidelines
------------------
* Authors should document anything that is exported.
* Within documentation, a period (`.`) should follow each sentence (or sentence fragment) in a comment block. The documentation may be limited to one sentence fragment, but if multiple sentences are within the documentation, each sentence after the first should be complete and in present tense.
* Documentation is parsed as ReStructuredText (RST).
* Inline code should be surrounded by double tick marks ("``````"). If you would like a character to immediately follow inline code (e.g., "``int8``s are great!"), escape the following character with a backslash (``\``). The preceding is typed as ``` ``int8``\s are great!```.
Module-level documentation
--------------------------
Documentation of a module is placed at the top of the module itself. Each line of documentation begins with double hashes (``##``).
Code samples are encouraged, and should follow the general RST syntax:
.. code-block:: Nim
## The ``universe`` module computes the answer to life, the universe, and everything.
##
## .. code-block:: Nim
## echo computeAnswerString() # "42"
Within this top-level comment, you can indicate the authorship and copyright of the code, which will be featured in the produced documentation.
.. code-block:: Nim
## This is the best module ever. It provides answers to everything!
##
## :Author: Steve McQueen
## :Copyright: 1965
##
Leave a space between the last line of top-level documentation and the beginning of Nim code (the imports, etc.).
Procs, Templates, Macros, Converters, and Iterators
---------------------------------------------------
The documentation of a procedure should begin with a capital letter and should be in present tense. Variables referenced in the documentation should be surrounded by double tick marks (``````).
.. code-block:: Nim
proc example1*(x: int) =
## Prints the value of ``x``.
echo x
Whenever an example of usage would be helpful to the user, you should include one within the documentation in RST format as below.
.. code-block:: Nim
proc addThree*(x, y, z: int8): int =
## Adds three ``int8`` values, treating them as unsigned and
## truncating the result.
##
## .. code-block:: nim
## echo addThree(3, 125, 6) # -122
result = x +% y +% z
The commands ``nim doc`` and ``nim doc2`` will then correctly syntax highlight the Nim code within the documentation.
Types
-----
Exported types should also be documented. This documentation can also contain code samples, but those are better placed with the functions to which they refer.
.. code-block:: Nim
type
NamedQueue*[T] = object ## Provides a linked data structure with names
## throughout. It is named for convenience. I'm making
## this comment long to show how you can, too.
name*: string ## The name of the item
val*: T ## Its value
next*: ref NamedQueue[T] ## The next item in the queue
You have some flexibility when placing the documentation:
.. code-block:: Nim
type
NamedQueue*[T] = object
## Provides a linked data structure with names
## throughout. It is named for convenience. I'm making
## this comment long to show how you can, too.
name*: string ## The name of the item
val*: T ## Its value
next*: ref NamedQueue[T] ## The next item in the queue
Make sure to place the documentation beside or within the object.
.. code-block:: Nim
type
## This documentation disappears because it annotates the ``type`` keyword
## above, not ``NamedQueue``.
NamedQueue*[T] = object
name*: string ## This becomes the main documentation for the object, which
## is not what we want.
val*: T ## Its value
next*: ref NamedQueue[T] ## The next item in the queue
Var, Let, and Const
-------------------
When declaring module-wide constants and values, documentation is encouraged. The placement of doc comments is similar to the ``type`` sections.
.. code-block:: Nim
const
X* = 42 ## An awesome number.
SpreadArray* = [
[1,2,3],
[2,3,1],
[3,1,2],
] ## Doc comment for ``SpreadArray``.
Placement of comments in other areas is usually allowed, but will not become part of the documentation output and should therefore be prefaced by a single hash (``#``).
.. code-block:: Nim
const
BadMathVals* = [
3.14, # pi
2.72, # e
0.58, # gamma
] ## A bunch of badly rounded values.
Nim supports Unicode in comments, so the above can be replaced with the following:
.. code-block:: Nim
const
BadMathVals* = [
3.14, # π
2.72, # e
0.58, # γ
] ## A bunch of badly rounded values (including π!).

View File

@@ -755,7 +755,7 @@ proc renderTocEntries*(d: var RstGenerator, j: var int, lvl: int,
proc renderImage(d: PDoc, n: PRstNode, result: var string) =
template valid(s): expr =
s.len > 0 and allCharsInSet(s, {'/',':','%','_','\\','\128'..'\xFF'} +
s.len > 0 and allCharsInSet(s, {'.','/',':','%','_','\\','\128'..'\xFF'} +
Digits + Letters + WhiteSpace)
var options = ""

View File

@@ -402,7 +402,7 @@ proc request*(url: string, httpMethod: string, extraHeaders = "",
headers.add(" HTTP/1.1\c\L")
add(headers, "Host: " & r.hostname & "\c\L")
add(headers, "Host: " & parseUri(url).hostname & "\c\L")
if userAgent != "":
add(headers, "User-Agent: " & userAgent & "\c\L")
if proxy != nil and proxy.auth != "":

View File

@@ -176,10 +176,16 @@ proc new*[T](a: var ref T) {.magic: "New", noSideEffect.}
## creates a new object of type ``T`` and returns a safe (traced)
## reference to it in ``a``.
proc new*(T: typedesc): ref T =
proc new*(T: typedesc): auto =
## creates a new object of type ``T`` and returns a safe (traced)
## reference to it as result value
new(result)
when (T is ref):
var r: T
else:
var r: ref T
new(r)
return r
proc internalNew*[T](a: var ref T) {.magic: "New", noSideEffect.}
## leaked implementation detail. Do not use.
@@ -481,6 +487,7 @@ type
## See the full `exception hierarchy`_.
ObjectConversionError* = object of Exception ## \
## Raised if an object is converted to an incompatible object type.
## You can use ``of`` operator to check if conversion will succeed.
##
## See the full `exception hierarchy`_.
FloatingPointError* = object of Exception ## \
@@ -1150,7 +1157,8 @@ const
hostCPU* {.magic: "HostCPU".}: string = ""
## a string that describes the host CPU. Possible values:
## "i386", "alpha", "powerpc", "powerpc64", "sparc", "amd64", "mips", "arm".
## "i386", "alpha", "powerpc", "powerpc64", "powerpc64el", "sparc",
## "amd64", "mips", "mipsel", "arm", "arm64".
seqShallowFlag = low(int)

View File

@@ -18,11 +18,14 @@ type
alpha, ## Alpha processor
powerpc, ## 32 bit PowerPC
powerpc64, ## 64 bit PowerPC
powerpc64el, ## Little Endian 64 bit PowerPC
sparc, ## Sparc based processor
ia64, ## Intel Itanium
amd64, ## x86_64 (AMD64); 64 bit x86 compatible CPU
mips, ## Mips based processor
mipsel, ## Little Endian Mips based processor
arm, ## ARM based processor
arm64, ## ARM64 based processor
vm, ## Some Virtual machine: Nim's VM or JavaScript
avr ## AVR based processor
@@ -63,11 +66,14 @@ const
elif defined(alpha): CpuPlatform.alpha
elif defined(powerpc): CpuPlatform.powerpc
elif defined(powerpc64): CpuPlatform.powerpc64
elif defined(powerpc64el): CpuPlatform.powerpc64el
elif defined(sparc): CpuPlatform.sparc
elif defined(ia64): CpuPlatform.ia64
elif defined(amd64): CpuPlatform.amd64
elif defined(mips): CpuPlatform.mips
elif defined(mipsel): CpuPlatform.mipsel
elif defined(arm): CpuPlatform.arm
elif defined(arm64): CpuPlatform.arm64
elif defined(vm): CpuPlatform.vm
elif defined(avr): CpuPlatform.avr
else: CpuPlatform.none

View File

@@ -256,7 +256,10 @@ when not defined(useNimRtl):
of tyBool: add result, reprBool(cast[ptr bool](p)[])
of tyChar: add result, reprChar(cast[ptr char](p)[])
of tyString: reprStrAux(result, cast[ptr string](p)[])
of tyCString: reprStrAux(result, $(cast[ptr cstring](p)[]))
of tyCString:
let cs = cast[ptr cstring](p)[]
if cs.isNil: add result, "nil"
else: reprStrAux(result, $cs)
of tyRange: reprAux(result, p, typ.base, cl)
of tyProc, tyPointer:
if cast[PPointer](p)[] == nil: add result, "nil"

View File

@@ -19,9 +19,9 @@ proc countBits32(n: int32): int {.compilerproc.} =
v = (v and 0x33333333'i32) +% ((v shr 2'i32) and 0x33333333'i32)
result = ((v +% (v shr 4'i32) and 0xF0F0F0F'i32) *% 0x1010101'i32) shr 24'i32
proc countBits64(n: int64): int {.compilerproc.} =
result = countBits32(toU32(n and 0xffff'i64)) +
countBits32(toU32(n shr 16'i64))
proc countBits64(n: int64): int {.compilerproc.} =
result = countBits32(toU32(n and 0xffffffff'i64)) +
countBits32(toU32(n shr 32'i64))
proc cardSet(s: NimSet, len: int): int {.compilerproc.} =
result = 0

View File

@@ -0,0 +1,8 @@
# bug #3171
template newDataWindow(): stmt =
let eventClosure = proc (closure: pointer): bool {.closure, cdecl.} =
discard
newDataWindow()

View File

@@ -2,344 +2,344 @@ discard """
output: "Success"
"""
# Ref:
# http://nim-lang.org/macros.html
# http://nim-lang.org/parseutils.html
# Imports
import tables, parseutils, macros, strutils
import annotate
export annotate
# Fields
const identChars = {'a'..'z', 'A'..'Z', '0'..'9', '_'}
# Procedure Declarations
proc parse_template(node: NimNode, value: string) {.compiletime.}
# Procedure Definitions
proc substring(value: string, index: int, length = -1): string {.compiletime.} =
## Returns a string at most `length` characters long, starting at `index`.
return if length < 0: value.substr(index)
elif length == 0: ""
else: value.substr(index, index + length-1)
proc parse_thru_eol(value: string, index: int): int {.compiletime.} =
## Reads until and past the end of the current line, unless
## a non-whitespace character is encountered first
var remainder: string
var read = value.parseUntil(remainder, {0x0A.char}, index)
if remainder.skipWhitespace() == read:
return read + 1
proc trim_after_eol(value: var string) {.compiletime.} =
## Trims any whitespace at end after \n
var toTrim = 0
for i in countdown(value.len-1, 0):
# If \n, return
if value[i] in [' ', '\t']: inc(toTrim)
else: break
if toTrim > 0:
value = value.substring(0, value.len - toTrim)
proc trim_eol(value: var string) {.compiletime.} =
## Removes everything after the last line if it contains nothing but whitespace
for i in countdown(value.len - 1, 0):
# If \n, trim and return
if value[i] == 0x0A.char:
value = value.substr(0, i)
break
# This is the first character
if i == 0:
value = ""
break
# Skip change
if not (value[i] in [' ', '\t']): break
proc detect_indent(value: string, index: int): int {.compiletime.} =
## Detects how indented the line at `index` is.
# Seek to the beginning of the line.
var lastChar = index
for i in countdown(index, 0):
if value[i] == 0x0A.char:
# if \n, return the indentation level
return lastChar - i
elif not (value[i] in [' ', '\t']):
# if non-whitespace char, decrement lastChar
dec(lastChar)
proc parse_thru_string(value: string, i: var int, strType = '"') {.compiletime.} =
## Parses until ending " or ' is reached.
inc(i)
if i < value.len-1:
inc(i, value.skipUntil({'\\', strType}, i))
proc parse_to_close(value: string, index: int, open='(', close=')', opened=0): int {.compiletime.} =
## Reads until all opened braces are closed
## ignoring any strings "" or ''
var remainder = value.substring(index)
var open_braces = opened
result = 0
while result < remainder.len:
var c = remainder[result]
if c == open: inc(open_braces)
elif c == close: dec(open_braces)
elif c == '"': remainder.parse_thru_string(result)
elif c == '\'': remainder.parse_thru_string(result, '\'')
if open_braces == 0: break
else: inc(result)
iterator parse_stmt_list(value: string, index: var int): string =
## Parses unguided ${..} block
var read = value.parse_to_close(index, open='{', close='}')
var expressions = value.substring(index + 1, read - 1).split({ ';', 0x0A.char })
for expression in expressions:
let value = expression.strip
if value.len > 0:
yield value
#Increment index & parse thru EOL
inc(index, read + 1)
inc(index, value.parse_thru_eol(index))
iterator parse_compound_statements(value, identifier: string, index: int): string =
## Parses through several statements, i.e. if {} elif {} else {}
## and returns the initialization of each as an empty statement
## i.e. if x == 5 { ... } becomes if x == 5: nil.
template get_next_ident(expected): stmt =
var nextIdent: string
discard value.parseWhile(nextIdent, {'$'} + identChars, i)
var next: string
var read: int
if nextIdent == "case":
# We have to handle case a bit differently
read = value.parseUntil(next, '$', i)
inc(i, read)
yield next.strip(leading=false) & "\n"
else:
read = value.parseUntil(next, '{', i)
if nextIdent in expected:
inc(i, read)
# Parse until closing }, then skip whitespace afterwards
read = value.parse_to_close(i, open='{', close='}')
inc(i, read + 1)
inc(i, value.skipWhitespace(i))
yield next & ": nil\n"
else: break
var i = index
while true:
# Check if next statement would be valid, given the identifier
if identifier in ["if", "when"]:
get_next_ident([identifier, "$elif", "$else"])
elif identifier == "case":
get_next_ident(["case", "$of", "$elif", "$else"])
elif identifier == "try":
get_next_ident(["try", "$except", "$finally"])
proc parse_complex_stmt(value, identifier: string, index: var int): NimNode {.compiletime.} =
## Parses if/when/try /elif /else /except /finally statements
# Build up complex statement string
var stmtString = newString(0)
var numStatements = 0
for statement in value.parse_compound_statements(identifier, index):
if statement[0] == '$': stmtString.add(statement.substr(1))
else: stmtString.add(statement)
inc(numStatements)
# Parse stmt string
result = parseExpr(stmtString)
var resultIndex = 0
# Fast forward a bit if this is a case statement
if identifier == "case":
inc(resultIndex)
while resultIndex < numStatements:
# Detect indentation
let indent = detect_indent(value, index)
# Parse until an open brace `{`
var read = value.skipUntil('{', index)
inc(index, read + 1)
# Parse through EOL
inc(index, value.parse_thru_eol(index))
# Parse through { .. }
read = value.parse_to_close(index, open='{', close='}', opened=1)
# Add parsed sub-expression into body
var body = newStmtList()
var stmtString = value.substring(index, read)
trim_after_eol(stmtString)
stmtString = reindent(stmtString, indent)
parse_template(body, stmtString)
inc(index, read + 1)
# Insert body into result
var stmtIndex = macros.high(result[resultIndex])
result[resultIndex][stmtIndex] = body
# Parse through EOL again & increment result index
inc(index, value.parse_thru_eol(index))
inc(resultIndex)
proc parse_simple_statement(value: string, index: var int): NimNode {.compiletime.} =
## Parses for/while
# Detect indentation
let indent = detect_indent(value, index)
# Parse until an open brace `{`
var splitValue: string
var read = value.parseUntil(splitValue, '{', index)
result = parseExpr(splitValue & ":nil")
inc(index, read + 1)
# Parse through EOL
inc(index, value.parse_thru_eol(index))
# Parse through { .. }
read = value.parse_to_close(index, open='{', close='}', opened=1)
# Add parsed sub-expression into body
var body = newStmtList()
var stmtString = value.substring(index, read)
trim_after_eol(stmtString)
stmtString = reindent(stmtString, indent)
parse_template(body, stmtString)
inc(index, read + 1)
# Insert body into result
var stmtIndex = macros.high(result)
result[stmtIndex] = body
# Parse through EOL again
inc(index, value.parse_thru_eol(index))
proc parse_until_symbol(node: NimNode, value: string, index: var int): bool {.compiletime.} =
## Parses a string until a $ symbol is encountered, if
## two $$'s are encountered in a row, a split will happen
## removing one of the $'s from the resulting output
var splitValue: string
var read = value.parseUntil(splitValue, '$', index)
var insertionPoint = node.len
inc(index, read + 1)
if index < value.len:
case value[index]
of '$':
# Check for duplicate `$`, meaning this is an escaped $
node.add newCall("add", ident("result"), newStrLitNode("$"))
inc(index)
of '(':
# Check for open `(`, which means parse as simple single-line expression.
trim_eol(splitValue)
read = value.parse_to_close(index) + 1
node.add newCall("add", ident("result"),
newCall(bindSym"strip", parseExpr("$" & value.substring(index, read)))
)
inc(index, read)
of '{':
# Check for open `{`, which means open statement list
trim_eol(splitValue)
for s in value.parse_stmt_list(index):
node.add parseExpr(s)
else:
# Otherwise parse while valid `identChars` and make expression w/ $
var identifier: string
read = value.parseWhile(identifier, identChars, index)
if identifier in ["for", "while"]:
## for/while means open simple statement
trim_eol(splitValue)
node.add value.parse_simple_statement(index)
elif identifier in ["if", "when", "case", "try"]:
## if/when/case/try means complex statement
trim_eol(splitValue)
node.add value.parse_complex_stmt(identifier, index)
elif identifier.len > 0:
## Treat as simple variable
node.add newCall("add", ident("result"), newCall("$", ident(identifier)))
inc(index, read)
result = true
# Insert
if splitValue.len > 0:
node.insert insertionPoint, newCall("add", ident("result"), newStrLitNode(splitValue))
proc parse_template(node: NimNode, value: string) =
## Parses through entire template, outputing valid
## Nim code into the input `node` AST.
var index = 0
while index < value.len and
parse_until_symbol(node, value, index): nil
macro tmpli*(body: expr): stmt =
result = newStmtList()
result.add parseExpr("result = \"\"")
var value = if body.kind in nnkStrLit..nnkTripleStrLit: body.strVal
else: body[1].strVal
parse_template(result, reindent(value))
macro tmpl*(body: expr): stmt =
result = newStmtList()
var value = if body.kind in nnkStrLit..nnkTripleStrLit: body.strVal
else: body[1].strVal
parse_template(result, reindent(value))
# Run tests
when isMainModule:
# Ref:
# http://nim-lang.org/macros.html
# http://nim-lang.org/parseutils.html
# Imports
import tables, parseutils, macros, strutils
import annotate
export annotate
# Fields
const identChars = {'a'..'z', 'A'..'Z', '0'..'9', '_'}
# Procedure Declarations
proc parse_template(node: NimNode, value: string) {.compiletime.}
# Procedure Definitions
proc substring(value: string, index: int, length = -1): string {.compiletime.} =
## Returns a string at most `length` characters long, starting at `index`.
return if length < 0: value.substr(index)
elif length == 0: ""
else: value.substr(index, index + length-1)
proc parse_thru_eol(value: string, index: int): int {.compiletime.} =
## Reads until and past the end of the current line, unless
## a non-whitespace character is encountered first
var remainder: string
var read = value.parseUntil(remainder, {0x0A.char}, index)
if remainder.skipWhitespace() == read:
return read + 1
proc trim_after_eol(value: var string) {.compiletime.} =
## Trims any whitespace at end after \n
var toTrim = 0
for i in countdown(value.len-1, 0):
# If \n, return
if value[i] in [' ', '\t']: inc(toTrim)
else: break
if toTrim > 0:
value = value.substring(0, value.len - toTrim)
proc trim_eol(value: var string) {.compiletime.} =
## Removes everything after the last line if it contains nothing but whitespace
for i in countdown(value.len - 1, 0):
# If \n, trim and return
if value[i] == 0x0A.char:
value = value.substr(0, i)
break
# This is the first character
if i == 0:
value = ""
break
# Skip change
if not (value[i] in [' ', '\t']): break
proc detect_indent(value: string, index: int): int {.compiletime.} =
## Detects how indented the line at `index` is.
# Seek to the beginning of the line.
var lastChar = index
for i in countdown(index, 0):
if value[i] == 0x0A.char:
# if \n, return the indentation level
return lastChar - i
elif not (value[i] in [' ', '\t']):
# if non-whitespace char, decrement lastChar
dec(lastChar)
proc parse_thru_string(value: string, i: var int, strType = '"') {.compiletime.} =
## Parses until ending " or ' is reached.
inc(i)
if i < value.len-1:
inc(i, value.skipUntil({'\\', strType}, i))
proc parse_to_close(value: string, index: int, open='(', close=')', opened=0): int {.compiletime.} =
## Reads until all opened braces are closed
## ignoring any strings "" or ''
var remainder = value.substring(index)
var open_braces = opened
result = 0
while result < remainder.len:
var c = remainder[result]
if c == open: inc(open_braces)
elif c == close: dec(open_braces)
elif c == '"': remainder.parse_thru_string(result)
elif c == '\'': remainder.parse_thru_string(result, '\'')
if open_braces == 0: break
else: inc(result)
iterator parse_stmt_list(value: string, index: var int): string =
## Parses unguided ${..} block
var read = value.parse_to_close(index, open='{', close='}')
var expressions = value.substring(index + 1, read - 1).split({ ';', 0x0A.char })
for expression in expressions:
let value = expression.strip
if value.len > 0:
yield value
#Increment index & parse thru EOL
inc(index, read + 1)
inc(index, value.parse_thru_eol(index))
iterator parse_compound_statements(value, identifier: string, index: int): string =
## Parses through several statements, i.e. if {} elif {} else {}
## and returns the initialization of each as an empty statement
## i.e. if x == 5 { ... } becomes if x == 5: nil.
template get_next_ident(expected): stmt =
var nextIdent: string
discard value.parseWhile(nextIdent, {'$'} + identChars, i)
var next: string
var read: int
if nextIdent == "case":
# We have to handle case a bit differently
read = value.parseUntil(next, '$', i)
inc(i, read)
yield next.strip(leading=false) & "\n"
else:
read = value.parseUntil(next, '{', i)
if nextIdent in expected:
inc(i, read)
# Parse until closing }, then skip whitespace afterwards
read = value.parse_to_close(i, open='{', close='}')
inc(i, read + 1)
inc(i, value.skipWhitespace(i))
yield next & ": nil\n"
else: break
var i = index
while true:
# Check if next statement would be valid, given the identifier
if identifier in ["if", "when"]:
get_next_ident([identifier, "$elif", "$else"])
elif identifier == "case":
get_next_ident(["case", "$of", "$elif", "$else"])
elif identifier == "try":
get_next_ident(["try", "$except", "$finally"])
proc parse_complex_stmt(value, identifier: string, index: var int): NimNode {.compiletime.} =
## Parses if/when/try /elif /else /except /finally statements
# Build up complex statement string
var stmtString = newString(0)
var numStatements = 0
for statement in value.parse_compound_statements(identifier, index):
if statement[0] == '$': stmtString.add(statement.substr(1))
else: stmtString.add(statement)
inc(numStatements)
# Parse stmt string
result = parseExpr(stmtString)
var resultIndex = 0
# Fast forward a bit if this is a case statement
if identifier == "case":
inc(resultIndex)
while resultIndex < numStatements:
# Detect indentation
let indent = detect_indent(value, index)
# Parse until an open brace `{`
var read = value.skipUntil('{', index)
inc(index, read + 1)
# Parse through EOL
inc(index, value.parse_thru_eol(index))
# Parse through { .. }
read = value.parse_to_close(index, open='{', close='}', opened=1)
# Add parsed sub-expression into body
var body = newStmtList()
var stmtString = value.substring(index, read)
trim_after_eol(stmtString)
stmtString = reindent(stmtString, indent)
parse_template(body, stmtString)
inc(index, read + 1)
# Insert body into result
var stmtIndex = result[resultIndex].len-1
result[resultIndex][stmtIndex] = body
# Parse through EOL again & increment result index
inc(index, value.parse_thru_eol(index))
inc(resultIndex)
proc parse_simple_statement(value: string, index: var int): NimNode {.compiletime.} =
## Parses for/while
# Detect indentation
let indent = detect_indent(value, index)
# Parse until an open brace `{`
var splitValue: string
var read = value.parseUntil(splitValue, '{', index)
result = parseExpr(splitValue & ":nil")
inc(index, read + 1)
# Parse through EOL
inc(index, value.parse_thru_eol(index))
# Parse through { .. }
read = value.parse_to_close(index, open='{', close='}', opened=1)
# Add parsed sub-expression into body
var body = newStmtList()
var stmtString = value.substring(index, read)
trim_after_eol(stmtString)
stmtString = reindent(stmtString, indent)
parse_template(body, stmtString)
inc(index, read + 1)
# Insert body into result
var stmtIndex = result.len-1
result[stmtIndex] = body
# Parse through EOL again
inc(index, value.parse_thru_eol(index))
proc parse_until_symbol(node: NimNode, value: string, index: var int): bool {.compiletime.} =
## Parses a string until a $ symbol is encountered, if
## two $$'s are encountered in a row, a split will happen
## removing one of the $'s from the resulting output
var splitValue: string
var read = value.parseUntil(splitValue, '$', index)
var insertionPoint = node.len
inc(index, read + 1)
if index < value.len:
case value[index]
of '$':
# Check for duplicate `$`, meaning this is an escaped $
node.add newCall("add", ident("result"), newStrLitNode("$"))
inc(index)
of '(':
# Check for open `(`, which means parse as simple single-line expression.
trim_eol(splitValue)
read = value.parse_to_close(index) + 1
node.add newCall("add", ident("result"),
newCall(bindSym"strip", parseExpr("$" & value.substring(index, read)))
)
inc(index, read)
of '{':
# Check for open `{`, which means open statement list
trim_eol(splitValue)
for s in value.parse_stmt_list(index):
node.add parseExpr(s)
else:
# Otherwise parse while valid `identChars` and make expression w/ $
var identifier: string
read = value.parseWhile(identifier, identChars, index)
if identifier in ["for", "while"]:
## for/while means open simple statement
trim_eol(splitValue)
node.add value.parse_simple_statement(index)
elif identifier in ["if", "when", "case", "try"]:
## if/when/case/try means complex statement
trim_eol(splitValue)
node.add value.parse_complex_stmt(identifier, index)
elif identifier.len > 0:
## Treat as simple variable
node.add newCall("add", ident("result"), newCall("$", ident(identifier)))
inc(index, read)
result = true
# Insert
if splitValue.len > 0:
node.insert insertionPoint, newCall("add", ident("result"), newStrLitNode(splitValue))
proc parse_template(node: NimNode, value: string) =
## Parses through entire template, outputing valid
## Nim code into the input `node` AST.
var index = 0
while index < value.len and
parse_until_symbol(node, value, index): nil
macro tmpli*(body: expr): stmt =
result = newStmtList()
result.add parseExpr("result = \"\"")
var value = if body.kind in nnkStrLit..nnkTripleStrLit: body.strVal
else: body[1].strVal
parse_template(result, reindent(value))
macro tmpl*(body: expr): stmt =
result = newStmtList()
var value = if body.kind in nnkStrLit..nnkTripleStrLit: body.strVal
else: body[1].strVal
parse_template(result, reindent(value))
# Run tests
when isMainModule:
include otests
echo "Success"

View File

@@ -0,0 +1,20 @@
discard """
errormsg: "cannot use symbol of kind 'var' as a 'param'"
line: 20
"""
# bug #3158
type
MyData = object
x: int
template newDataWindow(data: ref MyData): stmt =
proc testProc(data: ref MyData) =
echo "Hello, ", data.x
testProc(data)
var d: ref MyData
new(d)
d.x = 10
newDataWindow(d)