forked from HanniRock/aleo-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |