Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 195 files changed
  • 0 comments
  • 1 contributor
Apr 09, 2012
Tim Henderson added swork activate
Signed-off-by: Tim Henderson <tim.tadh@gmail.com>
4edc5bc
Tim Henderson Added a new test language "calc" 6b20457

Showing 195 changed files with 616 additions and 0 deletions. Show diff stats Hide diff stats

  1. +2 0  .swork.activate
  2. +104 0 test/calc/ast.py
  3. +39 0 test/calc/calc.grammar
  4. +1 0  test/calc/ex/0.ex
  5. +1 0  test/calc/ex/1.ex
  6. +1 0  test/calc/ex/10.ex
  7. +1 0  test/calc/ex/100.ex
  8. +1 0  test/calc/ex/101.ex
  9. +1 0  test/calc/ex/102.ex
  10. +1 0  test/calc/ex/103.ex
  11. +1 0  test/calc/ex/104.ex
  12. +1 0  test/calc/ex/105.ex
  13. +1 0  test/calc/ex/106.ex
  14. +1 0  test/calc/ex/107.ex
  15. +1 0  test/calc/ex/108.ex
  16. +1 0  test/calc/ex/109.ex
  17. +1 0  test/calc/ex/11.ex
  18. +1 0  test/calc/ex/110.ex
  19. +1 0  test/calc/ex/111.ex
  20. +1 0  test/calc/ex/112.ex
  21. +1 0  test/calc/ex/113.ex
  22. +1 0  test/calc/ex/114.ex
  23. +1 0  test/calc/ex/115.ex
  24. +1 0  test/calc/ex/116.ex
  25. +1 0  test/calc/ex/117.ex
  26. +1 0  test/calc/ex/118.ex
  27. +1 0  test/calc/ex/119.ex
  28. +1 0  test/calc/ex/12.ex
  29. +1 0  test/calc/ex/120.ex
  30. +1 0  test/calc/ex/121.ex
  31. +1 0  test/calc/ex/122.ex
  32. +1 0  test/calc/ex/123.ex
  33. +1 0  test/calc/ex/124.ex
  34. +1 0  test/calc/ex/125.ex
  35. +1 0  test/calc/ex/126.ex
  36. +1 0  test/calc/ex/127.ex
  37. +1 0  test/calc/ex/128.ex
  38. +1 0  test/calc/ex/129.ex
  39. +1 0  test/calc/ex/13.ex
  40. +1 0  test/calc/ex/130.ex
  41. +1 0  test/calc/ex/131.ex
  42. +1 0  test/calc/ex/132.ex
  43. +1 0  test/calc/ex/133.ex
  44. +1 0  test/calc/ex/134.ex
  45. +1 0  test/calc/ex/135.ex
  46. +1 0  test/calc/ex/136.ex
  47. +1 0  test/calc/ex/137.ex
  48. +1 0  test/calc/ex/138.ex
  49. +1 0  test/calc/ex/139.ex
  50. +1 0  test/calc/ex/14.ex
  51. +1 0  test/calc/ex/140.ex
  52. +1 0  test/calc/ex/141.ex
  53. +1 0  test/calc/ex/142.ex
  54. +1 0  test/calc/ex/143.ex
  55. +1 0  test/calc/ex/144.ex
  56. +1 0  test/calc/ex/145.ex
  57. +1 0  test/calc/ex/146.ex
  58. +1 0  test/calc/ex/147.ex
  59. +1 0  test/calc/ex/148.ex
  60. +1 0  test/calc/ex/149.ex
  61. +1 0  test/calc/ex/15.ex
  62. +1 0  test/calc/ex/150.ex
  63. +1 0  test/calc/ex/151.ex
  64. +1 0  test/calc/ex/152.ex
  65. +1 0  test/calc/ex/153.ex
  66. +1 0  test/calc/ex/154.ex
  67. +1 0  test/calc/ex/155.ex
  68. +1 0  test/calc/ex/156.ex
  69. +1 0  test/calc/ex/157.ex
  70. +1 0  test/calc/ex/158.ex
  71. +1 0  test/calc/ex/159.ex
  72. +1 0  test/calc/ex/16.ex
  73. +1 0  test/calc/ex/160.ex
  74. +1 0  test/calc/ex/161.ex
  75. +1 0  test/calc/ex/162.ex
  76. +1 0  test/calc/ex/163.ex
  77. +1 0  test/calc/ex/164.ex
  78. +1 0  test/calc/ex/165.ex
  79. +1 0  test/calc/ex/166.ex
  80. +1 0  test/calc/ex/167.ex
  81. +1 0  test/calc/ex/168.ex
  82. +1 0  test/calc/ex/169.ex
  83. +1 0  test/calc/ex/17.ex
  84. +1 0  test/calc/ex/170.ex
  85. +1 0  test/calc/ex/171.ex
  86. +1 0  test/calc/ex/172.ex
  87. +1 0  test/calc/ex/173.ex
  88. +1 0  test/calc/ex/174.ex
  89. +1 0  test/calc/ex/175.ex
  90. +1 0  test/calc/ex/176.ex
  91. +1 0  test/calc/ex/177.ex
  92. +1 0  test/calc/ex/178.ex
  93. +1 0  test/calc/ex/179.ex
  94. +1 0  test/calc/ex/18.ex
  95. +1 0  test/calc/ex/180.ex
  96. +1 0  test/calc/ex/181.ex
  97. +1 0  test/calc/ex/182.ex
  98. +1 0  test/calc/ex/183.ex
  99. +1 0  test/calc/ex/184.ex
  100. +1 0  test/calc/ex/185.ex
  101. +1 0  test/calc/ex/186.ex
  102. +1 0  test/calc/ex/187.ex
  103. +1 0  test/calc/ex/188.ex
  104. +1 0  test/calc/ex/189.ex
  105. +1 0  test/calc/ex/19.ex
  106. +1 0  test/calc/ex/2.ex
  107. +1 0  test/calc/ex/20.ex
  108. +1 0  test/calc/ex/21.ex
  109. +1 0  test/calc/ex/22.ex
  110. +1 0  test/calc/ex/23.ex
  111. +1 0  test/calc/ex/24.ex
  112. +1 0  test/calc/ex/25.ex
  113. +1 0  test/calc/ex/26.ex
  114. +1 0  test/calc/ex/27.ex
  115. +1 0  test/calc/ex/28.ex
  116. +1 0  test/calc/ex/29.ex
  117. +1 0  test/calc/ex/3.ex
  118. +1 0  test/calc/ex/30.ex
  119. +1 0  test/calc/ex/31.ex
  120. +1 0  test/calc/ex/32.ex
  121. +1 0  test/calc/ex/33.ex
  122. +1 0  test/calc/ex/34.ex
  123. +1 0  test/calc/ex/35.ex
  124. +1 0  test/calc/ex/36.ex
  125. +1 0  test/calc/ex/37.ex
  126. +1 0  test/calc/ex/38.ex
  127. +1 0  test/calc/ex/39.ex
  128. +1 0  test/calc/ex/4.ex
  129. +1 0  test/calc/ex/40.ex
  130. +1 0  test/calc/ex/41.ex
  131. +1 0  test/calc/ex/42.ex
  132. +1 0  test/calc/ex/43.ex
  133. +1 0  test/calc/ex/44.ex
  134. +1 0  test/calc/ex/45.ex
  135. +1 0  test/calc/ex/46.ex
  136. +1 0  test/calc/ex/47.ex
  137. +1 0  test/calc/ex/48.ex
  138. +1 0  test/calc/ex/49.ex
  139. +1 0  test/calc/ex/5.ex
  140. +1 0  test/calc/ex/50.ex
  141. +1 0  test/calc/ex/51.ex
  142. +1 0  test/calc/ex/52.ex
  143. +1 0  test/calc/ex/53.ex
  144. +1 0  test/calc/ex/54.ex
  145. +1 0  test/calc/ex/55.ex
  146. +1 0  test/calc/ex/56.ex
  147. +1 0  test/calc/ex/57.ex
  148. +1 0  test/calc/ex/58.ex
  149. +1 0  test/calc/ex/59.ex
  150. +1 0  test/calc/ex/6.ex
  151. +1 0  test/calc/ex/60.ex
  152. +1 0  test/calc/ex/61.ex
  153. +1 0  test/calc/ex/62.ex
  154. +1 0  test/calc/ex/63.ex
  155. +1 0  test/calc/ex/64.ex
  156. +1 0  test/calc/ex/65.ex
  157. +1 0  test/calc/ex/66.ex
  158. +1 0  test/calc/ex/67.ex
  159. +1 0  test/calc/ex/68.ex
  160. +1 0  test/calc/ex/69.ex
  161. +1 0  test/calc/ex/7.ex
  162. +1 0  test/calc/ex/70.ex
  163. +1 0  test/calc/ex/71.ex
  164. +1 0  test/calc/ex/72.ex
  165. +1 0  test/calc/ex/73.ex
  166. +1 0  test/calc/ex/74.ex
  167. +1 0  test/calc/ex/75.ex
  168. +1 0  test/calc/ex/76.ex
  169. +1 0  test/calc/ex/77.ex
  170. +1 0  test/calc/ex/78.ex
  171. +1 0  test/calc/ex/79.ex
  172. +1 0  test/calc/ex/8.ex
  173. +1 0  test/calc/ex/80.ex
  174. +1 0  test/calc/ex/81.ex
  175. +1 0  test/calc/ex/82.ex
  176. +1 0  test/calc/ex/83.ex
  177. +1 0  test/calc/ex/84.ex
  178. +1 0  test/calc/ex/85.ex
  179. +1 0  test/calc/ex/86.ex
  180. +1 0  test/calc/ex/87.ex
  181. +1 0  test/calc/ex/88.ex
  182. +1 0  test/calc/ex/89.ex
  183. +1 0  test/calc/ex/9.ex
  184. +1 0  test/calc/ex/90.ex
  185. +1 0  test/calc/ex/91.ex
  186. +1 0  test/calc/ex/92.ex
  187. +1 0  test/calc/ex/93.ex
  188. +1 0  test/calc/ex/94.ex
  189. +1 0  test/calc/ex/95.ex
  190. +1 0  test/calc/ex/96.ex
  191. +1 0  test/calc/ex/97.ex
  192. +1 0  test/calc/ex/98.ex
  193. +1 0  test/calc/ex/99.ex
  194. +100 0 test/calc/lexer.py
  195. +181 0 test/calc/parser.py
