On Mon, Nov 30, 2020 at 1:26 PM Philippe Mathieu-Daudé <f4...@amsat.org> wrote: > ISA datasheets often use binary or hexadecimal constant values. > By doing base conversion, we might introduce bugs. Safer is to > copy/paste the datasheet value. > Add support for bin/hex constants in argument field token. > > Signed-off-by: Philippe Mathieu-Daudé <f4...@amsat.org> > --- > Is there a more pythonic way to write this if/elif/else > loop without re.fullmatch?
BTW I discarded the simple "int(value, 0)" to have strict checks, but if this is enough it is certainly simpler. > --- > scripts/decodetree.py | 10 ++++++++-- > 1 file changed, 8 insertions(+), 2 deletions(-) > > diff --git a/scripts/decodetree.py b/scripts/decodetree.py > index 47aa9caf6d1..d2ecc61813f 100644 > --- a/scripts/decodetree.py > +++ b/scripts/decodetree.py > @@ -849,9 +849,15 @@ def parse_generic(lineno, parent_pat, name, toks): > continue > > # 'Foo=number' sets an argument field to a constant value > - if re.fullmatch(re_C_ident + '=[+-]?[0-9]+', t): > + if re.fullmatch(re_C_ident + '=[+-]?(0[bx])?[0-9]+', t): > (fname, value) = t.split('=') > - value = int(value) > + if re.fullmatch('[+-]?0b[0-9]+', value): > + base = 2 > + elif re.fullmatch('[+-]?0x[0-9]+', value): > + base = 16 > + else: > + base = 10 > + value = int(value, base) > flds = add_field(lineno, flds, fname, ConstField(value)) > continue > > -- > 2.26.2 >