|
| 1 | +from typing import Callable, Optional, Tuple |
| 2 | + |
| 3 | +try: |
| 4 | + import clvm_rs |
| 5 | +except ImportError: |
| 6 | + clvm_rs = None |
| 7 | + |
| 8 | +from . import core_ops, more_ops |
| 9 | +from .chainable_multi_op_fn import ChainableMultiOpFn |
| 10 | +from .handle_unknown_op import ( |
| 11 | + handle_unknown_op_softfork_ready, |
| 12 | + handle_unknown_op_strict, |
| 13 | +) |
| 14 | +from .run_program import _run_program |
| 15 | +from .types import CLVMObjectType, ConversionFn, MultiOpFn, OperatorDict |
| 16 | + |
| 17 | + |
| 18 | +OP_REWRITE = { |
| 19 | + "+": "add", |
| 20 | + "-": "subtract", |
| 21 | + "*": "multiply", |
| 22 | + "/": "div", |
| 23 | + "i": "if", |
| 24 | + "c": "cons", |
| 25 | + "f": "first", |
| 26 | + "r": "rest", |
| 27 | + "l": "listp", |
| 28 | + "x": "raise", |
| 29 | + "=": "eq", |
| 30 | + ">": "gr", |
| 31 | + ">s": "gr_bytes", |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +def op_table_for_module(mod): |
| 36 | + |
| 37 | + # python-implemented operators don't take `max_cost` and rust-implemented operators do |
| 38 | + # So we make the `max_cost` operator optional with this trick |
| 39 | + # TODO: have python-implemented ops also take `max_cost` and unify the API. |
| 40 | + |
| 41 | + def elide_max_cost(f): |
| 42 | + def inner_op(sexp, max_cost=None): |
| 43 | + try: |
| 44 | + return f(sexp, max_cost) |
| 45 | + except TypeError: |
| 46 | + return f(sexp) |
| 47 | + return inner_op |
| 48 | + |
| 49 | + return {k: elide_max_cost(v) for k, v in mod.__dict__.items() if k.startswith("op_")} |
| 50 | + |
| 51 | + |
| 52 | +def op_imp_table_for_backend(backend): |
| 53 | + if backend is None and clvm_rs: |
| 54 | + backend = "native" |
| 55 | + |
| 56 | + if backend == "native": |
| 57 | + if clvm_rs is None: |
| 58 | + raise RuntimeError("native backend not installed") |
| 59 | + return clvm_rs.native_opcodes_dict() |
| 60 | + |
| 61 | + table = {} |
| 62 | + table.update(op_table_for_module(core_ops)) |
| 63 | + table.update(op_table_for_module(more_ops)) |
| 64 | + return table |
| 65 | + |
| 66 | + |
| 67 | +def op_atom_to_imp_table(op_imp_table, keyword_to_atom, op_rewrite=OP_REWRITE): |
| 68 | + op_atom_to_imp_table = {} |
| 69 | + for op, bytecode in keyword_to_atom.items(): |
| 70 | + op_name = "op_%s" % op_rewrite.get(op, op) |
| 71 | + op_f = op_imp_table.get(op_name) |
| 72 | + if op_f: |
| 73 | + op_atom_to_imp_table[bytecode] = op_f |
| 74 | + return op_atom_to_imp_table |
| 75 | + |
| 76 | + |
| 77 | +def opcode_table_for_backend(keyword_to_atom, backend): |
| 78 | + op_imp_table = op_imp_table_for_backend(backend) |
| 79 | + return op_atom_to_imp_table(op_imp_table, keyword_to_atom) |
| 80 | + |
| 81 | + |
| 82 | +class Dialect: |
| 83 | + def __init__( |
| 84 | + self, |
| 85 | + quote_kw: bytes, |
| 86 | + apply_kw: bytes, |
| 87 | + multi_op_fn: MultiOpFn, |
| 88 | + to_python: ConversionFn, |
| 89 | + ): |
| 90 | + self.quote_kw = quote_kw |
| 91 | + self.apply_kw = apply_kw |
| 92 | + self.opcode_lookup = dict() |
| 93 | + self.multi_op_fn = ChainableMultiOpFn(self.opcode_lookup, multi_op_fn) |
| 94 | + self.to_python = to_python |
| 95 | + |
| 96 | + def update(self, d: OperatorDict) -> None: |
| 97 | + self.opcode_lookup.update(d) |
| 98 | + |
| 99 | + def clear(self) -> None: |
| 100 | + self.opcode_lookup.clear() |
| 101 | + |
| 102 | + def run_program( |
| 103 | + self, |
| 104 | + program: CLVMObjectType, |
| 105 | + env: CLVMObjectType, |
| 106 | + max_cost: int, |
| 107 | + pre_eval_f: Optional[ |
| 108 | + Callable[[CLVMObjectType, CLVMObjectType], Tuple[int, CLVMObjectType]] |
| 109 | + ] = None, |
| 110 | + ) -> Tuple[int, CLVMObjectType]: |
| 111 | + cost, r = _run_program( |
| 112 | + program, |
| 113 | + env, |
| 114 | + self.multi_op_fn, |
| 115 | + self.quote_kw, |
| 116 | + self.apply_kw, |
| 117 | + max_cost, |
| 118 | + pre_eval_f, |
| 119 | + ) |
| 120 | + return cost, self.to_python(r) |
| 121 | + |
| 122 | + |
| 123 | +def native_new_dialect( |
| 124 | + quote_kw: bytes, apply_kw: bytes, strict: bool, to_python: ConversionFn |
| 125 | +) -> Dialect: |
| 126 | + unknown_op_callback = ( |
| 127 | + clvm_rs.NATIVE_OP_UNKNOWN_STRICT |
| 128 | + if strict |
| 129 | + else clvm_rs.NATIVE_OP_UNKNOWN_NON_STRICT |
| 130 | + ) |
| 131 | + dialect = clvm_rs.Dialect( |
| 132 | + quote_kw, |
| 133 | + apply_kw, |
| 134 | + unknown_op_callback, |
| 135 | + to_python=to_python, |
| 136 | + ) |
| 137 | + return dialect |
| 138 | + |
| 139 | + |
| 140 | +def python_new_dialect( |
| 141 | + quote_kw: bytes, apply_kw: bytes, strict: bool, to_python: ConversionFn |
| 142 | +) -> Dialect: |
| 143 | + unknown_op_callback = ( |
| 144 | + handle_unknown_op_strict if strict else handle_unknown_op_softfork_ready |
| 145 | + ) |
| 146 | + dialect = Dialect( |
| 147 | + quote_kw, |
| 148 | + apply_kw, |
| 149 | + unknown_op_callback, |
| 150 | + to_python=to_python, |
| 151 | + ) |
| 152 | + return dialect |
| 153 | + |
| 154 | + |
| 155 | +def new_dialect(quote_kw: bytes, apply_kw: bytes, strict: bool, to_python: ConversionFn, backend=None): |
| 156 | + if backend is None: |
| 157 | + backend = "python" if clvm_rs is None else "native" |
| 158 | + backend_f = native_new_dialect if backend == "native" else python_new_dialect |
| 159 | + return backend_f(quote_kw, apply_kw, strict, to_python) |
0 commit comments