2  .swork.activate
... ... @@ -0,0 +1,2 @@
  1 +source env/bin/activate
  2 +
104 test/calc/ast.py
... ... @@ -0,0 +1,104 @@
  1 +#!/usr/bin/env python
  2 +# -*- coding: utf-8 -*-
  3 +#Author: Tim Henderson
  4 +#Email: tim.tadh@hackthology.com
  5 +#For licensing see the LICENSE file in the top level directory.
  6 +
  7 +import collections
  8 +
  9 +def build_tree(gen):
  10 + stack = list()
  11 + root = None
  12 + for children, sym in gen:
  13 + if not sym.value: node = Node(sym.sym)
  14 + else: node = Node(sym.value)
  15 + if not root: root = node
  16 +
  17 + if stack:
  18 + stack[-1]['node'].addkid(node)
  19 + stack[-1]['children'] -= 1
  20 + if stack[-1]['children'] <= 0:
  21 + stack.pop()
  22 + if children:
  23 + stack.append({'node':node, 'children':children})
  24 + return root
  25 +
  26 +class Node(object):
  27 +
  28 + def __init__(self, label, children=None):
  29 + self.label = label
  30 + self.children = children if children is not None else list()
  31 +
  32 + def addkid(self, node, before=False):
  33 + if before: self.children.insert(0, node)
  34 + else: self.children.append(node)
  35 + return self
  36 +
  37 + def get(self, label):
  38 + if self.label == label: return self
  39 + for c in self.children:
  40 + if label in c: return c.get(label)
  41 +
  42 + def iter(self):
  43 + queue = collections.deque()
  44 + queue.append(self)
  45 + while len(queue) > 0:
  46 + n = queue.popleft()
  47 + for c in n.children: queue.append(c)
  48 + yield n
  49 +
  50 + def __contains__(self, b):
  51 + if isinstance(b, str) and self.label == b: return 1
  52 + elif not isinstance(b, str) and self.label == b.label: return 1
  53 + elif (isinstance(b, str) and self.label != b) or self.label != b.label:
  54 + return sum(b in c for c in self.children)
  55 + raise TypeError, "Object %s is not of type str or Node" % repr(b)
  56 +
  57 + def __eq__(self, b):
  58 + if b is None: return False
  59 + if not isinstance(b, Node):
  60 + raise TypeError, "Must compare against type Node"
  61 + return self.label == b.label
  62 +
  63 + def __ne__(self, b):
  64 + return not self.__eq__(b)
  65 +
  66 + def __repr__(self):
  67 + return super(Node, self).__repr__()[:-1] + " %s>" % str(self.label)
  68 +
  69 + def __str__(self):
  70 + def string(s):
  71 + if isinstance(s, Node): return str(s)
  72 + return '0:%s' % str(s)
  73 + s = "%d:%s" % (len(self.children), str(self.label))
  74 + s = '\n'.join([s]+[string(c) for c in self.children])
  75 + return s
  76 +
  77 + def dotty(self):
  78 + def string(s):
  79 + if isinstance(s, Node): return str(s.label)
  80 + return str(s)
  81 + node = '%(name)s [shape=rect, label="%(label)s"];'
  82 + leaf = '%(name)s [shape=rect, label="%(label)s" style="filled" fillcolor="#dddddd"];'
  83 + edge = '%s -> %s;'
  84 + nodes = list()
  85 + edges = list()
  86 +
  87 + i = 0
  88 + queue = collections.deque()
  89 + queue.append((i, self))
  90 + i += 1
  91 + while len(queue) > 0:
  92 + c, n = queue.popleft()
  93 + name = 'n%d' % c
  94 + label = string(n)
  95 + if not hasattr(n, 'children'): nodes.append(leaf % locals())
  96 + elif not n.children: nodes.append(leaf % locals())
  97 + else: nodes.append(node % locals())
  98 + if not hasattr(n, 'children'): continue
  99 + for c in n.children:
  100 + edges.append(edge % (name, ('n%d' % i)))
  101 + queue.append((i, c))
  102 + i += 1
  103 + return 'digraph G {\n' + '\n'.join(nodes) + '\n' + '\n'.join(edges) + '\n}\n'
  104 +
