From 04d997c03186d1f0f1504411a4f067b1f7c8994f Mon Sep 17 00:00:00 2001 From: Alex Thannhauser Date: Tue, 10 Jun 2025 14:51:57 -0500 Subject: [PATCH] still fleshing-out ternaries --- main.py | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index c222f00..58e98ed 100755 --- a/main.py +++ b/main.py @@ -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):