From 517979faa4999fcf1b94de9b71ab1635df3c24ab Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Sat, 29 Aug 2020 14:19:11 +0100 Subject: [PATCH] allow pseudocode numbering to decrement in for-loops --- src/soc/decoder/pseudo/parser.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/soc/decoder/pseudo/parser.py b/src/soc/decoder/pseudo/parser.py index b2197371..04bd736f 100644 --- a/src/soc/decoder/pseudo/parser.py +++ b/src/soc/decoder/pseudo/parser.py @@ -448,10 +448,17 @@ class PowerParser: """for_stmt : FOR atom EQ test TO test COLON suite | DO atom EQ test TO test COLON suite """ - # auto-add-one (sigh) due to python range start = p[4] - end = ast.BinOp(p[6], ast.Add(), ast.Constant(1)) - it = ast.Call(ast.Name("range", ast.Load()), [start, end], []) + end = p[6] + if start.value > end.value: # start greater than end, must go -ve + # auto-subtract-one (sigh) due to python range + end = ast.BinOp(p[6], ast.Add(), ast.Constant(-1)) + arange = [start, end, ast.Constant(-1)] + else: + # auto-add-one (sigh) due to python range + end = ast.BinOp(p[6], ast.Add(), ast.Constant(1)) + arange = [start, end] + it = ast.Call(ast.Name("range", ast.Load()), arange, []) p[0] = ast.For(p[2], it, p[8], []) def p_while_stmt(self, p): -- 2.30.2