added system.slurp for easy embedding of resources
This commit is contained in:
parent
7000cf51b7
commit
72e7a98543
10 changed files with 63 additions and 24 deletions
|
|
@ -313,7 +313,7 @@ type
|
||||||
|
|
||||||
TMagic* = enum # symbols that require compiler magic:
|
TMagic* = enum # symbols that require compiler magic:
|
||||||
mNone, mDefined, mDefinedInScope, mLow, mHigh, mSizeOf, mIs, mOf,
|
mNone, mDefined, mDefinedInScope, mLow, mHigh, mSizeOf, mIs, mOf,
|
||||||
mEcho, mShallowCopy,
|
mEcho, mShallowCopy, mSlurp,
|
||||||
mUnaryLt, mSucc,
|
mUnaryLt, mSucc,
|
||||||
mPred, mInc, mDec, mOrd, mNew, mNewFinalize, mNewSeq, mLengthOpenArray,
|
mPred, mInc, mDec, mOrd, mNew, mNewFinalize, mNewSeq, mLengthOpenArray,
|
||||||
mLengthStr, mLengthArray, mLengthSeq, mIncl, mExcl, mCard, mChr, mGCref,
|
mLengthStr, mLengthArray, mLengthSeq, mIncl, mExcl, mCard, mChr, mGCref,
|
||||||
|
|
|
||||||
|
|
@ -14,16 +14,7 @@ import
|
||||||
|
|
||||||
var gSymbols*: TStrTable
|
var gSymbols*: TStrTable
|
||||||
|
|
||||||
proc InitDefines*()
|
proc DefineSymbol*(symbol: string) =
|
||||||
proc DeinitDefines*()
|
|
||||||
proc DefineSymbol*(symbol: string)
|
|
||||||
proc UndefSymbol*(symbol: string)
|
|
||||||
proc isDefined*(symbol: PIdent): bool
|
|
||||||
proc ListSymbols*()
|
|
||||||
proc countDefinedSymbols*(): int
|
|
||||||
# implementation
|
|
||||||
|
|
||||||
proc DefineSymbol(symbol: string) =
|
|
||||||
var i = getIdent(symbol)
|
var i = getIdent(symbol)
|
||||||
var sym = StrTableGet(gSymbols, i)
|
var sym = StrTableGet(gSymbols, i)
|
||||||
if sym == nil:
|
if sym == nil:
|
||||||
|
|
@ -33,15 +24,15 @@ proc DefineSymbol(symbol: string) =
|
||||||
StrTableAdd(gSymbols, sym)
|
StrTableAdd(gSymbols, sym)
|
||||||
sym.position = 1
|
sym.position = 1
|
||||||
|
|
||||||
proc UndefSymbol(symbol: string) =
|
proc UndefSymbol*(symbol: string) =
|
||||||
var sym = StrTableGet(gSymbols, getIdent(symbol))
|
var sym = StrTableGet(gSymbols, getIdent(symbol))
|
||||||
if sym != nil: sym.position = 0
|
if sym != nil: sym.position = 0
|
||||||
|
|
||||||
proc isDefined(symbol: PIdent): bool =
|
proc isDefined*(symbol: PIdent): bool =
|
||||||
var sym = StrTableGet(gSymbols, symbol)
|
var sym = StrTableGet(gSymbols, symbol)
|
||||||
result = (sym != nil) and (sym.position == 1)
|
result = sym != nil and sym.position == 1
|
||||||
|
|
||||||
proc ListSymbols() =
|
proc ListSymbols*() =
|
||||||
var it: TTabIter
|
var it: TTabIter
|
||||||
var s = InitTabIter(it, gSymbols)
|
var s = InitTabIter(it, gSymbols)
|
||||||
OutWriteln("-- List of currently defined symbols --")
|
OutWriteln("-- List of currently defined symbols --")
|
||||||
|
|
@ -50,7 +41,7 @@ proc ListSymbols() =
|
||||||
s = nextIter(it, gSymbols)
|
s = nextIter(it, gSymbols)
|
||||||
OutWriteln("-- End of list --")
|
OutWriteln("-- End of list --")
|
||||||
|
|
||||||
proc countDefinedSymbols(): int =
|
proc countDefinedSymbols*(): int =
|
||||||
var it: TTabIter
|
var it: TTabIter
|
||||||
var s = InitTabIter(it, gSymbols)
|
var s = InitTabIter(it, gSymbols)
|
||||||
result = 0
|
result = 0
|
||||||
|
|
@ -58,7 +49,7 @@ proc countDefinedSymbols(): int =
|
||||||
if s.position == 1: inc(result)
|
if s.position == 1: inc(result)
|
||||||
s = nextIter(it, gSymbols)
|
s = nextIter(it, gSymbols)
|
||||||
|
|
||||||
proc InitDefines() =
|
proc InitDefines*() =
|
||||||
initStrTable(gSymbols)
|
initStrTable(gSymbols)
|
||||||
DefineSymbol("nimrod") # 'nimrod' is always defined
|
DefineSymbol("nimrod") # 'nimrod' is always defined
|
||||||
|
|
||||||
|
|
@ -98,5 +89,3 @@ proc InitDefines() =
|
||||||
DefineSymbol(cpu[targetCPU].name)
|
DefineSymbol(cpu[targetCPU].name)
|
||||||
DefineSymbol(platform.os[targetOS].name)
|
DefineSymbol(platform.os[targetOS].name)
|
||||||
|
|
||||||
proc DeinitDefines() =
|
|
||||||
nil
|
|
||||||
|
|
|
||||||
|
|
@ -182,12 +182,12 @@ proc CommandSuggest(filename: string) =
|
||||||
compileProject(filename)
|
compileProject(filename)
|
||||||
|
|
||||||
proc WantFile(filename: string) =
|
proc WantFile(filename: string) =
|
||||||
if filename == "":
|
if filename.len == 0:
|
||||||
Fatal(newLineInfo("command line", 1, 1), errCommandExpectsFilename)
|
Fatal(newLineInfo("command line", 1, 1), errCommandExpectsFilename)
|
||||||
|
|
||||||
proc MainCommand(cmd, filename: string) =
|
proc MainCommand(cmd, filename: string) =
|
||||||
appendStr(searchPaths, options.libpath)
|
appendStr(searchPaths, options.libpath)
|
||||||
if filename != "":
|
if filename.len != 0:
|
||||||
# current path is always looked first for modules
|
# current path is always looked first for modules
|
||||||
prependStr(searchPaths, splitFile(filename).dir)
|
prependStr(searchPaths, splitFile(filename).dir)
|
||||||
setID(100)
|
setID(100)
|
||||||
|
|
@ -203,11 +203,13 @@ proc MainCommand(cmd, filename: string) =
|
||||||
extccomp.cExt = ".cpp"
|
extccomp.cExt = ".cpp"
|
||||||
gCmd = cmdCompileToCpp
|
gCmd = cmdCompileToCpp
|
||||||
wantFile(filename)
|
wantFile(filename)
|
||||||
|
DefineSymbol("cpp")
|
||||||
CommandCompileToC(filename)
|
CommandCompileToC(filename)
|
||||||
of "objc", "compiletooc":
|
of "objc", "compiletooc":
|
||||||
extccomp.cExt = ".m"
|
extccomp.cExt = ".m"
|
||||||
gCmd = cmdCompileToOC
|
gCmd = cmdCompileToOC
|
||||||
wantFile(filename)
|
wantFile(filename)
|
||||||
|
DefineSymbol("objc")
|
||||||
CommandCompileToC(filename)
|
CommandCompileToC(filename)
|
||||||
of "run":
|
of "run":
|
||||||
gCmd = cmdRun
|
gCmd = cmdRun
|
||||||
|
|
|
||||||
|
|
@ -883,6 +883,21 @@ proc setMs(n: PNode, s: PSym): PNode =
|
||||||
n.sons[0] = newSymNode(s)
|
n.sons[0] = newSymNode(s)
|
||||||
n.sons[0].info = n.info
|
n.sons[0].info = n.info
|
||||||
|
|
||||||
|
proc semSlurp(c: PContext, n: PNode, flags: TExprFlags): PNode =
|
||||||
|
if sonsLen(n) == 2:
|
||||||
|
var a = c.semConstExpr(c, n.sons[1])
|
||||||
|
if a.kind notin {nkStrLit, nkRStrLit, nkTripleStrLit}:
|
||||||
|
GlobalError(a.info, errStringLiteralExpected)
|
||||||
|
try:
|
||||||
|
var content = readFile(a.strVal)
|
||||||
|
result = newStrNode(nkStrLit, content)
|
||||||
|
result.typ = getSysType(tyString)
|
||||||
|
result.info = n.info
|
||||||
|
except EIO:
|
||||||
|
GlobalError(a.info, errCannotOpenFile, a.strVal)
|
||||||
|
else:
|
||||||
|
result = semDirectOp(c, n, flags)
|
||||||
|
|
||||||
proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
|
proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
|
||||||
# this is a hotspot in the compiler!
|
# this is a hotspot in the compiler!
|
||||||
result = n
|
result = n
|
||||||
|
|
@ -905,6 +920,7 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
|
||||||
result = semAsgn(c, result)
|
result = semAsgn(c, result)
|
||||||
else:
|
else:
|
||||||
result = semDirectOp(c, n, flags)
|
result = semDirectOp(c, n, flags)
|
||||||
|
of mSlurp: result = semSlurp(c, n, flags)
|
||||||
else: result = semDirectOp(c, n, flags)
|
else: result = semDirectOp(c, n, flags)
|
||||||
|
|
||||||
proc semIfExpr(c: PContext, n: PNode): PNode =
|
proc semIfExpr(c: PContext, n: PNode): PNode =
|
||||||
|
|
|
||||||
|
|
@ -226,7 +226,8 @@ interfacing with libraries written in C++:
|
||||||
header: irr, importcpp: "run".}
|
header: irr, importcpp: "run".}
|
||||||
|
|
||||||
The compiler needs to be told to generate C++ (command ``cpp``) for
|
The compiler needs to be told to generate C++ (command ``cpp``) for
|
||||||
this to work.
|
this to work. The conditional symbol ``cpp`` is defined when the compiler
|
||||||
|
emits C++ code.
|
||||||
|
|
||||||
|
|
||||||
ImportObjC pragma
|
ImportObjC pragma
|
||||||
|
|
@ -274,7 +275,8 @@ interfacing with libraries written in Objective C:
|
||||||
g.free()
|
g.free()
|
||||||
|
|
||||||
The compiler needs to be told to generate Objective C (command ``objc``) for
|
The compiler needs to be told to generate Objective C (command ``objc``) for
|
||||||
this to work.
|
this to work. The conditional symbol ``objc`` is defined when the compiler
|
||||||
|
emits Objective C code.
|
||||||
|
|
||||||
|
|
||||||
LineDir option
|
LineDir option
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,23 @@ type
|
||||||
|
|
||||||
TDoublyLinkedRing* {.pure, final.}[T] = object ## a doubly linked ring
|
TDoublyLinkedRing* {.pure, final.}[T] = object ## a doubly linked ring
|
||||||
head*: PDoublyLinkedNode[T]
|
head*: PDoublyLinkedNode[T]
|
||||||
|
|
||||||
|
proc initSinglyLinkedList*[T](): TSinglyLinkedList[T] =
|
||||||
|
## creates a new singly linked list that is empty.
|
||||||
|
nil
|
||||||
|
|
||||||
|
proc initDoublyLinkedList*[T](): TDoublyLinkedList[T] =
|
||||||
|
## creates a new doubly linked list that is empty.
|
||||||
|
nil
|
||||||
|
|
||||||
|
proc initSinglyLinkedRing*[T](): TSinglyLinkedRing[T] =
|
||||||
|
## creates a new singly linked ring that is empty.
|
||||||
|
nil
|
||||||
|
|
||||||
|
proc initDoublyLinkedRing*[T](): TDoublyLinkedRing[T] =
|
||||||
|
## creates a new doubly linked ring that is empty.
|
||||||
|
nil
|
||||||
|
|
||||||
proc newDoublyLinkedNode*[T](value: T): PDoublyLinkedNode[T] =
|
proc newDoublyLinkedNode*[T](value: T): PDoublyLinkedNode[T] =
|
||||||
## creates a new doubly linked node with the given `value`.
|
## creates a new doubly linked node with the given `value`.
|
||||||
new(result)
|
new(result)
|
||||||
|
|
|
||||||
|
|
@ -1974,4 +1974,10 @@ proc getTypeInfo*[T](x: T): pointer {.magic: "GetTypeInfo".}
|
||||||
## get type information for `x`. Ordinary code should not use this, but
|
## get type information for `x`. Ordinary code should not use this, but
|
||||||
## the `typeinfo` module instead.
|
## the `typeinfo` module instead.
|
||||||
|
|
||||||
|
proc slurp*(filename: string): string {.magic: "Slurp".}
|
||||||
|
## compiletime ``readFile`` proc for easy `resource`:idx: embedding:
|
||||||
|
## .. code-block:: nimrod
|
||||||
|
##
|
||||||
|
## const myResource = slurp"mydatafile.bin"
|
||||||
|
##
|
||||||
|
|
||||||
|
|
|
||||||
6
tests/accept/compile/tslurp.nim
Normal file
6
tests/accept/compile/tslurp.nim
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
|
||||||
|
const
|
||||||
|
myRes = slurp"readme.txt"
|
||||||
|
|
||||||
|
echo myRes
|
||||||
|
|
||||||
1
todo.txt
1
todo.txt
|
|
@ -19,6 +19,7 @@ version 0.9.0
|
||||||
- change overloading resolution
|
- change overloading resolution
|
||||||
- implement closures; implement proper coroutines
|
- implement closures; implement proper coroutines
|
||||||
- make exceptions compatible with C++ exceptions
|
- make exceptions compatible with C++ exceptions
|
||||||
|
- ``=`` should be overloadable; requires specialization for ``=``
|
||||||
|
|
||||||
Bugs
|
Bugs
|
||||||
----
|
----
|
||||||
|
|
|
||||||
|
|
@ -64,6 +64,7 @@ Library Additions
|
||||||
- Added explicit channels for thread communication.
|
- Added explicit channels for thread communication.
|
||||||
- Added ``matchers`` module for email address etc. matching.
|
- Added ``matchers`` module for email address etc. matching.
|
||||||
- Added ``strutils.unindent``.
|
- Added ``strutils.unindent``.
|
||||||
|
- Added ``system.slurp`` for easy resource embedding.
|
||||||
|
|
||||||
|
|
||||||
2011-07-10 Version 0.8.12 released
|
2011-07-10 Version 0.8.12 released
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue