From 875da821b006176a14527b019d77f22dd625867f Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Sun, 5 Apr 2020 17:40:01 +0100 Subject: [PATCH] construct switch/case/default from if/elif/elif/else --- src/soc/decoder/power_pseudo.py | 1 + src/soc/decoder/pseudo/parser.py | 38 +++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/soc/decoder/power_pseudo.py b/src/soc/decoder/power_pseudo.py index 7005bd08..a16c77bb 100644 --- a/src/soc/decoder/power_pseudo.py +++ b/src/soc/decoder/power_pseudo.py @@ -172,6 +172,7 @@ switch (n) case(2): fallthrough case(3): x <- 3 + case(4): fallthrough default: x <- 9 """ diff --git a/src/soc/decoder/pseudo/parser.py b/src/soc/decoder/pseudo/parser.py index 7906e825..03d56d5d 100644 --- a/src/soc/decoder/pseudo/parser.py +++ b/src/soc/decoder/pseudo/parser.py @@ -420,11 +420,43 @@ class PowerParser: print(astor.dump_tree(p[1])) cases = [] - current_cases = [] + current_cases = [] # for deferral for (case, suite) in p[8]: - print (case, suite) + print ("for", case, suite) + if suite is None: + for c in case: + current_cases.append(ast.Num(c)) + continue + if case == 'default': # last + break + for c in case: + current_cases.append(ast.Num(c)) + print ("cases", current_cases) + compare = ast.Compare(switchon, [ast.In()], + [ast.List(current_cases)]) + current_cases = [] + cases.append((compare, suite)) + + print ("ended", case, current_cases) + if case == 'default': + if current_cases: + compare = ast.Compare(switchon, [ast.In()], + [ast.List(current_cases)]) + cases.append((compare, suite)) + cases.append((None, suite)) + + cases.reverse() res = [] - p[0] = ast.If([], [], []) + for compare, suite in cases: + print ("after rev", compare, suite) + if compare is None: + assert len(res) == 0, "last case should be default" + res = suite + else: + if not isinstance(res, list): + res = [res] + res = ast.If(compare, suite, res) + p[0] = res def p_switches(self, p): """switches : switch_list switch_default -- 2.30.2