From: Michael Urman Date: Sun, 7 May 2006 16:41:16 +0000 (-0000) Subject: Rework yasm.Expression to accept strings like '+' to indicate operation. X-Git-Tag: v0.5.0~9^2~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=486ae1c73aad49b1a848411a9495f719622ed0e6;p=yasm Rework yasm.Expression to accept strings like '+' to indicate operation. Add tests for yasm.Expression and new yasm.ImmVal. svn path=/trunk/yasm/; revision=1527 --- diff --git a/tools/python-yasm/bytecode.pxi b/tools/python-yasm/bytecode.pxi index 62efb44e..3141d2bd 100644 --- a/tools/python-yasm/bytecode.pxi +++ b/tools/python-yasm/bytecode.pxi @@ -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((value).expr)) + elif PyCObject_Check(value): + self.imm = PyCObject_AsVoidPtr(value) + else: + raise TypeError + cdef class Bytecode: cdef yasm_bytecode *bc diff --git a/tools/python-yasm/expr.pxi b/tools/python-yasm/expr.pxi index c767b6d8..c8b47bfd 100644 --- a/tools/python-yasm/expr.pxi +++ b/tools/python-yasm/expr.pxi @@ -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): diff --git a/tools/python-yasm/tests/__init__.py b/tools/python-yasm/tests/__init__.py index bb51143e..6d608f28 100644 --- a/tools/python-yasm/tests/__init__.py +++ b/tools/python-yasm/tests/__init__.py @@ -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 index 00000000..ff7d7061 --- /dev/null +++ b/tools/python-yasm/tests/test_bytecode.py @@ -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 index 00000000..1fb930d0 --- /dev/null +++ b/tools/python-yasm/tests/test_expr.py @@ -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) diff --git a/tools/python-yasm/tests/test_symrec.py b/tools/python-yasm/tests/test_symrec.py index e3eb3710..a228eebd 100644 --- a/tools/python-yasm/tests/test_symrec.py +++ b/tools/python-yasm/tests/test_symrec.py @@ -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