summary refs log tree commit diff stats
path: root/rod
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2011-01-11 01:09:48 +0100
committerAraq <rumpf_a@web.de>2011-01-11 01:09:48 +0100
commit1a8c6fb49fd9e12168895266c0067240e2bcb897 (patch)
tree2facb8bc368d121ebaf63a9e113283af63e182e8 /rod
parentd48ab374959eed4c5acbbb38e7ea86a011854102 (diff)
downloadNim-1a8c6fb49fd9e12168895266c0067240e2bcb897.tar.gz
Feature: explicit string representation for enum fields
Diffstat (limited to 'rod')
-rwxr-xr-xrod/ccgtypes.nim10
-rwxr-xr-xrod/semfold.nim6
-rwxr-xr-xrod/semtypes.nim20
3 files changed, 28 insertions, 8 deletions
diff --git a/rod/ccgtypes.nim b/rod/ccgtypes.nim
index ca5b3990e..43b4f173d 100755
--- a/rod/ccgtypes.nim
+++ b/rod/ccgtypes.nim
@@ -1,14 +1,14 @@
 #
 #
 #           The Nimrod Compiler
-#        (c) Copyright 2009 Andreas Rumpf
+#        (c) Copyright 2011 Andreas Rumpf
 #
 #    See the file "copying.txt", included in this
 #    distribution, for details about the copyright.
 #
 
 #var
-#  newDummyVar: int; // just to check the symbol file mechanism
+#  newDummyVar: int # just to check the symbol file mechanism
 
 # ------------------------- Name Mangling --------------------------------
 
@@ -688,7 +688,11 @@ proc genEnumInfo(m: BModule, typ: PType, name: PRope) =
     assert(typ.n.sons[i].kind == nkSym)
     field = typ.n.sons[i].sym
     elemNode = getNimNode(m)
-    app(enumNames, makeCString(field.name.s))
+    if field.ast == nil:
+      # no explicit string literal for the enum field, so use field.name:
+      app(enumNames, makeCString(field.name.s))
+    else:
+      app(enumNames, makeCString(field.ast.strVal))
     if i < length - 1: app(enumNames, ", " & tnl)
     if field.position != i: 
       appf(specialCases, "$1.offset = $2;$n", [elemNode, toRope(field.position)])
diff --git a/rod/semfold.nim b/rod/semfold.nim
index a447ac66f..91d6ea3bc 100755
--- a/rod/semfold.nim
+++ b/rod/semfold.nim
@@ -58,7 +58,11 @@ proc ordinalValToString(a: PNode): string =
     for i in countup(0, sonsLen(n) - 1): 
       if n.sons[i].kind != nkSym: InternalError(a.info, "ordinalValToString")
       var field = n.sons[i].sym
-      if field.position == x: return field.name.s
+      if field.position == x: 
+        if field.ast == nil: 
+          return field.name.s
+        else:
+          return field.ast.strVal
     InternalError(a.info, "no symbol for ordinal value: " & $x)
   else:
     result = $x
diff --git a/rod/semtypes.nim b/rod/semtypes.nim
index 8083227fd..ccba76a21 100755
--- a/rod/semtypes.nim
+++ b/rod/semtypes.nim
@@ -1,7 +1,7 @@
 #
 #
 #           The Nimrod Compiler
-#        (c) Copyright 2010 Andreas Rumpf
+#        (c) Copyright 2011 Andreas Rumpf
 #
 #    See the file "copying.txt", included in this
 #    distribution, for details about the copyright.
@@ -25,7 +25,6 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType =
     counter, x: BiggestInt
     e: PSym
     base: PType
-    v: PNode
   counter = 0
   base = nil
   result = newOrPrevType(tyEnum, prev, c)
@@ -41,12 +40,25 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType =
     case n.sons[i].kind
     of nkEnumFieldDef: 
       e = newSymS(skEnumField, n.sons[i].sons[0], c)
-      v = semConstExpr(c, n.sons[i].sons[1])
-      x = getOrdValue(v)
+      var v = semConstExpr(c, n.sons[i].sons[1])
+      var strVal: PNode = nil
+      case skipTypes(v.typ, abstractInst).kind 
+      of tyTuple: 
+        if sonsLen(v) != 2: liMessage(v.info, errWrongNumberOfVariables)
+        strVal = v.sons[1] # second tuple part is the string value
+        if skipTypes(strVal.typ, abstractInst).kind notin {tyString, tyCstring}:
+          liMessage(strVal.info, errStringLiteralExpected)
+        x = getOrdValue(v.sons[0]) # first tuple part is the ordinal
+      of tyString, tyCstring:
+        strVal = v
+        x = counter
+      else:
+        x = getOrdValue(v)
       if i != 1: 
         if (x != counter): incl(result.flags, tfEnumHasWholes)
         if x < counter: 
           liMessage(n.sons[i].info, errInvalidOrderInEnumX, e.name.s)
+      e.ast = strVal # might be nil
       counter = x
     of nkSym: 
       e = n.sons[i].sym