Spaces:
Sleeping
Sleeping
File size: 9,722 Bytes
7cc8e29 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ุฃูู
ูุฑ CLI โ Command Line Interface
Entry point: python3 amr_cli.py program.ุฃู
ุฑ
Modes:
run (default) โ parse, emit Python, execute
compile โ parse, emit Python to stdout
check โ parse only, report errors
"""
import sys
import os
import argparse
import tempfile
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from amr_lexer import tokenize_file, tokenize_source, LexerError
from amr_parser import parse_source, parse_file, print_ast, ParseError
from amr_emitter import emit_source, emit_file, EmitterError
# Arabic translations for common Python runtime errors
RUNTIME_ERROR_MAP = {
'NameError': 'ุงูุณูู
ุบูููุฑ ู
ูุนูุฑููู', # undefined name
'TypeError': 'ุฎูุทูุฃ ููู ุงููููููุน', # type error
'ValueError': 'ุฎูุทูุฃ ููู ุงููููู
ูุฉ', # value error
'IndexError': 'ููููุฑูุณ ุฎูุงุฑูุฌ ุงูู
ูุฏูู', # index out of range
'KeyError': 'ู
ูููุชูุงุญ ุบูููุฑ ู
ูููุฌููุฏ', # key not found
'ZeroDivisionError': 'ููุณูู
ูุฉ ุนูููู ุตูููุฑ', # division by zero
'FileNotFoundError': 'ู
ููููู ุบูููุฑ ู
ูููุฌููุฏ', # file not found
'AttributeError': 'ุตูููุฉ ุบูููุฑ ู
ูููุฌููุฏูุฉ', # attribute not found
'RecursionError': 'ุนูู
ูู ุงูุชููููุฑูุงุฑ ุชูุฌูุงููุฒู ุงูุญูุฏู', # recursion limit
'OperationalError': 'ุฎูุทูุฃ ููู ุงููููููุญ', # sqlite operational error
'ุฎูุทูุฃ_ููููุญ': 'ุฎูุทูุฃ ููู ุงููููููุญ', # LAWH error
'ุฎูุทูุฃ_ุฃูุฑูุถ': 'ุฎูุทูุฃ ููู ุงูุฃูุฑูุถ', # ARD error
'ุฎูุทูุฃ_ุชูุฑูุฌูู
ูุฉ': 'ุฎูุทูุฃ ููู ุงูุชููุฑูุฌูู
ูุฉ', # Translation error
}
def format_runtime_error(e):
"""Format a Python exception with Arabic error type."""
err_type = type(e).__name__
arabic_type = RUNTIME_ERROR_MAP.get(err_type, err_type)
return f'{arabic_type}: {e}'
def run_file(filepath):
"""Parse, emit Python, and execute an ุฃูู
ูุฑ file."""
if not os.path.exists(filepath):
print(f'ู
ููููู ุบูููุฑ ู
ูููุฌููุฏ: {filepath}', file=sys.stderr)
return 1
try:
python_code = emit_file(filepath)
except (LexerError, ParseError, EmitterError) as e:
print(f'ุฎูุทูุฃ: {e}', file=sys.stderr)
return 1
# Write to temp file and execute
code_dir = os.path.dirname(os.path.abspath(__file__))
with tempfile.NamedTemporaryFile(
mode='w', suffix='.py', dir=code_dir,
delete=False, encoding='utf-8'
) as tmp:
tmp.write(python_code)
tmp_path = tmp.name
try:
# Execute the generated Python
exec_globals = {'__name__': '__main__', '__file__': tmp_path}
exec(compile(python_code, filepath, 'exec'), exec_globals)
return 0
except Exception as e:
print(f'ุฎูุทูุฃ ุชููููููุฐ: {format_runtime_error(e)}', file=sys.stderr)
return 1
finally:
try:
os.unlink(tmp_path)
except OSError:
pass
def run_string(source):
"""Parse, emit, and execute ุฃูู
ูุฑ source from string."""
try:
python_code = emit_source(source)
except (LexerError, ParseError, EmitterError) as e:
print(f'ุฎูุทูุฃ: {e}', file=sys.stderr)
return 1
try:
exec_globals = {'__name__': '__main__', '__file__': '<string>'}
exec(compile(python_code, '<string>', 'exec'), exec_globals)
return 0
except Exception as e:
print(f'ุฎูุทูุฃ ุชููููููุฐ: {format_runtime_error(e)}', file=sys.stderr)
return 1
def compile_file(filepath):
"""Parse and emit Python to stdout."""
if not os.path.exists(filepath):
print(f'ู
ููููู ุบูููุฑ ู
ูููุฌููุฏ: {filepath}', file=sys.stderr)
return 1
try:
python_code = emit_file(filepath)
print(python_code)
return 0
except (LexerError, ParseError, EmitterError) as e:
print(f'ุฎูุทูุฃ: {e}', file=sys.stderr)
return 1
def check_file(filepath):
"""Parse only, report errors or success."""
if not os.path.exists(filepath):
print(f'ู
ููููู ุบูููุฑ ู
ูููุฌููุฏ: {filepath}', file=sys.stderr)
return 1
try:
tokens = tokenize_file(filepath)
print(f'ุชูุญููููู: {len(tokens)} tokens')
from amr_parser import Parser
parser = Parser(tokens)
ast = parser.parse()
print(f'ุชูุญููููู: {len(ast.body)} statements')
print('ููุฌูุงุญ โ no errors found') # success
return 0
except (LexerError, ParseError) as e:
print(f'ุฎูุทูุฃ: {e}', file=sys.stderr)
return 1
def repl():
"""Interactive ุฃูู
ูุฑ REPL (Read-Evaluate-Print Loop)."""
print('ุจูุณูู
ู ุงูููููู ุงูุฑููุญูู
ููฐูู ุงูุฑููุญููู
ู')
print('ุฃูู
ูุฑ v1.0 โ ุงููุฑูุฃู')
print('ุงูููุชูุจู "ุฎูุฑููุฌ" ููููุฎูุฑููุฌ')
print()
# Persistent execution environment
exec_globals = {'__name__': '__main__', '__file__': '<repl>'}
# Pre-load runtime
runtime_code = (
'import sys, os\n'
'sys.path.insert(0, os.path.dirname(os.path.abspath("' +
os.path.dirname(os.path.abspath(__file__)).replace('\\', '\\\\') +
'")))\n'
'sys.path.insert(0, "' +
os.path.dirname(os.path.abspath(__file__)).replace('\\', '\\\\') +
'")\n'
'from amr_runtime import *\n'
)
exec(compile(runtime_code, '<repl-init>', 'exec'), exec_globals)
# Multiline input state
buffer = []
in_block = False
while True:
try:
prompt = '... ' if in_block else 'ุฃู
ุฑ> '
line = input(prompt)
except (EOFError, KeyboardInterrupt):
print('\nููุฏูุงุนูุง')
break
# Exit commands
if line.strip() in ('ุฎูุฑููุฌ', 'exit', 'quit'):
print('ููุฏูุงุนูุง')
break
# Empty line ends a block
if not line.strip() and in_block:
source = '\n'.join(buffer)
buffer = []
in_block = False
elif line.strip().endswith(':'):
buffer.append(line)
in_block = True
continue
elif in_block:
buffer.append(line)
continue
else:
source = line
if not source.strip():
continue
try:
python_code = emit_source(source)
# Strip the boilerplate (already loaded in exec_globals)
lines = python_code.split('\n')
# Remove the import/setup lines
code_lines = []
skip = True
for l in lines:
if skip and (l.startswith('#') or l.startswith('import ') or
l.startswith('sys.') or l.startswith('from amr_') or
l.strip() == ''):
continue
skip = False
code_lines.append(l)
clean_code = '\n'.join(code_lines)
if clean_code.strip():
exec(compile(clean_code, '<repl>', 'exec'), exec_globals)
except (LexerError, ParseError, EmitterError) as e:
print(f'ุฎูุทูุฃ: {e}', file=sys.stderr)
except Exception as e:
print(f'ุฎูุทูุฃ ุชููููููุฐ: {format_runtime_error(e)}', file=sys.stderr)
def main():
parser = argparse.ArgumentParser(
description='ุฃูู
ูุฑ โ Root-Derived Programming Language CLI',
epilog='ุจูุณูู
ู ุงูููููู ุงูุฑููุญูู
ููฐูู ุงูุฑููุญููู
ู'
)
parser.add_argument('file', nargs='?', help='Path to .ุฃู
ุฑ source file')
parser.add_argument('-c', '--compile', action='store_true',
help='Emit Python code to stdout (do not execute)')
parser.add_argument('--check', action='store_true',
help='Parse only, report errors')
parser.add_argument('-e', '--exec', dest='exec_str', metavar='CODE',
help='Execute ุฃูู
ูุฑ code from string')
parser.add_argument('--ast', action='store_true',
help='Print AST (debug)')
parser.add_argument('-i', '--interactive', action='store_true',
help='Start interactive REPL')
args = parser.parse_args()
# REPL mode
if args.interactive:
return repl()
# Execute from string
if args.exec_str:
if args.compile:
try:
python_code = emit_source(args.exec_str)
print(python_code)
return 0
except (LexerError, ParseError, EmitterError) as e:
print(f'ุฎูุทูุฃ: {e}', file=sys.stderr)
return 1
return run_string(args.exec_str)
# No file and no other mode โ REPL
if not args.file:
return repl()
# AST debug mode
if args.ast:
try:
ast = parse_file(args.file)
print_ast(ast)
return 0
except (LexerError, ParseError) as e:
print(f'ุฎูุทูุฃ: {e}', file=sys.stderr)
return 1
# Check mode
if args.check:
return check_file(args.file)
# Compile mode
if args.compile:
return compile_file(args.file)
# Default: run
return run_file(args.file)
if __name__ == '__main__':
sys.exit(main())
|