Skip to content

Commit ad1c8af

Browse files
committed
WIP - towards sorting out autoloading operators
1 parent ef56182 commit ad1c8af

File tree

3 files changed

+50
-20
lines changed

3 files changed

+50
-20
lines changed

mathics/builtin/no_meaning.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from sys import modules
1515

16+
import mathics.core.parser.operators
1617
from mathics.core.builtin import (
1718
OPERATOR_DATA,
1819
NoMeaningInfixOperator,
@@ -49,6 +50,9 @@
4950
},
5051
},
5152
)
53+
if affix == "infix":
54+
# FIXME get precedence from JSON. Value 100 is a hack for now.
55+
mathics.core.parser.operators.flat_binary_ops[operator_name] = 100
5256

5357
# Put the newly-created Builtin class inside this module.
5458
setattr(modules[__name__], operator_name, generated_operator_class)

mathics/core/load_builtin.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from typing import TYPE_CHECKING, Dict, List, Optional, Set, Tuple
1818

1919
from mathics.core.convert.sympy import mathics_to_sympy, sympy_to_mathics
20+
from mathics.core.parser.operators import calculate_operator_information
2021
from mathics.core.pattern import pattern_objects
2122
from mathics.core.symbols import Symbol
2223
from mathics.eval.makeboxes import builtins_precedence
@@ -133,6 +134,8 @@ def definition_contribute(definitions):
133134
# All builtins are loaded. Create dummy builtin definitions for
134135
# any remaining operators that don't have them. This allows
135136
# operators like \[Cup] to behave correctly.
137+
138+
calculate_operator_information()
136139
for operator in all_operator_names:
137140
if not definitions.have_definition(ensure_context(operator)):
138141
op = ensure_context(operator)

mathics/core/parser/operators.py

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -163,28 +163,51 @@
163163

164164
# binary_ops = left_binary_ops V right_binary_ops V flat_binary_ops V nonassoc_binary_ops
165165
binary_ops = {}
166-
for ops in (left_binary_ops, right_binary_ops, flat_binary_ops, nonassoc_binary_ops):
167-
for op, prec in ops.items():
168-
binary_ops[op] = prec
169-
170-
all_op_collections = (
171-
prefix_ops,
172-
postfix_ops,
173-
left_binary_ops,
174-
right_binary_ops,
175-
flat_binary_ops,
176-
ternary_ops,
177-
nonassoc_binary_ops,
178-
misc_ops,
179-
)
180166

181167
# all ops - check they're disjoint
182168
all_ops = defaultdict(lambda: 670)
183169

184-
for ops in all_op_collections:
185-
for op, prec in ops.items():
186-
if op in all_ops:
187-
raise AssertionError
188-
all_ops[op] = prec
170+
# Set below
171+
all_operator_names = []
189172

190-
all_operator_names = list(all_ops.keys())
173+
174+
def calculate_operator_information():
175+
global binary_ops
176+
global all_ops
177+
global all_operator_names
178+
179+
all_operator_names = []
180+
181+
for ops in (
182+
left_binary_ops,
183+
right_binary_ops,
184+
flat_binary_ops,
185+
nonassoc_binary_ops,
186+
):
187+
for op, prec in ops.items():
188+
binary_ops[op] = prec
189+
190+
all_op_collections = (
191+
prefix_ops,
192+
postfix_ops,
193+
left_binary_ops,
194+
right_binary_ops,
195+
flat_binary_ops,
196+
ternary_ops,
197+
nonassoc_binary_ops,
198+
misc_ops,
199+
)
200+
201+
for ops in all_op_collections:
202+
for op, prec in ops.items():
203+
all_ops[op] = prec
204+
205+
all_operator_names = list(all_ops.keys())
206+
207+
208+
# Calculating operator information is also done
209+
# after loading in Builtin operators with no-meaning.
210+
# However we also need to do this before reading that module in.
211+
# This is a mess!
212+
213+
calculate_operator_information()

0 commit comments

Comments
 (0)