still fleshing-out ternaries

This commit is contained in:
Alex Thannhauser 2025-06-10 14:51:57 -05:00
parent 98bf135199
commit 04d997c031

43
main.py
View file

@ -150,6 +150,21 @@ def pretty(exp, in_color = False, depth = 0):
retval += start_color + ")" + end_color;
case ("?:", cond, left, right):
retval += start_color + "(" + end_color;
retval += pretty(cond, in_color, depth + 1);
retval += " " + start_color + "?" + end_color + " ";
retval += pretty(left, in_color, depth + 1);
retval += " " + start_color + ":" + end_color + " ";
retval += pretty(right, in_color, depth + 1);
retval += start_color + ")" + end_color;
case _:
print(exp);
assert(not "TODO");
@ -254,6 +269,8 @@ def calculate_simplifications(args, available_operators):
assert(todo_count[0] <= 65536);
print([len(x) for x in todo]);
my_truthtable = min(truthtables);
truthtables.discard(my_truthtable);
my_cost = min_cost;
@ -318,7 +335,8 @@ def calculate_simplifications(args, available_operators):
for name, function in sorted(ternary_operators.items()):
if name in available_operators:
s = sorted(lookup.items());
for a_truthtable, a_expression in s:
for i, (a_truthtable, a_expression) in enumerate(s):
print(f'i = {i}');
for b_truthtable, b_expression in s:
# x ? y : z
ternary_truthtable = function(
@ -355,6 +373,15 @@ def get_simplifications(args, available_operators):
print("Oh! looks like you're running this for the first time");
print("I'll have to build up my cache of simplifications");
print("This may take a while.");
if "?:" in available_operators:
print();
print("You have selected ternary operators, so this WILL "
"take time. Weeks.");
print();
print("I'll only have to do this once.");
if not args.yes:
@ -381,20 +408,6 @@ def get_simplifications(args, available_operators):
return cache[available_operators];
# def create_parser():
# from pyparsing import infixNotation, opAssoc, Word, oneOf
#
# # this can't handle parenthesis very well...
# # do I really have to write my own parser?
# root = infixNotation(Word("01wxyz"), [
# ('!', 1, opAssoc.RIGHT),
# (oneOf('< <= > >='), 2, opAssoc.LEFT),
# (oneOf('== !='), 2, opAssoc.LEFT),
# (oneOf('&& !& &!'), 2, opAssoc.LEFT),
# (oneOf('|| !| |!'), 2, opAssoc.LEFT)]);
#
# return root;
def parse(text):
class Tokenizer:
def __init__(self, text):