From 6ee174af22d6c8b2e3a89d837feb615650cd5fb8 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Fri, 4 Dec 2015 13:47:29 -0500 Subject: [PATCH] nitcc: simplify the code of trans generation into two steps Signed-off-by: Jean Privat --- contrib/nitcc/src/autom.nit | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/contrib/nitcc/src/autom.nit b/contrib/nitcc/src/autom.nit index c419caa..5b545b8 100644 --- a/contrib/nitcc/src/autom.nit +++ b/contrib/nitcc/src/autom.nit @@ -726,22 +726,43 @@ private class DFAGenerator add("\tredef fun trans(char) do\n") add("\t\tvar c = char.code_point\n") - var haslast = false + + # Collect the sequence of tests in the dispatch sequence + # The point here is that for each transition, there is a first and a last + # So holes hare to be identified + var dispatch = new HashMap[Int, nullable State] + var haslast: nullable State = null + var last = -1 for sym, next in trans do - assert not haslast + assert haslast == null assert sym.first > last - if sym.first > last + 1 then add("\t\tif c <= {sym.first-1} then return null\n") + if sym.first > last + 1 then + dispatch[sym.first-1] = null + end var l = sym.last if l == null then - add("\t\treturn dfastate_{names[next]}\n") - haslast= true + haslast = next else - add("\t\tif c <= {l} then return dfastate_{names[next]}\n") + dispatch[l] = next last = l end end - if not haslast then add("\t\treturn null\n") + + # Generate a sequence of `if` for the dispatch + for c, next in dispatch do + if next == null then + add("\t\tif c <= {c} then return null\n") + else + add("\t\tif c <= {c} then return dfastate_{names[next]}\n") + end + end + if haslast == null then + add("\t\treturn null\n") + else + add("\t\treturn dfastate_{names[haslast]}\n") + end + add("\tend\n") end add("end\n") -- 1.7.9.5