Skip to content

Commit ee67773

Browse files
author
Marcus Cornes
committed
Pre-break progress checkpoint
1 parent a89efd2 commit ee67773

13 files changed

Lines changed: 120 additions & 64 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
.vscode/
22
.DS_Store
33
test_programs/
4+
asmx_venv/
5+
dist/
6+
TODO.md

asmx.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

asmx/__init__.py

Whitespace-only changes.

asmx/asmx.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# asmx assembler
2+
# This file is the main entry point into the assembler
3+
4+
def main():
5+
import asmx.asmx_core.tokeniser as tokeniser
6+
import asmx.asmx_core.parser as parser
7+
import asmx.asmx_core.macros as macros
8+
import asmx.asmx_core.codegen as codegen
9+
import asmx.asmx_core.packages as packages
10+
11+
import argparse
12+
from colorama import init, Fore, Style
13+
14+
init()
15+
16+
argparse_parser = argparse.ArgumentParser(
17+
description="ASMX Assembler, for the ASMX VM platform."
18+
)
19+
20+
# Optional flag
21+
argparse_parser.add_argument("--docs", action="store_true", help="Shows documentation.")
22+
argparse_parser.add_argument("--version", action="store_true", help="Shows the version number.")
23+
24+
# Positional args (required by default)
25+
argparse_parser.add_argument("input_file", nargs="?", help="Source .asmx file to assemble.")
26+
argparse_parser.add_argument("output_file", nargs="?", help="Output machine code file.")
27+
28+
29+
args = argparse_parser.parse_args()
30+
31+
if args.docs:
32+
# Shows docs
33+
print(f"{Fore.RED}Sorry, the documentation is in progress.{Style.RESET_ALL}")
34+
return
35+
36+
if args.version:
37+
# Shows the version
38+
print("ASMX Assembler version 0.1.0")
39+
return
40+
41+
# If neither docs/version flags given, input_file AND output_file must be present
42+
if not (args.input_file and args.output_file):
43+
argparse_parser.error(f"{Fore.RED}The following arguments are required: input_file, output_file{Style.RESET_ALL}")
44+
45+
print(f"{Fore.GREEN}Assembling {args.input_file} -> {args.output_file}{Style.RESET_ALL}")
46+
47+
48+
packages.load_all_packages()
49+
50+
51+
source_file_location = args.input_file
52+
dest_file_location = args.output_file
53+
source_file = tokeniser.read_source_file(source_file_location)
54+
55+
56+
results = []
57+
58+
for line_num, line in source_file:
59+
tokens = tokeniser.tokenize_line_with_strings(line)
60+
try:
61+
results.append(parser.parse_line(tokens, line_num))
62+
except SyntaxError as e:
63+
print(f"{Fore.RED}Syntax error: {e}{Style.RESET_ALL}")
64+
break
65+
66+
instructions = macros.dispatch_macros(results)
67+
68+
address_table = codegen.find_labels(instructions)
69+
70+
generated_code = codegen.generate_code(instructions, address_table)
71+
72+
codegen.write_output_file(dest_file_location, generated_code)
73+
print(f"{Fore.GREEN}Successfully assembled {args.input_file} -> {args.output_file}{Style.RESET_ALL}")

asmx/asmx_core/__init__.py

Whitespace-only changes.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
RESET = "\033[0m"
2-
RED = "\033[31m"
3-
GREEN = "\033[32m"
4-
YELLOW = "\033[33m"
1+
from colorama import init, Fore, Style
2+
RESET = Style.RESET_ALL
3+
RED = Fore.RED
4+
GREEN = Fore.GREEN
5+
YELLOW = Fore.YELLOW
56

67
def find_labels(instructions):
78
label_addresses = {}
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
TEMP_MIN = "a1"
66
TEMP_MAX = "a2"
77

8-
RESET = "\033[0m"
9-
RED = "\033[31m"
10-
GREEN = "\033[32m"
11-
YELLOW = "\033[33m"
8+
from colorama import init, Fore, Style
9+
RESET = Style.RESET_ALL
10+
RED = Fore.RED
11+
GREEN = Fore.GREEN
12+
YELLOW = Fore.YELLOW
1213

1314
def dispatch_macros(instruction_list, max_passes=50):
1415
i = 0
1516
passes = 0
1617

1718
while i < len(instruction_list):
18-
print(f"{YELLOW}[Macro Pass Count] Macro pass round: {passes}")
1919
instruction = instruction_list[i]
2020
if passes > max_passes:
2121
print(f"{RED}[Macro Recursion] Macro expansion exceeded max passes. Possible infinite recursion.{RESET}")
@@ -28,7 +28,6 @@ def dispatch_macros(instruction_list, max_passes=50):
2828
if instruction.get("type") == "macro":
2929
namespace = instruction["namespace"]
3030
subcommand = instruction["subcommand"].lower()
31-
print(f"{GREEN}[Macro Notification] Processing macro '{namespace} {subcommand}'{RESET}")
3231

3332
registry = packages.get_all_macro_registries().get(namespace)
3433
if not registry:
@@ -56,9 +55,6 @@ def dispatch_macros(instruction_list, max_passes=50):
5655
expansion = None
5756

5857
if expansion:
59-
print(f"{GREEN}[Expansion] Expansion result for {instruction}:{RESET}")
60-
print(expansion)
61-
6258
# Convert expanded lines to parsed instruction dicts
6359
parsed_instructions = []
6460
for line in expansion:
@@ -85,7 +81,6 @@ def dispatch_macros(instruction_list, max_passes=50):
8581
return instruction_list
8682

8783
def expand_immediates(instruction):
88-
print("Immediate is being expanded.")
8984
args = instruction.get("args", [])
9085
opcode = instruction["opcode"]
9186

@@ -130,8 +125,6 @@ def expand_rand(instruction):
130125
if not isinstance(min_val, int) and not isinstance(max_val, int):
131126
return None # Stop expanding here
132127

133-
print("RAND is being expanded.")
134-
135128
expanded_lines = []
136129

137130

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
import importlib.util
33
from . import parser
44

5-
macro_packages = {}
6-
RESET = "\033[0m"
7-
RED = "\033[31m"
8-
GREEN = "\033[32m"
9-
YELLOW = "\033[33m"
105

11-
def load_all_packages(package_dir="packages"):
6+
macro_packages = {}
7+
from colorama import init, Fore, Style
8+
RESET = Style.RESET_ALL
9+
RED = Fore.RED
10+
GREEN = Fore.GREEN
11+
YELLOW = Fore.YELLOW
12+
13+
def load_all_packages():
14+
package_dir = os.path.join(os.path.dirname(__file__), "packages")
1215

1316
for filename in os.listdir(package_dir):
1417
if not filename.endswith(".py") or filename.startswith("__"):
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
"hardcall"
1919
]
2020

21-
RESET = "\033[0m"
22-
RED = "\033[31m"
23-
GREEN = "\033[32m"
24-
YELLOW = "\033[33m"
21+
from colorama import init, Fore, Style
22+
RESET = Style.RESET_ALL
23+
RED = Fore.RED
24+
GREEN = Fore.GREEN
25+
YELLOW = Fore.YELLOW
2526

2627
valid_user_registers = { f"u{i}" for i in range(1, 9) }
2728
valid_auto_registers = { f"a{i}" for i in range(1, 5) }

0 commit comments

Comments
 (0)