cleaner toArray magic

This commit is contained in:
Andreas Rumpf
2016-02-05 17:47:08 +01:00
parent 9b44ca17c2
commit 4686f485a6
2 changed files with 25 additions and 22 deletions

View File

@@ -1435,29 +1435,29 @@ proc genConStrStrPHP(p: PProc, n: PNode, r: var TCompRes) =
else:
r.res.add(".$1" % [a.res])
proc genToArray(p: PProc; n: PNode; r: var TCompRes) =
# we map mArray to PHP's array constructor, a mild hack:
var a, b: TCompRes
r.kind = resExpr
r.res = rope("array(")
let x = skipConv(n[1])
if x.kind == nkBracket:
for i in countup(0, x.len - 1):
let it = x[i]
if it.kind == nkPar and it.len == 2:
if i > 0: r.res.add(", ")
gen(p, it[0], a)
gen(p, it[1], b)
r.res.add("$# => $#" % [a.rdLoc, b.rdLoc])
else:
localError(it.info, "'toArray' needs tuple constructors")
else:
localError(x.info, "'toArray' needs an array literal")
r.res.add(")")
proc genRepr(p: PProc, n: PNode, r: var TCompRes) =
if p.target == targetPHP:
if n.sons[0].sym.name.s == "repr":
localError(n.info, "'repr' not available for PHP backend")
else:
# we map mRepr to PHP's array constructor, a mild hack:
var a, b: TCompRes
r.kind = resExpr
r.res = rope("array(")
let x = skipConv(n[1])
if x.kind == nkBracket:
for i in countup(0, x.len - 1):
let it = x[i]
if it.kind == nkPar and it.len == 2:
if i > 0: r.res.add(", ")
gen(p, it[0], a)
gen(p, it[1], b)
r.res.add("$# => $#" % [a.rdLoc, b.rdLoc])
else:
localError(it.info, "'toArray' needs tuple constructors")
else:
localError(x.info, "'toArray' needs an array literal")
r.res.add(")")
localError(n.info, "'repr' not available for PHP backend")
return
let t = skipTypes(n.sons[1].typ, abstractVarRange)
case t.kind
@@ -1621,6 +1621,9 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
of mParseBiggestFloat:
useMagic(p, "nimParseBiggestFloat")
genCall(p, n, r)
of mArray:
if p.target == targetPHP: genToArray(p, n, r)
else: genCall(p, n, r)
else:
genCall(p, n, r)
#else internalError(e.info, 'genMagic: ' + magicToStr[op]);

View File

@@ -31,7 +31,7 @@ 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".}
"Array".}
template strtr*(s: string, replacePairs: openarray[(string, string)]): string =
strtr(toArray(replacePairs))