Loading

Paste #pescyyso6

  1. diff -r d94b30d4092f nml/actions/action2var.py
  2. --- a/nml/actions/action2var.py Fri Mar 20 20:42:28 2015 +0100
  3. +++ b/nml/actions/action2var.py Fri Mar 27 21:35:03 2015 +0100
  4. @@ -298,14 +298,6 @@
  5.          self.size = size
  6.          self.offset = offset
  7.  
  8. -def pow2(expr):
  9. -    #2**x = (1 ror (32 - x))
  10. -    if isinstance(expr, expression.ConstantNumeric):
  11. -        return expression.ConstantNumeric(1 << expr.value)
  12. -    expr = expression.BinOp(nmlop.SUB, expression.ConstantNumeric(32), expr)
  13. -    expr = expression.BinOp(nmlop.ROT_RIGHT, expression.ConstantNumeric(1), expr)
  14. -    return expr
  15. -
  16.  class Varaction2Parser(object):
  17.      def __init__(self, feature):
  18.          self.feature = feature # Depends on feature and var_range
  19. @@ -360,22 +352,13 @@
  20.              expr = expression.BinOp(nmlop.AND, expr, expression.ConstantNumeric(1))
  21.              expr = expression.BinOp(nmlop.XOR, expr, expression.ConstantNumeric(1))
  22.  
  23. -        elif expr.op == nmlop.SHIFT_LEFT:
  24. -            #a << b ==> a * (2**b)
  25. -            expr = expression.BinOp(nmlop.MUL, expr.expr1, pow2(expr.expr2))
  26. -        elif expr.op == nmlop.SHIFT_RIGHT:
  27. -            #a >> b ==> a / (2**b)
  28. -            expr = expression.BinOp(nmlop.DIV, expr.expr1, pow2(expr.expr2))
  29. -        elif expr.op == nmlop.SHIFTU_RIGHT:
  30. -            #a >>> b ==> (uint)a / (2**b)
  31. -            expr = expression.BinOp(nmlop.DIVU, expr.expr1, pow2(expr.expr2))
  32.          elif expr.op == nmlop.HASBIT:
  33.              # hasbit(x, n) ==> (x >> n) & 1
  34. -            expr = expression.BinOp(nmlop.DIV, expr.expr1, pow2(expr.expr2))
  35. +            expr = expression.BinOp(nmlop.SHIFTU_RIGHT, expr.expr1, expr.expr2)
  36.              expr = expression.BinOp(nmlop.AND, expr, expression.ConstantNumeric(1))
  37.          elif expr.op == nmlop.NOTHASBIT:
  38.              # !hasbit(x, n) ==> ((x >> n) & 1) ^ 1
  39. -            expr = expression.BinOp(nmlop.DIV, expr.expr1, pow2(expr.expr2))
  40. +            expr = expression.BinOp(nmlop.SHIFTU_RIGHT, expr.expr1, expr.expr2)
  41.              expr = expression.BinOp(nmlop.AND, expr, expression.ConstantNumeric(1))
  42.              expr = expression.BinOp(nmlop.XOR, expr, expression.ConstantNumeric(1))
  43.  
  44. diff -r d94b30d4092f nml/expression/functioncall.py
  45. --- a/nml/expression/functioncall.py    Fri Mar 20 20:42:28 2015 +0100
  46. +++ b/nml/expression/functioncall.py    Fri Mar 27 21:35:03 2015 +0100
  47. @@ -249,6 +249,22 @@
  48.          raise generic.ScriptError(name + "() must have exactly two parameters", pos)
  49.      return BinOp(nmlop.HASBIT, args[0], args[1], pos)
  50.  
  51. +def builtin_getbits(name, args, pos):
  52. +    """
  53. +    getbits(value, first, amount) builtin function.
  54. +
  55. +    @return Extract C{amount} bits starting at C{first} from C{value}, that is (C{value} >> C{first}) & (1 << C{amount} - 1)
  56. +    """
  57. +    if len(args) != 3:
  58. +        raise generic.ScriptError(name + "() must have exactly three parameters", pos)
  59. +
  60. +    # getbits(value, first, amount) = (value >> first) & ((0xFFFFFFFF << amount) ^ 0xFFFFFFFF)
  61. +    part1 = expression.BinOp(nmlop.SHIFTU_RIGHT, args[0], args[1], pos)
  62. +    part2 = expression.BinOp(nmlop.SHIFT_LEFT, expression.ConstantNumeric(0xFFFFFFFF), args[2], pos)
  63. +    part3 = expression.BinOp(nmlop.XOR, part2, expression.ConstantNumeric(0xFFFFFFFF), pos)
  64. +
  65. +    return BinOp(nmlop.AND, part1, part3, pos)
  66. +
  67.  def builtin_version_openttd(name, args, pos):
  68.      """
  69.      version_openttd(major, minor, revision[, build]) builtin function.
  70. @@ -623,6 +639,7 @@
  71.      'LOAD_TEMP' : builtin_storage,
  72.      'LOAD_PERM' : builtin_storage,
  73.      'hasbit' : builtin_hasbit,
  74. +    'getbits' : builtin_getbits,
  75.      'version_openttd' : builtin_version_openttd,
  76.      'cargotype_available' : builtin_cargotype_available,
  77.      'railtype_available' : builtin_railtype_available,
  78.  

Comments