Skip to content

Commit

Permalink
change no-meaning-operator from set to dict...
Browse files Browse the repository at this point in the history
For each (binary?) no-meaning operator we had just the name. Now we have
a mapping from the operator name to its unicode equivalent as found in
the character table.
  • Loading branch information
rocky committed Nov 19, 2024
1 parent 31de957 commit 4cf6cde
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion mathics_scanner/data/operators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6610,7 +6610,7 @@ Star:
# N-tokens: {}
# L-tokens: {"⋆"}
# O-tokens: {}
# usage: "expr1 ⋆ expr2 * expr3"
# usage: "expr1 ⋆ expr2 expr3"
# parse: {"Star", "[", "expr1", ",", "expr2", "]"}
FullForm: Star[expr1, expr2]
arity: Binary
Expand Down
36 changes: 23 additions & 13 deletions mathics_scanner/generate/build_operator_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os.path as osp
import sys
from pathlib import Path
from typing import Dict, Union
from typing import Dict

import click
import yaml
Expand Down Expand Up @@ -41,26 +41,35 @@ def read(*rnames) -> str:
return open(osp.join(get_srcdir(), *rnames)).read()


def compile_tables(data: Dict[str, dict]) -> Dict[str, Union[dict, tuple]]:
def compile_tables(
operator_data: Dict[str, dict], character_data: Dict[str, dict]
) -> Dict[str, dict]:
"""
Compiles the general table into the tables used internally by the library.
This facilitates fast access of this information by clients needing this
information.
"""
operator_precedence = {}

for k, v in data.items():
for k, v in operator_data.items():
operator_precedence[k] = v["precedence"]

no_meaning_operators = set()
no_meaning_operators = {}

for k, v in data.items():
if v.get("meaningful", True) is False:
no_meaning_operators.add(k)
for operator_name, v in operator_data.items():
if (
v.get("meaningful", True) is False
and (character_info := character_data.get(operator_name))
and (
character_info
and (unicode_char := character_info.get("unicode-equivalent"))
)
):
no_meaning_operators[operator_name] = unicode_char

return {
"operator-precedence": operator_precedence,
"no-meaning-operators": tuple(sorted(no_meaning_operators)),
"no-meaning-operators": no_meaning_operators,
}


Expand All @@ -80,14 +89,15 @@ def compile_tables(data: Dict[str, dict]) -> Dict[str, Union[dict, tuple]]:
"data_dir", type=click.Path(readable=True), default=DEFAULT_DATA_DIR, required=False
)
def main(output, data_dir):
with open(data_dir / "operators.yml", "r", encoding="utf8") as i, open(
output, "w"
) as o:
with open(data_dir / "operators.yml", "r", encoding="utf8") as operator_f, open(
data_dir / "named-characters.yml", "r", encoding="utf8"
) as character_f, open(output, "w") as o:
# Load the YAML data.
data = yaml.load(i, Loader=yaml.FullLoader)
operator_data = yaml.load(operator_f, Loader=yaml.FullLoader)
character_data = yaml.load(character_f, Loader=yaml.FullLoader)

# Precompile the tables.
data = compile_tables(data)
data = compile_tables(operator_data, character_data)

# Dump the preprocessed dictionaries to disk as JSON.
json.dump(data, o)
Expand Down

0 comments on commit 4cf6cde

Please sign in to comment.