39 test/calc/calc.grammar
... ... @@ -0,0 +1,39 @@
  1 +/* A grammar for the simple matrix calculator.
  2 + *
  3 + */
  4 +
  5 +Expr -> Expr PLUS Term
  6 + | Expr DASH Term
  7 + | Term
  8 + ;
  9 +Term -> Term STAR Exp
  10 + | Term SLASH Exp
  11 + | Exp
  12 + ;
  13 +Exp -> Exp EXP Unary
  14 + | Unary
  15 + ;
  16 +Unary -> DASH PostUnary
  17 + | PostUnary
  18 + ;
  19 +PostUnary -> Factor T
  20 + | Factor
  21 + ;
  22 +Factor -> Value
  23 + | LPAREN Expr RPAREN
  24 + ;
  25 +Value -> Atom
  26 + | Log
  27 + | LSQUARE Matrix RSQUARE
  28 + | LANGLE Vector RANGLE
  29 + ;
  30 +Atom -> NUMBER
  31 + ;
  32 +Log -> LOG LPAREN Expr COMMA Expr RPAREN
  33 + ;
  34 +Matrix -> Matrix SEMI Vector
  35 + | Vector
  36 + ;
  37 +Vector -> Vector COMMA Expr
  38 + | Expr
  39 + ;
1  test/calc/ex/0.ex
... ... @@ -0,0 +1 @@
  1 +12
1  test/calc/ex/1.ex
... ... @@ -0,0 +1 @@
  1 +<1,2,3,4,5>
1  test/calc/ex/10.ex
... ... @@ -0,0 +1 @@
  1 +-2
1  test/calc/ex/100.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>+<45-13,(5*6/2-3)/3,6^2.2>+<1.213, 22.34, 234.3>
1  test/calc/ex/101.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>+<45-13,(5*6/2-3)/3,6^2.2>-<1.213, 22.34, 234.3>
1  test/calc/ex/102.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>+<45-13,(5*6/2-3)/3,6^2.2>*<1.213, 22.34, 234.3>
1  test/calc/ex/103.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>+<45-13,(5*6/2-3)/3,6^2.2>/<1.213, 22.34, 234.3>
1  test/calc/ex/104.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>-<45-13,(5*6/2-3)/3,6^2.2>+<1.213, 22.34, 234.3>
1  test/calc/ex/105.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>-<45-13,(5*6/2-3)/3,6^2.2>-<1.213, 22.34, 234.3>
1  test/calc/ex/106.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>-<45-13,(5*6/2-3)/3,6^2.2>*<1.213, 22.34, 234.3>
1  test/calc/ex/107.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>-<45-13,(5*6/2-3)/3,6^2.2>/<1.213, 22.34, 234.3>
1  test/calc/ex/108.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>*<45-13,(5*6/2-3)/3,6^2.2>+<1.213, 22.34, 234.3>
1  test/calc/ex/109.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>*<45-13,(5*6/2-3)/3,6^2.2>-<1.213, 22.34, 234.3>
1  test/calc/ex/11.ex
... ... @@ -0,0 +1 @@
  1 +-(-2)
1  test/calc/ex/110.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>*<45-13,(5*6/2-3)/3,6^2.2>*<1.213, 22.34, 234.3>
1  test/calc/ex/111.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>*<45-13,(5*6/2-3)/3,6^2.2>/<1.213, 22.34, 234.3>
1  test/calc/ex/112.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>/<45-13,(5*6/2-3)/3,6^2.2>+<1.213, 22.34, 234.3>
1  test/calc/ex/113.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>/<45-13,(5*6/2-3)/3,6^2.2>-<1.213, 22.34, 234.3>
1  test/calc/ex/114.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>/<45-13,(5*6/2-3)/3,6^2.2>*<1.213, 22.34, 234.3>
1  test/calc/ex/115.ex
... ... @@ -0,0 +1 @@
  1 +<1.4+4,(67-2)/4,12.3+3>/<45-13,(5*6/2-3)/3,6^2.2>/<1.213, 22.34, 234.3>
1  test/calc/ex/116.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>+<45-13,(5*6/2-3)/3,6^2.2>)+<1.213, 22.34, 234.3>
1  test/calc/ex/117.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>+<45-13,(5*6/2-3)/3,6^2.2>)-<1.213, 22.34, 234.3>
1  test/calc/ex/118.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>+<45-13,(5*6/2-3)/3,6^2.2>)*<1.213, 22.34, 234.3>
1  test/calc/ex/119.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>+<45-13,(5*6/2-3)/3,6^2.2>)/<1.213, 22.34, 234.3>
1  test/calc/ex/12.ex
... ... @@ -0,0 +1 @@
  1 +2+3+4
