Skip to content

Commit

Permalink
wip disassembler
Browse files Browse the repository at this point in the history
  • Loading branch information
HarukaMa committed Mar 2, 2023
1 parent 90eb505 commit 71f2605
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 41 deletions.
42 changes: 1 addition & 41 deletions disasm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,41 +1 @@
# Temporary dumping ground for tool functions

from node.types import *

def plaintext_type_to_str(value: PlaintextType):
match value.type:
case PlaintextType.Type.Literal:
value: LiteralPlaintextType
return value.literal_type.name.lower()
case PlaintextType.Type.Interface:
value: InterfacePlaintextType
return str(value.interface)


def value_type_to_mode_type_str(value: ValueType):
mode = value.type.name.lower()
if "record" in mode:
mode = "private"
match value.type:
case ValueType.Type.Constant | ValueType.Type.Public | ValueType.Type.Private:
# noinspection PyUnresolvedReferences
t = plaintext_type_to_str(value.plaintext_type)
case ValueType.Type.Record:
value: RecordValueType
t = str(value.identifier)
case ValueType.Type.ExternalRecord:
value: ExternalRecordValueType
t = str(value.locator)
return mode, t

def finalize_type_to_str(value: FinalizeType):
match value.type:
case FinalizeType.Type.Public:
value: PublicFinalize
return plaintext_type_to_str(value.plaintext_type)
case FinalizeType.Type.Record:
value: RecordFinalize
return str(value.identifier)
case FinalizeType.Type.ExternalRecord:
value: ExternalRecordFinalize
return str(value.locator)
from .utils import *
44 changes: 44 additions & 0 deletions disasm/aleo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from node.types import *
from .utils import *


def disasm_finalize_type(value: FinalizeType) -> str:
match value.type:
case FinalizeType.Type.Public:
value: PublicFinalize
return plaintext_type_to_str(value.plaintext_type) + ".public"
case FinalizeType.Type.Record:
value: RecordFinalize
return str(value.identifier) + ".record"
case FinalizeType.Type.ExternalRecord:
raise NotImplementedError

def disassemble_program(program: Program) -> str:
res = disasm_str()
for i in program.imports:
i: Import
res.insert_line(f"import {i.program_id};")
res.insert_line("")
res.insert_line(f"program {program.id};")
res.insert_line("")
for m in program.mappings.values():
m: Mapping
res.insert_line(f"mapping {m.name}:")
res.indent()
res.insert_line(f"key {m.key.name} as {disasm_finalize_type(m.key.finalize_type)};")
res.insert_line(f"value {m.value.name} as {disasm_finalize_type(m.value.finalize_type)};")
res.unindent()
res.insert_line("")
for i in program.interfaces.values():
i: Interface
res.insert_line(f"struct {i.name}:")
res.indent()
for m, t in i.members:
res.insert_line(f"{m} as {plaintext_type_to_str(t)};")
res.unindent()
res.insert_line("")
for r in program.records.values():
r: RecordType
res.insert_line(f"record {r.name}:")

return res
66 changes: 66 additions & 0 deletions disasm/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from io import StringIO

from node.types import *


class disasm_str(StringIO):

def __init__(self):
super().__init__()
self.indent_level = 0

def indent(self):
self.indent_level += 1

def unindent(self):
self.indent_level -= 1

def insert_line(self, content: str):
self.write(" " * self.indent_level)
self.write(content)
self.write("\n")

def insert(self, content: str):
self.write(content)

def __str__(self):
return self.getvalue()


def plaintext_type_to_str(value: PlaintextType):
match value.type:
case PlaintextType.Type.Literal:
value: LiteralPlaintextType
return value.literal_type.name.lower()
case PlaintextType.Type.Interface:
value: InterfacePlaintextType
return str(value.interface)


def value_type_to_mode_type_str(value: ValueType):
mode = value.type.name.lower()
if "record" in mode:
mode = "private"
match value.type:
case ValueType.Type.Constant | ValueType.Type.Public | ValueType.Type.Private:
# noinspection PyUnresolvedReferences
t = plaintext_type_to_str(value.plaintext_type)
case ValueType.Type.Record:
value: RecordValueType
t = str(value.identifier)
case ValueType.Type.ExternalRecord:
value: ExternalRecordValueType
t = str(value.locator)
return mode, t

def finalize_type_to_str(value: FinalizeType):
match value.type:
case FinalizeType.Type.Public:
value: PublicFinalize
return plaintext_type_to_str(value.plaintext_type)
case FinalizeType.Type.Record:
value: RecordFinalize
return str(value.identifier)
case FinalizeType.Type.ExternalRecord:
value: ExternalRecordFinalize
return str(value.locator)

0 comments on commit 71f2605

Please sign in to comment.