added system.slurp for easy embedding of resources

This commit is contained in:
Araq
2011-08-10 00:43:58 +02:00
parent 7000cf51b7
commit 72e7a98543
10 changed files with 63 additions and 24 deletions

View File

@@ -313,7 +313,7 @@ type
TMagic* = enum # symbols that require compiler magic:
mNone, mDefined, mDefinedInScope, mLow, mHigh, mSizeOf, mIs, mOf,
mEcho, mShallowCopy,
mEcho, mShallowCopy, mSlurp,
mUnaryLt, mSucc,
mPred, mInc, mDec, mOrd, mNew, mNewFinalize, mNewSeq, mLengthOpenArray,
mLengthStr, mLengthArray, mLengthSeq, mIncl, mExcl, mCard, mChr, mGCref,

View File

@@ -14,16 +14,7 @@ import
var gSymbols*: TStrTable
proc InitDefines*()
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) =
proc DefineSymbol*(symbol: string) =
var i = getIdent(symbol)
var sym = StrTableGet(gSymbols, i)
if sym == nil:
@@ -33,15 +24,15 @@ proc DefineSymbol(symbol: string) =
StrTableAdd(gSymbols, sym)
sym.position = 1
proc UndefSymbol(symbol: string) =
proc UndefSymbol*(symbol: string) =
var sym = StrTableGet(gSymbols, getIdent(symbol))
if sym != nil: sym.position = 0
proc isDefined(symbol: PIdent): bool =
proc isDefined*(symbol: PIdent): bool =
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 s = InitTabIter(it, gSymbols)
OutWriteln("-- List of currently defined symbols --")
@@ -50,7 +41,7 @@ proc ListSymbols() =
s = nextIter(it, gSymbols)
OutWriteln("-- End of list --")
proc countDefinedSymbols(): int =
proc countDefinedSymbols*(): int =
var it: TTabIter
var s = InitTabIter(it, gSymbols)
result = 0
@@ -58,7 +49,7 @@ proc countDefinedSymbols(): int =
if s.position == 1: inc(result)
s = nextIter(it, gSymbols)
proc InitDefines() =
proc InitDefines*() =
initStrTable(gSymbols)
DefineSymbol("nimrod") # 'nimrod' is always defined
@@ -98,5 +89,3 @@ proc InitDefines() =
DefineSymbol(cpu[targetCPU].name)
DefineSymbol(platform.os[targetOS].name)
proc DeinitDefines() =
nil

View File

@@ -182,12 +182,12 @@ proc CommandSuggest(filename: string) =
compileProject(filename)
proc WantFile(filename: string) =
if filename == "":
if filename.len == 0:
Fatal(newLineInfo("command line", 1, 1), errCommandExpectsFilename)
proc MainCommand(cmd, filename: string) =
appendStr(searchPaths, options.libpath)
if filename != "":
if filename.len != 0:
# current path is always looked first for modules
prependStr(searchPaths, splitFile(filename).dir)
setID(100)
@@ -203,11 +203,13 @@ proc MainCommand(cmd, filename: string) =
extccomp.cExt = ".cpp"
gCmd = cmdCompileToCpp
wantFile(filename)
DefineSymbol("cpp")
CommandCompileToC(filename)
of "objc", "compiletooc":
extccomp.cExt = ".m"
gCmd = cmdCompileToOC
wantFile(filename)
DefineSymbol("objc")
CommandCompileToC(filename)
of "run":
gCmd = cmdRun

View File

@@ -883,6 +883,21 @@ proc setMs(n: PNode, s: PSym): PNode =
n.sons[0] = newSymNode(s)
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 =
# this is a hotspot in the compiler!
result = n
@@ -905,6 +920,7 @@ proc semMagic(c: PContext, n: PNode, s: PSym, flags: TExprFlags): PNode =
result = semAsgn(c, result)
else:
result = semDirectOp(c, n, flags)
of mSlurp: result = semSlurp(c, n, flags)
else: result = semDirectOp(c, n, flags)
proc semIfExpr(c: PContext, n: PNode): PNode =

View File

@@ -226,7 +226,8 @@ interfacing with libraries written in C++:
header: irr, importcpp: "run".}
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
@@ -274,7 +275,8 @@ interfacing with libraries written in Objective C:
g.free()
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

View File

@@ -35,7 +35,23 @@ type
TDoublyLinkedRing* {.pure, final.}[T] = object ## a doubly linked ring
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] =
## creates a new doubly linked node with the given `value`.
new(result)

View File

@@ -1974,4 +1974,10 @@ proc getTypeInfo*[T](x: T): pointer {.magic: "GetTypeInfo".}
## get type information for `x`. Ordinary code should not use this, but
## 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"
##

View File

@@ -0,0 +1,6 @@
const
myRes = slurp"readme.txt"
echo myRes

View File

@@ -19,6 +19,7 @@ version 0.9.0
- change overloading resolution
- implement closures; implement proper coroutines
- make exceptions compatible with C++ exceptions
- ``=`` should be overloadable; requires specialization for ``=``
Bugs
----

View File

@@ -64,6 +64,7 @@ Library Additions
- Added explicit channels for thread communication.
- Added ``matchers`` module for email address etc. matching.
- Added ``strutils.unindent``.
- Added ``system.slurp`` for easy resource embedding.
2011-07-10 Version 0.8.12 released