Files
metabuilder/libraries/mojo/mojo.py
2026-03-09 22:30:41 +00:00

144 lines
4.9 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Mojo Compiler Manager
Self-documenting script for Mojo compiler implementation
"""
import argparse
import subprocess
import sys
from pathlib import Path
def run_cmd(cmd, check=True, cwd=None):
"""Run shell command"""
result = subprocess.run(cmd, shell=True, check=check, cwd=cwd)
return result.returncode == 0
def main():
parser = argparse.ArgumentParser(
description='Mojo Compiler - Implementation and Examples',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Implementation:
- 21 source files (260 KB total)
- 5 compilation phases: frontend, semantic, IR, codegen, runtime
- 15 comprehensive test files
- 12 tests with 100%% pass rate
- 9 compiler usage examples
- 37 language sample programs
Phases:
1. Frontend: Lexing, parsing, AST generation
2. Semantic: Type checking, symbol resolution
3. IR: MLIR lowering
4. Codegen: LLVM IR generation, optimization
5. Runtime: FFI, memory management, execution
Examples:
# Run compiler
%(prog)s compile <file.mojo>
# Run specific phase
%(prog)s compile --phase frontend <file.mojo>
# Run tests
%(prog)s test
# Run specific test suite
%(prog)s test --suite snake_game
# Show samples
%(prog)s samples
# Show compiler info
%(prog)s info
"""
)
subparsers = parser.add_subparsers(dest='command', help='Command to run')
# compile
compile_parser = subparsers.add_parser('compile', help='Compile Mojo file')
compile_parser.add_argument('file', type=str, help='Mojo source file')
compile_parser.add_argument('--phase', choices=['frontend', 'semantic', 'ir', 'codegen', 'runtime'],
help='Stop after specific phase')
compile_parser.add_argument('--output', '-o', type=str, help='Output file')
# test
test_parser = subparsers.add_parser('test', help='Run tests')
test_parser.add_argument('--suite', type=str, help='Specific test suite')
test_parser.add_argument('--verbose', '-v', action='store_true', help='Verbose output')
# samples
subparsers.add_parser('samples', help='List language samples')
# info
subparsers.add_parser('info', help='Show compiler information')
args = parser.parse_args()
if not args.command:
parser.print_help()
return 0
base_dir = Path(__file__).parent
try:
if args.command == 'compile':
print(f"🔧 Compiling {args.file}...")
# Placeholder - actual compiler invocation would go here
cmd = f'python3 compiler/main.py {args.file}'
if hasattr(args, 'phase') and args.phase:
cmd += f' --phase {args.phase}'
if hasattr(args, 'output') and args.output:
cmd += f' -o {args.output}'
return 0 if run_cmd(cmd, cwd=base_dir, check=False) else 1
elif args.command == 'test':
print("🧪 Running Mojo compiler tests...")
cmd = 'pytest'
if hasattr(args, 'suite') and args.suite:
cmd += f' -k {args.suite}'
if hasattr(args, 'verbose') and args.verbose:
cmd += ' -v'
return 0 if run_cmd(cmd, cwd=base_dir, check=False) else 1
elif args.command == 'samples':
print("📚 Mojo Language Samples\n")
samples_dir = base_dir / 'samples'
if samples_dir.exists():
run_cmd(f'find {samples_dir} -name "*.mojo" -type f | sort')
run_cmd(f'echo "\nTotal: $(find {samples_dir} -name \'*.mojo\' | wc -l) sample files"')
else:
print("❌ Samples directory not found")
return 0
elif args.command == 'info':
print("📊 Mojo Compiler Information\n")
print("Implementation:")
run_cmd(f'echo " Source files: $(find {base_dir}/compiler -name \'*.py\' | wc -l)"', check=False)
run_cmd(f'echo " Test files: $(find {base_dir} -name \'test_*.py\' | wc -l)"', check=False)
run_cmd(f'echo " Examples: $(find {base_dir}/examples -name \'*.py\' | wc -l)"', check=False)
run_cmd(f'echo " Samples: $(find {base_dir}/samples -name \'*.mojo\' | wc -l)"', check=False)
print("\nPhases:")
print(" 1. Frontend - Lexing, parsing, AST")
print(" 2. Semantic - Type checking, symbols")
print(" 3. IR - MLIR lowering")
print(" 4. Codegen - LLVM IR, optimization")
print(" 5. Runtime - FFI, execution")
print("\nTest Status:")
run_cmd('pytest --collect-only -q 2>/dev/null | tail -1 || echo " Run: mojo.py test"', cwd=base_dir, check=False)
return 0
return 0
except KeyboardInterrupt:
print("\n⚠️ Interrupted")
return 1
except Exception as e:
print(f"❌ Error: {e}")
return 1
if __name__ == '__main__':
sys.exit(main())