summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2017-10-15 01:26:48 +0200
committerAndreas Rumpf <rumpf_a@web.de>2017-10-15 01:26:48 +0200
commit14ca49743f676b902196be06af0d433bc4f8e76a (patch)
tree3f84e11cbf1f1291b601e4ed8bdf258da7b972f0
parent3aaf1e498058418f09b1a2b25ca08e873de37783 (diff)
downloadNim-14ca49743f676b902196be06af0d433bc4f8e76a.tar.gz
make nnkGotoState and labels more flexible
-rw-r--r--compiler/ccgstmts.nim16
1 files changed, 11 insertions, 5 deletions
diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim
index 74779f598..5434d87b2 100644
--- a/compiler/ccgstmts.nim
+++ b/compiler/ccgstmts.nim
@@ -141,9 +141,13 @@ template preserveBreakIdx(body: untyped): untyped =
   p.breakIdx = oldBreakIdx
 
 proc genState(p: BProc, n: PNode) =
-  internalAssert n.len == 1 and n.sons[0].kind == nkIntLit
-  let idx = n.sons[0].intVal
-  linefmt(p, cpsStmts, "STATE$1: ;$n", idx.rope)
+  internalAssert n.len == 1
+  let n0 = n[0]
+  if n0.kind == nkIntLit:
+    let idx = n.sons[0].intVal
+    linefmt(p, cpsStmts, "STATE$1: ;$n", idx.rope)
+  elif n0.kind == nkStrLit:
+    linefmt(p, cpsStmts, "$1: ;$n", n0.strVal.rope)
 
 proc genGotoState(p: BProc, n: PNode) =
   # we resist the temptation to translate it into duff's device as it later
@@ -157,10 +161,12 @@ proc genGotoState(p: BProc, n: PNode) =
   p.beforeRetNeeded = true
   lineF(p, cpsStmts, "case -1: goto BeforeRet_;$n", [])
   var statesCounter = lastOrd(n.sons[0].typ)
-  if n.len == 2 and n[1].kind == nkIntLit:
+  if n.len >= 2 and n[1].kind == nkIntLit:
     statesCounter = n[1].intVal
+  let prefix = if n.len == 3 and n[2].kind == nkStrLit: n[2].strVal.rope
+               else: rope"STATE"
   for i in 0 .. statesCounter:
-    lineF(p, cpsStmts, "case $1: goto STATE$1;$n", [rope(i)])
+    lineF(p, cpsStmts, "case $2: goto $1$2;$n", [prefix, rope(i)])
   lineF(p, cpsStmts, "}$n", [])
 
 proc genBreakState(p: BProc, n: PNode) =