Skip to content

Commit

Permalink
Use Mathics3 precedence; add misc_op table...
Browse files Browse the repository at this point in the history
Add Infix
  • Loading branch information
rocky committed Nov 23, 2024
1 parent 7369794 commit 7469472
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 49 deletions.
1 change: 1 addition & 0 deletions mathics_scanner/data/named-characters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,7 @@ DirectedEdge:
esc-alias: de
has-unicode-inverse: false
is-letter-like: false
operator-name: DirectedEdge
unicode-equivalent: "\u2192"
unicode-equivalent-name: RIGHTWARDS ARROW
unicode-reference: https://www.compart.com/en/unicode/U+2192
Expand Down
85 changes: 52 additions & 33 deletions mathics_scanner/data/operators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
# - left
# - non-associative
# - right
# - unknown
#
#
#
Expand Down Expand Up @@ -136,7 +137,7 @@ And:
# HACK: although the should be 215 for all boolean_ops we adjust slightly
# to get the subprecedences correct
Precedence-Function: 225
precedence: 220
precedence: 225
WolframLanguageData: 55
WolframLanguageData-corrected: 55
UnicodeCharacters.tr: 330
Expand Down Expand Up @@ -176,7 +177,7 @@ AngleBracket:
Apply:
actual-precedence: 820
Precedence-Function: 620
precedence: 626
precedence: 620
WolframLanguageData: 16
WolframLanguageData-corrected: 16
UnicodeCharacters.tr:
Expand Down Expand Up @@ -595,7 +596,7 @@ CircleMinus:
FullForm: CircleMinus[expr1, expr2]
arity: Binary
affix: Infix
associativity: left
associativity: null
meaningful: false
# comments:

Expand Down Expand Up @@ -679,7 +680,7 @@ Colon:
Composition:
actual-precedence: 860
Precedence-Function: 625
precedence: 655
precedence: 625
WolframLanguageData: 13
WolframLanguageData-corrected: 13
UnicodeCharacters.tr:
Expand Down Expand Up @@ -794,7 +795,7 @@ Congruent:
Conjugate:
actual-precedence: 780
Precedence-Function: 670
precedence: 605
precedence: 670
WolframLanguageData: 18
WolframLanguageData-corrected: 18
UnicodeCharacters.tr: 695
Expand All @@ -814,7 +815,7 @@ Conjugate:
ConjugateTranspose:
actual-precedence: 780
Precedence-Function: 670
precedence: 605
precedence: 670
WolframLanguageData: 18
WolframLanguageData-corrected: 18
UnicodeCharacters.tr: 695
Expand Down Expand Up @@ -1050,7 +1051,7 @@ CurlyQuote:
Decrement:
actual-precedence: 880
Precedence-Function: 660
precedence: 665
precedence: 660
WolframLanguageData: 11
WolframLanguageData-corrected: 11
UnicodeCharacters.tr:
Expand Down Expand Up @@ -1178,7 +1179,7 @@ DirectedEdge:
FullForm: DirectedEdge[expr1, expr2]
arity: Binary
affix: Infix
associativity: non-associative
associativity: null
meaningful: true
# comments:

Expand Down Expand Up @@ -2075,7 +2076,7 @@ FormBox:
# parse: {"FormBox", "[", "expr2", ",", "expr1", "]"}
arity: Binary
affix: Infix
associativity: missing["unknown"]
associativity: "unknown"
meaningful: true
# comments:

Expand Down Expand Up @@ -2540,7 +2541,7 @@ Implies:
Increment:
actual-precedence: 880
Precedence-Function: 660
precedence: 665
precedence: 660
WolframLanguageData: 11
WolframLanguageData-corrected: 11
UnicodeCharacters.tr:
Expand All @@ -2557,9 +2558,27 @@ Increment:
meaningful: true
# comments:

Infix:
precedence: 630
WolframLanguageData: None
WolframLanguageData-corrected: None
UnicodeCharacters.tr: None
UnicodeCharacters-corrected.tr: None
# N-tokens: None
# L-tokens: None
# O-tokens: None
usage: x ~ f ~ y
# parse: Infix[f[x, y]]
FullForm: None
arity: Ternary
affix: Infix
associativity: null
meaningful: true
# comments: None

