Skip to content

Commit 3846c21

Browse files
authored
FormatValues (#1303)
This PR adds `FormatValues` to access the format values of Symbols.
1 parent a83a83f commit 3846c21

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ New Builtins
88
* ``Between``
99
* ``CheckAbort``
1010
* ``FileNameDrop``
11+
* ``FormatValues``
1112
* ``SetEnvironment``
1213
* ``SequenceForm``
1314

mathics/builtin/atomic/symbols.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,29 @@ def eval(self, symbol, evaluation):
400400
return get_symbol_values(symbol, "DownValues", "downvalues", evaluation)
401401

402402

403+
# In Mathematica 5, this appears under "Types of Values".
404+
class FormatValues(Builtin):
405+
"""
406+
<url>:WMA link:https://reference.wolfram.com/language/tutorial/PatternsAndTransformationRules.html#6025</url>
407+
<dl>
408+
<dt>'FormatValues[$symbol$]'
409+
<dd>gives the list of formatvalues associated with $symbol$.
410+
</dl>
411+
412+
>> Format[F[x_], OutputForm]:= Subscript[x, F]
413+
>> FormatValues[F]
414+
= {HoldPattern[Format[Subscript[x_, F], OutputForm]] :> Subscript[x, F]}
415+
"""
416+
417+
summary_text = (
418+
"give a list of formatting transformation rules associated with a symbol."
419+
)
420+
421+
def eval(self, symbol, evaluation):
422+
"""FormatValues[symbol_]"""
423+
return get_symbol_values(symbol, "FormatValues", "formatvalues", evaluation)
424+
425+
403426
class Information(PrefixOperator):
404427
"""
405428
<url>:WMA link:

mathics/core/assignment.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
SymbolAnd,
2222
SymbolBlank,
2323
SymbolCondition,
24+
SymbolFormat,
2425
SymbolHoldPattern,
2526
SymbolOptionValue,
2627
SymbolPart,
@@ -116,6 +117,46 @@ def get_symbol_values(
116117
return ListExpression()
117118

118119
elements = []
120+
121+
if position == "formatvalues":
122+
format_rules = definition.formatvalues
123+
for key, rules in format_rules.items():
124+
if key == "System`MakeBoxes":
125+
elements.extend(rules)
126+
continue
127+
if key:
128+
elements.extend(
129+
(
130+
Expression(
131+
SymbolRuleDelayed,
132+
Expression(
133+
SymbolHoldPattern,
134+
Expression(
135+
SymbolFormat, rule.pattern.expr, Symbol(key)
136+
),
137+
),
138+
rule.replace,
139+
)
140+
for rule in rules
141+
)
142+
)
143+
else:
144+
elements.extend(
145+
(
146+
Expression(
147+
SymbolRuleDelayed,
148+
Expression(
149+
SymbolHoldPattern,
150+
Expression(SymbolFormat, rule.pattern.expr),
151+
),
152+
rule.replace,
153+
)
154+
for rule in rules
155+
)
156+
)
157+
elements.sort()
158+
return ListExpression(*elements)
159+
119160
for rule in definition.get_values_list(position):
120161
if isinstance(rule, Rule):
121162
pattern = rule.pattern

0 commit comments

Comments
 (0)