summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
Diffstat (limited to 'compiler')
-rwxr-xr-xcompiler/ast.nim2
-rwxr-xr-xcompiler/condsyms.nim25
-rwxr-xr-xcompiler/main.nim6
-rwxr-xr-xcompiler/semexprs.nim16
4 files changed, 28 insertions, 21 deletions
diff --git a/compiler/ast.nim b/compiler/ast.nim
index 909c7c3bf..eb214e87f 100755
--- a/compiler/ast.nim
+++ b/compiler/ast.nim
@@ -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, 
diff --git a/compiler/condsyms.nim b/compiler/condsyms.nim
index 9c95e8fcc..b37b754f0 100755
--- a/compiler/condsyms.nim
+++ b/compiler/condsyms.nim
@@ -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
diff --git a/compiler/main.nim b/compiler/main.nim
index d08e94397..017d188b5 100755
--- a/compiler/main.nim
+++ b/compiler/main.nim
@@ -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
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim
index 58fbf3a25..aa069c971 100755
--- a/compiler/semexprs.nim
+++ b/compiler/semexprs.nim
@@ -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 =