Information:
actual-precedence: 670
precedence: 670
precedence: 5001 # Seems a bit extreme. This is the old Mathics data
WolframLanguageData: None
WolframLanguageData-corrected: None
UnicodeCharacters.tr: None
Expand All @@ -2570,8 +2589,8 @@ Information:
# usage: "None"
# parse: None
FullForm: None
arity: Binary
affix: Infix
arity: Unary
affix: Prefix
associativity: null
meaningful: true
# comments: None
Expand Down Expand Up @@ -3682,7 +3701,7 @@ NamedBlankSequenceHead:
Nand:
actual-precedence: 290
Precedence-Function: 215
precedence: 220
precedence: 225
WolframLanguageData: 55
WolframLanguageData-corrected: 55
UnicodeCharacters.tr: 330
Expand Down Expand Up @@ -3759,7 +3778,7 @@ NonCommutativeMultiply:
Nor:
actual-precedence: 270
Precedence-Function: 215
precedence: 210
precedence: 215
WolframLanguageData: 57
WolframLanguageData-corrected: 57
UnicodeCharacters.tr: 320
Expand Down Expand Up @@ -4884,7 +4903,7 @@ Optional:
Or:
actual-precedence: 270
Precedence-Function: 215
precedence: 210
precedence: 215
WolframLanguageData: 57
WolframLanguageData-corrected: 57
UnicodeCharacters.tr: 320
Expand Down Expand Up @@ -4956,7 +4975,7 @@ OverscriptBox:
# parse: {"OverscriptBox", "[", "expr1", ",", "expr2", "]"}
arity: Binary
affix: Infix
associativity: missing["unknown"]
associativity: "unknown"
meaningful: true
# comments:

Expand Down Expand Up @@ -5189,7 +5208,7 @@ PatternTest:
FullForm: PatternTest[expr1, expr2]
arity: Binary
affix: Infix
associativity: non-associative
associativity: null
meaningful: true
# comments:

Expand Down Expand Up @@ -5292,7 +5311,7 @@ PlusMinus:

Postfix:
actual-precedence: 640
precedence: 640
precedence: 70 # Seems weird but this is what old Mathics code has
WolframLanguageData: None
WolframLanguageData-corrected: None
UnicodeCharacters.tr: None
Expand All @@ -5305,7 +5324,7 @@ Postfix:
FullForm: None
arity: Binary
affix: Infix
associativity: null
associativity: left
meaningful: true
# comments: None

Expand Down Expand Up @@ -6171,7 +6190,7 @@ RuleDelayed:
SameQ:
actual-precedence: 330
Precedence-Function: 290
precedence: 260
precedence: 290
WolframLanguageData: 51
WolframLanguageData-corrected: 51
UnicodeCharacters.tr:
Expand Down Expand Up @@ -6427,7 +6446,7 @@ Span:
# N-tokens: {";;"}
# L-tokens: {";;"}
# O-tokens: {";;"}
# usage: "i", ";;", "j ;; k"
usage: ;;, i;;,; ;;j, ;;;;k, i;;j, i;;;;k, ;;j;;k i;;j;;k
# parse: {"Span", "[", "i", ",", "j", ",", "k", "]"}
FullForm: Span[i, j, k]
arity: Ternary
Expand Down Expand Up @@ -6682,7 +6701,7 @@ SubscriptBox:
FullForm:
arity: Binary
affix: Infix
associativity: missing["unknown"]
associativity: "unknown"
meaningful: true
# comments:

Expand Down Expand Up @@ -7199,11 +7218,11 @@ Times:
# N-tokens: {}
# L-tokens: {"*", "×", "
# O-tokens: {}
# usage: "{{"expr1", "*", "expr2"}, {"expr1", "expr2"}, {"expr1", "×", "expr2"}, {"expr1", ""
usage: x * y, x × y, x y,
# parse: {"Times", "[", "expr1", ",", "expr2", "]"}
FullForm: Times[expr1, expr2]
arity: Nullary
affix: None
arity: Binary
affix: Infix
associativity: null
meaningful: true
# comments: Operator Notations includes usages with invisible unicode characters.
Expand Down Expand Up @@ -7250,7 +7269,7 @@ TortoiseShellBracket:
Transpose:
actual-precedence: 780
Precedence-Function: 670
precedence: 605
precedence: 670
WolframLanguageData: 18
WolframLanguageData-corrected: 18
UnicodeCharacters.tr: 695
Expand Down Expand Up @@ -7382,7 +7401,7 @@ UnderscriptBox:
FullForm:
arity: Binary
affix: Infix
associativity: missing["unknown"]
associativity: "unknown"
meaningful: true
# comments:

Expand All @@ -7401,7 +7420,7 @@ UndirectedEdge:
FullForm: UndirectedEdge[u, v]
arity: Binary
affix: Infix
associativity: non-associative
associativity: null
meaningful: true
# comments:

Expand Down Expand Up @@ -7465,7 +7484,7 @@ UnionPlus:
UnsameQ:
actual-precedence: 330
Precedence-Function: 290
precedence: 260
precedence: 290
WolframLanguageData: 51
WolframLanguageData-corrected: 51
UnicodeCharacters.tr:
Expand All @@ -7485,7 +7504,7 @@ UnsameQ:
Unset:
actual-precedence: 50
Precedence-Function: 670
precedence: 40
precedence: 670
WolframLanguageData: 75
WolframLanguageData-corrected: 75
UnicodeCharacters.tr:
Expand Down Expand Up @@ -7828,7 +7847,7 @@ WhiteCornerBracket:

Xnor:
actual-precedence: 280
precedence: 215
precedence: 220
WolframLanguageData: 56
WolframLanguageData-corrected: 56
UnicodeCharacters.tr: 325
Expand All @@ -7847,7 +7866,7 @@ Xnor:

Xor:
actual-precedence: 280
precedence: 215
precedence: 220
WolframLanguageData: 56
WolframLanguageData-corrected: 56
UnicodeCharacters.tr: 325
Expand Down
40 changes: 24 additions & 16 deletions mathics_scanner/generate/build_operator_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def compile_tables(

flat_binary_operators = {}
left_binary_operators = {}
misc_operators = {}
no_meaning_infix_operators = {}
no_meaning_postfix_operators = {}
no_meaning_prefix_operators = {}
Expand All @@ -66,19 +67,18 @@ def compile_tables(
ternary_operators = {}

for operator_name, operator_info in operator_data.items():
character_info = character_data.get(operator_name)

if character_info is None:
continue

precedence = operator_info["precedence"]
unicode_char = character_info.get("unicode-equivalent")

affix = operator_info["affix"]
arity = operator_info["arity"]
operator_dict = None

if affix in ("Infix", "Binary"):
associativity = operator_info["associativity"]
associativity = operator_info["associativity"]
if arity == "Ternary":
operator_dict = ternary_operators
elif associativity == "unknown":
operator_dict = misc_operators
elif affix in ("Infix", "Binary"):
if associativity is None:
operator_dict = flat_binary_operators
elif associativity == "left":
Expand All @@ -96,16 +96,23 @@ def compile_tables(
operator_dict = prefix_operators
elif affix == "Postfix":
operator_dict = postfix_operators
elif affix == "Ternaryhh":
operator_dict = ternary_operators

character_info = character_data.get(operator_name)
if character_info is None:
unicode_char = "no-unicode"
else:
unicode_char = character_info.get("unicode-equivalent", "no-unicode")

if operator_dict is not None:
operator_dict[operator_name] = unicode_char, precedence

if character_info is None:
continue

if operator_info.get("meaningful", True) is False and (
character_data.get(operator_name)
):
if unicode_char is None:
if unicode_char == "no-unicode":
if (unicode_char := character_info.get("wl-unicode")) is None:
print(f"FIXME: no unicode or WMA equivalent for {operator_name}")
continue
Expand All @@ -121,15 +128,16 @@ def compile_tables(
print(f"FIXME: affix {affix} of {operator_name} not handled")

return {
"operator-precedence": operator_precedence,
"flat-binary-operators": flat_binary_operators,
"left-binary-operators": left_binary_operators,
"misc-ops": misc_operators,
"no-meaning-infix-operators": no_meaning_infix_operators,
"no-meaning-postfix-operators": no_meaning_postfix_operators,
"non-associative-binary-operators": nonassoc_binary_operators,
"flat-binary-operators": flat_binary_operators,
"right-binary-operators": right_binary_operators,
"left-binary-operators": left_binary_operators,
"operator-precedence": operator_precedence,
"postfix-operators": postfix_operators,
"prefix-operators": prefix_operators,
"postfix_operators": postfix_operators,
"right-binary-operators": right_binary_operators,
"ternary-operators": ternary_operators,
}

Expand Down

0 comments on commit 7469472

Please sign in to comment.