-
Notifications
You must be signed in to change notification settings - Fork 3
/
radon.py
executable file
·184 lines (151 loc) · 5.64 KB
/
radon.py
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
#!/usr/bin/python3.12
# By: Md. Almas Ali
import os
import platform
import sys
from typing import IO, Optional
from core.datatypes import Value
from core.errors import Error, RTError
try:
import readline
# Enable arrow key support
readline.parse_and_bind(r'"\e[A": history-search-backward')
readline.parse_and_bind(r'"\e[B": history-search-forward')
readline.parse_and_bind(r'"\e[C": forward-char')
readline.parse_and_bind(r'"\e[D": backward-char')
except ImportError:
pass
import core as base_core
from core.colortools import Log
from core.parser import Context
from core.tokens import Position
documentation_link = "https://radon-project.github.io/docs/"
def start_text() -> None:
print(
f"\033[1;34mRadon {base_core.__version__} on {platform.machine()} {platform.system()} ({sys.platform})\033[0m"
)
print(f"\033[1;33mDocumentation:\033[0m {documentation_link}")
print("\033[1;32mType \033[1;31mhelp(obj), copyright(), credits(), license()\033[1;32m for more info\033[0m")
print("\033[1;32mType \033[1;31mexit\033[1;32m to quit the shell.\033[0m")
def shell() -> None:
start_text()
brace_count = 0
while True:
try:
text = input(">>> ")
if text.strip() == "":
continue
if text.strip() == "exit":
break
if text.strip()[-1] == "{":
brace_count += 1
while True:
text += "\n" + input("... ")
if text.strip()[-1] == "{":
brace_count += 1
elif text.strip()[-1] == "}" or text.strip()[0] == "}":
brace_count -= 1
if brace_count == 0:
break
result: list[Optional[Value]]
error: Error | RTError | None
should_exit: Optional[bool]
(result, error, should_exit) = base_core.run("<stdin>", text, import_cwd=os.getcwd()) # type: ignore
if error:
print(error.as_string())
else:
if result:
if len(result) == 1:
# result = result[0]
print(repr(result[0]))
else:
print(repr(result))
if should_exit:
break
except KeyboardInterrupt:
print("KeyboardInterrupt")
def usage(program_name: str, stream: IO[str]) -> None:
print(
f"Usage: {program_name} [--source | -s] [--command | -c] [source_file] [--version | -v] [--help | -h]",
file=stream,
)
print(
"""
Options and arguments:
--source | -s Run a source file
--command | -c Run a command
--version | -v Print the version
--help | -h Print this help message
If no arguments are provided, the program will run in shell mode.
Example:
radon --source source_file.rn
radon --command 'print("Hello, World!")'
radon --version
radon --help
The Radon Programming Language.
"""
)
def main(argv: list[str]) -> None:
program_name = argv.pop(0)
source_file = None
command = None
while len(argv) > 0:
arg = argv.pop(0)
match arg:
case "--help" | "-h":
usage(program_name, sys.stdout)
exit(0)
case "--source" | "-s":
if len(argv) == 0:
usage(program_name, sys.stderr)
print(f"ERROR: {arg} requires an argument", file=sys.stderr)
exit(1)
source_file = argv.pop(0)
case "--version" | "-v":
print(base_core.__version__)
exit(0)
case "--command" | "-c":
if len(argv) == 0:
usage(program_name, sys.stderr)
print(f"ERROR: {arg} requires an argument", file=sys.stderr)
exit(1)
command = argv.pop(0)
# These flags starting with --allow should only be used for testing, and not be allowed to be set by a user
case "--allow-all" | "-A":
base_core.security.allow_all_permissions()
case "--allow-disk" | "-D":
base_core.security.allowed["disk_access"] = True
case "--allow-py" | "-P":
base_core.security.allowed["pyapi_access"] = True
case "--allow-network" | "-W":
base_core.security.allowed["network_access"] = True
case _:
usage(program_name, sys.stderr)
print(f"ERROR: Unknown argument '{arg}'", file=sys.stderr)
exit(1)
pos = Position(0, 0, 0, "<argv>", "<argv>")
base_core.global_symbol_table.set("argv", base_core.radonify(argv, pos, pos, Context("<global>")))
if source_file is not None:
head, _ = os.path.split(source_file)
try:
with open(source_file, "r") as f:
source = f.read()
except FileNotFoundError:
print(Log.deep_error(f"[!] FileNotFound: {Log.deep_error(source_file, bold=True)}"))
exit(1)
error: Error | RTError | None
should_exit: Optional[bool]
(_, error, should_exit) = base_core.run(source_file, source, import_cwd=head) # type: ignore
if error:
print(error.as_string())
exit(1)
if should_exit:
exit()
elif command is not None:
(_, error, should_exit) = base_core.run("<cli>", command) # type: ignore
if error:
print(error.as_string())
else:
shell()
if __name__ == "__main__":
main(sys.argv)