-
-
Notifications
You must be signed in to change notification settings - Fork 61
adding evaluate_without_side_effects for helping compile expressions.
#1539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
45c302b
36837e4
969a232
abe1bbd
66c6c6a
8e8d5cd
d4696d0
bd58f4e
4c10bf9
0102928
64d7790
8f50b4e
dc91eed
af8fb0a
88d07da
a95b65f
2ebd8ef
1e1e02b
07fb550
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| from typing import Callable, Optional, Tuple | ||
|
|
||
| from mathics.core.definitions import SIDE_EFFECT_BUILTINS, Definition | ||
| from mathics.core.element import BaseElement | ||
| from mathics.core.evaluation import Evaluation | ||
| from mathics.core.expression import Expression, from_python | ||
| from mathics.core.symbols import Symbol, SymbolFalse, SymbolTrue | ||
|
|
@@ -44,6 +46,32 @@ def __init__(self, var): | |
| self.var = var | ||
|
|
||
|
|
||
| def evaluate_without_side_effects( | ||
| expr: BaseElement, evaluation: Evaluation | ||
| ) -> BaseElement: | ||
| """ | ||
| Evaluate an expression leaving unevaluated subexpressions | ||
| related with side-effects (assignments, loops). | ||
| """ | ||
| definitions = evaluation.definitions | ||
| # Temporarily remove the builtin definitions | ||
| # of symbols with side effects | ||
| for name, defin in SIDE_EFFECT_BUILTINS.items(): | ||
| # Change the definition by a temporal definition setting | ||
| # just the name and the attributes. | ||
| definitions.builtin[name] = Definition( | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe these definitions can be stored in some place, and then each time we compile something, bring them up, instead of constructing them each time.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The two obvious places to start alternate representations or approximate computations of an expression (where a symbol is considered to be an expression) are either inside the compound Expression object or inside a definitions entry. I suspect we'll need to make a pass over the definitions table at some point. As with so many other things, it is one of those dark areas where, I suspect, things are a bit unconventional from both the conventional compiler standpoint as well as how WMA thinks of scoping. |
||
| name, attributes=defin.attributes, builtin=defin.builtin | ||
| ) | ||
| definitions.clear_cache(name) | ||
| try: | ||
| result = expr.evaluate(evaluation) | ||
| finally: | ||
| # Restore the definitions | ||
| for name, defin in SIDE_EFFECT_BUILTINS.items(): | ||
| definitions.builtin[name] = defin | ||
| return result | ||
|
|
||
|
|
||
| def expression_to_callable( | ||
| expr: Expression, | ||
| args: Optional[list] = None, | ||
|
|
@@ -56,6 +84,7 @@ def expression_to_callable( | |
| args: a list of CompileArg elements | ||
| evaluation: an Evaluation object used if the llvm compilation fails | ||
| """ | ||
| expr = evaluate_without_side_effects(expr, evaluation) | ||
| try: | ||
| cfunc = _compile(expr, args) if (use_llvm and args is not None) else None | ||
| except CompileError: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is the right place for this function.