Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My take on dialects, which I think is now uniform in interface with the run_program from stage_0 #98

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Allow 'native' dialect to work
  • Loading branch information
prozacchiwawa committed Jul 22, 2021
commit 46c708f238cfadfcc29cd45a914163dc6d7dac76
15 changes: 15 additions & 0 deletions clvm/chia_dialect_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@
KEYWORD_FROM_ATOM = {int_to_bytes(k): v for k, v in enumerate(KEYWORDS)}
KEYWORD_TO_ATOM = {v: k for k, v in KEYWORD_FROM_ATOM.items()}

KEYWORD_TO_LONG_KEYWORD = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you build this table algorithmically from OP_REWRITE?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or vice-versa?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this constant is actually Chia dialect-specific.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I included it there mainly to avoid another layer when resolving dependencies, but it does make sense to put this on a layer in between.

"i": "op_if",
"c": "op_cons",
"f": "op_first",
"r": "op_rest",
"l": "op_listp",
"x": "op_raise",
"=": "op_eq",
"+": "op_add",
"-": "op_subtract",
"*": "op_multiply",
"/": "op_divmod",
">": "op_gr",
">s": "op_gr_bytes",
}
71 changes: 70 additions & 1 deletion clvm/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .run_program import _run_program
from .types import CLVMObjectType, ConversionFn, MultiOpFn, OperatorDict
from clvm.serialize import sexp_from_stream, sexp_to_stream
from .chia_dialect_constants import KEYWORD_FROM_ATOM, KEYWORD_TO_LONG_KEYWORD


OP_REWRITE = {
Expand Down Expand Up @@ -112,6 +113,72 @@ def run_program(
return cost, self.to_python(r)


class NativeDialect:
def __init__(
self,
quote_kw: bytes,
apply_kw: bytes,
multi_op_fn: MultiOpFn,
to_python: ConversionFn,
):
native_dict = clvm_rs.native_opcodes_dict()
def get_native_op_for_kw(op, k):
kw = KEYWORD_TO_LONG_KEYWORD[k] if k in KEYWORD_TO_LONG_KEYWORD else "op_%s" % k
return (op, native_dict[kw])

native_opcode_names_by_opcode = dict(
get_native_op_for_kw(op, k)
for op, k in KEYWORD_FROM_ATOM.items()
if k not in "qa."
)

self.quote_kw = quote_kw
self.apply_kw = apply_kw
self.to_python = to_python
self.callbacks = multi_op_fn
self.held = clvm_rs.Dialect(
quote_kw,
apply_kw,
multi_op_fn,
to_python
)

self.held.update(native_opcode_names_by_opcode)


def update(self,d):
return self.held.update(d)


def clear(self) -> None:
return self.held.clear()


def run_program(
self,
program: CLVMObjectType,
env: CLVMObjectType,
max_cost: int,
pre_eval_f: Optional[
Callable[[CLVMObjectType, CLVMObjectType], Tuple[int, CLVMObjectType]]
] = None,
) -> Tuple[int, CLVMObjectType]:
prog = io.BytesIO()
e = io.BytesIO()
sexp_to_stream(program, prog)
sexp_to_stream(env, e)

return self.held.deserialize_and_run_program(
prog.getvalue(),
e.getvalue(),
max_cost,
pre_eval_f
)

def configure(self,**kwargs):
pass


def native_new_dialect(
quote_kw: bytes, apply_kw: bytes, strict: bool, to_python: ConversionFn
) -> Dialect:
Expand All @@ -120,7 +187,8 @@ def native_new_dialect(
if strict
else clvm_rs.NATIVE_OP_UNKNOWN_NON_STRICT
)
dialect = clvm_rs.Dialect(

dialect = NativeDialect(
quote_kw,
apply_kw,
unknown_op_callback,
Expand All @@ -135,6 +203,7 @@ def python_new_dialect(
unknown_op_callback = (
handle_unknown_op_strict if strict else handle_unknown_op_softfork_ready
)

dialect = Dialect(
quote_kw,
apply_kw,
Expand Down