Remove PHP backend. (#7606)
* remove php * fix * keep nimphpext * remove targetJS enum
This commit is contained in:
parent
a708fda733
commit
35b0cc67e8
9 changed files with 330 additions and 778 deletions
File diff suppressed because it is too large
Load diff
|
|
@ -121,26 +121,7 @@ proc genEnumInfo(p: PProc, typ: PType, name: Rope) =
|
||||||
addf(p.g.typeInfo, "$1.base = $2;$n",
|
addf(p.g.typeInfo, "$1.base = $2;$n",
|
||||||
[name, genTypeInfo(p, typ.sons[0])])
|
[name, genTypeInfo(p, typ.sons[0])])
|
||||||
|
|
||||||
proc genEnumInfoPHP(p: PProc; t: PType): Rope =
|
|
||||||
let t = t.skipTypes({tyGenericInst, tyDistinct, tyAlias, tySink})
|
|
||||||
result = "$$NTI$1" % [rope(t.id)]
|
|
||||||
p.declareGlobal(t.id, result)
|
|
||||||
if containsOrIncl(p.g.typeInfoGenerated, t.id): return
|
|
||||||
|
|
||||||
let length = sonsLen(t.n)
|
|
||||||
var s: Rope = nil
|
|
||||||
for i in countup(0, length - 1):
|
|
||||||
if (t.n.sons[i].kind != nkSym): internalError(t.n.info, "genEnumInfo")
|
|
||||||
let field = t.n.sons[i].sym
|
|
||||||
if i > 0: add(s, ", " & tnl)
|
|
||||||
let extName = if field.ast == nil: field.name.s else: field.ast.strVal
|
|
||||||
addf(s, "$# => $#$n",
|
|
||||||
[rope(field.position), makeJSString(extName)])
|
|
||||||
prepend(p.g.typeInfo, "$$$# = $#;$n" % [result, s])
|
|
||||||
|
|
||||||
proc genTypeInfo(p: PProc, typ: PType): Rope =
|
proc genTypeInfo(p: PProc, typ: PType): Rope =
|
||||||
if p.target == targetPHP:
|
|
||||||
return makeJSString(typeToString(typ, preferModuleInfo))
|
|
||||||
let t = typ.skipTypes({tyGenericInst, tyDistinct, tyAlias, tySink})
|
let t = typ.skipTypes({tyGenericInst, tyDistinct, tyAlias, tySink})
|
||||||
result = "NTI$1" % [rope(t.id)]
|
result = "NTI$1" % [rope(t.id)]
|
||||||
if containsOrIncl(p.g.typeInfoGenerated, t.id): return
|
if containsOrIncl(p.g.typeInfoGenerated, t.id): return
|
||||||
|
|
|
||||||
|
|
@ -230,7 +230,7 @@ template isIterator*(owner: PSym): bool =
|
||||||
proc liftingHarmful(owner: PSym): bool {.inline.} =
|
proc liftingHarmful(owner: PSym): bool {.inline.} =
|
||||||
## lambda lifting can be harmful for JS-like code generators.
|
## lambda lifting can be harmful for JS-like code generators.
|
||||||
let isCompileTime = sfCompileTime in owner.flags or owner.kind == skMacro
|
let isCompileTime = sfCompileTime in owner.flags or owner.kind == skMacro
|
||||||
result = gCmd in {cmdCompileToPHP, cmdCompileToJS} and not isCompileTime
|
result = gCmd == cmdCompileToJS and not isCompileTime
|
||||||
|
|
||||||
proc liftIterSym*(n: PNode; owner: PSym): PNode =
|
proc liftIterSym*(n: PNode; owner: PSym): PNode =
|
||||||
# transforms (iter) to (let env = newClosure[iter](); (iter, env))
|
# transforms (iter) to (let env = newClosure[iter](); (iter, env))
|
||||||
|
|
@ -813,7 +813,7 @@ proc liftLambdas*(fn: PSym, body: PNode; tooEarly: var bool): PNode =
|
||||||
let isCompileTime = sfCompileTime in fn.flags or fn.kind == skMacro
|
let isCompileTime = sfCompileTime in fn.flags or fn.kind == skMacro
|
||||||
|
|
||||||
if body.kind == nkEmpty or (
|
if body.kind == nkEmpty or (
|
||||||
gCmd in {cmdCompileToPHP, cmdCompileToJS} and not isCompileTime) or
|
gCmd == cmdCompileToJS and not isCompileTime) or
|
||||||
fn.skipGenericOwner.kind != skModule:
|
fn.skipGenericOwner.kind != skModule:
|
||||||
# ignore forward declaration:
|
# ignore forward declaration:
|
||||||
result = body
|
result = body
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,6 @@ proc commandCompileToJS(graph: ModuleGraph; cache: IdentCache) =
|
||||||
defineSymbol("nimrod") # 'nimrod' is always defined
|
defineSymbol("nimrod") # 'nimrod' is always defined
|
||||||
defineSymbol("ecmascript") # For backward compatibility
|
defineSymbol("ecmascript") # For backward compatibility
|
||||||
defineSymbol("js")
|
defineSymbol("js")
|
||||||
if gCmd == cmdCompileToPHP: defineSymbol("nimphp")
|
|
||||||
semanticPasses()
|
semanticPasses()
|
||||||
registerPass(JSgenPass)
|
registerPass(JSgenPass)
|
||||||
compileProject(graph, cache)
|
compileProject(graph, cache)
|
||||||
|
|
@ -189,9 +188,6 @@ proc mainCommand*(graph: ModuleGraph; cache: IdentCache) =
|
||||||
of "js", "compiletojs":
|
of "js", "compiletojs":
|
||||||
gCmd = cmdCompileToJS
|
gCmd = cmdCompileToJS
|
||||||
commandCompileToJS(graph, cache)
|
commandCompileToJS(graph, cache)
|
||||||
of "php":
|
|
||||||
gCmd = cmdCompileToPHP
|
|
||||||
commandCompileToJS(graph, cache)
|
|
||||||
of "doc0":
|
of "doc0":
|
||||||
wantMainModule()
|
wantMainModule()
|
||||||
gCmd = cmdDoc
|
gCmd = cmdDoc
|
||||||
|
|
|
||||||
|
|
@ -90,14 +90,6 @@ proc handleCmdLine(cache: IdentCache; config: ConfigRef) =
|
||||||
ex = quoteShell(
|
ex = quoteShell(
|
||||||
completeCFilePath(changeFileExt(gProjectFull, "js").prependCurDir))
|
completeCFilePath(changeFileExt(gProjectFull, "js").prependCurDir))
|
||||||
execExternalProgram(findNodeJs() & " " & ex & ' ' & commands.arguments)
|
execExternalProgram(findNodeJs() & " " & ex & ' ' & commands.arguments)
|
||||||
elif gCmd == cmdCompileToPHP:
|
|
||||||
var ex: string
|
|
||||||
if options.outFile.len > 0:
|
|
||||||
ex = options.outFile.prependCurDir.quoteShell
|
|
||||||
else:
|
|
||||||
ex = quoteShell(
|
|
||||||
completeCFilePath(changeFileExt(gProjectFull, "php").prependCurDir))
|
|
||||||
execExternalProgram("php " & ex & ' ' & commands.arguments)
|
|
||||||
else:
|
else:
|
||||||
var binPath: string
|
var binPath: string
|
||||||
if options.outFile.len > 0:
|
if options.outFile.len > 0:
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,6 @@ type
|
||||||
# **keep binary compatible**
|
# **keep binary compatible**
|
||||||
cmdNone, cmdCompileToC, cmdCompileToCpp, cmdCompileToOC,
|
cmdNone, cmdCompileToC, cmdCompileToCpp, cmdCompileToOC,
|
||||||
cmdCompileToJS,
|
cmdCompileToJS,
|
||||||
cmdCompileToPHP,
|
|
||||||
cmdCompileToLLVM, cmdInterpret, cmdPretty, cmdDoc,
|
cmdCompileToLLVM, cmdInterpret, cmdPretty, cmdDoc,
|
||||||
cmdGenDepend, cmdDump,
|
cmdGenDepend, cmdDump,
|
||||||
cmdCheck, # semantic checking for whole project
|
cmdCheck, # semantic checking for whole project
|
||||||
|
|
|
||||||
|
|
@ -586,7 +586,7 @@ proc cmdChangeTriggersRecompilation(old, new: TCommands): bool =
|
||||||
# new command forces us to consider it here :-)
|
# new command forces us to consider it here :-)
|
||||||
case old
|
case old
|
||||||
of cmdCompileToC, cmdCompileToCpp, cmdCompileToOC,
|
of cmdCompileToC, cmdCompileToCpp, cmdCompileToOC,
|
||||||
cmdCompileToJS, cmdCompileToPHP, cmdCompileToLLVM:
|
cmdCompileToJS, cmdCompileToLLVM:
|
||||||
if new in {cmdDoc, cmdCheck, cmdIdeTools, cmdPretty, cmdDef,
|
if new in {cmdDoc, cmdCheck, cmdIdeTools, cmdPretty, cmdDef,
|
||||||
cmdInteractive}:
|
cmdInteractive}:
|
||||||
return false
|
return false
|
||||||
|
|
|
||||||
|
|
@ -2404,7 +2404,7 @@ proc `==` *[T](x, y: seq[T]): bool {.noSideEffect.} =
|
||||||
if x.isNil and y.isNil:
|
if x.isNil and y.isNil:
|
||||||
return true
|
return true
|
||||||
else:
|
else:
|
||||||
when not defined(JS) or defined(nimphp):
|
when not defined(JS):
|
||||||
proc seqToPtr[T](x: seq[T]): pointer {.inline, nosideeffect.} =
|
proc seqToPtr[T](x: seq[T]): pointer {.inline, nosideeffect.} =
|
||||||
result = cast[pointer](x)
|
result = cast[pointer](x)
|
||||||
else:
|
else:
|
||||||
|
|
@ -2766,17 +2766,14 @@ type
|
||||||
|
|
||||||
when defined(JS):
|
when defined(JS):
|
||||||
proc add*(x: var string, y: cstring) {.asmNoStackFrame.} =
|
proc add*(x: var string, y: cstring) {.asmNoStackFrame.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """`x` .= `y`;"""
|
var len = `x`[0].length-1;
|
||||||
else:
|
for (var i = 0; i < `y`.length; ++i) {
|
||||||
asm """
|
`x`[0][len] = `y`.charCodeAt(i);
|
||||||
var len = `x`[0].length-1;
|
++len;
|
||||||
for (var i = 0; i < `y`.length; ++i) {
|
}
|
||||||
`x`[0][len] = `y`.charCodeAt(i);
|
`x`[0][len] = 0
|
||||||
++len;
|
"""
|
||||||
}
|
|
||||||
`x`[0][len] = 0
|
|
||||||
"""
|
|
||||||
proc add*(x: var cstring, y: cstring) {.magic: "AppendStrStr".}
|
proc add*(x: var cstring, y: cstring) {.magic: "AppendStrStr".}
|
||||||
|
|
||||||
elif hasAlloc:
|
elif hasAlloc:
|
||||||
|
|
|
||||||
|
|
@ -48,10 +48,7 @@ proc nimCharToStr(x: char): string {.compilerproc.} =
|
||||||
result[0] = x
|
result[0] = x
|
||||||
|
|
||||||
proc isNimException(): bool {.asmNoStackFrame.} =
|
proc isNimException(): bool {.asmNoStackFrame.} =
|
||||||
when defined(nimphp):
|
asm "return `lastJSError`.m_type;"
|
||||||
asm "return isset(`lastJSError`['m_type']);"
|
|
||||||
else:
|
|
||||||
asm "return `lastJSError`.m_type;"
|
|
||||||
|
|
||||||
proc getCurrentException*(): ref Exception {.compilerRtl, benign.} =
|
proc getCurrentException*(): ref Exception {.compilerRtl, benign.} =
|
||||||
if isNimException(): result = cast[ref Exception](lastJSError)
|
if isNimException(): result = cast[ref Exception](lastJSError)
|
||||||
|
|
@ -61,15 +58,14 @@ proc getCurrentExceptionMsg*(): string =
|
||||||
if isNimException():
|
if isNimException():
|
||||||
return cast[Exception](lastJSError).msg
|
return cast[Exception](lastJSError).msg
|
||||||
else:
|
else:
|
||||||
when not defined(nimphp):
|
var msg: cstring
|
||||||
var msg: cstring
|
{.emit: """
|
||||||
{.emit: """
|
if (`lastJSError`.message !== undefined) {
|
||||||
if (`lastJSError`.message !== undefined) {
|
`msg` = `lastJSError`.message;
|
||||||
`msg` = `lastJSError`.message;
|
}
|
||||||
}
|
""".}
|
||||||
""".}
|
if not msg.isNil:
|
||||||
if not msg.isNil:
|
return $msg
|
||||||
return $msg
|
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
proc auxWriteStackTrace(f: PCallFrame): string =
|
proc auxWriteStackTrace(f: PCallFrame): string =
|
||||||
|
|
@ -140,12 +136,9 @@ proc raiseException(e: ref Exception, ename: cstring) {.
|
||||||
e.name = ename
|
e.name = ename
|
||||||
if excHandler == 0:
|
if excHandler == 0:
|
||||||
unhandledException(e)
|
unhandledException(e)
|
||||||
when defined(nimphp):
|
when NimStackTrace:
|
||||||
asm """throw new Exception($`e`["message"]);"""
|
e.trace = rawWriteStackTrace()
|
||||||
else:
|
asm "throw `e`;"
|
||||||
when NimStackTrace:
|
|
||||||
e.trace = rawWriteStackTrace()
|
|
||||||
asm "throw `e`;"
|
|
||||||
|
|
||||||
proc reraiseException() {.compilerproc, asmNoStackFrame.} =
|
proc reraiseException() {.compilerproc, asmNoStackFrame.} =
|
||||||
if lastJSError == nil:
|
if lastJSError == nil:
|
||||||
|
|
@ -173,57 +166,35 @@ proc raiseFieldError(f: string) {.compilerproc, noreturn.} =
|
||||||
raise newException(FieldError, f & " is not accessible")
|
raise newException(FieldError, f & " is not accessible")
|
||||||
|
|
||||||
proc setConstr() {.varargs, asmNoStackFrame, compilerproc.} =
|
proc setConstr() {.varargs, asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
var result = {};
|
||||||
$args = func_get_args();
|
for (var i = 0; i < arguments.length; ++i) {
|
||||||
$result = array();
|
var x = arguments[i];
|
||||||
foreach ($args as $x) {
|
if (typeof(x) == "object") {
|
||||||
if (is_array($x)) {
|
for (var j = x[0]; j <= x[1]; ++j) {
|
||||||
for ($j = $x[0]; $j <= $x[1]; $j++) {
|
result[j] = true;
|
||||||
$result[$j] = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$result[$x] = true;
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
result[x] = true;
|
||||||
}
|
}
|
||||||
return $result;
|
}
|
||||||
"""
|
return result;
|
||||||
else:
|
"""
|
||||||
asm """
|
|
||||||
var result = {};
|
|
||||||
for (var i = 0; i < arguments.length; ++i) {
|
|
||||||
var x = arguments[i];
|
|
||||||
if (typeof(x) == "object") {
|
|
||||||
for (var j = x[0]; j <= x[1]; ++j) {
|
|
||||||
result[j] = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
result[x] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc makeNimstrLit(c: cstring): string {.asmNoStackFrame, compilerproc.} =
|
proc makeNimstrLit(c: cstring): string {.asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
{.emit: """
|
||||||
{.emit: """return `c`;""".}
|
var ln = `c`.length;
|
||||||
else:
|
var result = new Array(ln + 1);
|
||||||
{.emit: """
|
var i = 0;
|
||||||
var ln = `c`.length;
|
for (; i < ln; ++i) {
|
||||||
var result = new Array(ln + 1);
|
result[i] = `c`.charCodeAt(i);
|
||||||
var i = 0;
|
}
|
||||||
for (; i < ln; ++i) {
|
result[i] = 0; // terminating zero
|
||||||
result[i] = `c`.charCodeAt(i);
|
return result;
|
||||||
}
|
""".}
|
||||||
result[i] = 0; // terminating zero
|
|
||||||
return result;
|
|
||||||
""".}
|
|
||||||
|
|
||||||
proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
|
proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
{.emit: """
|
||||||
{.emit: """return `c`;""".}
|
|
||||||
else:
|
|
||||||
{.emit: """
|
|
||||||
var ln = `c`.length;
|
var ln = `c`.length;
|
||||||
var result = new Array(ln);
|
var result = new Array(ln);
|
||||||
var r = 0;
|
var r = 0;
|
||||||
|
|
@ -261,156 +232,93 @@ proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
|
||||||
""".}
|
""".}
|
||||||
|
|
||||||
proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} =
|
proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
{.emit: """return `s`;""".}
|
var len = `s`.length-1;
|
||||||
else:
|
var asciiPart = new Array(len);
|
||||||
asm """
|
var fcc = String.fromCharCode;
|
||||||
var len = `s`.length-1;
|
var nonAsciiPart = null;
|
||||||
var asciiPart = new Array(len);
|
var nonAsciiOffset = 0;
|
||||||
var fcc = String.fromCharCode;
|
for (var i = 0; i < len; ++i) {
|
||||||
var nonAsciiPart = null;
|
if (nonAsciiPart !== null) {
|
||||||
var nonAsciiOffset = 0;
|
var offset = (i - nonAsciiOffset) * 2;
|
||||||
for (var i = 0; i < len; ++i) {
|
var code = `s`[i].toString(16);
|
||||||
if (nonAsciiPart !== null) {
|
if (code.length == 1) {
|
||||||
var offset = (i - nonAsciiOffset) * 2;
|
code = "0"+code;
|
||||||
var code = `s`[i].toString(16);
|
|
||||||
if (code.length == 1) {
|
|
||||||
code = "0"+code;
|
|
||||||
}
|
|
||||||
nonAsciiPart[offset] = "%";
|
|
||||||
nonAsciiPart[offset + 1] = code;
|
|
||||||
}
|
|
||||||
else if (`s`[i] < 128)
|
|
||||||
asciiPart[i] = fcc(`s`[i]);
|
|
||||||
else {
|
|
||||||
asciiPart.length = i;
|
|
||||||
nonAsciiOffset = i;
|
|
||||||
nonAsciiPart = new Array((len - i) * 2);
|
|
||||||
--i;
|
|
||||||
}
|
}
|
||||||
|
nonAsciiPart[offset] = "%";
|
||||||
|
nonAsciiPart[offset + 1] = code;
|
||||||
}
|
}
|
||||||
asciiPart = asciiPart.join("");
|
else if (`s`[i] < 128)
|
||||||
return (nonAsciiPart === null) ?
|
asciiPart[i] = fcc(`s`[i]);
|
||||||
asciiPart : asciiPart + decodeURIComponent(nonAsciiPart.join(""));
|
else {
|
||||||
|
asciiPart.length = i;
|
||||||
|
nonAsciiOffset = i;
|
||||||
|
nonAsciiPart = new Array((len - i) * 2);
|
||||||
|
--i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
asciiPart = asciiPart.join("");
|
||||||
|
return (nonAsciiPart === null) ?
|
||||||
|
asciiPart : asciiPart + decodeURIComponent(nonAsciiPart.join(""));
|
||||||
"""
|
"""
|
||||||
|
|
||||||
proc mnewString(len: int): string {.asmNoStackFrame, compilerproc.} =
|
proc mnewString(len: int): string {.asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
var result = new Array(`len`+1);
|
||||||
return str_repeat(chr(0),`len`);
|
result[0] = 0;
|
||||||
"""
|
result[`len`] = 0;
|
||||||
else:
|
return result;
|
||||||
asm """
|
"""
|
||||||
var result = new Array(`len`+1);
|
|
||||||
result[0] = 0;
|
|
||||||
result[`len`] = 0;
|
|
||||||
return result;
|
|
||||||
"""
|
|
||||||
|
|
||||||
when defined(nimphp):
|
|
||||||
proc nimAt(x: string; i: int): string {.asmNoStackFrame, compilerproc.} =
|
|
||||||
asm """
|
|
||||||
return `x`[`i`];
|
|
||||||
"""
|
|
||||||
|
|
||||||
when defined(nimphp):
|
|
||||||
proc nimSubstr(s: string; a, b: int): string {.
|
|
||||||
asmNoStackFrame, compilerproc.} =
|
|
||||||
asm """return substr(`s`,`a`,`b`-`a`+1);"""
|
|
||||||
|
|
||||||
proc SetCard(a: int): int {.compilerproc, asmNoStackFrame.} =
|
proc SetCard(a: int): int {.compilerproc, asmNoStackFrame.} =
|
||||||
# argument type is a fake
|
# argument type is a fake
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
var result = 0;
|
||||||
return count(`a`);
|
for (var elem in `a`) { ++result; }
|
||||||
"""
|
return result;
|
||||||
else:
|
"""
|
||||||
asm """
|
|
||||||
var result = 0;
|
|
||||||
for (var elem in `a`) { ++result; }
|
|
||||||
return result;
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc SetEq(a, b: int): bool {.compilerproc, asmNoStackFrame.} =
|
proc SetEq(a, b: int): bool {.compilerproc, asmNoStackFrame.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
for (var elem in `a`) { if (!`b`[elem]) return false; }
|
||||||
foreach (`a` as $elem=>$_) { if (!isset(`b`[$elem])) return false; }
|
for (var elem in `b`) { if (!`a`[elem]) return false; }
|
||||||
foreach (`b` as $elem=>$_) { if (!isset(`a`[$elem])) return false; }
|
return true;
|
||||||
return true;
|
"""
|
||||||
"""
|
|
||||||
else:
|
|
||||||
asm """
|
|
||||||
for (var elem in `a`) { if (!`b`[elem]) return false; }
|
|
||||||
for (var elem in `b`) { if (!`a`[elem]) return false; }
|
|
||||||
return true;
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc SetLe(a, b: int): bool {.compilerproc, asmNoStackFrame.} =
|
proc SetLe(a, b: int): bool {.compilerproc, asmNoStackFrame.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
for (var elem in `a`) { if (!`b`[elem]) return false; }
|
||||||
foreach (`a` as $elem=>$_) { if (!isset(`b`[$elem])) return false; }
|
return true;
|
||||||
return true;
|
"""
|
||||||
"""
|
|
||||||
else:
|
|
||||||
asm """
|
|
||||||
for (var elem in `a`) { if (!`b`[elem]) return false; }
|
|
||||||
return true;
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc SetLt(a, b: int): bool {.compilerproc.} =
|
proc SetLt(a, b: int): bool {.compilerproc.} =
|
||||||
result = SetLe(a, b) and not SetEq(a, b)
|
result = SetLe(a, b) and not SetEq(a, b)
|
||||||
|
|
||||||
proc SetMul(a, b: int): int {.compilerproc, asmNoStackFrame.} =
|
proc SetMul(a, b: int): int {.compilerproc, asmNoStackFrame.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
var result = {};
|
||||||
var $result = array();
|
for (var elem in `a`) {
|
||||||
foreach (`a` as $elem=>$_) {
|
if (`b`[elem]) { result[elem] = true; }
|
||||||
if (isset(`b`[$elem])) { $result[$elem] = true; }
|
}
|
||||||
}
|
return result;
|
||||||
return $result;
|
"""
|
||||||
"""
|
|
||||||
else:
|
|
||||||
asm """
|
|
||||||
var result = {};
|
|
||||||
for (var elem in `a`) {
|
|
||||||
if (`b`[elem]) { result[elem] = true; }
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc SetPlus(a, b: int): int {.compilerproc, asmNoStackFrame.} =
|
proc SetPlus(a, b: int): int {.compilerproc, asmNoStackFrame.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
var result = {};
|
||||||
var $result = array();
|
for (var elem in `a`) { result[elem] = true; }
|
||||||
foreach (`a` as $elem=>$_) { $result[$elem] = true; }
|
for (var elem in `b`) { result[elem] = true; }
|
||||||
foreach (`b` as $elem=>$_) { $result[$elem] = true; }
|
return result;
|
||||||
return $result;
|
"""
|
||||||
"""
|
|
||||||
else:
|
|
||||||
asm """
|
|
||||||
var result = {};
|
|
||||||
for (var elem in `a`) { result[elem] = true; }
|
|
||||||
for (var elem in `b`) { result[elem] = true; }
|
|
||||||
return result;
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc SetMinus(a, b: int): int {.compilerproc, asmNoStackFrame.} =
|
proc SetMinus(a, b: int): int {.compilerproc, asmNoStackFrame.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
var result = {};
|
||||||
$result = array();
|
for (var elem in `a`) {
|
||||||
foreach (`a` as $elem=>$_) {
|
if (!`b`[elem]) { result[elem] = true; }
|
||||||
if (!isset(`b`[$elem])) { $result[$elem] = true; }
|
}
|
||||||
}
|
return result;
|
||||||
return $result;
|
"""
|
||||||
"""
|
|
||||||
else:
|
|
||||||
asm """
|
|
||||||
var result = {};
|
|
||||||
for (var elem in `a`) {
|
|
||||||
if (!`b`[elem]) { result[elem] = true; }
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc cmpStrings(a, b: string): int {.asmNoStackFrame, compilerProc.} =
|
proc cmpStrings(a, b: string): int {.asmNoStackFrame, compilerProc.} =
|
||||||
asm """
|
asm """
|
||||||
|
|
@ -424,15 +332,8 @@ proc cmpStrings(a, b: string): int {.asmNoStackFrame, compilerProc.} =
|
||||||
return `a`.length - `b`.length;
|
return `a`.length - `b`.length;
|
||||||
"""
|
"""
|
||||||
|
|
||||||
proc cmp(x, y: string): int =
|
proc cmp(x, y: string): int =
|
||||||
when defined(nimphp):
|
return cmpStrings(x, y)
|
||||||
asm """
|
|
||||||
if(`x` < `y`) `result` = -1;
|
|
||||||
elseif (`x` > `y`) `result` = 1;
|
|
||||||
else `result` = 0;
|
|
||||||
"""
|
|
||||||
else:
|
|
||||||
return cmpStrings(x, y)
|
|
||||||
|
|
||||||
proc eqStrings(a, b: string): bool {.asmNoStackFrame, compilerProc.} =
|
proc eqStrings(a, b: string): bool {.asmNoStackFrame, compilerProc.} =
|
||||||
asm """
|
asm """
|
||||||
|
|
@ -467,7 +368,7 @@ elif not defined(nimOldEcho):
|
||||||
console.log(buf);
|
console.log(buf);
|
||||||
"""
|
"""
|
||||||
|
|
||||||
elif not defined(nimphp):
|
else:
|
||||||
proc ewriteln(x: cstring) =
|
proc ewriteln(x: cstring) =
|
||||||
var node : JSRef
|
var node : JSRef
|
||||||
{.emit: "`node` = document.getElementsByTagName('body')[0];".}
|
{.emit: "`node` = document.getElementsByTagName('body')[0];".}
|
||||||
|
|
@ -493,127 +394,77 @@ elif not defined(nimphp):
|
||||||
|
|
||||||
# Arithmetic:
|
# Arithmetic:
|
||||||
proc addInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
proc addInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
var result = `a` + `b`;
|
||||||
return `a` + `b`;
|
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
|
||||||
"""
|
return result;
|
||||||
else:
|
"""
|
||||||
asm """
|
|
||||||
var result = `a` + `b`;
|
|
||||||
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
|
|
||||||
return result;
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc subInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
proc subInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
var result = `a` - `b`;
|
||||||
return `a` - `b`;
|
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
|
||||||
"""
|
return result;
|
||||||
else:
|
"""
|
||||||
asm """
|
|
||||||
var result = `a` - `b`;
|
|
||||||
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
|
|
||||||
return result;
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc mulInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
proc mulInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
var result = `a` * `b`;
|
||||||
return `a` * `b`;
|
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
|
||||||
"""
|
return result;
|
||||||
else:
|
"""
|
||||||
asm """
|
|
||||||
var result = `a` * `b`;
|
|
||||||
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
|
|
||||||
return result;
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc divInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
proc divInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
if (`b` == 0) `raiseDivByZero`();
|
||||||
return trunc(`a` / `b`);
|
if (`b` == -1 && `a` == 2147483647) `raiseOverflow`();
|
||||||
"""
|
return Math.trunc(`a` / `b`);
|
||||||
else:
|
"""
|
||||||
asm """
|
|
||||||
if (`b` == 0) `raiseDivByZero`();
|
|
||||||
if (`b` == -1 && `a` == 2147483647) `raiseOverflow`();
|
|
||||||
return Math.trunc(`a` / `b`);
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc modInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
proc modInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
if (`b` == 0) `raiseDivByZero`();
|
||||||
return `a` % `b`;
|
if (`b` == -1 && `a` == 2147483647) `raiseOverflow`();
|
||||||
"""
|
return Math.trunc(`a` % `b`);
|
||||||
else:
|
"""
|
||||||
asm """
|
|
||||||
if (`b` == 0) `raiseDivByZero`();
|
|
||||||
if (`b` == -1 && `a` == 2147483647) `raiseOverflow`();
|
|
||||||
return Math.trunc(`a` % `b`);
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc addInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
proc addInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
var result = `a` + `b`;
|
||||||
return `a` + `b`;
|
if (result > 9223372036854775807
|
||||||
"""
|
|| result < -9223372036854775808) `raiseOverflow`();
|
||||||
else:
|
return result;
|
||||||
asm """
|
"""
|
||||||
var result = `a` + `b`;
|
|
||||||
if (result > 9223372036854775807
|
|
||||||
|| result < -9223372036854775808) `raiseOverflow`();
|
|
||||||
return result;
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc subInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
proc subInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
var result = `a` - `b`;
|
||||||
return `a` - `b`;
|
if (result > 9223372036854775807
|
||||||
"""
|
|| result < -9223372036854775808) `raiseOverflow`();
|
||||||
else:
|
return result;
|
||||||
asm """
|
"""
|
||||||
var result = `a` - `b`;
|
|
||||||
if (result > 9223372036854775807
|
|
||||||
|| result < -9223372036854775808) `raiseOverflow`();
|
|
||||||
return result;
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc mulInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
proc mulInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
var result = `a` * `b`;
|
||||||
return `a` * `b`;
|
if (result > 9223372036854775807
|
||||||
"""
|
|| result < -9223372036854775808) `raiseOverflow`();
|
||||||
else:
|
return result;
|
||||||
asm """
|
"""
|
||||||
var result = `a` * `b`;
|
|
||||||
if (result > 9223372036854775807
|
|
||||||
|| result < -9223372036854775808) `raiseOverflow`();
|
|
||||||
return result;
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc divInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
proc divInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
if (`b` == 0) `raiseDivByZero`();
|
||||||
return trunc(`a` / `b`);
|
if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`();
|
||||||
"""
|
return Math.trunc(`a` / `b`);
|
||||||
else:
|
"""
|
||||||
asm """
|
|
||||||
if (`b` == 0) `raiseDivByZero`();
|
|
||||||
if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`();
|
|
||||||
return Math.trunc(`a` / `b`);
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc modInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
proc modInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||||
when defined(nimphp):
|
asm """
|
||||||
asm """
|
if (`b` == 0) `raiseDivByZero`();
|
||||||
return `a` % `b`;
|
if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`();
|
||||||
"""
|
return Math.trunc(`a` % `b`);
|
||||||
else:
|
"""
|
||||||
asm """
|
|
||||||
if (`b` == 0) `raiseDivByZero`();
|
|
||||||
if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`();
|
|
||||||
return Math.trunc(`a` % `b`);
|
|
||||||
"""
|
|
||||||
|
|
||||||
proc negInt(a: int): int {.compilerproc.} =
|
proc negInt(a: int): int {.compilerproc.} =
|
||||||
result = a*(-1)
|
result = a*(-1)
|
||||||
|
|
@ -767,24 +618,14 @@ proc genericReset(x: JSRef, ti: PNimType): JSRef {.compilerproc.} =
|
||||||
else:
|
else:
|
||||||
discard
|
discard
|
||||||
|
|
||||||
when defined(nimphp):
|
proc arrayConstr(len: int, value: JSRef, typ: PNimType): JSRef {.
|
||||||
proc arrayConstr(len: int, value: string, typ: string): JSRef {.
|
asmNoStackFrame, compilerproc.} =
|
||||||
asmNoStackFrame, compilerproc.} =
|
|
||||||
# types are fake
|
|
||||||
asm """
|
|
||||||
$result = array();
|
|
||||||
for ($i = 0; $i < `len`; $i++) $result[] = `value`;
|
|
||||||
return $result;
|
|
||||||
"""
|
|
||||||
else:
|
|
||||||
proc arrayConstr(len: int, value: JSRef, typ: PNimType): JSRef {.
|
|
||||||
asmNoStackFrame, compilerproc.} =
|
|
||||||
# types are fake
|
# types are fake
|
||||||
asm """
|
asm """
|
||||||
var result = new Array(`len`);
|
var result = new Array(`len`);
|
||||||
for (var i = 0; i < `len`; ++i) result[i] = nimCopy(null, `value`, `typ`);
|
for (var i = 0; i < `len`; ++i) result[i] = nimCopy(null, `value`, `typ`);
|
||||||
return result;
|
return result;
|
||||||
"""
|
"""
|
||||||
|
|
||||||
proc chckIndx(i, a, b: int): int {.compilerproc.} =
|
proc chckIndx(i, a, b: int): int {.compilerproc.} =
|
||||||
if i >= a and i <= b: return i
|
if i >= a and i <= b: return i
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue