]> granicus.if.org Git - yasm/commitdiff
Rework yasm.Expression to accept strings like '+' to indicate operation.
authorMichael Urman <mu@tortall.net>
Sun, 7 May 2006 16:41:16 +0000 (16:41 -0000)
committerMichael Urman <mu@tortall.net>
Sun, 7 May 2006 16:41:16 +0000 (16:41 -0000)
Add tests for yasm.Expression and new yasm.ImmVal.

svn path=/trunk/yasm/; revision=1527

tools/python-yasm/bytecode.pxi
tools/python-yasm/expr.pxi
tools/python-yasm/tests/__init__.py
tools/python-yasm/tests/test_bytecode.py [new file with mode: 0644]
tools/python-yasm/tests/test_expr.py [new file with mode: 0644]
tools/python-yasm/tests/test_symrec.py

index 62efb44e58e7f4fff7c48df0442ff42b426940a4..3141d2bdb4ef3bd7973274ace94e3ed7bdfc3585 100644 (file)
@@ -143,6 +143,21 @@ cdef extern from "libyasm/bc-int.h":
 
     cdef yasm_bytecode *yasm_bc__next(yasm_bytecode *bc)
 
+cdef object __make_immval(yasm_immval *imm):
+    return ImmVal(PyCObject_FromVoidPtr(imm, NULL))
+
+cdef class ImmVal:
+    cdef yasm_immval *imm
+
+    def __new__(self, value):
+        if isinstance(value, Expression):
+            self.imm = yasm_imm_create_expr(
+                    yasm_expr_copy((<Expression>value).expr))
+        elif PyCObject_Check(value):
+            self.imm = <yasm_immval *>PyCObject_AsVoidPtr(value)
+        else:
+            raise TypeError
+
 cdef class Bytecode:
     cdef yasm_bytecode *bc
 
index c767b6d8af919771e7a414ba7b27108bcbff8093..c8b47bfd142ff7fbda3e747da0c663156a887f40 100644 (file)
@@ -97,43 +97,30 @@ cdef extern from "libyasm/expr-int.h":
     cdef int yasm_expr__contains(yasm_expr *e, yasm_expr__type t)
 
 import operator
-__op = {
-    operator.__add__ : YASM_EXPR_ADD,
-    operator.add : YASM_EXPR_ADD,
-    operator.__and__ : YASM_EXPR_AND,
-    operator.and_ : YASM_EXPR_AND,
-    operator.__div__ : YASM_EXPR_SIGNDIV,
-    operator.__floordiv__: YASM_EXPR_SIGNDIV,
-    operator.div : YASM_EXPR_SIGNDIV,
-    operator.floordiv: YASM_EXPR_SIGNDIV,
-    operator.__ge__: YASM_EXPR_GE,
-    operator.ge: YASM_EXPR_GE,
-    operator.__gt__: YASM_EXPR_GT,
-    operator.gt: YASM_EXPR_GT,
-    operator.__inv__: YASM_EXPR_NOT,
-    operator.__invert__: YASM_EXPR_NOT,
-    operator.inv: YASM_EXPR_NOT,
-    operator.invert: YASM_EXPR_NOT,
-    operator.__le__: YASM_EXPR_LE,
-    operator.le: YASM_EXPR_LE,
-    operator.__lt__: YASM_EXPR_LT,
-    operator.lt: YASM_EXPR_LT,
-    operator.__mod__: YASM_EXPR_SIGNMOD,
-    operator.mod: YASM_EXPR_SIGNMOD,
-    operator.__mul__: YASM_EXPR_MUL,
-    operator.mul: YASM_EXPR_MUL,
-    operator.__neg__: YASM_EXPR_NEG,
-    operator.neg: YASM_EXPR_NEG,
-    operator.__not__: YASM_EXPR_LNOT,
-    operator.not_: YASM_EXPR_LNOT,
-    operator.__or__: YASM_EXPR_OR,
-    operator.or_: YASM_EXPR_OR,
-    operator.__sub__: YASM_EXPR_SUB,
-    operator.sub: YASM_EXPR_SUB,
-    operator.__xor__: YASM_EXPR_XOR,
-    operator.xor: YASM_EXPR_XOR,
-}
-del operator
+__op = {}
+for ops, operation in [
+    ((operator.__add__, operator.add, '+'), YASM_EXPR_ADD),
+    ((operator.__and__, operator.and_, '&'), YASM_EXPR_AND),
+    ((operator.__div__, operator.div, '/'), YASM_EXPR_SIGNDIV),
+    ((operator.__floordiv__, operator.floordiv, '//'), YASM_EXPR_SIGNDIV),
+    ((operator.__ge__, operator.ge, '>='), YASM_EXPR_GE),
+    ((operator.__gt__, operator.gt, '>'), YASM_EXPR_GT),
+    ((operator.__inv__, operator.inv, '~'), YASM_EXPR_NOT),
+    ((operator.__invert__, operator.invert), YASM_EXPR_NOT),
+    ((operator.__le__, operator.le, '<='), YASM_EXPR_LE),
+    ((operator.__lt__, operator.lt, '<'), YASM_EXPR_LT),
+    ((operator.__mod__, operator.mod, '%'), YASM_EXPR_SIGNMOD),
+    ((operator.__mul__, operator.mul, '*'), YASM_EXPR_MUL),
+    ((operator.__neg__, operator.neg), YASM_EXPR_NEG),
+    ((operator.__not__, operator.not_, 'not'), YASM_EXPR_LNOT),
+    ((operator.__or__, operator.or_, '|'), YASM_EXPR_OR),
+    ((operator.__sub__, operator.sub, '-'), YASM_EXPR_SUB),
+    ((operator.__xor__, operator.xor, '^'), YASM_EXPR_XOR),
+    ]:
+    for op in ops:
+        __op[op] = operation
+
+del operator, op, ops, operation
 
 cdef class Expression
 cdef object __make_expression(yasm_expr *expr):
index bb51143edaf4c9c8989a7e18392f1af828703c3e..6d608f28206df7f8741073a6a05e1f957c2ee6f8 100644 (file)
@@ -12,6 +12,8 @@ class Mock(object):
 
 import test_intnum
 import test_symrec
+import test_bytecode
+import test_expr
 
 class Result(unittest.TestResult):
 
diff --git a/tools/python-yasm/tests/test_bytecode.py b/tools/python-yasm/tests/test_bytecode.py
new file mode 100644 (file)
index 0000000..ff7d706
--- /dev/null
@@ -0,0 +1,11 @@
+# $Id$
+from tests import TestCase, add
+from yasm import Bytecode, ImmVal, Expression
+
+class TImmVal(TestCase):
+    def test_create(self):
+        self.assertRaises(TypeError, ImmVal, "notimmval")
+
+        imm = ImmVal(Expression('+', 2, 3))
+
+add(TImmVal)
diff --git a/tools/python-yasm/tests/test_expr.py b/tools/python-yasm/tests/test_expr.py
new file mode 100644 (file)
index 0000000..1fb930d
--- /dev/null
@@ -0,0 +1,19 @@
+# $Id$
+from tests import TestCase, add
+from yasm import Expression
+import operator
+
+class TExpression(TestCase):
+    def test_create(self):
+        e1 = Expression(operator.add, 1, 2)
+        e2 = Expression('+', 1, 2)
+        
+        self.assertEquals(e1.get_intnum(), e1.get_intnum())
+
+    def test_extract(self):
+        e1 = Expression('/', 15, 5)
+        self.assertEquals(e1.get_intnum(), 3)
+        self.assertRaises(ValueError, e1.extract_segoff)
+        self.assertRaises(ValueError, e1.extract_wrt)
+
+add(TExpression)
index e3eb371072c411b49031883a512506e06e0d7c9b..a228eebdb513c046bc316e7438ce60154c48d78a 100644 (file)
@@ -19,8 +19,7 @@ class TSymbolTable(TestCase):
         self.assert_("foo" in self.symtab)
 
     def test_exception(self):
-        from operator import add
-        expr = Expression(add, 1, 2)
+        expr = Expression('+', 1, 2)
         self.symtab.define_equ("foo", expr, 0)
         self.assertRaises(YasmError, self.symtab.define_equ, "foo", expr, 0)
         self.symtab.define_equ("bar", expr, 0) # cleared