Skip to content

Commit

Permalink
no-meanin JSON format changes slightly...
Browse files Browse the repository at this point in the history
Also, move OPERATOR_DATA to mathics.core.parser.operators
and pick up ROOT_DIR from mathics.settings
  • Loading branch information
rocky committed Nov 23, 2024
1 parent ad1c8af commit e715104
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion mathics/builtin/directories/system_directories.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from mathics.core.attributes import A_NO_ATTRIBUTES
from mathics.core.builtin import Predefined
from mathics.core.evaluation import Evaluation
from mathics.core.streams import ROOT_DIR
from mathics.eval.directories import INITIAL_DIR, SYS_ROOT_DIR, TMP_DIR
from mathics.settings import ROOT_DIR


class BaseDirectory_(Predefined):
Expand Down
11 changes: 7 additions & 4 deletions mathics/builtin/no_meaning.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,27 @@

import mathics.core.parser.operators
from mathics.core.builtin import (
OPERATOR_DATA,
NoMeaningInfixOperator,
NoMeaningPostfixOperator,
NoMeaningPrefixOperator,
)
from mathics.core.parser.operators import OPERATOR_DATA

# Generate no-meaning Mathics3 Builtin class from the operator name,
# affix, and Operator Unicode values found read from the JSON operators
# file.
# affix, and Operator Unicode values found in OPERATOR_DATA. This
# data ultimately comes from a YAML file in the MathicsScanner project
# which is processed into a JSON file.

for affix, format_fn, operator_base_class in (
("infix", "Infix", NoMeaningInfixOperator),
("postfix", "Postfix", NoMeaningPostfixOperator),
("prefix", "Prefix", NoMeaningPrefixOperator),
):
for operator_name, operator_string in OPERATOR_DATA[
for operator_name, operator_tuple in OPERATOR_DATA[
f"no-meaning-{affix}-operators"
].items():
# Create the Mathics3 Builtin class...
operator_string = operator_tuple[0]
generated_operator_class = type(
operator_name,
(operator_base_class,),
Expand Down
18 changes: 1 addition & 17 deletions mathics/core/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import importlib
import importlib.util
import os.path as osp
import re
from abc import ABC
from functools import total_ordering
Expand All @@ -27,7 +26,6 @@
)

import mpmath
import pkg_resources
import sympy

# Note: it is important *not* to use:
Expand Down Expand Up @@ -66,6 +64,7 @@
from mathics.core.interrupt import BreakInterrupt, ContinueInterrupt, ReturnInterrupt
from mathics.core.list import ListExpression
from mathics.core.number import PrecisionValueError, dps, get_precision, min_prec
from mathics.core.parser.operators import OPERATOR_DATA
from mathics.core.parser.util import PyMathicsDefinitions, SystemDefinitions
from mathics.core.pattern import BasePattern
from mathics.core.rules import BaseRule, FunctionApplyRule, Rule
Expand Down Expand Up @@ -93,21 +92,6 @@
from mathics.eval.scoping import dynamic_scoping
from mathics.eval.sympy import eval_sympy

try:
import ujson
except ImportError:
import json as ujson # type: ignore[no-redef]

ROOT_DIR = pkg_resources.resource_filename("mathics", "")

# Load the conversion tables from disk
operator_tables_path = osp.join(ROOT_DIR, "data", "operator-tables.json")
assert osp.exists(
operator_tables_path
), f"Internal error: Operator precedence tables are missing; expected to be in {operator_tables_path}"
with open(operator_tables_path, "r") as f:
OPERATOR_DATA = ujson.load(f)


# Exceptions...
class NegativeIntegerException(Exception):
Expand Down
3 changes: 1 addition & 2 deletions mathics/core/convert/op.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
import os.path as osp
from functools import lru_cache

import pkg_resources
from mathics.settings import ROOT_DIR

try:
import ujson
except ImportError:
import json as ujson # type: ignore[no-redef]

ROOT_DIR = pkg_resources.resource_filename("mathics", "")

# Load the conversion tables from disk
characters_path = osp.join(ROOT_DIR, "data", "op-tables.json")
Expand Down
17 changes: 17 additions & 0 deletions mathics/core/parser/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,25 @@
# -*- coding: utf-8 -*-


import os.path as osp
from collections import defaultdict

from mathics.settings import ROOT_DIR

try:
import ujson
except ImportError:
import json as ujson # type: ignore[no-redef]

# Load the conversion tables from disk
operator_tables_path = osp.join(ROOT_DIR, "data", "operator-tables.json")
assert osp.exists(
operator_tables_path
), f"Internal error: Operator precedence tables are missing; expected to be in {operator_tables_path}"
with open(operator_tables_path, "r") as f:
OPERATOR_DATA = ujson.load(f)


prefix_ops = {
"Get": 720,
"PreIncrement": 660,
Expand Down

0 comments on commit e715104

Please sign in to comment.