added system.slurp for easy embedding of resources

This commit is contained in:
Araq 2011-08-10 00:43:58 +02:00
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 =