symbolic.py 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518
  1. """Fortran/C symbolic expressions
  2. References:
  3. - J3/21-007: Draft Fortran 202x. https://j3-fortran.org/doc/year/21/21-007.pdf
  4. Copyright 1999 -- 2011 Pearu Peterson all rights reserved.
  5. Copyright 2011 -- present NumPy Developers.
  6. Permission to use, modify, and distribute this software is given under the
  7. terms of the NumPy License.
  8. NO WARRANTY IS EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
  9. """
  10. # To analyze Fortran expressions to solve dimensions specifications,
  11. # for instances, we implement a minimal symbolic engine for parsing
  12. # expressions into a tree of expression instances. As a first
  13. # instance, we care only about arithmetic expressions involving
  14. # integers and operations like addition (+), subtraction (-),
  15. # multiplication (*), division (Fortran / is Python //, Fortran // is
  16. # concatenate), and exponentiation (**). In addition, .pyf files may
  17. # contain C expressions that support here is implemented as well.
  18. #
  19. # TODO: support logical constants (Op.BOOLEAN)
  20. # TODO: support logical operators (.AND., ...)
  21. # TODO: support defined operators (.MYOP., ...)
  22. #
  23. __all__ = ['Expr']
  24. import re
  25. import warnings
  26. from enum import Enum
  27. from math import gcd
  28. class Language(Enum):
  29. """
  30. Used as Expr.tostring language argument.
  31. """
  32. Python = 0
  33. Fortran = 1
  34. C = 2
  35. class Op(Enum):
  36. """
  37. Used as Expr op attribute.
  38. """
  39. INTEGER = 10
  40. REAL = 12
  41. COMPLEX = 15
  42. STRING = 20
  43. ARRAY = 30
  44. SYMBOL = 40
  45. TERNARY = 100
  46. APPLY = 200
  47. INDEXING = 210
  48. CONCAT = 220
  49. RELATIONAL = 300
  50. TERMS = 1000
  51. FACTORS = 2000
  52. REF = 3000
  53. DEREF = 3001
  54. class RelOp(Enum):
  55. """
  56. Used in Op.RELATIONAL expression to specify the function part.
  57. """
  58. EQ = 1
  59. NE = 2
  60. LT = 3
  61. LE = 4
  62. GT = 5
  63. GE = 6
  64. @classmethod
  65. def fromstring(cls, s, language=Language.C):
  66. if language is Language.Fortran:
  67. return {'.eq.': RelOp.EQ, '.ne.': RelOp.NE,
  68. '.lt.': RelOp.LT, '.le.': RelOp.LE,
  69. '.gt.': RelOp.GT, '.ge.': RelOp.GE}[s.lower()]
  70. return {'==': RelOp.EQ, '!=': RelOp.NE, '<': RelOp.LT,
  71. '<=': RelOp.LE, '>': RelOp.GT, '>=': RelOp.GE}[s]
  72. def tostring(self, language=Language.C):
  73. if language is Language.Fortran:
  74. return {RelOp.EQ: '.eq.', RelOp.NE: '.ne.',
  75. RelOp.LT: '.lt.', RelOp.LE: '.le.',
  76. RelOp.GT: '.gt.', RelOp.GE: '.ge.'}[self]
  77. return {RelOp.EQ: '==', RelOp.NE: '!=',
  78. RelOp.LT: '<', RelOp.LE: '<=',
  79. RelOp.GT: '>', RelOp.GE: '>='}[self]
  80. class ArithOp(Enum):
  81. """
  82. Used in Op.APPLY expression to specify the function part.
  83. """
  84. POS = 1
  85. NEG = 2
  86. ADD = 3
  87. SUB = 4
  88. MUL = 5
  89. DIV = 6
  90. POW = 7
  91. class OpError(Exception):
  92. pass
  93. class Precedence(Enum):
  94. """
  95. Used as Expr.tostring precedence argument.
  96. """
  97. ATOM = 0
  98. POWER = 1
  99. UNARY = 2
  100. PRODUCT = 3
  101. SUM = 4
  102. LT = 6
  103. EQ = 7
  104. LAND = 11
  105. LOR = 12
  106. TERNARY = 13
  107. ASSIGN = 14
  108. TUPLE = 15
  109. NONE = 100
  110. integer_types = (int,)
  111. number_types = (int, float)
  112. def _pairs_add(d, k, v):
  113. # Internal utility method for updating terms and factors data.
  114. c = d.get(k)
  115. if c is None:
  116. d[k] = v
  117. else:
  118. c = c + v
  119. if c:
  120. d[k] = c
  121. else:
  122. del d[k]
  123. class ExprWarning(UserWarning):
  124. pass
  125. def ewarn(message):
  126. warnings.warn(message, ExprWarning, stacklevel=2)
  127. class Expr:
  128. """Represents a Fortran expression as an op-data pair.
  129. Expr instances are hashable and sortable.
  130. """
  131. @staticmethod
  132. def parse(s, language=Language.C):
  133. """Parse a Fortran expression to an Expr.
  134. """
  135. return fromstring(s, language=language)
  136. def __init__(self, op, data):
  137. assert isinstance(op, Op)
  138. # sanity checks
  139. if op is Op.INTEGER:
  140. # data is a 2-tuple of numeric object and a kind value
  141. # (default is 4)
  142. assert isinstance(data, tuple) and len(data) == 2
  143. assert isinstance(data[0], int)
  144. assert isinstance(data[1], (int, str)), data
  145. elif op is Op.REAL:
  146. # data is a 2-tuple of numeric object and a kind value
  147. # (default is 4)
  148. assert isinstance(data, tuple) and len(data) == 2
  149. assert isinstance(data[0], float)
  150. assert isinstance(data[1], (int, str)), data
  151. elif op is Op.COMPLEX:
  152. # data is a 2-tuple of constant expressions
  153. assert isinstance(data, tuple) and len(data) == 2
  154. elif op is Op.STRING:
  155. # data is a 2-tuple of quoted string and a kind value
  156. # (default is 1)
  157. assert isinstance(data, tuple) and len(data) == 2
  158. assert (isinstance(data[0], str)
  159. and data[0][::len(data[0]) - 1] in ('""', "''", '@@'))
  160. assert isinstance(data[1], (int, str)), data
  161. elif op is Op.SYMBOL:
  162. # data is any hashable object
  163. assert hash(data) is not None
  164. elif op in (Op.ARRAY, Op.CONCAT):
  165. # data is a tuple of expressions
  166. assert isinstance(data, tuple)
  167. assert all(isinstance(item, Expr) for item in data), data
  168. elif op in (Op.TERMS, Op.FACTORS):
  169. # data is {<term|base>:<coeff|exponent>} where dict values
  170. # are nonzero Python integers
  171. assert isinstance(data, dict)
  172. elif op is Op.APPLY:
  173. # data is (<function>, <operands>, <kwoperands>) where
  174. # operands are Expr instances
  175. assert isinstance(data, tuple) and len(data) == 3
  176. # function is any hashable object
  177. assert hash(data[0]) is not None
  178. assert isinstance(data[1], tuple)
  179. assert isinstance(data[2], dict)
  180. elif op is Op.INDEXING:
  181. # data is (<object>, <indices>)
  182. assert isinstance(data, tuple) and len(data) == 2
  183. # function is any hashable object
  184. assert hash(data[0]) is not None
  185. elif op is Op.TERNARY:
  186. # data is (<cond>, <expr1>, <expr2>)
  187. assert isinstance(data, tuple) and len(data) == 3
  188. elif op in (Op.REF, Op.DEREF):
  189. # data is Expr instance
  190. assert isinstance(data, Expr)
  191. elif op is Op.RELATIONAL:
  192. # data is (<relop>, <left>, <right>)
  193. assert isinstance(data, tuple) and len(data) == 3
  194. else:
  195. raise NotImplementedError(
  196. f'unknown op or missing sanity check: {op}')
  197. self.op = op
  198. self.data = data
  199. def __eq__(self, other):
  200. return (isinstance(other, Expr)
  201. and self.op is other.op
  202. and self.data == other.data)
  203. def __hash__(self):
  204. if self.op in (Op.TERMS, Op.FACTORS):
  205. data = tuple(sorted(self.data.items()))
  206. elif self.op is Op.APPLY:
  207. data = self.data[:2] + tuple(sorted(self.data[2].items()))
  208. else:
  209. data = self.data
  210. return hash((self.op, data))
  211. def __lt__(self, other):
  212. if isinstance(other, Expr):
  213. if self.op is not other.op:
  214. return self.op.value < other.op.value
  215. if self.op in (Op.TERMS, Op.FACTORS):
  216. return (tuple(sorted(self.data.items()))
  217. < tuple(sorted(other.data.items())))
  218. if self.op is Op.APPLY:
  219. if self.data[:2] != other.data[:2]:
  220. return self.data[:2] < other.data[:2]
  221. return tuple(sorted(self.data[2].items())) < tuple(
  222. sorted(other.data[2].items()))
  223. return self.data < other.data
  224. return NotImplemented
  225. def __le__(self, other): return self == other or self < other
  226. def __gt__(self, other): return not (self <= other)
  227. def __ge__(self, other): return not (self < other)
  228. def __repr__(self):
  229. return f'{type(self).__name__}({self.op}, {self.data!r})'
  230. def __str__(self):
  231. return self.tostring()
  232. def tostring(self, parent_precedence=Precedence.NONE,
  233. language=Language.Fortran):
  234. """Return a string representation of Expr.
  235. """
  236. if self.op in (Op.INTEGER, Op.REAL):
  237. precedence = (Precedence.SUM if self.data[0] < 0
  238. else Precedence.ATOM)
  239. r = str(self.data[0]) + (f'_{self.data[1]}'
  240. if self.data[1] != 4 else '')
  241. elif self.op is Op.COMPLEX:
  242. r = ', '.join(item.tostring(Precedence.TUPLE, language=language)
  243. for item in self.data)
  244. r = '(' + r + ')'
  245. precedence = Precedence.ATOM
  246. elif self.op is Op.SYMBOL:
  247. precedence = Precedence.ATOM
  248. r = str(self.data)
  249. elif self.op is Op.STRING:
  250. r = self.data[0]
  251. if self.data[1] != 1:
  252. r = self.data[1] + '_' + r
  253. precedence = Precedence.ATOM
  254. elif self.op is Op.ARRAY:
  255. r = ', '.join(item.tostring(Precedence.TUPLE, language=language)
  256. for item in self.data)
  257. r = '[' + r + ']'
  258. precedence = Precedence.ATOM
  259. elif self.op is Op.TERMS:
  260. terms = []
  261. for term, coeff in sorted(self.data.items()):
  262. if coeff < 0:
  263. op = ' - '
  264. coeff = -coeff
  265. else:
  266. op = ' + '
  267. if coeff == 1:
  268. term = term.tostring(Precedence.SUM, language=language)
  269. elif term == as_number(1):
  270. term = str(coeff)
  271. else:
  272. term = f'{coeff} * ' + term.tostring(
  273. Precedence.PRODUCT, language=language)
  274. if terms:
  275. terms.append(op)
  276. elif op == ' - ':
  277. terms.append('-')
  278. terms.append(term)
  279. r = ''.join(terms) or '0'
  280. precedence = Precedence.SUM if terms else Precedence.ATOM
  281. elif self.op is Op.FACTORS:
  282. factors = []
  283. tail = []
  284. for base, exp in sorted(self.data.items()):
  285. op = ' * '
  286. if exp == 1:
  287. factor = base.tostring(Precedence.PRODUCT,
  288. language=language)
  289. elif language is Language.C:
  290. if exp in range(2, 10):
  291. factor = base.tostring(Precedence.PRODUCT,
  292. language=language)
  293. factor = ' * '.join([factor] * exp)
  294. elif exp in range(-10, 0):
  295. factor = base.tostring(Precedence.PRODUCT,
  296. language=language)
  297. tail += [factor] * -exp
  298. continue
  299. else:
  300. factor = base.tostring(Precedence.TUPLE,
  301. language=language)
  302. factor = f'pow({factor}, {exp})'
  303. else:
  304. factor = base.tostring(Precedence.POWER,
  305. language=language) + f' ** {exp}'
  306. if factors:
  307. factors.append(op)
  308. factors.append(factor)
  309. if tail:
  310. if not factors:
  311. factors += ['1']
  312. factors += ['/', '(', ' * '.join(tail), ')']
  313. r = ''.join(factors) or '1'
  314. precedence = Precedence.PRODUCT if factors else Precedence.ATOM
  315. elif self.op is Op.APPLY:
  316. name, args, kwargs = self.data
  317. if name is ArithOp.DIV and language is Language.C:
  318. numer, denom = [arg.tostring(Precedence.PRODUCT,
  319. language=language)
  320. for arg in args]
  321. r = f'{numer} / {denom}'
  322. precedence = Precedence.PRODUCT
  323. else:
  324. args = [arg.tostring(Precedence.TUPLE, language=language)
  325. for arg in args]
  326. args += [k + '=' + v.tostring(Precedence.NONE)
  327. for k, v in kwargs.items()]
  328. r = f'{name}({", ".join(args)})'
  329. precedence = Precedence.ATOM
  330. elif self.op is Op.INDEXING:
  331. name = self.data[0]
  332. args = [arg.tostring(Precedence.TUPLE, language=language)
  333. for arg in self.data[1:]]
  334. r = f'{name}[{", ".join(args)}]'
  335. precedence = Precedence.ATOM
  336. elif self.op is Op.CONCAT:
  337. args = [arg.tostring(Precedence.PRODUCT, language=language)
  338. for arg in self.data]
  339. r = " // ".join(args)
  340. precedence = Precedence.PRODUCT
  341. elif self.op is Op.TERNARY:
  342. cond, expr1, expr2 = [a.tostring(Precedence.TUPLE,
  343. language=language)
  344. for a in self.data]
  345. if language is Language.C:
  346. r = f'({cond}?{expr1}:{expr2})'
  347. elif language is Language.Python:
  348. r = f'({expr1} if {cond} else {expr2})'
  349. elif language is Language.Fortran:
  350. r = f'merge({expr1}, {expr2}, {cond})'
  351. else:
  352. raise NotImplementedError(
  353. f'tostring for {self.op} and {language}')
  354. precedence = Precedence.ATOM
  355. elif self.op is Op.REF:
  356. r = '&' + self.data.tostring(Precedence.UNARY, language=language)
  357. precedence = Precedence.UNARY
  358. elif self.op is Op.DEREF:
  359. r = '*' + self.data.tostring(Precedence.UNARY, language=language)
  360. precedence = Precedence.UNARY
  361. elif self.op is Op.RELATIONAL:
  362. rop, left, right = self.data
  363. precedence = (Precedence.EQ if rop in (RelOp.EQ, RelOp.NE)
  364. else Precedence.LT)
  365. left = left.tostring(precedence, language=language)
  366. right = right.tostring(precedence, language=language)
  367. rop = rop.tostring(language=language)
  368. r = f'{left} {rop} {right}'
  369. else:
  370. raise NotImplementedError(f'tostring for op {self.op}')
  371. if parent_precedence.value < precedence.value:
  372. # If parent precedence is higher than operand precedence,
  373. # operand will be enclosed in parenthesis.
  374. return '(' + r + ')'
  375. return r
  376. def __pos__(self):
  377. return self
  378. def __neg__(self):
  379. return self * -1
  380. def __add__(self, other):
  381. other = as_expr(other)
  382. if isinstance(other, Expr):
  383. if self.op is other.op:
  384. if self.op in (Op.INTEGER, Op.REAL):
  385. return as_number(
  386. self.data[0] + other.data[0],
  387. max(self.data[1], other.data[1]))
  388. if self.op is Op.COMPLEX:
  389. r1, i1 = self.data
  390. r2, i2 = other.data
  391. return as_complex(r1 + r2, i1 + i2)
  392. if self.op is Op.TERMS:
  393. r = Expr(self.op, dict(self.data))
  394. for k, v in other.data.items():
  395. _pairs_add(r.data, k, v)
  396. return normalize(r)
  397. if self.op is Op.COMPLEX and other.op in (Op.INTEGER, Op.REAL):
  398. return self + as_complex(other)
  399. elif self.op in (Op.INTEGER, Op.REAL) and other.op is Op.COMPLEX:
  400. return as_complex(self) + other
  401. elif self.op is Op.REAL and other.op is Op.INTEGER:
  402. return self + as_real(other, kind=self.data[1])
  403. elif self.op is Op.INTEGER and other.op is Op.REAL:
  404. return as_real(self, kind=other.data[1]) + other
  405. return as_terms(self) + as_terms(other)
  406. return NotImplemented
  407. def __radd__(self, other):
  408. if isinstance(other, number_types):
  409. return as_number(other) + self
  410. return NotImplemented
  411. def __sub__(self, other):
  412. return self + (-other)
  413. def __rsub__(self, other):
  414. if isinstance(other, number_types):
  415. return as_number(other) - self
  416. return NotImplemented
  417. def __mul__(self, other):
  418. other = as_expr(other)
  419. if isinstance(other, Expr):
  420. if self.op is other.op:
  421. if self.op in (Op.INTEGER, Op.REAL):
  422. return as_number(self.data[0] * other.data[0],
  423. max(self.data[1], other.data[1]))
  424. elif self.op is Op.COMPLEX:
  425. r1, i1 = self.data
  426. r2, i2 = other.data
  427. return as_complex(r1 * r2 - i1 * i2, r1 * i2 + r2 * i1)
  428. if self.op is Op.FACTORS:
  429. r = Expr(self.op, dict(self.data))
  430. for k, v in other.data.items():
  431. _pairs_add(r.data, k, v)
  432. return normalize(r)
  433. elif self.op is Op.TERMS:
  434. r = Expr(self.op, {})
  435. for t1, c1 in self.data.items():
  436. for t2, c2 in other.data.items():
  437. _pairs_add(r.data, t1 * t2, c1 * c2)
  438. return normalize(r)
  439. if self.op is Op.COMPLEX and other.op in (Op.INTEGER, Op.REAL):
  440. return self * as_complex(other)
  441. elif other.op is Op.COMPLEX and self.op in (Op.INTEGER, Op.REAL):
  442. return as_complex(self) * other
  443. elif self.op is Op.REAL and other.op is Op.INTEGER:
  444. return self * as_real(other, kind=self.data[1])
  445. elif self.op is Op.INTEGER and other.op is Op.REAL:
  446. return as_real(self, kind=other.data[1]) * other
  447. if self.op is Op.TERMS:
  448. return self * as_terms(other)
  449. elif other.op is Op.TERMS:
  450. return as_terms(self) * other
  451. return as_factors(self) * as_factors(other)
  452. return NotImplemented
  453. def __rmul__(self, other):
  454. if isinstance(other, number_types):
  455. return as_number(other) * self
  456. return NotImplemented
  457. def __pow__(self, other):
  458. other = as_expr(other)
  459. if isinstance(other, Expr):
  460. if other.op is Op.INTEGER:
  461. exponent = other.data[0]
  462. # TODO: other kind not used
  463. if exponent == 0:
  464. return as_number(1)
  465. if exponent == 1:
  466. return self
  467. if exponent > 0:
  468. if self.op is Op.FACTORS:
  469. r = Expr(self.op, {})
  470. for k, v in self.data.items():
  471. r.data[k] = v * exponent
  472. return normalize(r)
  473. return self * (self ** (exponent - 1))
  474. elif exponent != -1:
  475. return (self ** (-exponent)) ** -1
  476. return Expr(Op.FACTORS, {self: exponent})
  477. return as_apply(ArithOp.POW, self, other)
  478. return NotImplemented
  479. def __truediv__(self, other):
  480. other = as_expr(other)
  481. if isinstance(other, Expr):
  482. # Fortran / is different from Python /:
  483. # - `/` is a truncate operation for integer operands
  484. return normalize(as_apply(ArithOp.DIV, self, other))
  485. return NotImplemented
  486. def __rtruediv__(self, other):
  487. other = as_expr(other)
  488. if isinstance(other, Expr):
  489. return other / self
  490. return NotImplemented
  491. def __floordiv__(self, other):
  492. other = as_expr(other)
  493. if isinstance(other, Expr):
  494. # Fortran // is different from Python //:
  495. # - `//` is a concatenate operation for string operands
  496. return normalize(Expr(Op.CONCAT, (self, other)))
  497. return NotImplemented
  498. def __rfloordiv__(self, other):
  499. other = as_expr(other)
  500. if isinstance(other, Expr):
  501. return other // self
  502. return NotImplemented
  503. def __call__(self, *args, **kwargs):
  504. # In Fortran, parenthesis () are use for both function call as
  505. # well as indexing operations.
  506. #
  507. # TODO: implement a method for deciding when __call__ should
  508. # return an INDEXING expression.
  509. return as_apply(self, *map(as_expr, args),
  510. **{k: as_expr(v) for k, v in kwargs.items()})
  511. def __getitem__(self, index):
  512. # Provided to support C indexing operations that .pyf files
  513. # may contain.
  514. index = as_expr(index)
  515. if not isinstance(index, tuple):
  516. index = index,
  517. if len(index) > 1:
  518. ewarn(f'C-index should be a single expression but got `{index}`')
  519. return Expr(Op.INDEXING, (self,) + index)
  520. def substitute(self, symbols_map):
  521. """Recursively substitute symbols with values in symbols map.
  522. Symbols map is a dictionary of symbol-expression pairs.
  523. """
  524. if self.op is Op.SYMBOL:
  525. value = symbols_map.get(self)
  526. if value is None:
  527. return self
  528. m = re.match(r'\A(@__f2py_PARENTHESIS_(\w+)_\d+@)\Z', self.data)
  529. if m:
  530. # complement to fromstring method
  531. items, paren = m.groups()
  532. if paren in ['ROUNDDIV', 'SQUARE']:
  533. return as_array(value)
  534. assert paren == 'ROUND', (paren, value)
  535. return value
  536. if self.op in (Op.INTEGER, Op.REAL, Op.STRING):
  537. return self
  538. if self.op in (Op.ARRAY, Op.COMPLEX):
  539. return Expr(self.op, tuple(item.substitute(symbols_map)
  540. for item in self.data))
  541. if self.op is Op.CONCAT:
  542. return normalize(Expr(self.op, tuple(item.substitute(symbols_map)
  543. for item in self.data)))
  544. if self.op is Op.TERMS:
  545. r = None
  546. for term, coeff in self.data.items():
  547. if r is None:
  548. r = term.substitute(symbols_map) * coeff
  549. else:
  550. r += term.substitute(symbols_map) * coeff
  551. if r is None:
  552. ewarn('substitute: empty TERMS expression interpreted as'
  553. ' int-literal 0')
  554. return as_number(0)
  555. return r
  556. if self.op is Op.FACTORS:
  557. r = None
  558. for base, exponent in self.data.items():
  559. if r is None:
  560. r = base.substitute(symbols_map) ** exponent
  561. else:
  562. r *= base.substitute(symbols_map) ** exponent
  563. if r is None:
  564. ewarn('substitute: empty FACTORS expression interpreted'
  565. ' as int-literal 1')
  566. return as_number(1)
  567. return r
  568. if self.op is Op.APPLY:
  569. target, args, kwargs = self.data
  570. if isinstance(target, Expr):
  571. target = target.substitute(symbols_map)
  572. args = tuple(a.substitute(symbols_map) for a in args)
  573. kwargs = {k: v.substitute(symbols_map)
  574. for k, v in kwargs.items()}
  575. return normalize(Expr(self.op, (target, args, kwargs)))
  576. if self.op is Op.INDEXING:
  577. func = self.data[0]
  578. if isinstance(func, Expr):
  579. func = func.substitute(symbols_map)
  580. args = tuple(a.substitute(symbols_map) for a in self.data[1:])
  581. return normalize(Expr(self.op, (func,) + args))
  582. if self.op is Op.TERNARY:
  583. operands = tuple(a.substitute(symbols_map) for a in self.data)
  584. return normalize(Expr(self.op, operands))
  585. if self.op in (Op.REF, Op.DEREF):
  586. return normalize(Expr(self.op, self.data.substitute(symbols_map)))
  587. if self.op is Op.RELATIONAL:
  588. rop, left, right = self.data
  589. left = left.substitute(symbols_map)
  590. right = right.substitute(symbols_map)
  591. return normalize(Expr(self.op, (rop, left, right)))
  592. raise NotImplementedError(f'substitute method for {self.op}: {self!r}')
  593. def traverse(self, visit, *args, **kwargs):
  594. """Traverse expression tree with visit function.
  595. The visit function is applied to an expression with given args
  596. and kwargs.
  597. Traverse call returns an expression returned by visit when not
  598. None, otherwise return a new normalized expression with
  599. traverse-visit sub-expressions.
  600. """
  601. result = visit(self, *args, **kwargs)
  602. if result is not None:
  603. return result
  604. if self.op in (Op.INTEGER, Op.REAL, Op.STRING, Op.SYMBOL):
  605. return self
  606. elif self.op in (Op.COMPLEX, Op.ARRAY, Op.CONCAT, Op.TERNARY):
  607. return normalize(Expr(self.op, tuple(
  608. item.traverse(visit, *args, **kwargs)
  609. for item in self.data)))
  610. elif self.op in (Op.TERMS, Op.FACTORS):
  611. data = {}
  612. for k, v in self.data.items():
  613. k = k.traverse(visit, *args, **kwargs)
  614. v = (v.traverse(visit, *args, **kwargs)
  615. if isinstance(v, Expr) else v)
  616. if k in data:
  617. v = data[k] + v
  618. data[k] = v
  619. return normalize(Expr(self.op, data))
  620. elif self.op is Op.APPLY:
  621. obj = self.data[0]
  622. func = (obj.traverse(visit, *args, **kwargs)
  623. if isinstance(obj, Expr) else obj)
  624. operands = tuple(operand.traverse(visit, *args, **kwargs)
  625. for operand in self.data[1])
  626. kwoperands = {k: v.traverse(visit, *args, **kwargs)
  627. for k, v in self.data[2].items()}
  628. return normalize(Expr(self.op, (func, operands, kwoperands)))
  629. elif self.op is Op.INDEXING:
  630. obj = self.data[0]
  631. obj = (obj.traverse(visit, *args, **kwargs)
  632. if isinstance(obj, Expr) else obj)
  633. indices = tuple(index.traverse(visit, *args, **kwargs)
  634. for index in self.data[1:])
  635. return normalize(Expr(self.op, (obj,) + indices))
  636. elif self.op in (Op.REF, Op.DEREF):
  637. return normalize(Expr(self.op,
  638. self.data.traverse(visit, *args, **kwargs)))
  639. elif self.op is Op.RELATIONAL:
  640. rop, left, right = self.data
  641. left = left.traverse(visit, *args, **kwargs)
  642. right = right.traverse(visit, *args, **kwargs)
  643. return normalize(Expr(self.op, (rop, left, right)))
  644. raise NotImplementedError(f'traverse method for {self.op}')
  645. def contains(self, other):
  646. """Check if self contains other.
  647. """
  648. found = []
  649. def visit(expr, found=found):
  650. if found:
  651. return expr
  652. elif expr == other:
  653. found.append(1)
  654. return expr
  655. self.traverse(visit)
  656. return len(found) != 0
  657. def symbols(self):
  658. """Return a set of symbols contained in self.
  659. """
  660. found = set()
  661. def visit(expr, found=found):
  662. if expr.op is Op.SYMBOL:
  663. found.add(expr)
  664. self.traverse(visit)
  665. return found
  666. def polynomial_atoms(self):
  667. """Return a set of expressions used as atoms in polynomial self.
  668. """
  669. found = set()
  670. def visit(expr, found=found):
  671. if expr.op is Op.FACTORS:
  672. for b in expr.data:
  673. b.traverse(visit)
  674. return expr
  675. if expr.op in (Op.TERMS, Op.COMPLEX):
  676. return
  677. if expr.op is Op.APPLY and isinstance(expr.data[0], ArithOp):
  678. if expr.data[0] is ArithOp.POW:
  679. expr.data[1][0].traverse(visit)
  680. return expr
  681. return
  682. if expr.op in (Op.INTEGER, Op.REAL):
  683. return expr
  684. found.add(expr)
  685. if expr.op in (Op.INDEXING, Op.APPLY):
  686. return expr
  687. self.traverse(visit)
  688. return found
  689. def linear_solve(self, symbol):
  690. """Return a, b such that a * symbol + b == self.
  691. If self is not linear with respect to symbol, raise RuntimeError.
  692. """
  693. b = self.substitute({symbol: as_number(0)})
  694. ax = self - b
  695. a = ax.substitute({symbol: as_number(1)})
  696. zero, _ = as_numer_denom(a * symbol - ax)
  697. if zero != as_number(0):
  698. raise RuntimeError(f'not a {symbol}-linear equation:'
  699. f' {a} * {symbol} + {b} == {self}')
  700. return a, b
  701. def normalize(obj):
  702. """Normalize Expr and apply basic evaluation methods.
  703. """
  704. if not isinstance(obj, Expr):
  705. return obj
  706. if obj.op is Op.TERMS:
  707. d = {}
  708. for t, c in obj.data.items():
  709. if c == 0:
  710. continue
  711. if t.op is Op.COMPLEX and c != 1:
  712. t = t * c
  713. c = 1
  714. if t.op is Op.TERMS:
  715. for t1, c1 in t.data.items():
  716. _pairs_add(d, t1, c1 * c)
  717. else:
  718. _pairs_add(d, t, c)
  719. if len(d) == 0:
  720. # TODO: determine correct kind
  721. return as_number(0)
  722. elif len(d) == 1:
  723. (t, c), = d.items()
  724. if c == 1:
  725. return t
  726. return Expr(Op.TERMS, d)
  727. if obj.op is Op.FACTORS:
  728. coeff = 1
  729. d = {}
  730. for b, e in obj.data.items():
  731. if e == 0:
  732. continue
  733. if b.op is Op.TERMS and isinstance(e, integer_types) and e > 1:
  734. # expand integer powers of sums
  735. b = b * (b ** (e - 1))
  736. e = 1
  737. if b.op in (Op.INTEGER, Op.REAL):
  738. if e == 1:
  739. coeff *= b.data[0]
  740. elif e > 0:
  741. coeff *= b.data[0] ** e
  742. else:
  743. _pairs_add(d, b, e)
  744. elif b.op is Op.FACTORS:
  745. if e > 0 and isinstance(e, integer_types):
  746. for b1, e1 in b.data.items():
  747. _pairs_add(d, b1, e1 * e)
  748. else:
  749. _pairs_add(d, b, e)
  750. else:
  751. _pairs_add(d, b, e)
  752. if len(d) == 0 or coeff == 0:
  753. # TODO: determine correct kind
  754. assert isinstance(coeff, number_types)
  755. return as_number(coeff)
  756. elif len(d) == 1:
  757. (b, e), = d.items()
  758. if e == 1:
  759. t = b
  760. else:
  761. t = Expr(Op.FACTORS, d)
  762. if coeff == 1:
  763. return t
  764. return Expr(Op.TERMS, {t: coeff})
  765. elif coeff == 1:
  766. return Expr(Op.FACTORS, d)
  767. else:
  768. return Expr(Op.TERMS, {Expr(Op.FACTORS, d): coeff})
  769. if obj.op is Op.APPLY and obj.data[0] is ArithOp.DIV:
  770. dividend, divisor = obj.data[1]
  771. t1, c1 = as_term_coeff(dividend)
  772. t2, c2 = as_term_coeff(divisor)
  773. if isinstance(c1, integer_types) and isinstance(c2, integer_types):
  774. g = gcd(c1, c2)
  775. c1, c2 = c1 // g, c2 // g
  776. else:
  777. c1, c2 = c1 / c2, 1
  778. if t1.op is Op.APPLY and t1.data[0] is ArithOp.DIV:
  779. numer = t1.data[1][0] * c1
  780. denom = t1.data[1][1] * t2 * c2
  781. return as_apply(ArithOp.DIV, numer, denom)
  782. if t2.op is Op.APPLY and t2.data[0] is ArithOp.DIV:
  783. numer = t2.data[1][1] * t1 * c1
  784. denom = t2.data[1][0] * c2
  785. return as_apply(ArithOp.DIV, numer, denom)
  786. d = dict(as_factors(t1).data)
  787. for b, e in as_factors(t2).data.items():
  788. _pairs_add(d, b, -e)
  789. numer, denom = {}, {}
  790. for b, e in d.items():
  791. if e > 0:
  792. numer[b] = e
  793. else:
  794. denom[b] = -e
  795. numer = normalize(Expr(Op.FACTORS, numer)) * c1
  796. denom = normalize(Expr(Op.FACTORS, denom)) * c2
  797. if denom.op in (Op.INTEGER, Op.REAL) and denom.data[0] == 1:
  798. # TODO: denom kind not used
  799. return numer
  800. return as_apply(ArithOp.DIV, numer, denom)
  801. if obj.op is Op.CONCAT:
  802. lst = [obj.data[0]]
  803. for s in obj.data[1:]:
  804. last = lst[-1]
  805. if (
  806. last.op is Op.STRING
  807. and s.op is Op.STRING
  808. and last.data[0][0] in '"\''
  809. and s.data[0][0] == last.data[0][-1]
  810. ):
  811. new_last = as_string(last.data[0][:-1] + s.data[0][1:],
  812. max(last.data[1], s.data[1]))
  813. lst[-1] = new_last
  814. else:
  815. lst.append(s)
  816. if len(lst) == 1:
  817. return lst[0]
  818. return Expr(Op.CONCAT, tuple(lst))
  819. if obj.op is Op.TERNARY:
  820. cond, expr1, expr2 = map(normalize, obj.data)
  821. if cond.op is Op.INTEGER:
  822. return expr1 if cond.data[0] else expr2
  823. return Expr(Op.TERNARY, (cond, expr1, expr2))
  824. return obj
  825. def as_expr(obj):
  826. """Convert non-Expr objects to Expr objects.
  827. """
  828. if isinstance(obj, complex):
  829. return as_complex(obj.real, obj.imag)
  830. if isinstance(obj, number_types):
  831. return as_number(obj)
  832. if isinstance(obj, str):
  833. # STRING expression holds string with boundary quotes, hence
  834. # applying repr:
  835. return as_string(repr(obj))
  836. if isinstance(obj, tuple):
  837. return tuple(map(as_expr, obj))
  838. return obj
  839. def as_symbol(obj):
  840. """Return object as SYMBOL expression (variable or unparsed expression).
  841. """
  842. return Expr(Op.SYMBOL, obj)
  843. def as_number(obj, kind=4):
  844. """Return object as INTEGER or REAL constant.
  845. """
  846. if isinstance(obj, int):
  847. return Expr(Op.INTEGER, (obj, kind))
  848. if isinstance(obj, float):
  849. return Expr(Op.REAL, (obj, kind))
  850. if isinstance(obj, Expr):
  851. if obj.op in (Op.INTEGER, Op.REAL):
  852. return obj
  853. raise OpError(f'cannot convert {obj} to INTEGER or REAL constant')
  854. def as_integer(obj, kind=4):
  855. """Return object as INTEGER constant.
  856. """
  857. if isinstance(obj, int):
  858. return Expr(Op.INTEGER, (obj, kind))
  859. if isinstance(obj, Expr):
  860. if obj.op is Op.INTEGER:
  861. return obj
  862. raise OpError(f'cannot convert {obj} to INTEGER constant')
  863. def as_real(obj, kind=4):
  864. """Return object as REAL constant.
  865. """
  866. if isinstance(obj, int):
  867. return Expr(Op.REAL, (float(obj), kind))
  868. if isinstance(obj, float):
  869. return Expr(Op.REAL, (obj, kind))
  870. if isinstance(obj, Expr):
  871. if obj.op is Op.REAL:
  872. return obj
  873. elif obj.op is Op.INTEGER:
  874. return Expr(Op.REAL, (float(obj.data[0]), kind))
  875. raise OpError(f'cannot convert {obj} to REAL constant')
  876. def as_string(obj, kind=1):
  877. """Return object as STRING expression (string literal constant).
  878. """
  879. return Expr(Op.STRING, (obj, kind))
  880. def as_array(obj):
  881. """Return object as ARRAY expression (array constant).
  882. """
  883. if isinstance(obj, Expr):
  884. obj = obj,
  885. return Expr(Op.ARRAY, obj)
  886. def as_complex(real, imag=0):
  887. """Return object as COMPLEX expression (complex literal constant).
  888. """
  889. return Expr(Op.COMPLEX, (as_expr(real), as_expr(imag)))
  890. def as_apply(func, *args, **kwargs):
  891. """Return object as APPLY expression (function call, constructor, etc.)
  892. """
  893. return Expr(Op.APPLY,
  894. (func, tuple(map(as_expr, args)),
  895. {k: as_expr(v) for k, v in kwargs.items()}))
  896. def as_ternary(cond, expr1, expr2):
  897. """Return object as TERNARY expression (cond?expr1:expr2).
  898. """
  899. return Expr(Op.TERNARY, (cond, expr1, expr2))
  900. def as_ref(expr):
  901. """Return object as referencing expression.
  902. """
  903. return Expr(Op.REF, expr)
  904. def as_deref(expr):
  905. """Return object as dereferencing expression.
  906. """
  907. return Expr(Op.DEREF, expr)
  908. def as_eq(left, right):
  909. return Expr(Op.RELATIONAL, (RelOp.EQ, left, right))
  910. def as_ne(left, right):
  911. return Expr(Op.RELATIONAL, (RelOp.NE, left, right))
  912. def as_lt(left, right):
  913. return Expr(Op.RELATIONAL, (RelOp.LT, left, right))
  914. def as_le(left, right):
  915. return Expr(Op.RELATIONAL, (RelOp.LE, left, right))
  916. def as_gt(left, right):
  917. return Expr(Op.RELATIONAL, (RelOp.GT, left, right))
  918. def as_ge(left, right):
  919. return Expr(Op.RELATIONAL, (RelOp.GE, left, right))
  920. def as_terms(obj):
  921. """Return expression as TERMS expression.
  922. """
  923. if isinstance(obj, Expr):
  924. obj = normalize(obj)
  925. if obj.op is Op.TERMS:
  926. return obj
  927. if obj.op is Op.INTEGER:
  928. return Expr(Op.TERMS, {as_integer(1, obj.data[1]): obj.data[0]})
  929. if obj.op is Op.REAL:
  930. return Expr(Op.TERMS, {as_real(1, obj.data[1]): obj.data[0]})
  931. return Expr(Op.TERMS, {obj: 1})
  932. raise OpError(f'cannot convert {type(obj)} to terms Expr')
  933. def as_factors(obj):
  934. """Return expression as FACTORS expression.
  935. """
  936. if isinstance(obj, Expr):
  937. obj = normalize(obj)
  938. if obj.op is Op.FACTORS:
  939. return obj
  940. if obj.op is Op.TERMS:
  941. if len(obj.data) == 1:
  942. (term, coeff), = obj.data.items()
  943. if coeff == 1:
  944. return Expr(Op.FACTORS, {term: 1})
  945. return Expr(Op.FACTORS, {term: 1, Expr.number(coeff): 1})
  946. if (obj.op is Op.APPLY
  947. and obj.data[0] is ArithOp.DIV
  948. and not obj.data[2]):
  949. return Expr(Op.FACTORS, {obj.data[1][0]: 1, obj.data[1][1]: -1})
  950. return Expr(Op.FACTORS, {obj: 1})
  951. raise OpError(f'cannot convert {type(obj)} to terms Expr')
  952. def as_term_coeff(obj):
  953. """Return expression as term-coefficient pair.
  954. """
  955. if isinstance(obj, Expr):
  956. obj = normalize(obj)
  957. if obj.op is Op.INTEGER:
  958. return as_integer(1, obj.data[1]), obj.data[0]
  959. if obj.op is Op.REAL:
  960. return as_real(1, obj.data[1]), obj.data[0]
  961. if obj.op is Op.TERMS:
  962. if len(obj.data) == 1:
  963. (term, coeff), = obj.data.items()
  964. return term, coeff
  965. # TODO: find common divisor of coefficients
  966. if obj.op is Op.APPLY and obj.data[0] is ArithOp.DIV:
  967. t, c = as_term_coeff(obj.data[1][0])
  968. return as_apply(ArithOp.DIV, t, obj.data[1][1]), c
  969. return obj, 1
  970. raise OpError(f'cannot convert {type(obj)} to term and coeff')
  971. def as_numer_denom(obj):
  972. """Return expression as numer-denom pair.
  973. """
  974. if isinstance(obj, Expr):
  975. obj = normalize(obj)
  976. if obj.op in (Op.INTEGER, Op.REAL, Op.COMPLEX, Op.SYMBOL,
  977. Op.INDEXING, Op.TERNARY):
  978. return obj, as_number(1)
  979. elif obj.op is Op.APPLY:
  980. if obj.data[0] is ArithOp.DIV and not obj.data[2]:
  981. numers, denoms = map(as_numer_denom, obj.data[1])
  982. return numers[0] * denoms[1], numers[1] * denoms[0]
  983. return obj, as_number(1)
  984. elif obj.op is Op.TERMS:
  985. numers, denoms = [], []
  986. for term, coeff in obj.data.items():
  987. n, d = as_numer_denom(term)
  988. n = n * coeff
  989. numers.append(n)
  990. denoms.append(d)
  991. numer, denom = as_number(0), as_number(1)
  992. for i in range(len(numers)):
  993. n = numers[i]
  994. for j in range(len(numers)):
  995. if i != j:
  996. n *= denoms[j]
  997. numer += n
  998. denom *= denoms[i]
  999. if denom.op in (Op.INTEGER, Op.REAL) and denom.data[0] < 0:
  1000. numer, denom = -numer, -denom
  1001. return numer, denom
  1002. elif obj.op is Op.FACTORS:
  1003. numer, denom = as_number(1), as_number(1)
  1004. for b, e in obj.data.items():
  1005. bnumer, bdenom = as_numer_denom(b)
  1006. if e > 0:
  1007. numer *= bnumer ** e
  1008. denom *= bdenom ** e
  1009. elif e < 0:
  1010. numer *= bdenom ** (-e)
  1011. denom *= bnumer ** (-e)
  1012. return numer, denom
  1013. raise OpError(f'cannot convert {type(obj)} to numer and denom')
  1014. def _counter():
  1015. # Used internally to generate unique dummy symbols
  1016. counter = 0
  1017. while True:
  1018. counter += 1
  1019. yield counter
  1020. COUNTER = _counter()
  1021. def eliminate_quotes(s):
  1022. """Replace quoted substrings of input string.
  1023. Return a new string and a mapping of replacements.
  1024. """
  1025. d = {}
  1026. def repl(m):
  1027. kind, value = m.groups()[:2]
  1028. if kind:
  1029. # remove trailing underscore
  1030. kind = kind[:-1]
  1031. p = {"'": "SINGLE", '"': "DOUBLE"}[value[0]]
  1032. k = f'{kind}@__f2py_QUOTES_{p}_{COUNTER.__next__()}@'
  1033. d[k] = value
  1034. return k
  1035. new_s = re.sub(r'({kind}_|)({single_quoted}|{double_quoted})'.format(
  1036. kind=r'\w[\w\d_]*',
  1037. single_quoted=r"('([^'\\]|(\\.))*')",
  1038. double_quoted=r'("([^"\\]|(\\.))*")'),
  1039. repl, s)
  1040. assert '"' not in new_s
  1041. assert "'" not in new_s
  1042. return new_s, d
  1043. def insert_quotes(s, d):
  1044. """Inverse of eliminate_quotes.
  1045. """
  1046. for k, v in d.items():
  1047. kind = k[:k.find('@')]
  1048. if kind:
  1049. kind += '_'
  1050. s = s.replace(k, kind + v)
  1051. return s
  1052. def replace_parenthesis(s):
  1053. """Replace substrings of input that are enclosed in parenthesis.
  1054. Return a new string and a mapping of replacements.
  1055. """
  1056. # Find a parenthesis pair that appears first.
  1057. # Fortran deliminator are `(`, `)`, `[`, `]`, `(/', '/)`, `/`.
  1058. # We don't handle `/` deliminator because it is not a part of an
  1059. # expression.
  1060. left, right = None, None
  1061. mn_i = len(s)
  1062. for left_, right_ in (('(/', '/)'),
  1063. '()',
  1064. '{}', # to support C literal structs
  1065. '[]'):
  1066. i = s.find(left_)
  1067. if i == -1:
  1068. continue
  1069. if i < mn_i:
  1070. mn_i = i
  1071. left, right = left_, right_
  1072. if left is None:
  1073. return s, {}
  1074. i = mn_i
  1075. j = s.find(right, i)
  1076. if j == -1:
  1077. raise ValueError(f'Mismatch of {left + right} parenthesis in {s!r}')
  1078. while s.count(left, i + 1, j) != s.count(right, i + 1, j):
  1079. j = s.find(right, j + 1)
  1080. if j == -1:
  1081. raise ValueError(f'Mismatch of {left + right} parenthesis in {s!r}')
  1082. p = {'(': 'ROUND', '[': 'SQUARE', '{': 'CURLY', '(/': 'ROUNDDIV'}[left]
  1083. k = f'@__f2py_PARENTHESIS_{p}_{COUNTER.__next__()}@'
  1084. v = s[i + len(left):j]
  1085. r, d = replace_parenthesis(s[j + len(right):])
  1086. d[k] = v
  1087. return s[:i] + k + r, d
  1088. def _get_parenthesis_kind(s):
  1089. assert s.startswith('@__f2py_PARENTHESIS_'), s
  1090. return s.split('_')[4]
  1091. def unreplace_parenthesis(s, d):
  1092. """Inverse of replace_parenthesis.
  1093. """
  1094. for k, v in d.items():
  1095. p = _get_parenthesis_kind(k)
  1096. left = {'ROUND': '(', 'SQUARE': '[', 'CURLY': '{', 'ROUNDDIV': '(/'}[p]
  1097. right = {'ROUND': ')', 'SQUARE': ']', 'CURLY': '}', 'ROUNDDIV': '/)'}[p]
  1098. s = s.replace(k, left + v + right)
  1099. return s
  1100. def fromstring(s, language=Language.C):
  1101. """Create an expression from a string.
  1102. This is a "lazy" parser, that is, only arithmetic operations are
  1103. resolved, non-arithmetic operations are treated as symbols.
  1104. """
  1105. r = _FromStringWorker(language=language).parse(s)
  1106. if isinstance(r, Expr):
  1107. return r
  1108. raise ValueError(f'failed to parse `{s}` to Expr instance: got `{r}`')
  1109. class _Pair:
  1110. # Internal class to represent a pair of expressions
  1111. def __init__(self, left, right):
  1112. self.left = left
  1113. self.right = right
  1114. def substitute(self, symbols_map):
  1115. left, right = self.left, self.right
  1116. if isinstance(left, Expr):
  1117. left = left.substitute(symbols_map)
  1118. if isinstance(right, Expr):
  1119. right = right.substitute(symbols_map)
  1120. return _Pair(left, right)
  1121. def __repr__(self):
  1122. return f'{type(self).__name__}({self.left}, {self.right})'
  1123. class _FromStringWorker:
  1124. def __init__(self, language=Language.C):
  1125. self.original = None
  1126. self.quotes_map = None
  1127. self.language = language
  1128. def finalize_string(self, s):
  1129. return insert_quotes(s, self.quotes_map)
  1130. def parse(self, inp):
  1131. self.original = inp
  1132. unquoted, self.quotes_map = eliminate_quotes(inp)
  1133. return self.process(unquoted)
  1134. def process(self, s, context='expr'):
  1135. """Parse string within the given context.
  1136. The context may define the result in case of ambiguous
  1137. expressions. For instance, consider expressions `f(x, y)` and
  1138. `(x, y) + (a, b)` where `f` is a function and pair `(x, y)`
  1139. denotes complex number. Specifying context as "args" or
  1140. "expr", the subexpression `(x, y)` will be parse to an
  1141. argument list or to a complex number, respectively.
  1142. """
  1143. if isinstance(s, (list, tuple)):
  1144. return type(s)(self.process(s_, context) for s_ in s)
  1145. assert isinstance(s, str), (type(s), s)
  1146. # replace subexpressions in parenthesis with f2py @-names
  1147. r, raw_symbols_map = replace_parenthesis(s)
  1148. r = r.strip()
  1149. def restore(r):
  1150. # restores subexpressions marked with f2py @-names
  1151. if isinstance(r, (list, tuple)):
  1152. return type(r)(map(restore, r))
  1153. return unreplace_parenthesis(r, raw_symbols_map)
  1154. # comma-separated tuple
  1155. if ',' in r:
  1156. operands = restore(r.split(','))
  1157. if context == 'args':
  1158. return tuple(self.process(operands))
  1159. if context == 'expr':
  1160. if len(operands) == 2:
  1161. # complex number literal
  1162. return as_complex(*self.process(operands))
  1163. raise NotImplementedError(
  1164. f'parsing comma-separated list (context={context}): {r}')
  1165. # ternary operation
  1166. m = re.match(r'\A([^?]+)[?]([^:]+)[:](.+)\Z', r)
  1167. if m:
  1168. assert context == 'expr', context
  1169. oper, expr1, expr2 = restore(m.groups())
  1170. oper = self.process(oper)
  1171. expr1 = self.process(expr1)
  1172. expr2 = self.process(expr2)
  1173. return as_ternary(oper, expr1, expr2)
  1174. # relational expression
  1175. if self.language is Language.Fortran:
  1176. m = re.match(
  1177. r'\A(.+)\s*[.](eq|ne|lt|le|gt|ge)[.]\s*(.+)\Z', r, re.I)
  1178. else:
  1179. m = re.match(
  1180. r'\A(.+)\s*([=][=]|[!][=]|[<][=]|[<]|[>][=]|[>])\s*(.+)\Z', r)
  1181. if m:
  1182. left, rop, right = m.groups()
  1183. if self.language is Language.Fortran:
  1184. rop = '.' + rop + '.'
  1185. left, right = self.process(restore((left, right)))
  1186. rop = RelOp.fromstring(rop, language=self.language)
  1187. return Expr(Op.RELATIONAL, (rop, left, right))
  1188. # keyword argument
  1189. m = re.match(r'\A(\w[\w\d_]*)\s*[=](.*)\Z', r)
  1190. if m:
  1191. keyname, value = m.groups()
  1192. value = restore(value)
  1193. return _Pair(keyname, self.process(value))
  1194. # addition/subtraction operations
  1195. operands = re.split(r'((?<!\d[edED])[+-])', r)
  1196. if len(operands) > 1:
  1197. result = self.process(restore(operands[0] or '0'))
  1198. for op, operand in zip(operands[1::2], operands[2::2]):
  1199. operand = self.process(restore(operand))
  1200. op = op.strip()
  1201. if op == '+':
  1202. result += operand
  1203. else:
  1204. assert op == '-'
  1205. result -= operand
  1206. return result
  1207. # string concatenate operation
  1208. if self.language is Language.Fortran and '//' in r:
  1209. operands = restore(r.split('//'))
  1210. return Expr(Op.CONCAT,
  1211. tuple(self.process(operands)))
  1212. # multiplication/division operations
  1213. operands = re.split(r'(?<=[@\w\d_])\s*([*]|/)',
  1214. (r if self.language is Language.C
  1215. else r.replace('**', '@__f2py_DOUBLE_STAR@')))
  1216. if len(operands) > 1:
  1217. operands = restore(operands)
  1218. if self.language is not Language.C:
  1219. operands = [operand.replace('@__f2py_DOUBLE_STAR@', '**')
  1220. for operand in operands]
  1221. # Expression is an arithmetic product
  1222. result = self.process(operands[0])
  1223. for op, operand in zip(operands[1::2], operands[2::2]):
  1224. operand = self.process(operand)
  1225. op = op.strip()
  1226. if op == '*':
  1227. result *= operand
  1228. else:
  1229. assert op == '/'
  1230. result /= operand
  1231. return result
  1232. # referencing/dereferencing
  1233. if r.startswith(('*', '&')):
  1234. op = {'*': Op.DEREF, '&': Op.REF}[r[0]]
  1235. operand = self.process(restore(r[1:]))
  1236. return Expr(op, operand)
  1237. # exponentiation operations
  1238. if self.language is not Language.C and '**' in r:
  1239. operands = list(reversed(restore(r.split('**'))))
  1240. result = self.process(operands[0])
  1241. for operand in operands[1:]:
  1242. operand = self.process(operand)
  1243. result = operand ** result
  1244. return result
  1245. # int-literal-constant
  1246. m = re.match(r'\A({digit_string})({kind}|)\Z'.format(
  1247. digit_string=r'\d+',
  1248. kind=r'_(\d+|\w[\w\d_]*)'), r)
  1249. if m:
  1250. value, _, kind = m.groups()
  1251. if kind and kind.isdigit():
  1252. kind = int(kind)
  1253. return as_integer(int(value), kind or 4)
  1254. # real-literal-constant
  1255. m = re.match(r'\A({significant}({exponent}|)|\d+{exponent})({kind}|)\Z'
  1256. .format(
  1257. significant=r'[.]\d+|\d+[.]\d*',
  1258. exponent=r'[edED][+-]?\d+',
  1259. kind=r'_(\d+|\w[\w\d_]*)'), r)
  1260. if m:
  1261. value, _, _, kind = m.groups()
  1262. if kind and kind.isdigit():
  1263. kind = int(kind)
  1264. value = value.lower()
  1265. if 'd' in value:
  1266. return as_real(float(value.replace('d', 'e')), kind or 8)
  1267. return as_real(float(value), kind or 4)
  1268. # string-literal-constant with kind parameter specification
  1269. if r in self.quotes_map:
  1270. kind = r[:r.find('@')]
  1271. return as_string(self.quotes_map[r], kind or 1)
  1272. # array constructor or literal complex constant or
  1273. # parenthesized expression
  1274. if r in raw_symbols_map:
  1275. paren = _get_parenthesis_kind(r)
  1276. items = self.process(restore(raw_symbols_map[r]),
  1277. 'expr' if paren == 'ROUND' else 'args')
  1278. if paren == 'ROUND':
  1279. if isinstance(items, Expr):
  1280. return items
  1281. if paren in ['ROUNDDIV', 'SQUARE']:
  1282. # Expression is an array constructor
  1283. if isinstance(items, Expr):
  1284. items = (items,)
  1285. return as_array(items)
  1286. # function call/indexing
  1287. m = re.match(r'\A(.+)\s*(@__f2py_PARENTHESIS_(ROUND|SQUARE)_\d+@)\Z',
  1288. r)
  1289. if m:
  1290. target, args, paren = m.groups()
  1291. target = self.process(restore(target))
  1292. args = self.process(restore(args)[1:-1], 'args')
  1293. if not isinstance(args, tuple):
  1294. args = args,
  1295. if paren == 'ROUND':
  1296. kwargs = {a.left: a.right for a in args
  1297. if isinstance(a, _Pair)}
  1298. args = tuple(a for a in args if not isinstance(a, _Pair))
  1299. # Warning: this could also be Fortran indexing operation..
  1300. return as_apply(target, *args, **kwargs)
  1301. else:
  1302. # Expression is a C/Python indexing operation
  1303. # (e.g. used in .pyf files)
  1304. assert paren == 'SQUARE'
  1305. return target[args]
  1306. # Fortran standard conforming identifier
  1307. m = re.match(r'\A\w[\w\d_]*\Z', r)
  1308. if m:
  1309. return as_symbol(r)
  1310. # fall-back to symbol
  1311. r = self.finalize_string(restore(r))
  1312. ewarn(
  1313. f'fromstring: treating {r!r} as symbol (original={self.original})')
  1314. return as_symbol(r)