Browse Source

Add a SWP swap operation (xchg two values on top of stack)

Yann Weber 6 years ago
parent
commit
bbc9ced95e
2 changed files with 22 additions and 0 deletions
  1. 9
    0
      gte/turmit.py
  2. 13
    0
      tests/test_turmit.py

+ 9
- 0
gte/turmit.py View File

@@ -365,6 +365,15 @@ class Turmit(object):
365 365
             @param a int : value
366 366
         '''
367 367
         pass
368
+
369
+    @RpnOp
370
+    def swp(self, a, b):
371
+        ''' @brief Swap the two values on top of the stack
372
+            @param a int : v1
373
+            @param b int : v2
374
+        '''
375
+        self._push(b)
376
+        self._push(a)
368 377
     
369 378
     @RpnOp
370 379
     def jmp(self, offset):

+ 13
- 0
tests/test_turmit.py View File

@@ -285,3 +285,16 @@ class TurmitOperationTestCase(unittest.TestCase):
285 285
         self.assertEqual(t.shead, 10)
286 286
         t.pop()
287 287
         self.assertEqual(t._cur, len(t._stack) - 1)
288
+
289
+    def test_swp(self):
290
+        ''' Test turmit swp() method '''
291
+        self._rpn('swp', 2)
292
+
293
+        t = Turmit()
294
+        t._push(42)
295
+        t._push(1337)
296
+        t.swp()
297
+        self.assertEqual(t._cur, 1)
298
+        self.assertEqual(t.shead, 42)
299
+        t._pop()
300
+        self.assertEqual(t.shead, 1337)

Loading…
Cancel
Save