PageRenderTime 67ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/module/micronumpy/compile.py

https://bitbucket.org/kostialopuhin/pypy
Python | 1085 lines | 1051 code | 28 blank | 6 comment | 18 complexity | a9ee07d6bc389910b99bbb62c50d7c7e MD5 | raw file
Possible License(s): Apache-2.0, AGPL-3.0, BSD-3-Clause
  1. """ This is a set of tools for standalone compiling of numpy expressions.
  2. It should not be imported by the module itself
  3. """
  4. import re
  5. import py
  6. from pypy.interpreter import special
  7. from pypy.interpreter.baseobjspace import InternalSpaceCache, W_Root, ObjSpace
  8. from pypy.interpreter.error import OperationError
  9. from rpython.rlib.objectmodel import specialize, instantiate
  10. from rpython.rlib.nonconst import NonConstant
  11. from rpython.rlib.rarithmetic import base_int
  12. from pypy.module.micronumpy import boxes, ufuncs
  13. from pypy.module.micronumpy.arrayops import where
  14. from pypy.module.micronumpy.ndarray import W_NDimArray
  15. from pypy.module.micronumpy.ctors import array
  16. from pypy.module.micronumpy.descriptor import get_dtype_cache
  17. from pypy.interpreter.miscutils import ThreadLocals, make_weak_value_dictionary
  18. from pypy.interpreter.executioncontext import (ExecutionContext, ActionFlag,
  19. UserDelAction)
  20. from pypy.interpreter.pyframe import PyFrame
  21. class BogusBytecode(Exception):
  22. pass
  23. class ArgumentMismatch(Exception):
  24. pass
  25. class ArgumentNotAnArray(Exception):
  26. pass
  27. class WrongFunctionName(Exception):
  28. pass
  29. class TokenizerError(Exception):
  30. pass
  31. class BadToken(Exception):
  32. pass
  33. SINGLE_ARG_FUNCTIONS = ["sum", "prod", "max", "min", "all", "any",
  34. "unegative", "flat", "tostring", "count_nonzero",
  35. "argsort", "cumsum", "logical_xor_reduce"]
  36. TWO_ARG_FUNCTIONS = ["dot", 'take', 'searchsorted', 'multiply']
  37. TWO_ARG_FUNCTIONS_OR_NONE = ['view', 'astype', 'reshape']
  38. THREE_ARG_FUNCTIONS = ['where']
  39. class W_TypeObject(W_Root):
  40. def __init__(self, name):
  41. self.name = name
  42. def lookup(self, name):
  43. return self.getdictvalue(self, name)
  44. def getname(self, space):
  45. return self.name
  46. class FakeSpace(ObjSpace):
  47. w_ValueError = W_TypeObject("ValueError")
  48. w_TypeError = W_TypeObject("TypeError")
  49. w_IndexError = W_TypeObject("IndexError")
  50. w_OverflowError = W_TypeObject("OverflowError")
  51. w_NotImplementedError = W_TypeObject("NotImplementedError")
  52. w_AttributeError = W_TypeObject("AttributeError")
  53. w_StopIteration = W_TypeObject("StopIteration")
  54. w_KeyError = W_TypeObject("KeyError")
  55. w_SystemExit = W_TypeObject("SystemExit")
  56. w_KeyboardInterrupt = W_TypeObject("KeyboardInterrupt")
  57. w_VisibleDeprecationWarning = W_TypeObject("VisibleDeprecationWarning")
  58. w_None = None
  59. w_bool = W_TypeObject("bool")
  60. w_int = W_TypeObject("int")
  61. w_float = W_TypeObject("float")
  62. w_list = W_TypeObject("list")
  63. w_long = W_TypeObject("long")
  64. w_tuple = W_TypeObject('tuple')
  65. w_slice = W_TypeObject("slice")
  66. w_str = W_TypeObject("str")
  67. w_unicode = W_TypeObject("unicode")
  68. w_complex = W_TypeObject("complex")
  69. w_dict = W_TypeObject("dict")
  70. w_object = W_TypeObject("object")
  71. w_buffer = W_TypeObject("buffer")
  72. w_type = W_TypeObject("type")
  73. def __init__(self, config=None):
  74. """NOT_RPYTHON"""
  75. self.fromcache = InternalSpaceCache(self).getorbuild
  76. self.w_Ellipsis = special.Ellipsis()
  77. self.w_NotImplemented = special.NotImplemented()
  78. if config is None:
  79. from pypy.config.pypyoption import get_pypy_config
  80. config = get_pypy_config(translating=False)
  81. self.config = config
  82. self.interned_strings = make_weak_value_dictionary(self, str, W_Root)
  83. self.builtin = DictObject({})
  84. self.FrameClass = PyFrame
  85. self.threadlocals = ThreadLocals()
  86. self.actionflag = ActionFlag() # changed by the signal module
  87. self.check_signal_action = None # changed by the signal module
  88. def _freeze_(self):
  89. return True
  90. def is_none(self, w_obj):
  91. return w_obj is None or w_obj is self.w_None
  92. def issequence_w(self, w_obj):
  93. return isinstance(w_obj, ListObject) or isinstance(w_obj, W_NDimArray)
  94. def len(self, w_obj):
  95. if isinstance(w_obj, ListObject):
  96. return self.wrap(len(w_obj.items))
  97. elif isinstance(w_obj, DictObject):
  98. return self.wrap(len(w_obj.items))
  99. raise NotImplementedError
  100. def getattr(self, w_obj, w_attr):
  101. assert isinstance(w_attr, StringObject)
  102. if isinstance(w_obj, DictObject):
  103. return w_obj.getdictvalue(self, w_attr)
  104. return None
  105. def isinstance_w(self, w_obj, w_tp):
  106. try:
  107. return w_obj.tp == w_tp
  108. except AttributeError:
  109. return False
  110. def iter(self, w_iter):
  111. if isinstance(w_iter, ListObject):
  112. raise NotImplementedError
  113. #return IterObject(space, w_iter.items)
  114. elif isinstance(w_iter, DictObject):
  115. return IterDictObject(self, w_iter)
  116. def next(self, w_iter):
  117. return w_iter.next()
  118. def contains(self, w_iter, w_key):
  119. if isinstance(w_iter, DictObject):
  120. return self.wrap(w_key in w_iter.items)
  121. raise NotImplementedError
  122. def decode_index4(self, w_idx, size):
  123. if isinstance(w_idx, IntObject):
  124. return (self.int_w(w_idx), 0, 0, 1)
  125. else:
  126. assert isinstance(w_idx, SliceObject)
  127. start, stop, step = w_idx.start, w_idx.stop, w_idx.step
  128. if step == 0:
  129. return (0, size, 1, size)
  130. if start < 0:
  131. start += size
  132. if stop < 0:
  133. stop += size + 1
  134. if step < 0:
  135. start, stop = stop, start
  136. start -= 1
  137. stop -= 1
  138. lgt = (stop - start + 1) / step + 1
  139. else:
  140. lgt = (stop - start - 1) / step + 1
  141. return (start, stop, step, lgt)
  142. def unicode_from_object(self, w_item):
  143. # XXX
  144. return StringObject("")
  145. @specialize.argtype(1)
  146. def wrap(self, obj):
  147. if isinstance(obj, float):
  148. return FloatObject(obj)
  149. elif isinstance(obj, bool):
  150. return BoolObject(obj)
  151. elif isinstance(obj, int):
  152. return IntObject(obj)
  153. elif isinstance(obj, base_int):
  154. return LongObject(obj)
  155. elif isinstance(obj, W_Root):
  156. return obj
  157. elif isinstance(obj, str):
  158. return StringObject(obj)
  159. raise NotImplementedError
  160. def newlist(self, items):
  161. return ListObject(items)
  162. def newcomplex(self, r, i):
  163. return ComplexObject(r, i)
  164. def newfloat(self, f):
  165. return self.float(f)
  166. def le(self, w_obj1, w_obj2):
  167. assert isinstance(w_obj1, boxes.W_GenericBox)
  168. assert isinstance(w_obj2, boxes.W_GenericBox)
  169. return w_obj1.descr_le(self, w_obj2)
  170. def lt(self, w_obj1, w_obj2):
  171. assert isinstance(w_obj1, boxes.W_GenericBox)
  172. assert isinstance(w_obj2, boxes.W_GenericBox)
  173. return w_obj1.descr_lt(self, w_obj2)
  174. def ge(self, w_obj1, w_obj2):
  175. assert isinstance(w_obj1, boxes.W_GenericBox)
  176. assert isinstance(w_obj2, boxes.W_GenericBox)
  177. return w_obj1.descr_ge(self, w_obj2)
  178. def add(self, w_obj1, w_obj2):
  179. assert isinstance(w_obj1, boxes.W_GenericBox)
  180. assert isinstance(w_obj2, boxes.W_GenericBox)
  181. return w_obj1.descr_add(self, w_obj2)
  182. def sub(self, w_obj1, w_obj2):
  183. return self.wrap(1)
  184. def mul(self, w_obj1, w_obj2):
  185. assert isinstance(w_obj1, boxes.W_GenericBox)
  186. assert isinstance(w_obj2, boxes.W_GenericBox)
  187. return w_obj1.descr_mul(self, w_obj2)
  188. def pow(self, w_obj1, w_obj2, _):
  189. return self.wrap(1)
  190. def neg(self, w_obj1):
  191. return self.wrap(0)
  192. def repr(self, w_obj1):
  193. return self.wrap('fake')
  194. def getitem(self, obj, index):
  195. if isinstance(obj, DictObject):
  196. w_dict = obj.getdict(self)
  197. if w_dict is not None:
  198. try:
  199. return w_dict[index]
  200. except KeyError, e:
  201. raise OperationError(self.w_KeyError, self.wrap("key error"))
  202. assert isinstance(obj, ListObject)
  203. assert isinstance(index, IntObject)
  204. return obj.items[index.intval]
  205. def listview(self, obj, number=-1):
  206. assert isinstance(obj, ListObject)
  207. if number != -1:
  208. assert number == 2
  209. return [obj.items[0], obj.items[1]]
  210. return obj.items
  211. fixedview = listview
  212. def float(self, w_obj):
  213. if isinstance(w_obj, FloatObject):
  214. return w_obj
  215. assert isinstance(w_obj, boxes.W_GenericBox)
  216. return self.float(w_obj.descr_float(self))
  217. def float_w(self, w_obj, allow_conversion=True):
  218. assert isinstance(w_obj, FloatObject)
  219. return w_obj.floatval
  220. def int_w(self, w_obj, allow_conversion=True):
  221. if isinstance(w_obj, IntObject):
  222. return w_obj.intval
  223. elif isinstance(w_obj, FloatObject):
  224. return int(w_obj.floatval)
  225. elif isinstance(w_obj, SliceObject):
  226. raise OperationError(self.w_TypeError, self.wrap("slice."))
  227. raise NotImplementedError
  228. def unpackcomplex(self, w_obj):
  229. if isinstance(w_obj, ComplexObject):
  230. return w_obj.r, w_obj.i
  231. raise NotImplementedError
  232. def index(self, w_obj):
  233. return self.wrap(self.int_w(w_obj))
  234. def str_w(self, w_obj):
  235. if isinstance(w_obj, StringObject):
  236. return w_obj.v
  237. raise NotImplementedError
  238. def unicode_w(self, w_obj):
  239. # XXX
  240. if isinstance(w_obj, StringObject):
  241. return unicode(w_obj.v)
  242. raise NotImplementedError
  243. def int(self, w_obj):
  244. if isinstance(w_obj, IntObject):
  245. return w_obj
  246. assert isinstance(w_obj, boxes.W_GenericBox)
  247. return self.int(w_obj.descr_int(self))
  248. def long(self, w_obj):
  249. if isinstance(w_obj, LongObject):
  250. return w_obj
  251. assert isinstance(w_obj, boxes.W_GenericBox)
  252. return self.int(w_obj.descr_long(self))
  253. def str(self, w_obj):
  254. if isinstance(w_obj, StringObject):
  255. return w_obj
  256. assert isinstance(w_obj, boxes.W_GenericBox)
  257. return self.str(w_obj.descr_str(self))
  258. def is_true(self, w_obj):
  259. assert isinstance(w_obj, BoolObject)
  260. return bool(w_obj.intval)
  261. def gt(self, w_lhs, w_rhs):
  262. return BoolObject(self.int_w(w_lhs) > self.int_w(w_rhs))
  263. def lt(self, w_lhs, w_rhs):
  264. return BoolObject(self.int_w(w_lhs) < self.int_w(w_rhs))
  265. def is_w(self, w_obj, w_what):
  266. return w_obj is w_what
  267. def eq_w(self, w_obj, w_what):
  268. return w_obj == w_what
  269. def issubtype(self, w_type1, w_type2):
  270. return BoolObject(True)
  271. def type(self, w_obj):
  272. if self.is_none(w_obj):
  273. return self.w_None
  274. try:
  275. return w_obj.tp
  276. except AttributeError:
  277. if isinstance(w_obj, W_NDimArray):
  278. return W_NDimArray
  279. return self.w_None
  280. def lookup(self, w_obj, name):
  281. w_type = self.type(w_obj)
  282. if not self.is_none(w_type):
  283. return w_type.lookup(name)
  284. def gettypefor(self, w_obj):
  285. return W_TypeObject(w_obj.typedef.name)
  286. def call_function(self, tp, w_dtype, *args):
  287. if tp is self.w_float:
  288. if isinstance(w_dtype, boxes.W_Float64Box):
  289. return FloatObject(float(w_dtype.value))
  290. if isinstance(w_dtype, boxes.W_Float32Box):
  291. return FloatObject(float(w_dtype.value))
  292. if isinstance(w_dtype, boxes.W_Int64Box):
  293. return FloatObject(float(int(w_dtype.value)))
  294. if isinstance(w_dtype, boxes.W_Int32Box):
  295. return FloatObject(float(int(w_dtype.value)))
  296. if isinstance(w_dtype, boxes.W_Int16Box):
  297. return FloatObject(float(int(w_dtype.value)))
  298. if isinstance(w_dtype, boxes.W_Int8Box):
  299. return FloatObject(float(int(w_dtype.value)))
  300. if isinstance(w_dtype, IntObject):
  301. return FloatObject(float(w_dtype.intval))
  302. if tp is self.w_int:
  303. if isinstance(w_dtype, FloatObject):
  304. return IntObject(int(w_dtype.floatval))
  305. return w_dtype
  306. @specialize.arg(2)
  307. def call_method(self, w_obj, s, *args):
  308. # XXX even the hacks have hacks
  309. if s == 'size': # used in _array() but never called by tests
  310. return IntObject(0)
  311. return getattr(w_obj, 'descr_' + s)(self, *args)
  312. @specialize.arg(1)
  313. def interp_w(self, tp, what):
  314. assert isinstance(what, tp)
  315. return what
  316. def allocate_instance(self, klass, w_subtype):
  317. return instantiate(klass)
  318. def newtuple(self, list_w):
  319. return ListObject(list_w)
  320. def newdict(self, module=True):
  321. return DictObject({})
  322. def newint(self, i):
  323. if isinstance(i, IntObject):
  324. return i
  325. return IntObject(i)
  326. def setitem(self, obj, index, value):
  327. obj.items[index] = value
  328. def exception_match(self, w_exc_type, w_check_class):
  329. assert isinstance(w_exc_type, W_TypeObject)
  330. assert isinstance(w_check_class, W_TypeObject)
  331. return w_exc_type.name == w_check_class.name
  332. def warn(self, w_msg, w_warn_type):
  333. pass
  334. class FloatObject(W_Root):
  335. tp = FakeSpace.w_float
  336. def __init__(self, floatval):
  337. self.floatval = floatval
  338. class BoolObject(W_Root):
  339. tp = FakeSpace.w_bool
  340. def __init__(self, boolval):
  341. self.intval = boolval
  342. FakeSpace.w_True = BoolObject(True)
  343. FakeSpace.w_False = BoolObject(False)
  344. class IntObject(W_Root):
  345. tp = FakeSpace.w_int
  346. def __init__(self, intval):
  347. self.intval = intval
  348. class LongObject(W_Root):
  349. tp = FakeSpace.w_long
  350. def __init__(self, intval):
  351. self.intval = intval
  352. class ListObject(W_Root):
  353. tp = FakeSpace.w_list
  354. def __init__(self, items):
  355. self.items = items
  356. class DictObject(W_Root):
  357. tp = FakeSpace.w_dict
  358. def __init__(self, items):
  359. self.items = items
  360. def getdict(self, space):
  361. return self.items
  362. def getdictvalue(self, space, key):
  363. return self.items[key]
  364. class IterDictObject(W_Root):
  365. def __init__(self, space, w_dict):
  366. self.space = space
  367. self.items = w_dict.items.items()
  368. self.i = 0
  369. def __iter__(self):
  370. return self
  371. def next(self):
  372. space = self.space
  373. if self.i >= len(self.items):
  374. raise OperationError(space.w_StopIteration, space.wrap("stop iteration"))
  375. self.i += 1
  376. return self.items[self.i-1][0]
  377. class SliceObject(W_Root):
  378. tp = FakeSpace.w_slice
  379. def __init__(self, start, stop, step):
  380. self.start = start
  381. self.stop = stop
  382. self.step = step
  383. class StringObject(W_Root):
  384. tp = FakeSpace.w_str
  385. def __init__(self, v):
  386. self.v = v
  387. class ComplexObject(W_Root):
  388. tp = FakeSpace.w_complex
  389. def __init__(self, r, i):
  390. self.r = r
  391. self.i = i
  392. class InterpreterState(object):
  393. def __init__(self, code):
  394. self.code = code
  395. self.variables = {}
  396. self.results = []
  397. def run(self, space):
  398. self.space = space
  399. for stmt in self.code.statements:
  400. stmt.execute(self)
  401. class Node(object):
  402. def __eq__(self, other):
  403. return (self.__class__ == other.__class__ and
  404. self.__dict__ == other.__dict__)
  405. def __ne__(self, other):
  406. return not self == other
  407. def wrap(self, space):
  408. raise NotImplementedError
  409. def execute(self, interp):
  410. raise NotImplementedError
  411. class Assignment(Node):
  412. def __init__(self, name, expr):
  413. self.name = name
  414. self.expr = expr
  415. def execute(self, interp):
  416. interp.variables[self.name] = self.expr.execute(interp)
  417. def __repr__(self):
  418. return "%r = %r" % (self.name, self.expr)
  419. class ArrayAssignment(Node):
  420. def __init__(self, name, index, expr):
  421. self.name = name
  422. self.index = index
  423. self.expr = expr
  424. def execute(self, interp):
  425. arr = interp.variables[self.name]
  426. w_index = self.index.execute(interp)
  427. # cast to int
  428. if isinstance(w_index, FloatObject):
  429. w_index = IntObject(int(w_index.floatval))
  430. w_val = self.expr.execute(interp)
  431. assert isinstance(arr, W_NDimArray)
  432. arr.descr_setitem(interp.space, w_index, w_val)
  433. def __repr__(self):
  434. return "%s[%r] = %r" % (self.name, self.index, self.expr)
  435. class Variable(Node):
  436. def __init__(self, name):
  437. self.name = name.strip(" ")
  438. def execute(self, interp):
  439. if self.name == 'None':
  440. return None
  441. return interp.variables[self.name]
  442. def __repr__(self):
  443. return 'v(%s)' % self.name
  444. class Operator(Node):
  445. def __init__(self, lhs, name, rhs):
  446. self.name = name
  447. self.lhs = lhs
  448. self.rhs = rhs
  449. def execute(self, interp):
  450. w_lhs = self.lhs.execute(interp)
  451. if isinstance(self.rhs, SliceConstant):
  452. w_rhs = self.rhs.wrap(interp.space)
  453. else:
  454. w_rhs = self.rhs.execute(interp)
  455. if not isinstance(w_lhs, W_NDimArray):
  456. # scalar
  457. dtype = get_dtype_cache(interp.space).w_float64dtype
  458. w_lhs = W_NDimArray.new_scalar(interp.space, dtype, w_lhs)
  459. assert isinstance(w_lhs, W_NDimArray)
  460. if self.name == '+':
  461. w_res = w_lhs.descr_add(interp.space, w_rhs)
  462. elif self.name == '*':
  463. w_res = w_lhs.descr_mul(interp.space, w_rhs)
  464. elif self.name == '-':
  465. w_res = w_lhs.descr_sub(interp.space, w_rhs)
  466. elif self.name == '**':
  467. w_res = w_lhs.descr_pow(interp.space, w_rhs)
  468. elif self.name == '->':
  469. if isinstance(w_rhs, FloatObject):
  470. w_rhs = IntObject(int(w_rhs.floatval))
  471. assert isinstance(w_lhs, W_NDimArray)
  472. w_res = w_lhs.descr_getitem(interp.space, w_rhs)
  473. if isinstance(w_rhs, IntObject):
  474. if isinstance(w_res, boxes.W_Float64Box):
  475. print "access", w_lhs, "[", w_rhs.intval, "] => ", float(w_res.value)
  476. if isinstance(w_res, boxes.W_Float32Box):
  477. print "access", w_lhs, "[", w_rhs.intval, "] => ", float(w_res.value)
  478. if isinstance(w_res, boxes.W_Int64Box):
  479. print "access", w_lhs, "[", w_rhs.intval, "] => ", int(w_res.value)
  480. if isinstance(w_res, boxes.W_Int32Box):
  481. print "access", w_lhs, "[", w_rhs.intval, "] => ", int(w_res.value)
  482. else:
  483. raise NotImplementedError
  484. if (not isinstance(w_res, W_NDimArray) and
  485. not isinstance(w_res, boxes.W_GenericBox)):
  486. dtype = get_dtype_cache(interp.space).w_float64dtype
  487. w_res = W_NDimArray.new_scalar(interp.space, dtype, w_res)
  488. return w_res
  489. def __repr__(self):
  490. return '(%r %s %r)' % (self.lhs, self.name, self.rhs)
  491. class NumberConstant(Node):
  492. def __init__(self, v):
  493. if isinstance(v, int):
  494. self.v = v
  495. elif isinstance(v, float):
  496. self.v = v
  497. else:
  498. assert isinstance(v, str)
  499. assert len(v) > 0
  500. c = v[-1]
  501. if c == 'f':
  502. self.v = float(v[:-1])
  503. elif c == 'i':
  504. self.v = int(v[:-1])
  505. else:
  506. self.v = float(v)
  507. def __repr__(self):
  508. return "Const(%s)" % self.v
  509. def wrap(self, space):
  510. return space.wrap(self.v)
  511. def execute(self, interp):
  512. return interp.space.wrap(self.v)
  513. class ComplexConstant(Node):
  514. def __init__(self, r, i):
  515. self.r = float(r)
  516. self.i = float(i)
  517. def __repr__(self):
  518. return 'ComplexConst(%s, %s)' % (self.r, self.i)
  519. def wrap(self, space):
  520. return space.newcomplex(self.r, self.i)
  521. def execute(self, interp):
  522. return self.wrap(interp.space)
  523. class RangeConstant(Node):
  524. def __init__(self, v):
  525. self.v = int(v)
  526. def execute(self, interp):
  527. w_list = interp.space.newlist(
  528. [interp.space.wrap(float(i)) for i in range(self.v)]
  529. )
  530. dtype = get_dtype_cache(interp.space).w_float64dtype
  531. return array(interp.space, w_list, w_dtype=dtype, w_order=None)
  532. def __repr__(self):
  533. return 'Range(%s)' % self.v
  534. class Code(Node):
  535. def __init__(self, statements):
  536. self.statements = statements
  537. def __repr__(self):
  538. return "\n".join([repr(i) for i in self.statements])
  539. class ArrayConstant(Node):
  540. def __init__(self, items):
  541. self.items = items
  542. def wrap(self, space):
  543. return space.newlist([item.wrap(space) for item in self.items])
  544. def execute(self, interp):
  545. w_list = self.wrap(interp.space)
  546. return array(interp.space, w_list)
  547. def __repr__(self):
  548. return "[" + ", ".join([repr(item) for item in self.items]) + "]"
  549. class SliceConstant(Node):
  550. def __init__(self, start, stop, step):
  551. self.start = start
  552. self.stop = stop
  553. self.step = step
  554. def wrap(self, space):
  555. return SliceObject(self.start, self.stop, self.step)
  556. def execute(self, interp):
  557. return SliceObject(self.start, self.stop, self.step)
  558. def __repr__(self):
  559. return 'slice(%s,%s,%s)' % (self.start, self.stop, self.step)
  560. class ArrayClass(Node):
  561. def __init__(self):
  562. self.v = W_NDimArray
  563. def execute(self, interp):
  564. return self.v
  565. def __repr__(self):
  566. return '<class W_NDimArray>'
  567. class DtypeClass(Node):
  568. def __init__(self, dt):
  569. self.v = dt
  570. def execute(self, interp):
  571. if self.v == 'int':
  572. dtype = get_dtype_cache(interp.space).w_int64dtype
  573. elif self.v == 'int8':
  574. dtype = get_dtype_cache(interp.space).w_int8dtype
  575. elif self.v == 'int16':
  576. dtype = get_dtype_cache(interp.space).w_int16dtype
  577. elif self.v == 'int32':
  578. dtype = get_dtype_cache(interp.space).w_int32dtype
  579. elif self.v == 'uint':
  580. dtype = get_dtype_cache(interp.space).w_uint64dtype
  581. elif self.v == 'uint8':
  582. dtype = get_dtype_cache(interp.space).w_uint8dtype
  583. elif self.v == 'uint16':
  584. dtype = get_dtype_cache(interp.space).w_uint16dtype
  585. elif self.v == 'uint32':
  586. dtype = get_dtype_cache(interp.space).w_uint32dtype
  587. elif self.v == 'float':
  588. dtype = get_dtype_cache(interp.space).w_float64dtype
  589. elif self.v == 'float32':
  590. dtype = get_dtype_cache(interp.space).w_float32dtype
  591. else:
  592. raise BadToken('unknown v to dtype "%s"' % self.v)
  593. return dtype
  594. def __repr__(self):
  595. return '<class %s dtype>' % self.v
  596. class Execute(Node):
  597. def __init__(self, expr):
  598. self.expr = expr
  599. def __repr__(self):
  600. return repr(self.expr)
  601. def execute(self, interp):
  602. interp.results.append(self.expr.execute(interp))
  603. class FunctionCall(Node):
  604. def __init__(self, name, args):
  605. self.name = name.strip(" ")
  606. self.args = args
  607. def __repr__(self):
  608. return "%s(%s)" % (self.name, ", ".join([repr(arg)
  609. for arg in self.args]))
  610. def execute(self, interp):
  611. arr = self.args[0].execute(interp)
  612. if not isinstance(arr, W_NDimArray):
  613. raise ArgumentNotAnArray
  614. if self.name in SINGLE_ARG_FUNCTIONS:
  615. if len(self.args) != 1 and self.name != 'sum':
  616. raise ArgumentMismatch
  617. if self.name == "sum":
  618. if len(self.args)>1:
  619. var = self.args[1]
  620. if isinstance(var, DtypeClass):
  621. w_res = arr.descr_sum(interp.space, None, var.execute(interp))
  622. else:
  623. w_res = arr.descr_sum(interp.space,
  624. self.args[1].execute(interp))
  625. else:
  626. w_res = arr.descr_sum(interp.space)
  627. elif self.name == "prod":
  628. w_res = arr.descr_prod(interp.space)
  629. elif self.name == "max":
  630. w_res = arr.descr_max(interp.space)
  631. elif self.name == "min":
  632. w_res = arr.descr_min(interp.space)
  633. elif self.name == "any":
  634. w_res = arr.descr_any(interp.space)
  635. elif self.name == "all":
  636. w_res = arr.descr_all(interp.space)
  637. elif self.name == "cumsum":
  638. w_res = arr.descr_cumsum(interp.space)
  639. elif self.name == "logical_xor_reduce":
  640. logical_xor = ufuncs.get(interp.space).logical_xor
  641. w_res = logical_xor.reduce(interp.space, arr, None)
  642. elif self.name == "unegative":
  643. neg = ufuncs.get(interp.space).negative
  644. w_res = neg.call(interp.space, [arr], None, 'unsafe', None)
  645. elif self.name == "cos":
  646. cos = ufuncs.get(interp.space).cos
  647. w_res = cos.call(interp.space, [arr], None, 'unsafe', None)
  648. elif self.name == "flat":
  649. w_res = arr.descr_get_flatiter(interp.space)
  650. elif self.name == "argsort":
  651. w_res = arr.descr_argsort(interp.space)
  652. elif self.name == "tostring":
  653. arr.descr_tostring(interp.space)
  654. w_res = None
  655. else:
  656. assert False # unreachable code
  657. elif self.name in TWO_ARG_FUNCTIONS:
  658. if len(self.args) != 2:
  659. raise ArgumentMismatch
  660. arg = self.args[1].execute(interp)
  661. if not isinstance(arg, W_NDimArray):
  662. raise ArgumentNotAnArray
  663. if self.name == "dot":
  664. w_res = arr.descr_dot(interp.space, arg)
  665. elif self.name == 'multiply':
  666. w_res = arr.descr_mul(interp.space, arg)
  667. elif self.name == 'take':
  668. w_res = arr.descr_take(interp.space, arg)
  669. elif self.name == "searchsorted":
  670. w_res = arr.descr_searchsorted(interp.space, arg,
  671. interp.space.wrap('left'))
  672. else:
  673. assert False # unreachable code
  674. elif self.name in THREE_ARG_FUNCTIONS:
  675. if len(self.args) != 3:
  676. raise ArgumentMismatch
  677. arg1 = self.args[1].execute(interp)
  678. arg2 = self.args[2].execute(interp)
  679. if not isinstance(arg1, W_NDimArray):
  680. raise ArgumentNotAnArray
  681. if not isinstance(arg2, W_NDimArray):
  682. raise ArgumentNotAnArray
  683. if self.name == "where":
  684. w_res = where(interp.space, arr, arg1, arg2)
  685. else:
  686. assert False # unreachable code
  687. elif self.name in TWO_ARG_FUNCTIONS_OR_NONE:
  688. if len(self.args) != 2:
  689. raise ArgumentMismatch
  690. arg = self.args[1].execute(interp)
  691. if self.name == 'view':
  692. w_res = arr.descr_view(interp.space, arg)
  693. elif self.name == 'astype':
  694. w_res = arr.descr_astype(interp.space, arg)
  695. elif self.name == 'reshape':
  696. w_arg = self.args[1]
  697. assert isinstance(w_arg, ArrayConstant)
  698. order = -1
  699. w_res = arr.reshape(interp.space, w_arg.wrap(interp.space), order)
  700. else:
  701. assert False
  702. else:
  703. raise WrongFunctionName
  704. if isinstance(w_res, W_NDimArray):
  705. return w_res
  706. if isinstance(w_res, FloatObject):
  707. dtype = get_dtype_cache(interp.space).w_float64dtype
  708. elif isinstance(w_res, IntObject):
  709. dtype = get_dtype_cache(interp.space).w_int64dtype
  710. elif isinstance(w_res, BoolObject):
  711. dtype = get_dtype_cache(interp.space).w_booldtype
  712. elif isinstance(w_res, boxes.W_GenericBox):
  713. dtype = w_res.get_dtype(interp.space)
  714. else:
  715. dtype = None
  716. return W_NDimArray.new_scalar(interp.space, dtype, w_res)
  717. _REGEXES = [
  718. ('-?[\d\.]+(i|f)?', 'number'),
  719. ('\[', 'array_left'),
  720. (':', 'colon'),
  721. ('\w+', 'identifier'),
  722. ('\]', 'array_right'),
  723. ('(->)|[\+\-\*\/]+', 'operator'),
  724. ('=', 'assign'),
  725. (',', 'comma'),
  726. ('\|', 'pipe'),
  727. ('\(', 'paren_left'),
  728. ('\)', 'paren_right'),
  729. ]
  730. REGEXES = []
  731. for r, name in _REGEXES:
  732. REGEXES.append((re.compile(r' *(' + r + ')'), name))
  733. del _REGEXES
  734. class Token(object):
  735. def __init__(self, name, v):
  736. self.name = name
  737. self.v = v
  738. def __repr__(self):
  739. return '(%s, %s)' % (self.name, self.v)
  740. empty = Token('', '')
  741. class TokenStack(object):
  742. def __init__(self, tokens):
  743. self.tokens = tokens
  744. self.c = 0
  745. def pop(self):
  746. token = self.tokens[self.c]
  747. self.c += 1
  748. return token
  749. def get(self, i):
  750. if self.c + i >= len(self.tokens):
  751. return empty
  752. return self.tokens[self.c + i]
  753. def remaining(self):
  754. return len(self.tokens) - self.c
  755. def push(self):
  756. self.c -= 1
  757. def __repr__(self):
  758. return repr(self.tokens[self.c:])
  759. class Parser(object):
  760. def tokenize(self, line):
  761. tokens = []
  762. while True:
  763. for r, name in REGEXES:
  764. m = r.match(line)
  765. if m is not None:
  766. g = m.group(0)
  767. tokens.append(Token(name, g))
  768. line = line[len(g):]
  769. if not line:
  770. return TokenStack(tokens)
  771. break
  772. else:
  773. raise TokenizerError(line)
  774. def parse_number_or_slice(self, tokens):
  775. start_tok = tokens.pop()
  776. if start_tok.name == 'colon':
  777. start = 0
  778. else:
  779. if tokens.get(0).name != 'colon':
  780. return NumberConstant(start_tok.v)
  781. start = int(start_tok.v)
  782. tokens.pop()
  783. if not tokens.get(0).name in ['colon', 'number']:
  784. stop = -1
  785. step = 1
  786. else:
  787. next = tokens.pop()
  788. if next.name == 'colon':
  789. stop = -1
  790. step = int(tokens.pop().v)
  791. else:
  792. stop = int(next.v)
  793. if tokens.get(0).name == 'colon':
  794. tokens.pop()
  795. step = int(tokens.pop().v)
  796. else:
  797. step = 1
  798. return SliceConstant(start, stop, step)
  799. def parse_expression(self, tokens, accept_comma=False):
  800. stack = []
  801. while tokens.remaining():
  802. token = tokens.pop()
  803. if token.name == 'identifier':
  804. if tokens.remaining() and tokens.get(0).name == 'paren_left':
  805. stack.append(self.parse_function_call(token.v, tokens))
  806. elif token.v.strip(' ') == 'ndarray':
  807. stack.append(ArrayClass())
  808. elif token.v.strip(' ') == 'int':
  809. stack.append(DtypeClass('int'))
  810. elif token.v.strip(' ') == 'int8':
  811. stack.append(DtypeClass('int8'))
  812. elif token.v.strip(' ') == 'int16':
  813. stack.append(DtypeClass('int16'))
  814. elif token.v.strip(' ') == 'int32':
  815. stack.append(DtypeClass('int32'))
  816. elif token.v.strip(' ') == 'int64':
  817. stack.append(DtypeClass('int'))
  818. elif token.v.strip(' ') == 'uint':
  819. stack.append(DtypeClass('uint'))
  820. elif token.v.strip(' ') == 'uint8':
  821. stack.append(DtypeClass('uint8'))
  822. elif token.v.strip(' ') == 'uint16':
  823. stack.append(DtypeClass('uint16'))
  824. elif token.v.strip(' ') == 'uint32':
  825. stack.append(DtypeClass('uint32'))
  826. elif token.v.strip(' ') == 'uint64':
  827. stack.append(DtypeClass('uint'))
  828. elif token.v.strip(' ') == 'float':
  829. stack.append(DtypeClass('float'))
  830. elif token.v.strip(' ') == 'float32':
  831. stack.append(DtypeClass('float32'))
  832. elif token.v.strip(' ') == 'float64':
  833. stack.append(DtypeClass('float'))
  834. else:
  835. stack.append(Variable(token.v.strip(' ')))
  836. elif token.name == 'array_left':
  837. stack.append(ArrayConstant(self.parse_array_const(tokens)))
  838. elif token.name == 'operator':
  839. stack.append(Variable(token.v))
  840. elif token.name == 'number' or token.name == 'colon':
  841. tokens.push()
  842. stack.append(self.parse_number_or_slice(tokens))
  843. elif token.name == 'pipe':
  844. stack.append(RangeConstant(tokens.pop().v))
  845. end = tokens.pop()
  846. assert end.name == 'pipe'
  847. elif token.name == 'paren_left':
  848. stack.append(self.parse_complex_constant(tokens))
  849. elif accept_comma and token.name == 'comma':
  850. continue
  851. else:
  852. tokens.push()
  853. break
  854. if accept_comma:
  855. return stack
  856. stack.reverse()
  857. lhs = stack.pop()
  858. while stack:
  859. op = stack.pop()
  860. assert isinstance(op, Variable)
  861. rhs = stack.pop()
  862. lhs = Operator(lhs, op.name, rhs)
  863. return lhs
  864. def parse_function_call(self, name, tokens):
  865. args = []
  866. tokens.pop() # lparen
  867. while tokens.get(0).name != 'paren_right':
  868. args += self.parse_expression(tokens, accept_comma=True)
  869. return FunctionCall(name, args)
  870. def parse_complex_constant(self, tokens):
  871. r = tokens.pop()
  872. assert r.name == 'number'
  873. assert tokens.pop().name == 'comma'
  874. i = tokens.pop()
  875. assert i.name == 'number'
  876. assert tokens.pop().name == 'paren_right'
  877. return ComplexConstant(r.v, i.v)
  878. def parse_array_const(self, tokens):
  879. elems = []
  880. while True:
  881. token = tokens.pop()
  882. if token.name == 'number':
  883. elems.append(NumberConstant(token.v))
  884. elif token.name == 'array_left':
  885. elems.append(ArrayConstant(self.parse_array_const(tokens)))
  886. elif token.name == 'paren_left':
  887. elems.append(self.parse_complex_constant(tokens))
  888. else:
  889. raise BadToken()
  890. token = tokens.pop()
  891. if token.name == 'array_right':
  892. return elems
  893. assert token.name == 'comma'
  894. def parse_statement(self, tokens):
  895. if (tokens.get(0).name == 'identifier' and
  896. tokens.get(1).name == 'assign'):
  897. lhs = tokens.pop().v
  898. tokens.pop()
  899. rhs = self.parse_expression(tokens)
  900. return Assignment(lhs, rhs)
  901. elif (tokens.get(0).name == 'identifier' and
  902. tokens.get(1).name == 'array_left'):
  903. name = tokens.pop().v
  904. tokens.pop()
  905. index = self.parse_expression(tokens)
  906. tokens.pop()
  907. tokens.pop()
  908. return ArrayAssignment(name, index, self.parse_expression(tokens))
  909. return Execute(self.parse_expression(tokens))
  910. def parse(self, code):
  911. statements = []
  912. for line in code.split("\n"):
  913. if '#' in line:
  914. line = line.split('#', 1)[0]
  915. line = line.strip(" ")
  916. if line:
  917. tokens = self.tokenize(line)
  918. statements.append(self.parse_statement(tokens))
  919. return Code(statements)
  920. def numpy_compile(code):
  921. parser = Parser()
  922. return InterpreterState(parser.parse(code))