|
| 1 | +from io import StringIO |
| 2 | +from dataclasses import field, dataclass |
| 3 | + |
| 4 | +import stmts |
| 5 | +from group import beer |
| 6 | +from dialect import dialect |
| 7 | + |
| 8 | +from kirin import ir, interp |
| 9 | +from lattice import Item, ItemPints, AtLeastXItem, ConstIntItem |
| 10 | +from kirin.emit import EmitStr, EmitStrFrame |
| 11 | +from kirin.dialects import func |
| 12 | +from kirin.emit.exceptions import EmitError |
| 13 | + |
| 14 | + |
| 15 | +def default_menu_price(): |
| 16 | + return { |
| 17 | + "budlight": 3.0, |
| 18 | + "heineken": 4.0, |
| 19 | + "tsingdao": 2.0, |
| 20 | + } |
| 21 | + |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class EmitReceptMain(EmitStr): |
| 25 | + keys = ["emit.recept"] |
| 26 | + dialects: ir.DialectGroup = field(default=beer) |
| 27 | + file: StringIO = field(default_factory=StringIO) |
| 28 | + menu_price: dict[str, float] = field(default_factory=default_menu_price) |
| 29 | + recept_analysis_result: dict[ir.SSAValue, Item] = field(default_factory=dict) |
| 30 | + |
| 31 | + def initialize(self): |
| 32 | + super().initialize() |
| 33 | + self.file.truncate(0) |
| 34 | + self.file.seek(0) |
| 35 | + return self |
| 36 | + |
| 37 | + def eval_stmt_fallback( |
| 38 | + self, frame: EmitStrFrame, stmt: ir.Statement |
| 39 | + ) -> tuple[str, ...]: |
| 40 | + return (stmt.name,) |
| 41 | + |
| 42 | + def emit_block(self, frame: EmitStrFrame, block: ir.Block) -> str | None: |
| 43 | + for stmt in block.stmts: |
| 44 | + result = self.eval_stmt(frame, stmt) |
| 45 | + if isinstance(result, tuple): |
| 46 | + frame.set_values(stmt.results, result) |
| 47 | + return None |
| 48 | + |
| 49 | + def get_output(self) -> str: |
| 50 | + self.file.seek(0) |
| 51 | + return "\n".join( |
| 52 | + [ |
| 53 | + "item \tamount \t price", |
| 54 | + "-----------------------------------", |
| 55 | + self.file.read(), |
| 56 | + ] |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +@func.dialect.register(key="emit.recept") |
| 61 | +class FuncEmit(interp.MethodTable): |
| 62 | + |
| 63 | + @interp.impl(func.Function) |
| 64 | + def emit_func(self, emit: EmitReceptMain, frame: EmitStrFrame, stmt: func.Function): |
| 65 | + _ = emit.run_ssacfg_region(frame, stmt.body) |
| 66 | + return () |
| 67 | + |
| 68 | + |
| 69 | +@dialect.register(key="emit.recept") |
| 70 | +class BeerEmit(interp.MethodTable): |
| 71 | + |
| 72 | + @interp.impl(stmts.Pour) |
| 73 | + def emit_pour(self, emit: EmitReceptMain, frame: EmitStrFrame, stmt: stmts.Pour): |
| 74 | + pints_item: ItemPints = emit.recept_analysis_result[stmt.result] |
| 75 | + |
| 76 | + amount_str = "" |
| 77 | + price_str = "" |
| 78 | + if isinstance(pints_item.count, AtLeastXItem): |
| 79 | + amount_str = f">={pints_item.count.data}" |
| 80 | + price_str = ( |
| 81 | + f" >=${emit.menu_price[pints_item.brand] * pints_item.count.data}" |
| 82 | + ) |
| 83 | + elif isinstance(pints_item.count, ConstIntItem): |
| 84 | + amount_str = f" {pints_item.count.data}" |
| 85 | + price_str = ( |
| 86 | + f" ${emit.menu_price[pints_item.brand] * pints_item.count.data}" |
| 87 | + ) |
| 88 | + else: |
| 89 | + raise EmitError("invalid analysis result.") |
| 90 | + |
| 91 | + emit.writeln(frame, f"{pints_item.brand}\t{amount_str}\t{price_str}") |
| 92 | + |
| 93 | + return () |
0 commit comments