1  test/calc/ex/120.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>-<45-13,(5*6/2-3)/3,6^2.2>)+<1.213, 22.34, 234.3>
1  test/calc/ex/121.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>-<45-13,(5*6/2-3)/3,6^2.2>)-<1.213, 22.34, 234.3>
1  test/calc/ex/122.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>-<45-13,(5*6/2-3)/3,6^2.2>)*<1.213, 22.34, 234.3>
1  test/calc/ex/123.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>-<45-13,(5*6/2-3)/3,6^2.2>)/<1.213, 22.34, 234.3>
1  test/calc/ex/124.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>*<45-13,(5*6/2-3)/3,6^2.2>)+<1.213, 22.34, 234.3>
1  test/calc/ex/125.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>*<45-13,(5*6/2-3)/3,6^2.2>)-<1.213, 22.34, 234.3>
1  test/calc/ex/126.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>*<45-13,(5*6/2-3)/3,6^2.2>)*<1.213, 22.34, 234.3>
1  test/calc/ex/127.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>*<45-13,(5*6/2-3)/3,6^2.2>)/<1.213, 22.34, 234.3>
1  test/calc/ex/128.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>/<45-13,(5*6/2-3)/3,6^2.2>)+<1.213, 22.34, 234.3>
1  test/calc/ex/129.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>/<45-13,(5*6/2-3)/3,6^2.2>)-<1.213, 22.34, 234.3>
1  test/calc/ex/13.ex
... ... @@ -0,0 +1 @@
  1 +2+3-4
1  test/calc/ex/130.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>/<45-13,(5*6/2-3)/3,6^2.2>)*<1.213, 22.34, 234.3>
1  test/calc/ex/131.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>/<45-13,(5*6/2-3)/3,6^2.2>)/<1.213, 22.34, 234.3>
1  test/calc/ex/132.ex
... ... @@ -0,0 +1 @@
  1 +-[4,5,6]
1  test/calc/ex/133.ex
... ... @@ -0,0 +1 @@
  1 +-(-[4,5,6])
1  test/calc/ex/134.ex
... ... @@ -0,0 +1 @@
  1 +<4,5,6>T
1  test/calc/ex/135.ex
... ... @@ -0,0 +1 @@
  1 +[4,5,6]T
1  test/calc/ex/136.ex
... ... @@ -0,0 +1 @@
  1 +[1,2,3]+[4,5,6]
1  test/calc/ex/137.ex
... ... @@ -0,0 +1 @@
  1 +[4,5,6]-[1,2,3]
1  test/calc/ex/138.ex
... ... @@ -0,0 +1 @@
  1 +[1,2,3]*[4,5,6]T
1  test/calc/ex/139.ex
... ... @@ -0,0 +1 @@
  1 +[4,5,6]/[1,2,3]
1  test/calc/ex/14.ex
... ... @@ -0,0 +1 @@
  1 +2+3/4
1  test/calc/ex/140.ex
... ... @@ -0,0 +1 @@
  1 +2+[4,5,6]
1  test/calc/ex/141.ex
... ... @@ -0,0 +1 @@
  1 +2-[1,2,3]
1  test/calc/ex/142.ex
... ... @@ -0,0 +1 @@
  1 +2*[4,5,6]
1  test/calc/ex/143.ex
... ... @@ -0,0 +1 @@
  1 +2/[1,2,3]
1  test/calc/ex/144.ex
... ... @@ -0,0 +1 @@
  1 +[4,5,6]+2
1  test/calc/ex/145.ex
... ... @@ -0,0 +1 @@
  1 +[1,2,3]-2
1  test/calc/ex/146.ex
... ... @@ -0,0 +1 @@
  1 +[4,5,6]*2
1  test/calc/ex/147.ex
... ... @@ -0,0 +1 @@
  1 +[1,2,3]/2
1  test/calc/ex/148.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]+[45-13,(5*6/2-3)/3,6^2.2]+[1.213, 22.34, 234.3]
1  test/calc/ex/149.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]+[45-13,(5*6/2-3)/3,6^2.2]-[1.213, 22.34, 234.3]
1  test/calc/ex/15.ex
... ... @@ -0,0 +1 @@
  1 +2+3*4
1  test/calc/ex/150.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]+[45-13,(5*6/2-3)/3,6^2.2]*[1.213, 22.34, 234.3]T
1  test/calc/ex/151.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]+[45-13,(5*6/2-3)/3,6^2.2]/[1.213, 22.34, 234.3]
1  test/calc/ex/152.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]-[45-13,(5*6/2-3)/3,6^2.2]+[1.213, 22.34, 234.3]
1  test/calc/ex/153.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]-[45-13,(5*6/2-3)/3,6^2.2]-[1.213, 22.34, 234.3]
1  test/calc/ex/154.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]-[45-13,(5*6/2-3)/3,6^2.2]*[1.213, 22.34, 234.3]T
1  test/calc/ex/155.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]-[45-13,(5*6/2-3)/3,6^2.2]/[1.213, 22.34, 234.3]
1  test/calc/ex/156.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]*[45-13,(5*6/2-3)/3,6^2.2]T+[1.213, 22.34, 234.3]
1  test/calc/ex/157.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]*[45-13,(5*6/2-3)/3,6^2.2]T-[1.213, 22.34, 234.3]
1  test/calc/ex/158.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]*[45-13,(5*6/2-3)/3,6^2.2]T*[1.213, 22.34, 234.3]
1  test/calc/ex/159.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]*[45-13,(5*6/2-3)/3,6^2.2]T/[1.213, 22.34, 234.3]
1  test/calc/ex/16.ex
... ... @@ -0,0 +1 @@
  1 +2+3^4
1  test/calc/ex/160.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]/[45-13,(5*6/2-3)/3,6^2.2]+[1.213, 22.34, 234.3]
1  test/calc/ex/161.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]/[45-13,(5*6/2-3)/3,6^2.2]-[1.213, 22.34, 234.3]
1  test/calc/ex/162.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]/[45-13,(5*6/2-3)/3,6^2.2]*[1.213, 22.34, 234.3]T
1  test/calc/ex/163.ex
... ... @@ -0,0 +1 @@
  1 +[1.4+4,(67-2)/4,12.3+3]/[45-13,(5*6/2-3)/3,6^2.2]/[1.213, 22.34, 234.3]
