Skip to content

Commit

Permalink
do_replace -> apply_rule (#1083)
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky authored Sep 8, 2024
1 parent 10d9e04 commit d95df64
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
14 changes: 7 additions & 7 deletions mathics/builtin/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from mathics.core.symbols import SymbolFalse, SymbolNull, SymbolTrue, strip_context


def traced_do_replace(self, expression, vars, options: dict, evaluation: Evaluation):
def traced_apply_rule(self, expression, vars, options: dict, evaluation: Evaluation):
if options and self.check_options:
if not self.check_options(options, evaluation):
return None
Expand Down Expand Up @@ -185,7 +185,7 @@ class TraceBuiltins(_TraceBase):
"""

definitions_copy: Definitions
do_replace_copy: Callable
apply_rule_copy: Callable

function_stats: "defaultdict" = defaultdict(
lambda: {"count": 0, "elapsed_milliseconds": 0.0}
Expand Down Expand Up @@ -238,19 +238,19 @@ def sort_by_name(tup: tuple):
@staticmethod
def enable_trace(evaluation) -> None:
if TraceBuiltins.traced_definitions is None:
TraceBuiltins.do_replace_copy = BuiltinRule.do_replace
TraceBuiltins.apply_rule_copy = BuiltinRule.apply_rule
TraceBuiltins.definitions_copy = evaluation.definitions

# Replaces do_replace by the custom one
BuiltinRule.do_replace = traced_do_replace
# Create new definitions uses the new do_replace
# Replaces apply_rule by the custom one
BuiltinRule.apply_rule = traced_apply_rule
# Create new definitions uses the new apply_rule
evaluation.definitions = Definitions(add_builtin=True)
else:
evaluation.definitions = TraceBuiltins.definitions_copy

@staticmethod
def disable_trace(evaluation) -> None:
BuiltinRule.do_replace = TraceBuiltins.do_replace_copy
BuiltinRule.apply_rule = TraceBuiltins.apply_rule_copy
evaluation.definitions = TraceBuiltins.definitions_copy

def eval(self, expr, evaluation, options={}):
Expand Down
12 changes: 6 additions & 6 deletions mathics/core/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BaseRule(KeyComparable):
https://en.wikipedia.org/wiki/Rewriting
This class is not complete in of itself and subclasses should
adapt or fill in what is needed. In particular ``do_replace()``
adapt or fill in what is needed. In particular ``apply_rule()``
needs to be implemented.
Important subclasses: BuiltinRule and Rule.
Expand Down Expand Up @@ -75,7 +75,7 @@ def yield_match(vars, rest):
if name.startswith("_option_"):
options[name[len("_option_") :]] = value
del vars[name]
new_expression = self.do_replace(expression, vars, options, evaluation)
new_expression = self.apply_rule(expression, vars, options, evaluation)
if new_expression is None:
new_expression = expression
if rest[0] or rest[1]:
Expand Down Expand Up @@ -120,7 +120,7 @@ def yield_match(vars, rest):
else:
return None

def do_replace(self):
def apply_rule(self):
raise NotImplementedError

def get_sort_key(self) -> tuple:
Expand Down Expand Up @@ -160,7 +160,7 @@ def __init__(
super(Rule, self).__init__(pattern, system=system, evaluation=evaluation)
self.replace = replace

def do_replace(
def apply_rule(
self, expression: BaseElement, vars: dict, options: dict, evaluation: Evaluation
):
new = self.replace.replace_vars(vars)
Expand Down Expand Up @@ -249,9 +249,9 @@ def __init__(
self.check_options = check_options
self.pass_expression = "expression" in function_arguments(function)

# If you update this, you must also update traced_do_replace
# If you update this, you must also update traced_apply_rule
# (that's in the same file TraceBuiltins is)
def do_replace(
def apply_rule(
self, expression: BaseElement, vars: dict, options: dict, evaluation: Evaluation
):
if options and self.check_options:
Expand Down
4 changes: 2 additions & 2 deletions mathics/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import sys

from mathics import __version__, license_string, settings, version_string
from mathics.builtin.trace import TraceBuiltins, traced_do_replace
from mathics.builtin.trace import TraceBuiltins, traced_apply_rule
from mathics.core.atoms import String
from mathics.core.definitions import Definitions, Symbol, autoload_files
from mathics.core.evaluation import Evaluation, Output
Expand Down Expand Up @@ -385,7 +385,7 @@ def main() -> int:
extension_modules = default_pymathics_modules

if args.trace_builtins:
BuiltinRule.do_replace = traced_do_replace
BuiltinRule.apply_rule = traced_apply_rule

def dump_tracing_stats():
TraceBuiltins.dump_tracing_stats(sort_by="count", evaluation=None)
Expand Down

0 comments on commit d95df64

Please sign in to comment.