first version of an PHP codegen
This commit is contained in:
parent
58f6bfb1dc
commit
35567a1eb9
8 changed files with 665 additions and 411 deletions
File diff suppressed because it is too large
Load diff
|
|
@ -116,6 +116,8 @@ proc genEnumInfo(p: PProc, typ: PType, name: Rope) =
|
|||
[name, genTypeInfo(p, typ.sons[0])])
|
||||
|
||||
proc genTypeInfo(p: PProc, typ: PType): Rope =
|
||||
if p.target == targetPHP:
|
||||
return makeJSString(typeToString(typ, preferModuleInfo))
|
||||
let t = typ.skipTypes({tyGenericInst, tyDistinct})
|
||||
result = "NTI$1" % [rope(t.id)]
|
||||
if containsOrIncl(p.g.typeInfoGenerated, t.id): return
|
||||
|
|
|
|||
|
|
@ -108,6 +108,7 @@ proc commandCompileToJS =
|
|||
defineSymbol("nimrod") # 'nimrod' is always defined
|
||||
defineSymbol("ecmascript") # For backward compatibility
|
||||
defineSymbol("js")
|
||||
if gCmd == cmdCompileToPHP: defineSymbol("nimphp")
|
||||
semanticPasses()
|
||||
registerPass(JSgenPass)
|
||||
compileProject()
|
||||
|
|
@ -267,6 +268,9 @@ proc mainCommand* =
|
|||
of "js", "compiletojs":
|
||||
gCmd = cmdCompileToJS
|
||||
commandCompileToJS()
|
||||
of "php":
|
||||
gCmd = cmdCompileToPHP
|
||||
commandCompileToJS()
|
||||
of "doc":
|
||||
wantMainModule()
|
||||
gCmd = cmdDoc
|
||||
|
|
|
|||
|
|
@ -84,6 +84,14 @@ proc handleCmdLine() =
|
|||
ex = quoteShell(
|
||||
completeCFilePath(changeFileExt(gProjectFull, "js").prependCurDir))
|
||||
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:
|
||||
var binPath: string
|
||||
if options.outFile.len > 0:
|
||||
|
|
|
|||
|
|
@ -70,7 +70,9 @@ type # please make sure we have under 32 options
|
|||
TCommands* = enum # Nim's commands
|
||||
# **keep binary compatible**
|
||||
cmdNone, cmdCompileToC, cmdCompileToCpp, cmdCompileToOC,
|
||||
cmdCompileToJS, cmdCompileToLLVM, cmdInterpret, cmdPretty, cmdDoc,
|
||||
cmdCompileToJS,
|
||||
cmdCompileToPHP,
|
||||
cmdCompileToLLVM, cmdInterpret, cmdPretty, cmdDoc,
|
||||
cmdGenDepend, cmdDump,
|
||||
cmdCheck, # semantic checking for whole project
|
||||
cmdParse, # parse a single file (for debugging)
|
||||
|
|
|
|||
|
|
@ -527,7 +527,7 @@ proc cmdChangeTriggersRecompilation(old, new: TCommands): bool =
|
|||
# new command forces us to consider it here :-)
|
||||
case old
|
||||
of cmdCompileToC, cmdCompileToCpp, cmdCompileToOC,
|
||||
cmdCompileToJS, cmdCompileToLLVM:
|
||||
cmdCompileToJS, cmdCompileToPHP, cmdCompileToLLVM:
|
||||
if new in {cmdDoc, cmdCheck, cmdIdeTools, cmdPretty, cmdDef,
|
||||
cmdInteractive}:
|
||||
return false
|
||||
|
|
|
|||
|
|
@ -2452,6 +2452,9 @@ type
|
|||
|
||||
when defined(JS):
|
||||
proc add*(x: var string, y: cstring) {.asmNoStackFrame.} =
|
||||
when defined(nimphp):
|
||||
asm """`x` .= `y`;"""
|
||||
else:
|
||||
asm """
|
||||
var len = `x`[0].length-1;
|
||||
for (var i = 0; i < `y`.length; ++i) {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#
|
||||
#
|
||||
# Nim's Runtime Library
|
||||
# (c) Copyright 2012 Andreas Rumpf
|
||||
# (c) Copyright 2015 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
|
|
@ -61,7 +61,6 @@ proc getCurrentExceptionMsg*(): string =
|
|||
proc auxWriteStackTrace(f: PCallFrame): string =
|
||||
type
|
||||
TempFrame = tuple[procname: cstring, line: int]
|
||||
{.deprecated: [TTempFrame: TempFrame].}
|
||||
var
|
||||
it = f
|
||||
i = 0
|
||||
|
|
@ -128,7 +127,10 @@ proc reraiseException() {.compilerproc, asmNoStackFrame.} =
|
|||
else:
|
||||
when not defined(noUnhandledHandler):
|
||||
if excHandler == 0:
|
||||
var isNimException : bool
|
||||
var isNimException: bool
|
||||
when defined(nimphp):
|
||||
asm "`isNimException` = isset(`lastJSError`['m_type']);"
|
||||
else:
|
||||
asm "`isNimException` = lastJSError.m_type;"
|
||||
if isNimException:
|
||||
unhandledException(cast[ref Exception](lastJSError))
|
||||
|
|
@ -150,6 +152,22 @@ proc raiseFieldError(f: string) {.compilerproc, noreturn.} =
|
|||
raise newException(FieldError, f & " is not accessible")
|
||||
|
||||
proc SetConstr() {.varargs, asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
$args = func_get_args();
|
||||
$result = array();
|
||||
foreach ($args as $key=>$x) {
|
||||
if (is_array(($val)) {
|
||||
for ($j = $x[0]; $j <= $x[1]; $j++) {
|
||||
result[$j] = true;
|
||||
}
|
||||
} else {
|
||||
result[$x] = true;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
var result = {};
|
||||
for (var i = 0; i < arguments.length; ++i) {
|
||||
|
|
@ -166,6 +184,9 @@ proc SetConstr() {.varargs, asmNoStackFrame, compilerproc.} =
|
|||
"""
|
||||
|
||||
proc makeNimstrLit(c: cstring): string {.asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
{.emit: """return `c`;""".}
|
||||
else:
|
||||
{.emit: """
|
||||
var ln = `c`.length;
|
||||
var result = new Array(ln + 1);
|
||||
|
|
@ -178,6 +199,9 @@ proc makeNimstrLit(c: cstring): string {.asmNoStackFrame, compilerproc.} =
|
|||
""".}
|
||||
|
||||
proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
{.emit: """return `c`;""".}
|
||||
else:
|
||||
{.emit: """
|
||||
var ln = `c`.length;
|
||||
var result = new Array(ln);
|
||||
|
|
@ -207,6 +231,9 @@ proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
|
|||
""".}
|
||||
|
||||
proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
{.emit: """return `s`;""".}
|
||||
else:
|
||||
asm """
|
||||
var len = `s`.length-1;
|
||||
var asciiPart = new Array(len);
|
||||
|
|
@ -234,6 +261,13 @@ proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} =
|
|||
"""
|
||||
|
||||
proc mnewString(len: int): string {.asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
$result = array();
|
||||
for($i = 0; $i < `len`; $i++) $result[] = chr(0);
|
||||
return $result;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
var result = new Array(`len`+1);
|
||||
result[0] = 0;
|
||||
|
|
@ -243,6 +277,11 @@ proc mnewString(len: int): string {.asmNoStackFrame, compilerproc.} =
|
|||
|
||||
proc SetCard(a: int): int {.compilerproc, asmNoStackFrame.} =
|
||||
# argument type is a fake
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
return count(`a`);
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
var result = 0;
|
||||
for (var elem in `a`) { ++result; }
|
||||
|
|
@ -250,6 +289,13 @@ proc SetCard(a: int): int {.compilerproc, asmNoStackFrame.} =
|
|||
"""
|
||||
|
||||
proc SetEq(a, b: int): bool {.compilerproc, asmNoStackFrame.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
foreach (`a` as $elem=>$_) { if (!isset(`b`[$elem])) return false; }
|
||||
foreach (`b` as $elem=>$_) { if (!isset(`a`[$elem])) return false; }
|
||||
return true;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
for (var elem in `a`) { if (!`b`[elem]) return false; }
|
||||
for (var elem in `b`) { if (!`a`[elem]) return false; }
|
||||
|
|
@ -257,6 +303,12 @@ proc SetEq(a, b: int): bool {.compilerproc, asmNoStackFrame.} =
|
|||
"""
|
||||
|
||||
proc SetLe(a, b: int): bool {.compilerproc, asmNoStackFrame.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
foreach (`a` as $elem=>$_) { if (!isset(`b`[$elem])) return false; }
|
||||
return true;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
for (var elem in `a`) { if (!`b`[elem]) return false; }
|
||||
return true;
|
||||
|
|
@ -266,6 +318,15 @@ proc SetLt(a, b: int): bool {.compilerproc.} =
|
|||
result = SetLe(a, b) and not SetEq(a, b)
|
||||
|
||||
proc SetMul(a, b: int): int {.compilerproc, asmNoStackFrame.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
var $result = array();
|
||||
foreach (`a` as $elem=>$_) {
|
||||
if (isset(`b`[$elem])) { $result[$elem] = true; }
|
||||
}
|
||||
return $result;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
var result = {};
|
||||
for (var elem in `a`) {
|
||||
|
|
@ -275,6 +336,14 @@ proc SetMul(a, b: int): int {.compilerproc, asmNoStackFrame.} =
|
|||
"""
|
||||
|
||||
proc SetPlus(a, b: int): int {.compilerproc, asmNoStackFrame.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
var $result = array();
|
||||
foreach (`a` as $elem=>$_) { $result[$elem] = true; }
|
||||
foreach (`b` as $elem=>$_) { $result[$elem] = true; }
|
||||
return $result;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
var result = {};
|
||||
for (var elem in `a`) { result[elem] = true; }
|
||||
|
|
@ -283,6 +352,15 @@ proc SetPlus(a, b: int): int {.compilerproc, asmNoStackFrame.} =
|
|||
"""
|
||||
|
||||
proc SetMinus(a, b: int): int {.compilerproc, asmNoStackFrame.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
$result = array();
|
||||
foreach (`a` as $elem=>$_) {
|
||||
if (!isset(`b`[$elem])) { $result[$elem] = true; }
|
||||
}
|
||||
return $result;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
var result = {};
|
||||
for (var elem in `a`) {
|
||||
|
|
@ -303,7 +381,15 @@ proc cmpStrings(a, b: string): int {.asmNoStackFrame, compilerProc.} =
|
|||
return 0;
|
||||
"""
|
||||
|
||||
proc cmp(x, y: string): int = return cmpStrings(x, y)
|
||||
proc cmp(x, y: string): int =
|
||||
when defined(nimphp):
|
||||
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.} =
|
||||
asm """
|
||||
|
|
@ -338,7 +424,7 @@ elif defined(nodejs):
|
|||
console.log(buf);
|
||||
"""
|
||||
|
||||
else:
|
||||
elif not defined(nimphp):
|
||||
proc ewriteln(x: cstring) =
|
||||
var node : JSRef
|
||||
{.emit: "`node` = document.getElementsByTagName('body')[0];".}
|
||||
|
|
@ -364,6 +450,11 @@ else:
|
|||
|
||||
# Arithmetic:
|
||||
proc addInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
return `a` + `b`;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
var result = `a` + `b`;
|
||||
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
|
||||
|
|
@ -371,6 +462,11 @@ proc addInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
|||
"""
|
||||
|
||||
proc subInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
return `a` - `b`;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
var result = `a` - `b`;
|
||||
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
|
||||
|
|
@ -378,6 +474,11 @@ proc subInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
|||
"""
|
||||
|
||||
proc mulInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
return `a` * `b`;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
var result = `a` * `b`;
|
||||
if (result > 2147483647 || result < -2147483648) `raiseOverflow`();
|
||||
|
|
@ -385,6 +486,11 @@ proc mulInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
|||
"""
|
||||
|
||||
proc divInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
return floor(`a` / `b`);
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
if (`b` == 0) `raiseDivByZero`();
|
||||
if (`b` == -1 && `a` == 2147483647) `raiseOverflow`();
|
||||
|
|
@ -392,6 +498,11 @@ proc divInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
|||
"""
|
||||
|
||||
proc modInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
return `a` % `b`;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
if (`b` == 0) `raiseDivByZero`();
|
||||
if (`b` == -1 && `a` == 2147483647) `raiseOverflow`();
|
||||
|
|
@ -399,6 +510,11 @@ proc modInt(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
|||
"""
|
||||
|
||||
proc addInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
return `a` + `b`;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
var result = `a` + `b`;
|
||||
if (result > 9223372036854775807
|
||||
|
|
@ -407,6 +523,11 @@ proc addInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
|||
"""
|
||||
|
||||
proc subInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
return `a` - `b`;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
var result = `a` - `b`;
|
||||
if (result > 9223372036854775807
|
||||
|
|
@ -415,6 +536,11 @@ proc subInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
|||
"""
|
||||
|
||||
proc mulInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
return `a` * `b`;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
var result = `a` * `b`;
|
||||
if (result > 9223372036854775807
|
||||
|
|
@ -423,6 +549,11 @@ proc mulInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
|||
"""
|
||||
|
||||
proc divInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
return floor(`a` / `b`);
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
if (`b` == 0) `raiseDivByZero`();
|
||||
if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`();
|
||||
|
|
@ -430,6 +561,11 @@ proc divInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
|||
"""
|
||||
|
||||
proc modInt64(a, b: int): int {.asmNoStackFrame, compilerproc.} =
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
return `a` % `b`;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
if (`b` == 0) `raiseDivByZero`();
|
||||
if (`b` == -1 && `a` == 9223372036854775807) `raiseOverflow`();
|
||||
|
|
@ -587,6 +723,13 @@ proc genericReset(x: JSRef, ti: PNimType): JSRef {.compilerproc.} =
|
|||
proc arrayConstr(len: int, value: JSRef, typ: PNimType): JSRef {.
|
||||
asmNoStackFrame, compilerproc.} =
|
||||
# types are fake
|
||||
when defined(nimphp):
|
||||
asm """
|
||||
$result = array();
|
||||
for ($i = 0; $i < `len`; $i++) $result[] = `value`;
|
||||
return $result;
|
||||
"""
|
||||
else:
|
||||
asm """
|
||||
var result = new Array(`len`);
|
||||
for (var i = 0; i < `len`; ++i) result[i] = nimCopy(null, `value`, `typ`);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue