summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
Diffstat (limited to 'compiler')
-rw-r--r--compiler/jsgen.nim13
-rw-r--r--compiler/options.nim29
2 files changed, 32 insertions, 10 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim
index 8f5ef4bce..eadad27d0 100644
--- a/compiler/jsgen.nim
+++ b/compiler/jsgen.nim
@@ -451,6 +451,15 @@ proc binaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string) =
   r.res = ropef(frmt, [x.rdLoc, y.rdLoc])
   r.kind = resExpr
 
+proc ternaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string) =
+  var x, y, z: TCompRes
+  useMagic(p, magic)
+  gen(p, n.sons[1], x)
+  gen(p, n.sons[2], y)
+  gen(p, n.sons[3], z)
+  r.res = ropef(frmt, [x.rdLoc, y.rdLoc, z.rdLoc])
+  r.kind = resExpr
+
 proc unaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string) =
   useMagic(p, magic)
   gen(p, n.sons[1], r)
@@ -1351,6 +1360,10 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
   of mEcho: genEcho(p, n, r)
   of mSlurp, mStaticExec:
     localError(n.info, errXMustBeCompileTime, n.sons[0].sym.name.s)
+  of mCopyStr: binaryExpr(p, n, r, "", "($1.slice($2,-1))")
+  of mCopyStrLast: ternaryExpr(p, n, r, "", "($1.slice($2, ($3)+1).concat(0))")
+  of mNewString: unaryExpr(p, n, r, "mnewString", "mnewString($1)")
+  of mNewStringOfCap: unaryExpr(p, n, r, "mnewString", "mnewString(0)")    
   else:
     genCall(p, n, r)
     #else internalError(e.info, 'genMagic: ' + magicToStr[op]);
diff --git a/compiler/options.nim b/compiler/options.nim
index e50535b8e..2b607a48b 100644
--- a/compiler/options.nim
+++ b/compiler/options.nim
@@ -209,17 +209,26 @@ proc getGeneratedPath: string =
   result = if nimcacheDir.len > 0: nimcacheDir else: gProjectPath.shortenDir /
                                                          genSubDir
 
+proc getPackageName(path: string): string =
+  var q = 1
+  var b = 0
+  if path[len(path)-1] in {dirsep, altsep}: q = 2
+  for i in countdown(len(path)-q, 0):
+    if path[i] in {dirsep, altsep}:
+      if b == 0: b = i
+      else:
+        let x = path.substr(i+1, b-1)
+        case x.normalize
+        of "lib", "src", "source", "package", "pckg", "library": b = i
+        else: return x
+
 proc withPackageName*(path: string): string =
-  var x = path
-  while true:
-    x = parentDir(x)
-    if x.len == 0: break
-    case x.normalize
-    of "lib", "src", "source", "package", "pckg", "library": discard
-    else:
-      let (path, file, ext) = path.splitFile
-      return (path / (x & '_' & file)) & ext
-  result = path
+  let x = path.getPackageName
+  if x.isNil:
+    result = path
+  else:
+    let (p, file, ext) = path.splitFile
+    result = (p / (x & '_' & file)) & ext
 
 proc toGeneratedFile*(path, ext: string): string = 
   ## converts "/home/a/mymodule.nim", "rod" to "/home/a/nimcache/mymodule.rod"