|
13 | 13 | from mathics_scanner import TranslateError |
14 | 14 |
|
15 | 15 | from mathics import settings |
16 | | -from mathics.core.expression import ensure_context, KeyComparable, SymbolAborted, SymbolList, SymbolNull |
| 16 | +from mathics.core.expression import ensure_context, KeyComparable, SymbolAborted, SymbolList, SymbolNull, String, Symbol |
17 | 17 |
|
18 | 18 | FORMATS = [ |
19 | 19 | "StandardForm", |
@@ -231,10 +231,23 @@ def __init__( |
231 | 231 | self, definitions=None, output=None, format="text", catch_interrupt=True |
232 | 232 | ) -> None: |
233 | 233 | from mathics.core.definitions import Definitions |
234 | | - from mathics.core.expression import Symbol |
235 | | - |
| 234 | + from mathics.core.expression import Symbol, String |
236 | 235 | if definitions is None: |
237 | 236 | definitions = Definitions() |
| 237 | + # This code is for debugging, to avoid to pass |
| 238 | + # through evaluation in format. |
| 239 | + from mathics.builtin.strings import ToString |
| 240 | + self.tostring = ToString(expression=False) |
| 241 | + tostropts = {'System`CharacterEncoding': String("Unicode"), |
| 242 | + 'System`FormatType': Symbol('OutputForm'), |
| 243 | + 'System`NumberMarks': Symbol('$NumberMarks'), |
| 244 | + 'System`PageHeight': Symbol('Infinity'), |
| 245 | + 'System`PageWidth': Symbol('Infinity'), |
| 246 | + 'System`TotalHeight': Symbol('Infinity'), |
| 247 | + 'System`TotalWidth': Symbol('Infinity') |
| 248 | + } |
| 249 | + self.tostring.options = tostropts |
| 250 | + ####### |
238 | 251 | self.definitions = definitions |
239 | 252 | self.recursion_depth = 0 |
240 | 253 | self.timeout = False |
@@ -437,23 +450,31 @@ def format_output(self, expr, format=None): |
437 | 450 |
|
438 | 451 | from mathics.core.expression import Expression, BoxError |
439 | 452 |
|
| 453 | + fmtsymbol = None |
440 | 454 | if format == "text": |
441 | | - result = expr.format(self, "System`OutputForm") |
| 455 | + fmtsymbol = Symbol("System`OutputForm") |
442 | 456 | elif format == "xml": |
443 | | - result = Expression("StandardForm", expr).format(self, "System`MathMLForm") |
| 457 | + fmtsymbol = Symbol("System`MathMLForm") |
444 | 458 | elif format == "tex": |
445 | | - result = Expression("StandardForm", expr).format(self, "System`TeXForm") |
| 459 | + fmtsymbol = Symbol("System`TeXForm") |
446 | 460 | elif format == "unformatted": |
447 | 461 | self.exc_result = None |
448 | 462 | return expr |
449 | 463 | else: |
450 | 464 | raise ValueError |
451 | | - |
452 | 465 | try: |
453 | | - boxes = result.boxes_to_text(evaluation=self) |
| 466 | + hfexpr = Expression("HoldForm", expr) |
| 467 | + # The next uncommented lines are just for debug. Eventually, |
| 468 | + # we can go bach to the commented line... |
| 469 | + # Expression("ToString", hfexpr, fmtsymbol).evaluate(self) |
| 470 | + opts = self.tostring.options |
| 471 | + result = self.tostring.apply_form(hfexpr, fmtsymbol, self, opts) |
| 472 | + boxes = result.value if result is not None else None |
454 | 473 | except BoxError: |
| 474 | + result = self.tostring.apply_form(hfexpr, Symbol("FullForm"), self, opts) |
455 | 475 | self.message( |
456 | | - "General", "notboxes", Expression("FullForm", result).evaluate(self) |
| 476 | + "General", "notboxes", |
| 477 | + Expression("MakeBoxes", result, fmtsymbol) |
457 | 478 | ) |
458 | 479 | boxes = None |
459 | 480 | return boxes |
|
0 commit comments