Skip to content

Commit

Permalink
Increased use of binary operator string values from JSON (#1256)
Browse files Browse the repository at this point in the history
Get the binary operator string from enhanced Mathics-Scanner JSON operator tables.

For now, we have to limit to binary operators only.
  • Loading branch information
rocky authored Dec 30, 2024
1 parent 2a27d46 commit 1ed7495
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 40 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ jobs:
python -m pip install --upgrade pip
- name: Install Mathics3 with full Python dependencies
run: |
# We can comment out after next Mathics-Scanner release
python -m pip install -e git+https://github.com/Mathics3/mathics-scanner#egg=Mathics-Scanner[full]
git clone https://github.com/Mathics3/mathics-scanner.git
# We can comment out after next Mathics-Scanner release
git clone --depth 1 https://github.com/Mathics3/mathics-scanner.git
# git clone --single-branch --branch operator-refactor-part1.5 https://github.com/Mathics3/mathics-scanner.git
cd mathics-scanner/
pip install -e .
cd ..
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ jobs:
run: |
sudo apt update -qq && sudo apt install llvm-dev remake
python -m pip install --upgrade pip
# We can comment out after next Mathics-Scanner release
python -m pip install -e git+https://github.com/Mathics3/mathics-scanner#egg=Mathics-Scanner[full]
git clone https://github.com/Mathics3/mathics-scanner.git
# We can comment out after next Mathics-Scanner release
git clone --depth 1 https://github.com/Mathics3/mathics-scanner.git
# git clone --single-branch --branch operator-refactor-part1.5 https://github.com/Mathics3/mathics-scanner.git
cd mathics-scanner/
pip install -e .
cd ..
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
python -m pip install --upgrade pip
# We can comment out after next Mathics-Scanner release
python -m pip install -e git+https://github.com/Mathics3/mathics-scanner#egg=Mathics-Scanner[full]
git clone --depth 1 https://github.com/Mathics3/mathics-scanner.git
# git clone --single-branch --branch operator-refactor-part1.5 https://github.com/Mathics3/mathics-scanner.git
cd src/mathics-scanner/
pip install -e .
python -m mathics_scanner.generate.build_tables
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/pyodide.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ jobs:
pip install "setuptools>=70.0.0" PyYAML click packaging pytest
git clone https://github.com/Mathics3/mathics-scanner.git
# We can comment out after next Mathics-Scanner release
git clone --depth 1 https://github.com/Mathics3/mathics-scanner.git
# git clone --single-branch --branch operator-refactor-part1.5 https://github.com/Mathics3/mathics-scanner.git
cd mathics-scanner/
pip install --no-build-isolation -e .
cd ..
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ubuntu-cython.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ jobs:
python -m pip install --upgrade pip
# We can comment out after next Mathics-Scanner release
python -m pip install -e git+https://github.com/Mathics3/mathics-scanner#egg=Mathics-Scanner[full]
git clone https://github.com/Mathics3/mathics-scanner.git
git clone --depth 1 https://github.com/Mathics3/mathics-scanner.git
# git clone --single-branch --branch operator-refactor-part1.5 https://github.com/Mathics3/mathics-scanner.git
cd mathics-scanner/
pip install -e .
cd ..
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ jobs:
python -m pip install --upgrade pip
# We can comment out after next Mathics-Scanner release
# python -m pip install -e git+https://github.com/Mathics3/mathics-scanner#egg=Mathics-Scanner[full]
git clone https://github.com/Mathics3/mathics-scanner.git
git clone --depth 1 https://github.com/Mathics3/mathics-scanner.git
# git clone --single-branch --branch operator-refactor-part1.5 https://github.com/Mathics3/mathics-scanner.git
cd mathics-scanner/
pip install -e .
python -m mathics_scanner.generate.build_tables
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ jobs:
run: |
# We can comment out after next Mathics-Scanner release
python -m pip install -e git+https://github.com/Mathics3/mathics-scanner#egg=Mathics-Scanner[full]
git clone https://github.com/Mathics3/mathics-scanner.git
git clone --depth 1 https://github.com/Mathics3/mathics-scanner.git
# git clone --single-branch --branch operator-refactor-part1.5 https://github.com/Mathics3/mathics-scanner.git
cd mathics-scanner
pip install -e .
python -m mathics_scanner.generate.build_tables
Expand Down
9 changes: 0 additions & 9 deletions mathics/builtin/arithfns/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ class Divide(InfixOperator):
}

