|
| 1 | +import utils |
| 2 | +import tython |
| 3 | +import logging |
| 4 | +from typing import List, Dict, Callable, Tuple, Generator, Set, Sequence |
| 5 | +from tython import Program, nt |
| 6 | + |
| 7 | +logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +def extract_constants(prog) -> Dict: |
| 11 | + ''' |
| 12 | + Extract all constants from program. Does not (yet) allow copying of comprehensions, e.g., '[i*i for i in range(10)]' |
| 13 | + ''' |
| 14 | + |
| 15 | + from collections import defaultdict |
| 16 | + consts = defaultdict(list) |
| 17 | + |
| 18 | + def handle_args(args_node): |
| 19 | + |
| 20 | + if args_node.rule.name == 'cast:ARGS': |
| 21 | + handle_args(args_node.children[0]) |
| 22 | + else: |
| 23 | + if len(args_node.children) >= 3 and args_node.children[1].nt == nt.TYPE: |
| 24 | + annotation_node = args_node.children[1] |
| 25 | + t = nt.type2nt(eval(annotation_node.src())) |
| 26 | + consts[t].append(args_node.children[0]) |
| 27 | + if args_node.children and args_node.children[-1].nt in {nt.ARGS, nt.DEFAULT_ARGS}: |
| 28 | + handle_args(args_node.children[-1]) |
| 29 | + |
| 30 | + def helper(node): |
| 31 | + if node.rule.name == 'def': # it's a function |
| 32 | + name_node, args_node, body_node = node.children |
| 33 | + if name_node.src() == 'sat': |
| 34 | + handle_args(args_node.children[-1]) # skip first arg for `def sat` |
| 35 | + else: |
| 36 | + handle_args(args_node) |
| 37 | + helper(body_node) |
| 38 | + return False |
| 39 | + elif node.nt in {nt.NAME}: |
| 40 | + return False |
| 41 | + elif node.nt in {nt.STMT}: |
| 42 | + for c in node.children: |
| 43 | + helper(c) |
| 44 | + return False |
| 45 | + if node.rule.name not in {"int-const", "str-const"} and not all([helper(c) for c in node.children]): |
| 46 | + return False |
| 47 | + if node.nt.isa(nt.LIST, nt.SET, nt.DICT, nt.TUPLE, nt.RANGE, |
| 48 | + nt.INT, nt.FLOAT, nt.BOOL, nt.STR): |
| 49 | + consts[node.nt].append(node) |
| 50 | + return True |
| 51 | + |
| 52 | + if prog is not None: |
| 53 | + helper(prog.tree) |
| 54 | + |
| 55 | + return dict(consts) |
| 56 | + |
| 57 | +# |
| 58 | +# q = Program(""" |
| 59 | +# def sat(i: List[str], a=5): |
| 60 | +# return i==['5'] |
| 61 | +# """) |
| 62 | +# |
| 63 | +# extract_constants(q) |
| 64 | +# |
| 65 | +# |
| 66 | +# %% |
| 67 | +class Solution(): |
| 68 | + def __init__(self, string=None, prog=None, likelihood=None, time=None, count=None): |
| 69 | + self.string = string |
| 70 | + self.prog = prog |
| 71 | + self.likelihood = likelihood |
| 72 | + self.time = time |
| 73 | + self.count = count |
| 74 | + |
| 75 | + |
| 76 | +class SolverSolution(Solution): |
| 77 | + def __init__(self, string=None, prog=None, likelihood=None, time=None, count=None): |
| 78 | + super().__init__(string=string, prog=prog, likelihood=likelihood) |
| 79 | + self.time = time |
| 80 | + self.count = count |
| 81 | + |
| 82 | + |
| 83 | +def get_arg_type_str(sat_str): |
| 84 | + assert sat_str.startswith("def sat(") and ":" in sat_str |
| 85 | + depth = 0 |
| 86 | + for i, c in enumerate(sat_str): |
| 87 | + if c == '[': |
| 88 | + depth += 1 |
| 89 | + elif c == ']': |
| 90 | + depth -= 1 |
| 91 | + elif c in ")," and depth == 0: |
| 92 | + return sat_str[sat_str.index(":") + 1:i].lstrip() |
| 93 | + assert False |
| 94 | + |
| 95 | + |
| 96 | +class Challenge(): |
| 97 | + def __init__(self, challenge_config, max_ticks=100000000): |
| 98 | + self.name = challenge_config["name"] |
| 99 | + self.f_str = challenge_config["sat"] |
| 100 | + self.type_str = get_arg_type_str(challenge_config["sat"]) |
| 101 | + self.type = eval(self.type_str) |
| 102 | + self.gold_solutions = [] |
| 103 | + self.solver_solutions = [] |
| 104 | + for sol in challenge_config["sols"]: |
| 105 | + self.gold_solutions.append(Solution(string=sol)) |
| 106 | + if "sol_tries" in challenge_config: |
| 107 | + for i, x in enumerate(challenge_config["sol_tries"]): |
| 108 | + self.gold_solutions[i].count = x |
| 109 | + |
| 110 | + if "sol_time" in challenge_config: |
| 111 | + for i, x in enumerate(challenge_config["sol_time"]): |
| 112 | + self.gold_solutions[i].time = x |
| 113 | + |
| 114 | + self.solution_strs = challenge_config["sols"] |
| 115 | + self.max_ticks = max_ticks |
| 116 | + |
| 117 | + self._parse_challenge() |
| 118 | + |
| 119 | + def _parse_challenge(self): |
| 120 | + ''' |
| 121 | + Converts the challenge string to a tython program. |
| 122 | + ''' |
| 123 | + self.sol_kind = tython.nt.type2nt(self.type) |
| 124 | + self.prog = None |
| 125 | + self.f = None |
| 126 | + try: |
| 127 | + self.prog = tython.Program( |
| 128 | + self.f_str) |
| 129 | + self.f = self.prog.run(max_ticks=self.max_ticks) |
| 130 | + except Program.EvalException as e: |
| 131 | + logger.warning(f"Exception evaluating {self.name} '{self.f_str}': {e}") |
| 132 | + except Exception as e: |
| 133 | + logger.warning(f"Exception parsing {self.name} '{self.f_str}': {e}") |
0 commit comments