implemented $/repr for enums for the JS target

This commit is contained in:
Araq
2013-01-27 01:38:55 +01:00
parent 76ed657c9b
commit 0e1b67cfff

View File

@@ -227,27 +227,24 @@ proc genTupleInfo(p: var TProc, typ: PType, name: PRope) =
[toRope(typ.id), genTupleFields(p, typ)])
appf(p.g.typeInfo, "$1.node = NNI$2;$n", [name, toRope(typ.id)])
proc genEnumInfo(p: var TProc, typ: PType, name: PRope) =
var
s, n: PRope
length: int
field: PSym
length = sonsLen(typ.n)
s = nil
proc genEnumInfo(p: var TProc, typ: PType, name: PRope) =
let length = sonsLen(typ.n)
var s: PRope = nil
for i in countup(0, length - 1):
if (typ.n.sons[i].kind != nkSym): InternalError(typ.n.info, "genEnumInfo")
field = typ.n.sons[i].sym
let field = typ.n.sons[i].sym
if i > 0: app(s, ", " & tnl)
let extName = if field.ast == nil: field.name.s else: field.ast.strVal
appf(s, "{kind: 1, offset: $1, typ: $2, name: $3, len: 0, sons: null}",
[toRope(field.position), name, makeCString(field.name.s)])
n = ropef("var NNI$1 = {kind: 2, offset: 0, typ: null, " &
[toRope(field.position), name, makeCString(extName)])
var n = ropef("var NNI$1 = {kind: 2, offset: 0, typ: null, " &
"name: null, len: $2, sons: [$3]};$n", [toRope(typ.id), toRope(length), s])
s = ropef("var $1 = {size: 0, kind: $2, base: null, node: null, " &
"finalizer: null};$n", [name, toRope(ord(typ.kind))])
prepend(p.g.typeInfo, s)
app(p.g.typeInfo, n)
appf(p.g.typeInfo, "$1.node = NNI$2;$n", [name, toRope(typ.id)])
if typ.sons[0] != nil:
if typ.sons[0] != nil:
appf(p.g.typeInfo, "$1.base = $2;$n",
[name, genTypeInfo(p, typ.sons[0])])
@@ -959,7 +956,9 @@ proc genSym(p: var TProc, n: PNode, r: var TCompRes) =
of skProc, skConverter, skMethod:
discard mangleName(s)
r.res = s.loc.r
if lfNoDecl in s.loc.flags or s.magic != mNone or isGenericRoutine(s) or {sfImportc, sfInfixCall} * s.flags != {}: nil
if lfNoDecl in s.loc.flags or s.magic != mNone or isGenericRoutine(s) or
{sfImportc, sfInfixCall} * s.flags != {}:
nil
elif s.kind == skMethod and s.getBody.kind == nkEmpty:
# we cannot produce code for the dispatcher yet:
nil
@@ -1218,10 +1217,13 @@ proc genConStrStr(p: var TProc, n: PNode, r: var TCompRes) =
proc genRepr(p: var TProc, n: PNode, r: var TCompRes) =
var t = skipTypes(n.sons[1].typ, abstractVarRange)
case t.kind
of tyInt..tyInt64:
unaryExpr(p, n, r, "", "reprInt($1)")
of tyInt..tyUInt64:
unaryExpr(p, n, r, "", "(\"\"+ ($1))")
of tyEnum, tyOrdinal:
binaryExpr(p, n, r, "", "reprEnum($1, $2)")
gen(p, n.sons[1], r)
useMagic(p, "cstrToNimstr")
r.res = ropef("cstrToNimstr($1.node.sons[$2].name)",
[genTypeInfo(p, t), r.res])
else:
# XXX:
internalError(n.info, "genRepr: Not implemented")
@@ -1603,7 +1605,7 @@ proc newModule(module: PSym, filename: string): BModule =
proc genHeader(): PRope =
result = ropef("/* Generated by the Nimrod Compiler v$1 */$n" &
"/* (c) 2012 Andreas Rumpf */$n$n" & "$nvar Globals = this;$n" &
"/* (c) 2013 Andreas Rumpf */$n$n" & "$nvar Globals = this;$n" &
"var framePtr = null;$n" & "var excHandler = null;$n",
[toRope(versionAsString)])