JS codegen: supports more builtins

This commit is contained in:
Araq 2013-09-24 23:56:23 +02:00
commit 9bfcdc40ef
3 changed files with 32 additions and 11 deletions

View file

@ -451,6 +451,15 @@ proc binaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string) =
r.res = ropef(frmt, [x.rdLoc, y.rdLoc]) r.res = ropef(frmt, [x.rdLoc, y.rdLoc])
r.kind = resExpr r.kind = resExpr
proc ternaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string) =
var x, y, z: TCompRes
useMagic(p, magic)
gen(p, n.sons[1], x)
gen(p, n.sons[2], y)
gen(p, n.sons[3], z)
r.res = ropef(frmt, [x.rdLoc, y.rdLoc, z.rdLoc])
r.kind = resExpr
proc unaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string) = proc unaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string) =
useMagic(p, magic) useMagic(p, magic)
gen(p, n.sons[1], r) gen(p, n.sons[1], r)
@ -1351,6 +1360,10 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
of mEcho: genEcho(p, n, r) of mEcho: genEcho(p, n, r)
of mSlurp, mStaticExec: of mSlurp, mStaticExec:
localError(n.info, errXMustBeCompileTime, n.sons[0].sym.name.s) localError(n.info, errXMustBeCompileTime, n.sons[0].sym.name.s)
of mCopyStr: binaryExpr(p, n, r, "", "($1.slice($2,-1))")
of mCopyStrLast: ternaryExpr(p, n, r, "", "($1.slice($2, ($3)+1).concat(0))")
of mNewString: unaryExpr(p, n, r, "mnewString", "mnewString($1)")
of mNewStringOfCap: unaryExpr(p, n, r, "mnewString", "mnewString(0)")
else: else:
genCall(p, n, r) genCall(p, n, r)
#else internalError(e.info, 'genMagic: ' + magicToStr[op]); #else internalError(e.info, 'genMagic: ' + magicToStr[op]);

View file

@ -209,17 +209,26 @@ proc getGeneratedPath: string =
result = if nimcacheDir.len > 0: nimcacheDir else: gProjectPath.shortenDir / result = if nimcacheDir.len > 0: nimcacheDir else: gProjectPath.shortenDir /
genSubDir genSubDir
proc withPackageName*(path: string): string = proc getPackageName(path: string): string =
var x = path var q = 1
while true: var b = 0
x = parentDir(x) if path[len(path)-1] in {dirsep, altsep}: q = 2
if x.len == 0: break for i in countdown(len(path)-q, 0):
case x.normalize if path[i] in {dirsep, altsep}:
of "lib", "src", "source", "package", "pckg", "library": discard if b == 0: b = i
else: else:
let (path, file, ext) = path.splitFile let x = path.substr(i+1, b-1)
return (path / (x & '_' & file)) & ext case x.normalize
of "lib", "src", "source", "package", "pckg", "library": b = i
else: return x
proc withPackageName*(path: string): string =
let x = path.getPackageName
if x.isNil:
result = path result = path
else:
let (p, file, ext) = path.splitFile
result = (p / (x & '_' & file)) & ext
proc toGeneratedFile*(path, ext: string): string = proc toGeneratedFile*(path, ext: string): string =
## converts "/home/a/mymodule.nim", "rod" to "/home/a/nimcache/mymodule.rod" ## converts "/home/a/mymodule.nim", "rod" to "/home/a/nimcache/mymodule.rod"

View file

@ -2,7 +2,6 @@ version 0.9.4
============= =============
- change search path algorithm to care about packages - change search path algorithm to care about packages
- XXX how can 'lib_system' ever be generated?
- new VM: - new VM:
- implement the glue to replace evals.nim - implement the glue to replace evals.nim
- implement missing magics - implement missing magics