|
| 1 | +from collections.abc import Hashable |
| 2 | +from textwrap import dedent |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from .finch_logic import ( |
| 6 | + Alias, |
| 7 | + Deferred, |
| 8 | + Field, |
| 9 | + Immediate, |
| 10 | + LogicNode, |
| 11 | + MapJoin, |
| 12 | + Query, |
| 13 | + Reformat, |
| 14 | + Relabel, |
| 15 | + Reorder, |
| 16 | + Subquery, |
| 17 | + Table, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def get_or_insert(dictionary: dict[Hashable, Any], key: Hashable, default: Any) -> Any: |
| 22 | + if key in dictionary: |
| 23 | + return dictionary[key] |
| 24 | + dictionary[key] = default |
| 25 | + return default |
| 26 | + |
| 27 | + |
| 28 | +def get_structure(node: LogicNode, fields: dict[str, LogicNode], aliases: dict[str, LogicNode]) -> LogicNode: |
| 29 | + match node: |
| 30 | + case Field(name): |
| 31 | + return get_or_insert(fields, name, Immediate(len(fields) + len(aliases))) |
| 32 | + case Alias(name): |
| 33 | + return get_or_insert(aliases, name, Immediate(len(fields) + len(aliases))) |
| 34 | + case Subquery(Alias(name) as lhs, arg): |
| 35 | + if name in aliases: |
| 36 | + return aliases[name] |
| 37 | + return Subquery(get_structure(lhs, fields, aliases), get_structure(arg, fields, aliases)) |
| 38 | + case Table(tns, idxs): |
| 39 | + return Table(Immediate(type(tns.val)), tuple(get_structure(idx, fields, aliases) for idx in idxs)) |
| 40 | + case any if any.is_tree(): |
| 41 | + return any.from_arguments(*[get_structure(arg, fields, aliases) for arg in any.get_arguments()]) |
| 42 | + case _: |
| 43 | + return node |
| 44 | + |
| 45 | + |
| 46 | +class PointwiseLowerer: |
| 47 | + def __init__(self): |
| 48 | + self.bound_idxs = [] |
| 49 | + |
| 50 | + def __call__(self, ex): |
| 51 | + match ex: |
| 52 | + case MapJoin(Immediate(val), args): |
| 53 | + return f":({val}({','.join([self(arg) for arg in args])}))" |
| 54 | + case Reorder(Relabel(Alias(name), idxs_1), idxs_2): |
| 55 | + self.bound_idxs.append(idxs_1) |
| 56 | + return f":({name}[{','.join([idx.name if idx in idxs_2 else 1 for idx in idxs_1])}])" |
| 57 | + case Reorder(Immediate(val), _): |
| 58 | + return val |
| 59 | + case Immediate(val): |
| 60 | + return val |
| 61 | + case _: |
| 62 | + raise Exception(f"Unrecognized logic: {ex}") |
| 63 | + |
| 64 | + |
| 65 | +def compile_pointwise_logic(ex: LogicNode) -> tuple: |
| 66 | + ctx = PointwiseLowerer() |
| 67 | + code = ctx(ex) |
| 68 | + return (code, ctx.bound_idxs) |
| 69 | + |
| 70 | + |
| 71 | +def compile_logic_constant(ex: LogicNode) -> str: |
| 72 | + match ex: |
| 73 | + case Immediate(val): |
| 74 | + return val |
| 75 | + case Deferred(ex, type_): |
| 76 | + return f":({ex}::{type_})" |
| 77 | + case _: |
| 78 | + raise Exception(f"Invalid constant: {ex}") |
| 79 | + |
| 80 | + |
| 81 | +def intersect(x1: tuple, x2: tuple) -> tuple: |
| 82 | + return tuple(x for x in x1 if x in x2) |
| 83 | + |
| 84 | + |
| 85 | +def with_subsequence(x1: tuple, x2: tuple) -> tuple: |
| 86 | + res = list(x2) |
| 87 | + indices = [idx for idx, val in enumerate(x2) if val in x1] |
| 88 | + for idx, i in enumerate(indices): |
| 89 | + res[i] = x1[idx] |
| 90 | + return tuple(res) |
| 91 | + |
| 92 | + |
| 93 | +class LogicLowerer: |
| 94 | + def __init__(self, mode: str = "fast"): |
| 95 | + self.mode = mode |
| 96 | + |
| 97 | + def __call__(self, ex: LogicNode): |
| 98 | + match ex: |
| 99 | + case Query(Alias(name), Table(tns, _)): |
| 100 | + return f":({name} = {compile_logic_constant(tns)})" |
| 101 | + |
| 102 | + case Query(Alias(_) as lhs, Reformat(tns, Reorder(Relabel(Alias(_) as arg, idxs_1), idxs_2))): |
| 103 | + loop_idxs = [idx.name for idx in with_subsequence(intersect(idxs_1, idxs_2), idxs_2)] |
| 104 | + lhs_idxs = [idx.name for idx in idxs_2] |
| 105 | + (rhs, rhs_idxs) = compile_pointwise_logic(Reorder(Relabel(arg, idxs_1), idxs_2)) |
| 106 | + body = f":({lhs.name}[{','.join(lhs_idxs)}] = {rhs})" |
| 107 | + for idx in loop_idxs: |
| 108 | + if Field(idx) in rhs_idxs: |
| 109 | + body = f":(for {idx} = _ \n {body} end)" |
| 110 | + elif idx in lhs_idxs: |
| 111 | + body = f":(for {idx} = 1:1 \n {body} end)" |
| 112 | + |
| 113 | + result = f"""\ |
| 114 | + quote |
| 115 | + {lhs.name} = {compile_logic_constant(tns)} |
| 116 | + @finch mode = {self.mode} begin |
| 117 | + {lhs.name} .= {tns.fill_value} |
| 118 | + {body} |
| 119 | + return {lhs.name} |
| 120 | + end |
| 121 | + end |
| 122 | + """ |
| 123 | + return dedent(result) |
| 124 | + |
| 125 | + # TODO: ... |
| 126 | + |
| 127 | + case _: |
| 128 | + raise Exception(f"Unrecognized logic: {ex}") |
| 129 | + |
| 130 | + |
| 131 | +class LogicCompiler: |
| 132 | + def __init__(self): |
| 133 | + self.ll = LogicLowerer() |
| 134 | + |
| 135 | + def __call__(self, prgm): |
| 136 | + # prgm = format_queries(prgm, True) # noqa: F821 |
| 137 | + return self.ll(prgm) |
0 commit comments