grouping = "Left"
operator = "/"

rules = {
"Divide[x_, y_]": "Times[x, Power[y, -1]]",
Expand Down Expand Up @@ -206,8 +205,6 @@ class Minus(PrefixOperator):
),
}

operator = "-"

rules = {
"Minus[x_]": "Times[-1, x]",
}
Expand Down Expand Up @@ -280,8 +277,6 @@ class Plus(InfixOperator, SympyFunction):
None: "0",
}

operator = "+"

summary_text = "add"

# FIXME Note this is deprecated in 1.11
Expand Down Expand Up @@ -431,8 +426,6 @@ class Power(InfixOperator, MPMathFunction):
}

nargs = {2}
operator = "^"

rules = {
"Power[]": "1",
"Power[x_]": "x",
Expand Down Expand Up @@ -551,7 +544,6 @@ class Subtract(InfixOperator):
attributes = A_LISTABLE | A_NUMERIC_FUNCTION | A_PROTECTED
grouping = "Left"

operator = "-"
rules = {
"Subtract[x_, y_]": "Plus[x, Times[-1, y]]",
}
Expand Down Expand Up @@ -614,7 +606,6 @@ class Times(InfixOperator, SympyFunction):

formats = {}

operator = "*"
operator_display = " "

rules = {}
Expand Down
2 changes: 1 addition & 1 deletion mathics/builtin/assignments/assign_binaryop.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class AddTo(InfixOperator, InplaceInfixOperator):
"""

attributes = A_HOLD_FIRST | A_PROTECTED
grouping = "Right"
operator = "+="

operator_symbol = SymbolPlus
Expand Down Expand Up @@ -147,7 +148,6 @@ class DivideBy(InplaceInfixOperator, InfixOperator):

attributes = A_HOLD_FIRST | A_PROTECTED
grouping = "Right"
operator = "/="

rules = {
"x_ /= dx_": "x = x / dx",
Expand Down
5 changes: 0 additions & 5 deletions mathics/builtin/assignments/assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ class Set(InfixOperator, _SetOperator):
"shape": "Lists `1` and `2` are not the same shape.",
}

operator = "="

summary_text = "assign a value"

def eval(self, lhs, rhs, evaluation):
Expand Down Expand Up @@ -246,7 +244,6 @@ class SetDelayed(Set):
# In Mathics, this last line would return 3
# """

operator = ":="
attributes = A_HOLD_ALL | A_PROTECTED | A_SEQUENCE_HOLD

summary_text = "test a delayed value; used in defining functions"
Expand Down Expand Up @@ -366,7 +363,6 @@ class UpSet(InfixOperator, _SetOperator):

attributes = A_HOLD_FIRST | A_PROTECTED | A_SEQUENCE_HOLD
grouping = "Right"
operator = "^="

summary_text = (
"set value and associate the assignment with symbols that occur at level one"
Expand Down Expand Up @@ -400,7 +396,6 @@ class UpSetDelayed(UpSet):
"""

attributes = A_HOLD_ALL | A_PROTECTED | A_SEQUENCE_HOLD
operator = "^:="
summary_text = "set a delayed value and associate the assignment with symbols that occur at level one"

def eval(self, lhs, rhs, evaluation):
Expand Down
1 change: 0 additions & 1 deletion mathics/builtin/assignments/clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ class Unset(PostfixOperator):
"""

attributes = A_HOLD_FIRST | A_LISTABLE | A_PROTECTED | A_READ_PROTECTED
operator = "=."

messages = {
"norep": "Assignment on `2` for `1` not found.",
Expand Down
1 change: 0 additions & 1 deletion mathics/builtin/exp_structure/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class MapApply(InfixOperator):
"""

grouping = "Right"
operator = "@@@"

rules = {
"MapApply[f_, expr_]": "Apply[f, expr, {1}]",
Expand Down
1 change: 0 additions & 1 deletion mathics/builtin/patterns/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class Alternatives(InfixOperator, PatternObject):

arg_counts = None
needs_verbatim = True
operator = "|"
summary_text = "match to any of several patterns"

def init(
Expand Down
3 changes: 0 additions & 3 deletions mathics/builtin/patterns/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ class ReplaceAll(InfixOperator):

grouping = "Left"
needs_verbatim = True
operator = "/."

messages = {
"reps": "`1` is not a valid replacement rule.",
Expand Down Expand Up @@ -435,7 +434,6 @@ class ReplaceRepeated(InfixOperator):

grouping = "Left"
needs_verbatim = True
operator = "//."

messages = {
"reps": "`1` is not a valid replacement rule.",
Expand Down Expand Up @@ -542,7 +540,6 @@ class RuleDelayed(InfixOperator):

attributes = A_SEQUENCE_HOLD | A_HOLD_REST | A_PROTECTED
needs_verbatim = True
operator = ":>"
summary_text = "a rule that keeps the replacement unevaluated"

def eval_rule_delayed(self, elems, evaluation):
Expand Down
18 changes: 15 additions & 3 deletions mathics/core/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
A_PROTECTED,
)
from mathics.core.convert.expression import to_expression
from mathics.core.convert.op import ascii_operator_to_symbol
from mathics.core.convert.op import ascii_operator_to_symbol, operator_to_unicode
from mathics.core.convert.python import from_bool
from mathics.core.convert.sympy import from_sympy
from mathics.core.definitions import Definition, Definitions
Expand Down Expand Up @@ -1166,11 +1166,12 @@ def get_precedence(self, name: str) -> int:
precedence = operator_info.get(name)
assert isinstance(
precedence, int
), f'Internal error: "precedence" field for "{name}" should be an integer is {precedence}'
), f'Internal error: "precedence" field for "{name}" should be an integer; is {precedence}'
return precedence

def get_operator(self) -> Optional[str]:
return self.operator
name = self.__class__.__name__
return operator_to_unicode.get(name)

def get_operator_display(self) -> Optional[str]:
if hasattr(self, "operator_display"):
Expand Down Expand Up @@ -1200,6 +1201,17 @@ class InfixOperator(Operator):
def __init__(self, *args, **kwargs):
super(InfixOperator, self).__init__(*args, **kwargs)
name = self.get_name(short=True)

# Pick up operator string from JSON table if
# it appears there.
operator_string = self.get_operator()
if operator_string:
self.operator = operator_string
# else:
# if self.operator is None:
# breakpoint()
# print("FIX UP", self.operator, name)

self.precedence = self.get_precedence(name)

# Prevent pattern matching symbols from gaining meaning here using
Expand Down
16 changes: 10 additions & 6 deletions mathics/core/convert/op.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
characters_path
), f"ASCII operator to Unicode tables are missing from {characters_path}"
with open(characters_path, "r") as f:
op_data = ujson.load(f)
OPERATOR_CONVERSION_TABLES = ujson.load(f)

ascii_operator_to_symbol = op_data["ascii-operator-to-symbol"]
operator_to_unicode = op_data["operator-to-unicode"]
operator_to_ascii = op_data["operator-to-ascii"]
ascii_operator_to_symbol = OPERATOR_CONVERSION_TABLES["ascii-operator-to-symbol"]
operator_to_unicode = OPERATOR_CONVERSION_TABLES["operator-to-unicode"]
operator_to_ascii = OPERATOR_CONVERSION_TABLES["operator-to-ascii"]


@lru_cache(maxsize=1024)
Expand All @@ -34,7 +34,11 @@ def ascii_op_to_unicode(ascii_op: str, encoding: str) -> str:
If we can't come up with a unicode equivalent, just return "ascii_op".
"""
if encoding in ("UTF-8", "utf-8", "Unicode"):
return op_data["ascii-operator-to-unicode"].get(ascii_op, ascii_op)
return OPERATOR_CONVERSION_TABLES["ascii-operator-to-unicode"].get(
ascii_op, ascii_op
)
if encoding in ("WMA",):
return op_data["ascii-operator-to-wl-unicode"].get(ascii_op, ascii_op)
return OPERATOR_CONVERSION_TABLES["ascii-operator-to-wl-unicode"].get(
ascii_op, ascii_op
)
return ascii_op
2 changes: 1 addition & 1 deletion mathics/core/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def get_quiet_messages(self):
return []
return value.elements

def message(self, symbol_name: str, tag, *msgs) -> Optional["Message"]:
def message(self, symbol_name: str, tag: str, *msgs) -> Optional["Message"]:
"""
Format message given its components, ``symbol_name``, ``tag``
Expand Down

0 comments on commit 1ed7495

Please sign in to comment.