Skip to content

Commit c039d54

Browse files
committed
First-cut auto-generated no-meaning operators
1 parent d1cf6a4 commit c039d54

File tree

2 files changed

+67
-26
lines changed

2 files changed

+67
-26
lines changed

mathics/builtin/no_meaning.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,49 @@
55
Operators without Built-in Meanings
66
77
Not all operators recognized by the Mathics3 are associated with functions that have built‐in meanings.
8-
You can use these operators as a way to build up your own notation within the Mathics3.
8+
You can use these operators as a way to build up your own notation within Mathics3.
99
"""
1010

11-
from mathics.core.attributes import A_NO_ATTRIBUTES
12-
from mathics.core.builtin import InfixOperator
11+
from inspect import getmembers, isclass
12+
from sys import modules
1313

14+
from mathics.core.builtin import OPERATOR_DATA, NoMeaningInfixOperator
1415

15-
class Star(InfixOperator):
16-
r"""
17-
Star <url>
18-
:WML link:
19-
https://reference.wolfram.com/language/ref/Star.html</url>
16+
# Note: classes in this file must *only* be "no-meaning"
17+
# builtin operator classes.
2018

21-
<dl>
22-
<dt>'Star[$x$, $y$, ...]'
23-
<dd>displays $x$ ⋆ $y$ ⋆ ...
24-
</dl>
2519

26-
>> Star[x, y, z]
27-
= x ⋆ y ⋆ z
20+
class Because(NoMeaningInfixOperator):
21+
r"""This text is replaced! But it needs to be here for documentation detection."""
2822

29-
>> a \[Star] b
30-
= a ⋆ b
31-
"""
3223

33-
attributes = A_NO_ATTRIBUTES
34-
default_formats = False # Don't use any default format rules. Instead, see below.
35-
formats = {
36-
(("InputForm", "OutputForm", "StandardForm"), "Star[args__]"): (
37-
'Infix[{args}, "⋆"]'
38-
),
39-
}
24+
class Cap(NoMeaningInfixOperator):
25+
r"""This text is replaced! But it needs to be here for documentation detection."""
4026

41-
operator = "⋆" # \u22C6
42-
summary_text = "star symbol"
27+
28+
class CenterDot(NoMeaningInfixOperator):
29+
r"""This text is replaced! But it needs to be here for documentation detection."""
30+
31+
32+
class Star(NoMeaningInfixOperator):
33+
r"""This text is replaced! But it needs to be here for documentation detection."""
34+
35+
36+
# Generate Builtin No-meaning Builtin Infix operators, using
37+
# the Operator name and Operator Unicode found by reading
38+
# the operators JSON file.
39+
for name, operator_class in getmembers(modules[__name__]):
40+
if isclass(operator_class):
41+
operator_name = operator_class.__name__
42+
operator_string = OPERATOR_DATA["no-meaning-infix-operators"].get(operator_name)
43+
if operator_string is not None:
44+
operator_class.operator = operator_string
45+
operator_class.__doc__ = NoMeaningInfixOperator.__doc_pattern__.format(
46+
operator_name=operator_name, operator_string=operator_string
47+
)
48+
operator_class.summary_text = f"""{operator_name} infix operator "{operator_string}" (no pre-set meaning attached)"""
49+
operator_class.formats = {
50+
(("InputForm", "OutputForm", "StandardForm"), f"{operator_name}[args__]"): (
51+
('Infix[{args}, "%s"]' % operator_string)
52+
)
53+
}

mathics/core/builtin.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,36 @@ def __init__(self, *args, **kwargs):
12541254
self.rules = default_rules
12551255

12561256

1257+
class NoMeaningInfixOperator(InfixOperator, ABC):
1258+
"""
1259+
Operators that have no pre-defined meaning are derived from this class.
1260+
"""
1261+
1262+
# This will be used to create a docstring
1263+
__doc_pattern__ = r"""
1264+
{operator_name} <url>
1265+
:WML link:
1266+
https://reference.wolfram.com/language/ref/{operator_name}.html</url>
1267+
1268+
<dl>
1269+
<dt>'{operator_name}[$x$, $y$, ...]'
1270+
<dd>displays $x$ {operator_string} $y$ {operator_string} ...
1271+
</dl>
1272+
1273+
>> {operator_name}[x, y, z]
1274+
= x {operator_string} y {operator_string} z
1275+
1276+
>> a \[{operator_name}] b
1277+
= a {operator_string} b
1278+
1279+
"""
1280+
attributes = A_NO_ATTRIBUTES
1281+
default_formats = False # Don't use any default format rules. Instead, see below.
1282+
1283+
operator = "This should be overwritten"
1284+
summary_text = "This should be overwritten"
1285+
1286+
12571287
class Predefined(Builtin, ABC):
12581288
def __init__(self, *args, **kwargs):
12591289
super().__init__(*args, **kwargs)

0 commit comments

Comments
 (0)