1  test/calc/ex/164.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]+[45-13,(5*6/2-3)/3,6^2.2])+[1.213, 22.34, 234.3]
1  test/calc/ex/165.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]+[45-13,(5*6/2-3)/3,6^2.2])-[1.213, 22.34, 234.3]
1  test/calc/ex/166.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]+[45-13,(5*6/2-3)/3,6^2.2])*[1.213, 22.34, 234.3]T
1  test/calc/ex/167.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]+[45-13,(5*6/2-3)/3,6^2.2])/[1.213, 22.34, 234.3]
1  test/calc/ex/168.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]-[45-13,(5*6/2-3)/3,6^2.2])+[1.213, 22.34, 234.3]
1  test/calc/ex/169.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]-[45-13,(5*6/2-3)/3,6^2.2])-[1.213, 22.34, 234.3]
1  test/calc/ex/17.ex
... ... @@ -0,0 +1 @@
  1 +2+log(3, 4)
1  test/calc/ex/170.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]-[45-13,(5*6/2-3)/3,6^2.2])*[1.213, 22.34, 234.3]T
1  test/calc/ex/171.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]-[45-13,(5*6/2-3)/3,6^2.2])/[1.213, 22.34, 234.3]
1  test/calc/ex/172.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]*[45-13,(5*6/2-3)/3,6^2.2]T)+[1.213, 22.34, 234.3]
1  test/calc/ex/173.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]*[45-13,(5*6/2-3)/3,6^2.2]T)-[1.213, 22.34, 234.3]
1  test/calc/ex/174.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]*[45-13,(5*6/2-3)/3,6^2.2]T)*[1.213, 22.34, 234.3]T
1  test/calc/ex/175.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]*[45-13,(5*6/2-3)/3,6^2.2]T)/[1.213, 22.34, 234.3]
1  test/calc/ex/176.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]/[45-13,(5*6/2-3)/3,6^2.2])+[1.213, 22.34, 234.3]
1  test/calc/ex/177.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]/[45-13,(5*6/2-3)/3,6^2.2])-[1.213, 22.34, 234.3]
1  test/calc/ex/178.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]/[45-13,(5*6/2-3)/3,6^2.2])*[1.213, 22.34, 234.3]T
1  test/calc/ex/179.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]/[45-13,(5*6/2-3)/3,6^2.2])/[1.213, 22.34, 234.3]
1  test/calc/ex/18.ex
... ... @@ -0,0 +1 @@
  1 +(2+3)+4
1  test/calc/ex/180.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]T*[45-13,(5*6/2-3)/3,6^2.2])+[1.213, 22.34, 234.3]T
1  test/calc/ex/181.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]T*[45-13,(5*6/2-3)/3,6^2.2])-[1.213, 22.34, 234.3]T
1  test/calc/ex/182.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]T*[45-13,(5*6/2-3)/3,6^2.2])*[1.213, 22.34, 234.3]T
1  test/calc/ex/183.ex
... ... @@ -0,0 +1 @@
  1 +([1.4+4,(67-2)/4,12.3+3]T*[45-13,(5*6/2-3)/3,6^2.2])/[1.213, 22.34, 234.3]T
1  test/calc/ex/184.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>T*<45-13,(5*6/2-3)/3,6^2.2>)+<1.213, 22.34, 234.3>T
1  test/calc/ex/185.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>T*<45-13,(5*6/2-3)/3,6^2.2>)-<1.213, 22.34, 234.3>T
1  test/calc/ex/186.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>T*<45-13,(5*6/2-3)/3,6^2.2>)*<1.213, 22.34, 234.3>T
1  test/calc/ex/187.ex
... ... @@ -0,0 +1 @@
  1 +(<1.4+4,(67-2)/4,12.3+3>T*<45-13,(5*6/2-3)/3,6^2.2>)/<1.213, 22.34, 234.3>T
1  test/calc/ex/188.ex
... ... @@ -0,0 +1 @@
  1 +(5 + log((-<1,2>*[1,2;3,4]*-[1,2]T)^2, 3)-3)/2
1  test/calc/ex/189.ex
... ... @@ -0,0 +1 @@
  1 +[1+3, 5*2, 6-1; 2^3, log(100, 10), 15/3]
1  test/calc/ex/19.ex
... ... @@ -0,0 +1 @@
  1 +(2+3)-4
1  test/calc/ex/2.ex
... ... @@ -0,0 +1 @@
  1 +[1,2;3,4;5,6]
1  test/calc/ex/20.ex
... ... @@ -0,0 +1 @@
  1 +(2+3)/4
1  test/calc/ex/21.ex
... ... @@ -0,0 +1 @@
  1 +(2+3)*4
1  test/calc/ex/22.ex
... ... @@ -0,0 +1 @@
  1 +(2+3)^4
1  test/calc/ex/23.ex
... ... @@ -0,0 +1 @@
  1 +log(2,3) + 4
1  test/calc/ex/24.ex
... ... @@ -0,0 +1 @@
  1 +2-3+4
1  test/calc/ex/25.ex
... ... @@ -0,0 +1 @@
  1 +2-3-4
1  test/calc/ex/26.ex
... ... @@ -0,0 +1 @@
  1 +2-3/4
1  test/calc/ex/27.ex
... ... @@ -0,0 +1 @@
  1 +2-3*4
1  test/calc/ex/28.ex
... ... @@ -0,0 +1 @@
  1 +2-3^4
1  test/calc/ex/29.ex
... ... @@ -0,0 +1 @@
  1 +2-log(3, 4)
1  test/calc/ex/3.ex
... ... @@ -0,0 +1 @@
  1 +1+2
1  test/calc/ex/30.ex
... ... @@ -0,0 +1 @@
  1 +(2-3)+4
1  test/calc/ex/31.ex
... ... @@ -0,0 +1 @@
  1 +(2-3)-4
1  test/calc/ex/32.ex
... ... @@ -0,0 +1 @@
  1 +(2-3)/4
1  test/calc/ex/33.ex
... ... @@ -0,0 +1 @@
  1 +(2-3)*4
