toy_compiler/
āāā toy_compiler.py # All-in-one compiler logic
āāā datasets/ # Sample input scripts
ā āāā hello_world.txt
ā āāā math_demo.txt
ā āāā error_cases.txt
āāā tests/ # Unit tests (optional expansion)
āāā README.md # Project documentation
āāā requirements.txt # Dependencies (if needed)PRINT Hello, Nokwazi!
ADD 5 7
PRINT Compilation complete.# toy_compiler.py
def tokenize(source_code):
tokens = []
for line in source_code.strip().split('\n'):
parts = line.strip().split()
tokens.append(parts)
return tokens
def parse(tokens):
ast = []
for token in tokens:
if token[0] == "PRINT":
ast.append(("print", " ".join(token[1:])))
elif token[0] == "ADD":
if len(token) != 3:
raise SyntaxError("ADD requires two numeric arguments.")
try:
left = int(token[1])
right = int(token[2])
ast.append(("add", left, right))
except ValueError:
raise SyntaxError("ADD arguments must be integers.")
else:
raise SyntaxError(f"Unknown command: {token[0]}")
return ast
def generate_code(ast):
code = ""
for node in ast:
if node[0] == "print":
code += f'print("{node[1]}")\n'
elif node[0] == "add":
code += f'print({node[1]} + {node[2]})\n'
return code
def execute(python_code):
exec(python_code)
if name == "main":
source = """
PRINT Hello, Nokwazi!
ADD 5 7
PRINT This is your toy compiler in action.
"""
try:
tokens = tokenize(source)
ast = parse(tokens)
code = generate_code(ast)
execute(code)
except Exception as e:
print(f"Compilation error: {e}")