summary refs log tree commit diff stats
path: root/compiler/ecmasgen.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2013-01-27 01:38:55 +0100
committerAraq <rumpf_a@web.de>2013-01-27 01:38:55 +0100
commit0e1b67cfff7f9e7352439888e06e2c255599e193 (patch)
treec7bc1ab8307ecba0c9ca652118498a3260284e69 /compiler/ecmasgen.nim
parent76ed657c9bea39ba0aa7445b20921437685c9292 (diff)
downloadNim-0e1b67cfff7f9e7352439888e06e2c255599e193.tar.gz
implemented $/repr for enums for the JS target
Diffstat (limited to 'compiler/ecmasgen.nim')
-rwxr-xr-xcompiler/ecmasgen.nim34
1 files changed, 18 insertions, 16 deletions
diff --git a/compiler/ecmasgen.nim b/compiler/ecmasgen.nim
index 6ecda9a7c..9883acf6d 100755
--- a/compiler/ecmasgen.nim
+++ b/compiler/ecmasgen.nim
@@ -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)])