1  test/calc/ex/34.ex
... ... @@ -0,0 +1 @@
  1 +(2-3)^4
1  test/calc/ex/35.ex
... ... @@ -0,0 +1 @@
  1 +log(2,3) - 4
1  test/calc/ex/36.ex
... ... @@ -0,0 +1 @@
  1 +2*3+4
1  test/calc/ex/37.ex
... ... @@ -0,0 +1 @@
  1 +2*3-4
1  test/calc/ex/38.ex
... ... @@ -0,0 +1 @@
  1 +2*3/4
1  test/calc/ex/39.ex
... ... @@ -0,0 +1 @@
  1 +2*3*4
1  test/calc/ex/4.ex
... ... @@ -0,0 +1 @@
  1 +1-2
1  test/calc/ex/40.ex
... ... @@ -0,0 +1 @@
  1 +2*3^4
1  test/calc/ex/41.ex
... ... @@ -0,0 +1 @@
  1 +2*log(3, 4)
1  test/calc/ex/42.ex
... ... @@ -0,0 +1 @@
  1 +(2*3)+4
1  test/calc/ex/43.ex
... ... @@ -0,0 +1 @@
  1 +(2*3)-4
1  test/calc/ex/44.ex
... ... @@ -0,0 +1 @@
  1 +(2*3)/4
1  test/calc/ex/45.ex
... ... @@ -0,0 +1 @@
  1 +(2*3)*4
1  test/calc/ex/46.ex
... ... @@ -0,0 +1 @@
  1 +(2*3)^4
1  test/calc/ex/47.ex
... ... @@ -0,0 +1 @@
  1 +log(2,3) * 4
1  test/calc/ex/48.ex
... ... @@ -0,0 +1 @@
  1 +2/3+4
1  test/calc/ex/49.ex
... ... @@ -0,0 +1 @@
  1 +2/3-4
1  test/calc/ex/5.ex
... ... @@ -0,0 +1 @@
  1 +1*2
1  test/calc/ex/50.ex
... ... @@ -0,0 +1 @@
  1 +2/3/4
1  test/calc/ex/51.ex
... ... @@ -0,0 +1 @@
  1 +2/3*4
1  test/calc/ex/52.ex
... ... @@ -0,0 +1 @@
  1 +2/3^4
1  test/calc/ex/53.ex
... ... @@ -0,0 +1 @@
  1 +2/log(3, 4)
1  test/calc/ex/54.ex
... ... @@ -0,0 +1 @@
  1 +(2/3)+4
1  test/calc/ex/55.ex
... ... @@ -0,0 +1 @@
  1 +(2/3)-4
1  test/calc/ex/56.ex
... ... @@ -0,0 +1 @@
  1 +(2/3)/4
1  test/calc/ex/57.ex
... ... @@ -0,0 +1 @@
  1 +(2/3)*4
1  test/calc/ex/58.ex
... ... @@ -0,0 +1 @@
  1 +(2/3)^4
1  test/calc/ex/59.ex
... ... @@ -0,0 +1 @@
  1 +log(2,3) / 4
1  test/calc/ex/6.ex
... ... @@ -0,0 +1 @@
  1 +1/2
1  test/calc/ex/60.ex
... ... @@ -0,0 +1 @@
  1 +2^3+4
1  test/calc/ex/61.ex
... ... @@ -0,0 +1 @@
  1 +2^3-4
1  test/calc/ex/62.ex
... ... @@ -0,0 +1 @@
  1 +2^3/4
1  test/calc/ex/63.ex
... ... @@ -0,0 +1 @@
  1 +2^3*4
1  test/calc/ex/64.ex
... ... @@ -0,0 +1 @@
  1 +2^3^4
1  test/calc/ex/65.ex
... ... @@ -0,0 +1 @@
  1 +2^log(3, 4)
1  test/calc/ex/66.ex
... ... @@ -0,0 +1 @@
  1 +(2^3)+4
1  test/calc/ex/67.ex
... ... @@ -0,0 +1 @@
  1 +(2^3)-4
1  test/calc/ex/68.ex
... ... @@ -0,0 +1 @@
  1 +(2^3)/4
1  test/calc/ex/69.ex
... ... @@ -0,0 +1 @@
  1 +(2^3)*4
1  test/calc/ex/7.ex
... ... @@ -0,0 +1 @@
  1 +2^(1/2)
1  test/calc/ex/70.ex
... ... @@ -0,0 +1 @@
  1 +(2^3)^4
1  test/calc/ex/71.ex
... ... @@ -0,0 +1 @@
  1 +log(2,3) ^ 4
1  test/calc/ex/72.ex
... ... @@ -0,0 +1 @@
  1 +4.*3./2.
1  test/calc/ex/73.ex
... ... @@ -0,0 +1 @@
  1 +(4./2.*3.)
1  test/calc/ex/74.ex
... ... @@ -0,0 +1 @@
  1 +3+((5+9)*2)
1  test/calc/ex/75.ex
... ... @@ -0,0 +1 @@
  1 +(3.+9.)*4./8.
1  test/calc/ex/76.ex
... ... @@ -0,0 +1 @@
  1 +4.^2^2/2
1  test/calc/ex/77.ex
... ... @@ -0,0 +1 @@
  1 +((9.-3.)*(5.-3.))/2. + 2.
1  test/calc/ex/78.ex
... ... @@ -0,0 +1 @@
  1 +(5.*4./2.-10.+5.-2.+3.)
1  test/calc/ex/79.ex
... ... @@ -0,0 +1 @@
  1 +(5./4.*2.+10.-5.*2./3.)
1  test/calc/ex/8.ex
... ... @@ -0,0 +1 @@
  1 +2^5
1  test/calc/ex/80.ex
... ... @@ -0,0 +1 @@
  1 +1234.3231*432-(12.4+32)-9*7/4^3
1  test/calc/ex/81.ex
... ... @@ -0,0 +1 @@
  1 +log(1234.3231, 432)^(12.4+32)-12.9*7/4^3.23
1  test/calc/ex/82.ex
... ... @@ -0,0 +1 @@
  1 +(-12.9+7/4^3.23)*3.3245
1  test/calc/ex/83.ex
... ... @@ -0,0 +1 @@
  1 +-<4,5,6>
1  test/calc/ex/84.ex
... ... @@ -0,0 +1 @@
  1 +-(-<4,5,6>)
