Skip to content

Commit 56879a3

Browse files
committed
feat: enhance output formatting and add replace method for instruction fetching
1 parent f9e8b6b commit 56879a3

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

smalig/cli/app.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def app(file_path, target, json, out, exact_match) -> None:
5353
return
5454
if out:
5555
with open(out, "w") as f:
56-
f.write(str(result))
56+
f.write(str(result.replace("plain")))
5757
else:
58-
print(result)
58+
print(result.replace())
5959
return
6060

6161

smalig/grammar.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
args_info: ""
2828
short_desc: "No operation"
2929
long_desc: "Does nothing, Waste cycles."
30-
note: 'Data-bearing pseudo-instructions are tagged with this opcode, in which case the high-order byte of the opcode unit indicates the nature of the data. See "packed-switch-payload Format", "sparse-switch-payload Format", and "fill-array-data-payload Format".'
30+
note: "Data-bearing pseudo-instructions are tagged with this opcode, in which case the high-order byte of the opcode unit indicates the nature of the data. See `packed-switch-payload Format`, `sparse-switch-payload Format`, and `fill-array-data-payload Format`."
3131
example: "0000 - nop"
3232
example_desc: "Does nothing, Waste cycles"
3333

@@ -399,7 +399,7 @@
399399
args_info: "A: reference-bearing register (8 bits), B: type index (16 bits)"
400400
short_desc: "Throw a ClassCastException if the reference in the given register cannot be cast to the indicated type."
401401
long_desc: "Check that the object referenced by vAA is an instance of the class resolved by type@BBBB. If it is not, throw a ClassCastException."
402-
note: "Since 'A' must always be a reference (and not a primitive value), this will necessarily fail at runtime (that is, it will throw an exception) if 'B' refers to a primitive type."
402+
note: "Since $A$ must always be a reference (and not a primitive value), this will necessarily fail at runtime (that is, it will throw an exception) if $B$ refers to a primitive type."
403403
example: "1F04 0100 - check-cast v4, Test3 #type@0001"
404404
example_desc: "Checks whether the object reference in v4 can be cast to type@0001 (entry #1 in the type id table) and throws a ClassCastException if it cannot."
405405

@@ -411,7 +411,7 @@
411411
args_info: "A: destination register (4 bits), B: reference-bearing register (4 bits), C: type index (16 bits)"
412412
short_desc: "Store in the given destination register 1 if the indicated reference is an instance of the given type, or 0 if not."
413413
long_desc: "Check whether the object referenced by vB is an instance of the class resolved by type@CCCC. If it is, the value 1 is stored in vA; otherwise, the value 0 is stored in vA."
414-
note: "Since 'B' must always be a reference (and not a primitive value), this will always result in 0 being stored if C refers to a primitive type. In short, This will always result in 0 if the type is primitive."
414+
note: "Since $B$ must always be a reference (and not a primitive value), this will always result in 0 being stored if C refers to a primitive type. In short, This will always result in 0 if the type is primitive."
415415
example: "2040 0100 - instance-of v0, v4, type@0001"
416416
example_desc: "Checks whether the object reference in v4 is an instance of type@0001 (entry #1 in the type id table) and stores the result in v0."
417417

smalig/utils/__init__.py

+40
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ def __init__(self, instructions: list[dict], target: str, exact_match: bool = Tr
3838
)
3939

4040
def __str__(self):
41+
"""
42+
Returns the result in a human-readable format.
43+
"""
4144
if isinstance(self.result, dict):
4245
if not self.result:
4346
results = f"No instruction found for {self.target}!"
@@ -93,6 +96,7 @@ def __str__(self):
9396
return results
9497

9598
def __repr__(self):
99+
"""Return a string representation of the InstructionFetch object."""
96100
return (
97101
f"InstructionFetch(instructions={self.instructions}, "
98102
f"target={self.target}, name={self.name}, "
@@ -104,6 +108,7 @@ def __repr__(self):
104108
)
105109

106110
def fetch(self) -> dict:
111+
"""Fetch the instruction from the instruction set."""
107112
for instruction in self.instructions:
108113
if instruction["opcode"] == self.target:
109114
return instruction
@@ -113,20 +118,55 @@ def fetch(self) -> dict:
113118
return {}
114119

115120
def fetch_fuzzy(self) -> list[dict]:
121+
"""Fetch the instruction from the instruction set using fuzzy matching."""
116122
results = []
117123
for instruction in self.instructions:
118124
if self.target.lower() in instruction["name"].lower():
119125
results.append(instruction)
120126
return results
121127

122128
def fetch_opcode(self) -> dict:
129+
"""Fetch the instruction from the instruction set using the opcode."""
123130
for instruction in self.instructions:
124131
if instruction["opcode"] == self.target:
125132
return instruction
126133
return {}
127134

128135
def fetch_inst(self) -> dict:
136+
"""Fetch the instruction from the instruction set using the name."""
129137
for instruction in self.instructions:
130138
if instruction["name"] == self.target:
131139
return instruction
132140
return {}
141+
142+
def replace(self, type: str = "ansi"):
143+
"""
144+
Replace $ and ` symbols with given type.
145+
type: str = "ansi" -> "ansi" or "html" or "markdown" or "md" or "plain"
146+
"""
147+
if type in ["html", "markdown", "md"]:
148+
result = self.__str__()
149+
while result.find("$") != -1:
150+
result = result.replace("$", "<span style='color: #ff0;'>", 1)
151+
result = result.replace("$", "</span>", 1)
152+
while result.find("`") != -1:
153+
result = result.replace("`", "<code>", 1)
154+
result = result.replace("`", "</code>", 1)
155+
elif type == "plain":
156+
result = self.__str__()
157+
while result.find("$") != -1:
158+
result = result.replace("$", "", 1)
159+
while result.find("`") != -1:
160+
result = result.replace("`", "", 1)
161+
else:
162+
NC = "\033[0m" # No Color
163+
YELLOW = "\033[0;33m"
164+
BLUE = "\033[3;34m"
165+
result = self.__str__()
166+
while result.find("$") != -1:
167+
result = result.replace("$", YELLOW, 1)
168+
result = result.replace("$", NC, 1)
169+
while result.find("`") != -1:
170+
result = result.replace("`", BLUE, 1)
171+
result = result.replace("`", NC, 1)
172+
return result

0 commit comments

Comments
 (0)