|
1 | 1 | import ctypes |
2 | 2 |
|
3 | 3 | from mathics.builtin.base import Builtin, BoxConstruct |
| 4 | +from mathics.core.evaluation import Evaluation |
4 | 5 | from mathics.core.expression import ( |
5 | 6 | Atom, |
6 | | - Evaluation, |
7 | 7 | Expression, |
8 | 8 | Symbol, |
9 | 9 | String, |
|
14 | 14 |
|
15 | 15 |
|
16 | 16 | class Compile(Builtin): |
17 | | - ''' |
| 17 | + """ |
18 | 18 | <dl> |
19 | 19 | <dt>'Compile[{x1, x2, ...}, expr_]' |
20 | 20 | <dd>Compiles $expr$ assuming each $xi$ is a $Real$ number. |
@@ -61,8 +61,7 @@ class Compile(Builtin): |
61 | 61 | Loops and variable assignments are supported as python (not llvmlite) |
62 | 62 | functions |
63 | 63 | >> Compile[{{a, _Integer}, {b, _Integer}}, While[b != 0, {a, b} = {b, Mod[a, b]}]; a] (* GCD of a, b *) |
64 | | - : Expression While[b != 0, {a, b} = {b, Mod[a, b]}] ; a could not be compiled. |
65 | | - = Function[{Global`a, Global`b}, While[b != 0, {a, b} = {b, Mod[a, b]}] ; a] |
| 64 | + = CompiledFunction[{a, b}, a, -CompiledCode-] |
66 | 65 | """ |
67 | 66 |
|
68 | 67 | requires = ("llvmlite",) |
@@ -128,20 +127,22 @@ def apply(self, vars, expr, evaluation): |
128 | 127 |
|
129 | 128 | if cfunc is None: |
130 | 129 | try: |
| 130 | + |
131 | 131 | def _pythonized_mathics_expr(*x): |
132 | 132 | inner_evaluation = Evaluation(definitions=evaluation.definitions) |
133 | | - vars = dict(list(zip(names, x[:len(names)]))) |
| 133 | + vars = dict(list(zip(names, x[: len(names)]))) |
134 | 134 | pyexpr = expr.replace_vars(vars) |
135 | 135 | pyexpr = Expression("N", pyexpr).evaluate(inner_evaluation) |
136 | 136 | res = pyexpr.to_python(n_evaluation=inner_evaluation) |
137 | 137 | return res |
138 | | - #TODO: check if we can use numba to compile this... |
| 138 | + |
| 139 | + # TODO: check if we can use numba to compile this... |
139 | 140 | cfunc = _pythonized_mathics_expr |
140 | 141 | except Exception as e: |
141 | 142 | cfunc = None |
142 | 143 |
|
143 | 144 | if cfunc is None: |
144 | | - evaluation.message('Compile', 'comperr', expr) |
| 145 | + evaluation.message("Compile", "comperr", expr) |
145 | 146 | args = Expression("List", *names) |
146 | 147 | return Expression("Function", args, expr) |
147 | 148 |
|
|
0 commit comments