vuvova / gdb-tools

Various tools to improve the gdb experience
BSD 3-Clause "New" or "Revised" License
123 stars 17 forks source link

Use negative numbers in [[x]] operator as counting from the end #16

Closed ssbssa closed 2 years ago

ssbssa commented 2 years ago

Basically, when in [[x]] the x value is negative, add the sequence count to it, so e.g. [[-1]] would get you the last value in the sequence. Then you wouldn't need the #/ operator where you have to duplicate the full expression, just to get the final value.

This would give you the final 3 values:

(gdb) dl (100..199)[[-3..-1]]
(100..199)[[-3]] = 197
(100..199)[[-2]] = 198
(100..199)[[-1]] = 199

My implementation looks like this now:

--- a/duel/expr.py
+++ b/duel/expr.py
@@ -143,7 +143,14 @@ class StructWalk(BinaryBase):
 class TakeNth(BinaryBase):
     name_ = '{0}[[{1}]]'
     def eval(self):
+        l = None
         for n2,v2 in self.arg2_.eval():
+            if v2 < 0:
+                if l is None:
+                    l = sum(1 for i in self.arg1_.eval())
+                v2 += l
+                if v2 < 0:
+                    continue
             val = self.arg1_.eval()
             for i in xrange(0,v2): next(val)
             n1, v1 = next(val)

What do you think?

Btw, duel is awesome.

vuvova commented 2 years ago

It's a questionable feature. I agree that it's very convenient. But it's following a pythonic logic, while duel and gdb expression syntax is built on C logic, e.g. a[-1] has a well defined semantics in C and gdb and duel, and it can never mean "count from the end", as it has to mean *(a-1). The conventional C semantics does not apply to [[ ]], so it can use pythonic interpretation. But it'll make the language inconsistent and I suspect it might create problems somewhere later.

But these all aren't very strong arguments against this feature, after all the whole point of duel existence is ease of use. So I'll accept it, provisionally. And if it'll start causing problems (I mean, conceptual problems, not implementation bugs), I'll remove it again.

ssbssa commented 2 years ago

That's fine with me. I agree with you, if this causes any problems, it should be removed again.