implemented $/repr for enums for the JS target

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

View file

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