summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/phpcl.nim50
-rw-r--r--lib/system/jssys.nim10
2 files changed, 55 insertions, 5 deletions
diff --git a/lib/phpcl.nim b/lib/phpcl.nim
new file mode 100644
index 000000000..6d7aa0ea6
--- /dev/null
+++ b/lib/phpcl.nim
@@ -0,0 +1,50 @@
+#
+#
+#            Nim's Runtime Library
+#        (c) Copyright 2015 Andreas Rumpf
+#
+#    See the file "copying.txt", included in this
+#    distribution, for details about the copyright.
+#
+
+## PHP compatibility layer.
+
+type
+  PhpArray*[Key, Val] = ref object
+
+  PhpObj* = ref object ## can be a string, an int etc.
+
+proc explode*(sep, x: string): seq[string] {.importc: "explode".}
+template split*(x, sep: string): seq[string] = explode(sep, x)
+
+proc `$`*(x: PhpObj): string {.importcpp: "(#)".}
+proc `++`*(x: PhpObj) {.importcpp: "++(#)".}
+
+proc `==`*(x, y: PhpObj): string {.importcpp: "((#) == (#))".}
+proc `<=`*(x, y: PhpObj): string {.importcpp: "((#) <= (#))".}
+proc `<`*(x, y: PhpObj): string {.importcpp: "((#) < (#))".}
+
+proc toUpper*(x: string): string {.importc: "strtoupper".}
+proc toLower*(x: string): string {.importc: "strtolower".}
+
+proc strtr*(s: string, replacePairs: PhpArray[string, string]): string {.importc.}
+proc strtr*(s, fromm, to: string): string {.importc.}
+
+proc toArray*[K,V](pairs: openarray[(K,V)]): PhpArray[K,V] {.magic:
+  "Repr".}
+template strtr*(s: string, replacePairs: openarray[(string, string)]): string =
+  strtr(toArray(replacePairs))
+
+iterator pairs*[K,V](d: PhpArray[K,V]): (K,V) =
+  var k: K
+  var v: V
+  {.emit: "foreach (`d` as `k`=>`v`) {".}
+  yield (k, v)
+  {.emit: "}".}
+
+proc `[]`*[K,V](d: PhpArray[K,V]; k: K): V {.importcpp: "#[#]".}
+proc `[]=`*[K,V](d: PhpArray[K,V]; k: K; v: V) {.importcpp: "#[#] = #".}
+
+proc ksort*[K,V](d: PhpArray[K,V]) {.importc.}
+proc krsort*[K,V](d: PhpArray[K,V]) {.importc.}
+proc keys*[K,V](d: PhpArray[K,V]): seq[K] {.importc.}
diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim
index 90f02da62..8394396b5 100644
--- a/lib/system/jssys.nim
+++ b/lib/system/jssys.nim
@@ -156,16 +156,16 @@ proc SetConstr() {.varargs, asmNoStackFrame, compilerproc.} =
     asm """
       $args = func_get_args();
       $result = array();
-      foreach ($args as $key=>$x) {
-        if (is_array(($val)) {
+      foreach ($args as $x) {
+        if (is_array($x)) {
           for ($j = $x[0]; $j <= $x[1]; $j++) {
-            result[$j] = true;
+            $result[$j] = true;
           }
         } else {
-          result[$x] = true;
+          $result[$x] = true;
         }
       }
-      return result;
+      return $result;
     """
   else:
     asm """