Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions mathics/builtin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ def init(self, *args, **kwargs):
pass


class AtomBuiltin(Builtin):
# allows us to define apply functions, rules, messages, etc. for Atoms
# which are by default not in the definitions' contribution pipeline.
# see Image[] for an example of this.

def get_name(self):
name = super(AtomBuiltin, self).get_name()
return re.sub(r"Atom$", "", name)


class Operator(Builtin):
operator = None
precedence = None
Expand All @@ -253,6 +263,7 @@ def get_operator_display(self):
else:
return self.operator


class Predefined(Builtin):
def get_functions(self, prefix='apply'):
functions = list(super(Predefined, self).get_functions(prefix))
Expand Down
19 changes: 2 additions & 17 deletions mathics/builtin/inout.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,15 +421,7 @@ def apply_general(self, expr, f, evaluation):
f:TraditionalForm|StandardForm|OutputForm|InputForm|FullForm]'''

if expr.is_atom():
x = expr
if isinstance(x, Symbol):
return String(evaluation.definitions.shorten_name(x.name))
elif isinstance(x, String):
return String('"' + six.text_type(x.value) + '"')
elif isinstance(x, (Integer, Real)):
return x.make_boxes(f.get_name())
elif isinstance(x, (Rational, Complex)):
return x.format(evaluation, f.get_name())
return expr.atom_to_boxes(f, evaluation)
else:
head = expr.head
leaves = expr.leaves
Expand Down Expand Up @@ -468,14 +460,7 @@ def _apply_atom(self, x, f, evaluation):
'''MakeBoxes[x_?AtomQ,
f:TraditionalForm|StandardForm|OutputForm|InputForm|FullForm]'''

if isinstance(x, Symbol):
return String(evaluation.definitions.shorten_name(x.name))
elif isinstance(x, String):
return String('"' + x.value + '"')
elif isinstance(x, (Integer, Real)):
return x.make_boxes(f.get_name())
elif isinstance(x, (Rational, Complex)):
return x.format(evaluation, f.get_name())
return x.atom_to_boxes(f, evaluation)

def apply_outerprecedenceform(self, expr, prec, f, evaluation):
'''MakeBoxes[OuterPrecedenceForm[expr_, prec_],
Expand Down
21 changes: 21 additions & 0 deletions mathics/core/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,9 @@ def get_sort_key(self, pattern_sort=False):
def get_atoms(self, include_heads=True):
return [self]

def atom_to_boxes(self, f, evaluation):
raise NotImplementedError


class Symbol(Atom):
def __init__(self, name, sympy_dummy=None, **kwargs):
Expand All @@ -1317,6 +1320,9 @@ def do_copy(self):
def boxes_to_text(self, **options):
return str(self.name)

def atom_to_boxes(self, f, evaluation):
return String(evaluation.definitions.shorten_name(self.name))

def to_sympy(self, **kwargs):
from mathics.builtin import mathics_to_sympy

Expand Down Expand Up @@ -1501,6 +1507,9 @@ def boxes_to_tex(self, **options):
def make_boxes(self, form):
return String(str(self.value))

def atom_to_boxes(self, f, evaluation):
return self.make_boxes(f.get_name())

def default_format(self, evaluation, form):
return str(self.value)

Expand Down Expand Up @@ -1553,6 +1562,9 @@ def __getstate__(self):
def __setstate__(self, dict):
self.value = sympy.Rational(dict['value'])

def atom_to_boxes(self, f, evaluation):
return self.format(evaluation, f.get_name())

def to_sympy(self, **kwargs):
return self.value

Expand Down Expand Up @@ -1670,6 +1682,9 @@ def make_boxes(self, form):
_number_form_options['_Form'] = form # passed to _NumberFormat
return number_form(self, dps(self.prec), None, None, _number_form_options)

def atom_to_boxes(self, f, evaluation):
return self.make_boxes(f.get_name())

def to_sympy(self, **kwargs):
return self.value

Expand Down Expand Up @@ -1761,6 +1776,9 @@ def __init__(self, real, imag, p=None, **kwargs):
self.value = (self.real, self.imag)
self.prec = p

def atom_to_boxes(self, f, evaluation):
return self.format(evaluation, f.get_name())

def to_sympy(self, **kwargs):
return self.sympy

Expand Down Expand Up @@ -1980,6 +1998,9 @@ def boxes_to_tex(self, show_string_characters=False, **options):
else:
return encode_tex(text)

def atom_to_boxes(self, f, evaluation):
return String('"' + six.text_type(self.value) + '"')

def do_copy(self):
return String(self.value)

Expand Down