diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2016-02-05 13:49:52 +0100 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2016-02-06 15:39:56 +0100 |
commit | 9b44ca17c2343b8df34e8a958a8dd46eea00cdd4 (patch) | |
tree | d6f6ee8d1b0321b616bf8fc2edc61e63a2728793 /lib/phpcl.nim | |
parent | 35567a1eb9b32f492d8f8d14bfe8d0d49b49ba32 (diff) | |
download | Nim-9b44ca17c2343b8df34e8a958a8dd46eea00cdd4.tar.gz |
PHP codegen can generate PHP classes
Diffstat (limited to 'lib/phpcl.nim')
-rw-r--r-- | lib/phpcl.nim | 50 |
1 files changed, 50 insertions, 0 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.} |