1  test/calc/ex/85.ex
... ... @@ -0,0 +1 @@
  1 +<1,2,3>+<4,5,6>
1  test/calc/ex/86.ex
... ... @@ -0,0 +1 @@
  1 +<4,5,6>-<1,2,3>
1  test/calc/ex/87.ex
... ... @@ -0,0 +1 @@
  1 +<1,2,3>*<4,5,6>
1  test/calc/ex/88.ex
... ... @@ -0,0 +1 @@
  1 +<4,5,6>/<1,2,3>
1  test/calc/ex/89.ex
... ... @@ -0,0 +1 @@
  1 +2+<4,5,6>
1  test/calc/ex/9.ex
... ... @@ -0,0 +1 @@
  1 +log(1024, 2)
1  test/calc/ex/90.ex
... ... @@ -0,0 +1 @@
  1 +2-<1,2,3>
1  test/calc/ex/91.ex
... ... @@ -0,0 +1 @@
  1 +2*<4,5,6>
1  test/calc/ex/92.ex
... ... @@ -0,0 +1 @@
  1 +2/<1,2,3>
1  test/calc/ex/93.ex
... ... @@ -0,0 +1 @@
  1 +<4,5,6>+2
1  test/calc/ex/94.ex
... ... @@ -0,0 +1 @@
  1 +<1,2,3>-2
1  test/calc/ex/95.ex
... ... @@ -0,0 +1 @@
  1 +<4,5,6>*2
1  test/calc/ex/96.ex
... ... @@ -0,0 +1 @@
  1 +<1,2,3>/2
1  test/calc/ex/97.ex
... ... @@ -0,0 +1 @@
  1 +-(-(<2, 3>*<2, 2>)*4-<7,4>)+10/2
1  test/calc/ex/98.ex
... ... @@ -0,0 +1 @@
  1 +-<1,5,6,4,6,1>
1  test/calc/ex/99.ex
... ... @@ -0,0 +1 @@
  1 +-(-<1.23/5,5*12,6.34,42.1,6.5322,1/1>)
100 test/calc/lexer.py
... ... @@ -0,0 +1,100 @@
  1 +#!/usr/bin/env python
  2 +# -*- coding: utf-8 -*-
  3 +#Author: Tim Henderson
  4 +#Email: tim.tadh@hackthology.com
  5 +#For licensing see the LICENSE file in the top level directory.
  6 +
  7 +from ply import lex
  8 +from ply.lex import Token
  9 +
  10 +reserved = dict(
  11 + (word.lower(), word) for word in (
  12 + 'LOG',
  13 + )
  14 +)
  15 +
  16 +tokens = reserved.values() + [
  17 + 'NUMBER', 'PLUS', 'DASH', 'STAR', 'SLASH', 'LPAREN', 'RPAREN', 'LSQUARE',
  18 + 'RSQUARE', 'LANGLE', 'RANGLE', 'COMMA', 'SEMI', 'EXP', 'T'
  19 +]
  20 +
  21 +# Common Regex Parts
  22 +
  23 +D = r'[0-9]'
  24 +L = r'[a-zA-Z_]'
  25 +H = r'[a-fA-F0-9]'
  26 +E = r'[Ee][+-]?(' + D + ')+'
  27 +
  28 +
  29 +## Normally PLY works at the module level. I perfer having it encapsulated as
  30 +## a class. Thus the strange construction of this class in the new method allows
  31 +## PLY to do its magic.
  32 +class Lexer(object):
  33 +
  34 + def __new__(cls, **kwargs):
  35 + self = super(Lexer, cls).__new__(cls, **kwargs)
  36 + self.lexer = lex.lex(object=self, **kwargs)
  37 + return self.lexer
  38 +
  39 + tokens = tokens
  40 +
  41 + 'LPAREN', 'RPAREN', 'LSQUARE',
  42 + 'RSQUARE', 'LANGLE', 'RANGLE', 'COMMA', 'SEMI', 'EXP'
  43 + t_T = r'T'
  44 + t_EXP = r'\^'
  45 + t_PLUS = r'\+'
  46 + t_DASH = r'-'
  47 + t_STAR = r'\*'
  48 + t_SEMI = r';'
  49 + t_SLASH = r'/'
  50 + t_COMMA = r','
  51 + t_LPAREN = r'\('
  52 + t_RPAREN = r'\)'
  53 + t_LANGLE = r'\<'
  54 + t_RANGLE = r'\>'
  55 + t_LSQUARE = r'\['
  56 + t_RSQUARE = r'\]'
  57 +
  58 +
  59 + name = '(' + L + ')((' + L + ')|(' + D + '))*'
  60 + @Token(name)
  61 + def t_NAME(self, token):
  62 + if token.value in reserved:
  63 + token.type = reserved[token.value]
  64 + return token
  65 + return None
  66 +
  67 + const_float = '(' + D + ')+\.?(' + D + ')*(' + E + ')?'
  68 + @Token(const_float)
  69 + def t_CONST_FLOAT(self, token):
  70 + token.type = 'NUMBER'
  71 + token.value = float(token.value)
  72 + return token
  73 +
  74 + @Token(r'\n+')
  75 + def t_newline(self, t):
  76 + #t.type = 'NEWLINE'
  77 + t.lexer.lineno += t.value.count("\n")
  78 + return None
  79 +
  80 + @Token(r'(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|(//.*)')
  81 + def t_COMMENT(self, token):
  82 + #print token.lexer.lineno, len(token.value.split('\n')), token.value.split('\n')
  83 + lines = len(token.value.split('\n')) - 1
  84 + if lines < 0: lines = 0
  85 + token.lexer.lineno += lines
  86 +
  87 + # Ignored characters
  88 + t_ignore = " \t"
  89 +
  90 + def t_error(self, t):
  91 + raise Exception, "Illegal character '%s'" % t
  92 + t.lexer.skip(1)
  93 +
  94 +if __name__ == '__main__':
  95 + lexer = Lexer()
  96 + lexer.input('''
  97 + var r = 10
  98 + print r
  99 + ''')
  100 + print [x for x in lexer]
181 test/calc/parser.py
... ... @@ -0,0 +1,181 @@
  1 +#!/usr/bin/env python
  2 +# -*- coding: utf-8 -*-
  3 +#Author: Tim Henderson
  4 +#Email: tim.tadh@hackthology.com
  5 +#For licensing see the LICENSE file in the top level directory.
  6 +
  7 +import os, sys, traceback, subprocess, itertools, contextlib
  8 +
  9 +import coverage
  10 +from ply import yacc
  11 +from lexer import tokens, Lexer
  12 +from ast import Node
  13 +
  14 +
  15 +def get(self, n):
  16 + if n >= 0: return self.slice[n]
  17 + else: return self.stack[n].value
  18 +
  19 +setattr(yacc.YaccProduction, 'get', get)
  20 +
  21 +
  22 +
  23 +## If you are confused about the syntax in this file I recommend reading the
  24 +## documentation on the PLY website to see how this compiler compiler's syntax
  25 +## works.
  26 +class Parser(object):
  27 +
  28 + tokens = tokens
  29 + precedence = (
  30 + )
  31 +
  32 + def __new__(cls, **kwargs):
  33 + ## Does magic to allow PLY to do its thing.
  34 + self = super(Parser, cls).__new__(cls, **kwargs)
  35 + self.table = dict()
  36 + self.loc = list()
  37 + self.yacc = yacc.yacc(module=self, tabmodule="sl_parser_tab", debug=0, **kwargs)
  38 + return self.yacc
  39 +
  40 + def __create(self, t):
  41 + #print type(t), help(t)
  42 +
  43 + doc = self.__getattribute__(traceback.extract_stack()[-2][2]).__doc__
  44 + name = doc.split(':', 1)[0].strip()
  45 + node = Node(name)
  46 + for i, x in enumerate(t[1:]):
  47 + if isinstance(x, Node):
  48 + node.addkid(x)
  49 + else:
  50 + tok = t.get(i+1)
  51 + node.addkid(Node(tok.type + ':' + str(tok.value)))
  52 + return node
  53 +
  54 + def p_Start(self, t):
  55 + 'Start : Expr'
  56 + t[0] = self.__create(t)
  57 +
  58 + def p_Expr1(self, t):
  59 + 'Expr : Expr PLUS Term'
  60 + t[0] = self.__create(t)
  61 +
  62 + def p_Expr2(self, t):
  63 + 'Expr : Expr DASH Term'
  64 + t[0] = self.__create(t)
  65 +
  66 + def p_Expr3(self, t):
  67 + 'Expr : Term'
  68 + t[0] = self.__create(t)
  69 +
  70 + def p_Term1(self, t):
  71 + 'Term : Term STAR Exp'
  72 + t[0] = self.__create(t)
  73 +
  74 + def p_Term2(self, t):
  75 + 'Term : Term SLASH Exp'
  76 + t[0] = self.__create(t)
  77 +
  78 + def p_Term3(self, t):
  79 + 'Term : Exp'
  80 + t[0] = self.__create(t)
  81 +
  82 + def p_Exp1(self, t):
  83 + 'Exp : Exp EXP Unary'
  84 + t[0] = self.__create(t)
  85 +
  86 + def p_Exp2(self, t):
  87 + 'Exp : Unary'
  88 + t[0] = self.__create(t)
  89 +
  90 + def p_Unary1(self, t):
  91 + 'Unary : DASH PostUnary'
  92 + t[0] = self.__create(t)
  93 +
  94 + def p_Unary2(self, t):
  95 + 'Unary : PostUnary'
  96 + t[0] = self.__create(t)
  97 +
  98 + def p_PostUnary1(self, t):
  99 + 'PostUnary : Factor T'
  100 + t[0] = self.__create(t)
  101 +
  102 + def p_PostUnary2(self, t):
  103 + 'PostUnary : Factor'
  104 + t[0] = self.__create(t)
  105 +
  106 + def p_Factor1(self, t):
  107 + 'Factor : Value'
  108 + t[0] = self.__create(t)
  109 +
  110 + def p_Factor2(self, t):
  111 + 'Factor : LPAREN Expr RPAREN'
  112 + t[0] = self.__create(t)
  113 +
  114 + def p_Value1(self, t):
  115 + 'Value : Atom'
  116 + t[0] = self.__create(t)
  117 +
  118 + def p_Value2(self, t):
  119 + 'Value : Log'
  120 + t[0] = self.__create(t)
  121 +
  122 + def p_Value3(self, t):
  123 + 'Value : LSQUARE Matrix RSQUARE'
  124 + t[0] = self.__create(t)
  125 +
  126 + def p_Value4(self, t):
  127 + 'Value : LANGLE Vector RANGLE'
  128 + t[0] = self.__create(t)
  129 +
  130 + def p_Atom1(self, t):
  131 + 'Atom : NUMBER'
  132 + t[0] = self.__create(t)
  133 +
  134 + def p_Log1(self, t):
  135 + 'Log : LOG LPAREN Expr COMMA Expr RPAREN'
  136 + t[0] = self.__create(t)
  137 +
  138 + def p_Matrix1(self, t):
  139 + 'Matrix : Matrix SEMI Vector'
  140 + t[0] = self.__create(t)
  141 +
  142 + def p_Matrix2(self, t):
  143 + 'Matrix : Vector'
  144 + t[0] = self.__create(t)
  145 +
  146 + def p_Vector1(self, t):
  147 + 'Vector : Vector COMMA Expr'
  148 + t[0] = self.__create(t)
  149 +
  150 + def p_Vector2(self, t):
  151 + 'Vector : Expr'
  152 + t[0] = self.__create(t)
  153 +
  154 + def p_error(self, t):
  155 + raise SyntaxError, "Syntax error at '%s', %s.%s" % (t,t.lineno,t.lexpos)
  156 +
  157 +def read(path):
  158 + f = open(path, 'r')
  159 + s = f.read()
  160 + f.close()
  161 + return s
  162 +
  163 +def parse(s):
  164 + return Parser().parse(s, lexer=Lexer())
  165 +
  166 +def main():
  167 + if not sys.argv[1:]: sys.exit(1)
  168 + file_names = [os.path.abspath(s) for s in sys.argv[1:]]
  169 + files = [(path, read(path)) for path in file_names]
  170 + trees = list()
  171 + for path, text in files:
  172 + print path, text
  173 + trees.append((path, parse(text)))
  174 + for path, tree in trees:
  175 + f = open(path+'.ast', 'w')
  176 + f.write(str(tree))
  177 + f.close()
  178 +
  179 +if __name__ == '__main__':
  180 + main()
  181 +

No commit comments for this range

Something went wrong with that request. Please try again.