Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitec232cd

Browse files
committed
Add TinyEngine.get_functions and get_function_signatures() so that repl does not to peek into private vars; refactor repl to pull meta-command handling out to its own function (to keep down the size of the repl loop)
1 parent0bfab4a commitec232cd

File tree

2 files changed

+109
-88
lines changed

2 files changed

+109
-88
lines changed

‎examples/tiny/tiny_engine.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ def register_function_signature(
113113
"""
114114
self._function_sigs[name]= (return_type,params)
115115

116+
defget_functions(self)->dict[str,TinyNode]:
117+
return {**self._functions}
118+
119+
defget_function_signatures(self)->dict[str,tuple[str,list[tuple[str,str]]]]:
120+
return {**self._function_sigs}
121+
116122
# ----- Frame management -----
117123
@property
118124
defcurrent_frame(self)->TinyFrame:

‎examples/tiny/tiny_repl.py‎

Lines changed: 103 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,106 @@ def _try_execute(engine: TinyEngine, source: str) -> None:
159159
engine.output_text()
160160
print()
161161

162+
defhandle_meta_command(engine:TinyEngine,cmd:str,debug:list[bool])->bool:
163+
# normalize to lowercase, and collapse whitespace
164+
lower=" ".join(cmd.lower().split())
165+
line=cmd.strip()
166+
167+
iflower=="help":
168+
print(f"TINY REPL v{TINY_VERSION}")
169+
print("Commands:")
170+
print(" help - list commands and descriptions")
171+
print(" quit - exit the REPL")
172+
print(" import <file> - load functions from a .tiny file")
173+
print(" reimport <file> - load functions, overwriting existing")
174+
print(" clear vars - clear current local variables")
175+
print(" clear all - reset engine state (all vars, funcs)")
176+
print(" list - list variables and functions")
177+
print(" list vars - list only current variables")
178+
print(" list functions - list defined function names")
179+
print(" debug on - show full Python tracebacks")
180+
print(" debug off - concise errors; hide tracebacks")
181+
returnTrue
182+
183+
iflower=="quit":
184+
returnTrue
185+
186+
iflowerin ("list","list vars","list functions"):
187+
# Listing helpers
188+
def_print_vars()->None:
189+
frame=engine.current_frame
190+
vars_dict=getattr(frame,"_vars", {})# type: ignore[attr-defined]
191+
names=sorted(vars_dict.keys())
192+
ifnotnames:
193+
print("[variables] (none)")
194+
else:
195+
print("[variables]")
196+
fornameinnames:
197+
dtype,value=vars_dict[name]
198+
print(f"{name} ={value!r} :{dtype}")
199+
200+
def_print_functions()->None:
201+
funcs=engine.get_functions()
202+
names=sorted(funcs.keys())
203+
ifnotnames:
204+
print("[functions] (none)")
205+
else:
206+
print("[functions]")
207+
sigs=engine.get_function_signatures()
208+
fornameinnames:
209+
fn_ret_type,fn_params=sigs[name]
210+
print(f"{fn_ret_type}{name}({', '.join(' '.join(p)forpinfn_params)})")
211+
212+
iflowerin ("list","list vars"):
213+
_print_vars()
214+
iflowerin ("list","list functions"):
215+
_print_functions()
216+
returnTrue
217+
218+
# Debug mode commands
219+
iflower=="debug on":
220+
debug[0]=True
221+
print("[debug: on]")
222+
returnTrue
223+
iflower=="debug off":
224+
debug[0]=False
225+
print("[debug: off]")
226+
returnTrue
227+
228+
# import commands
229+
ifcmd.startswith("import "):
230+
try:
231+
_,rest=line.split(None,1)
232+
exceptValueError:
233+
print("usage: import <file>")
234+
returnTrue
235+
_load_functions_from_file(engine,rest.strip(),overwrite=False,debug=debug[0])
236+
returnTrue
237+
ifline.lower().startswith("reimport "):
238+
try:
239+
_,rest=line.split(None,1)
240+
exceptValueError:
241+
print("usage: reimport <file>")
242+
returnTrue
243+
_load_functions_from_file(engine,rest.strip(),overwrite=True,debug=debug[0])
244+
returnTrue
245+
246+
# clear/reset commands
247+
iflower=="clear vars":
248+
ifengine._frames:# type: ignore[attr-defined]
249+
engine._frames[-1]=TinyFrame()# type: ignore[attr-defined]
250+
else:
251+
engine.push_frame()
252+
print("[locals cleared]")
253+
returnTrue
254+
255+
iflower=="clear all":
256+
engine=TinyEngine()
257+
engine.push_frame()
258+
print("[engine reset]")
259+
returnTrue
260+
261+
returnFalse
162262

163263
defrepl()->int:
164264
print(f"TINY REPL v{TINY_VERSION} — enter statements on one or more lines. Ctrl-C to cancel current input; `quit` to exit.")
@@ -187,99 +287,14 @@ def repl() -> int:
187287
# If starting fresh, allow immediate REPL commands
188288
ifnotbuffer_lines:
189289
cmd=line.strip()
190-
lower=cmd.lower()
191290
ifnotcmd:
192291
# ignore empty input
193292
continue
194-
iflower=="help":
195-
print(f"TINY REPL v{TINY_VERSION}")
196-
print("Commands:")
197-
print(" help - list commands and descriptions")
198-
print(" quit - exit the REPL")
199-
print(" import <file> - load functions from a .tiny file")
200-
print(" reimport <file> - load functions, overwriting existing")
201-
print(" clear vars - clear current local variables")
202-
print(" clear all - reset engine state (all vars, funcs)")
203-
print(" list - list variables and functions")
204-
print(" list vars - list only current variables")
205-
print(" list functions - list defined function names")
206-
print(" debug on - show full Python tracebacks")
207-
print(" debug off - concise errors; hide tracebacks")
208-
continue
209-
iflower=="quit":
210-
break
211-
iflower=="list"orlower=="list vars"orlower=="list functions":
212-
# Listing helpers
213-
def_print_vars()->None:
214-
frame=engine.current_frame
215-
vars_dict=getattr(frame,"_vars", {})# type: ignore[attr-defined]
216-
names=sorted(vars_dict.keys())
217-
ifnotnames:
218-
print("[variables] (none)")
219-
else:
220-
print("[variables]")
221-
fornameinnames:
222-
dtype,value=vars_dict[name]
223-
print(f"{name} ={value!r} :{dtype}")
224-
225-
def_print_functions()->None:
226-
funcs=getattr(engine,"_functions", {})# type: ignore[attr-defined]
227-
names=sorted(funcs.keys())
228-
ifnotnames:
229-
print("[functions] (none)")
230-
else:
231-
print("[functions]")
232-
fornameinnames:
233-
print(f"{name}")
234-
235-
iflower=="list":
236-
_print_vars()
237-
_print_functions()
238-
continue
239-
iflower=="list vars":
240-
_print_vars()
241-
continue
242-
iflower=="list functions":
243-
_print_functions()
244-
continue
245293

246-
# Debug mode commands
247-
iflower=="debug on":
248-
debug=True
249-
print("[debug: on]")
250-
continue
251-
iflower=="debug off":
252-
debug=False
253-
print("[debug: off]")
254-
continue
294+
ifhandle_meta_command(engine,cmd, [debug]):
295+
ifcmd.strip().lower()=="quit":
296+
break
255297

256-
ifline.lower().startswith("import "):
257-
try:
258-
_,rest=line.split(None,1)
259-
exceptValueError:
260-
print("usage: import <file>")
261-
continue
262-
_load_functions_from_file(engine,rest.strip(),overwrite=False,debug=debug)
263-
continue
264-
ifline.lower().startswith("reimport "):
265-
try:
266-
_,rest=line.split(None,1)
267-
exceptValueError:
268-
print("usage: reimport <file>")
269-
continue
270-
_load_functions_from_file(engine,rest.strip(),overwrite=True,debug=debug)
271-
continue
272-
iflower=="clear vars":
273-
ifengine._frames:# type: ignore[attr-defined]
274-
engine._frames[-1]=TinyFrame()# type: ignore[attr-defined]
275-
else:
276-
engine.push_frame()
277-
print("[locals cleared]")
278-
continue
279-
iflower=="clear all":
280-
engine=TinyEngine()
281-
engine.push_frame()
282-
print("[engine reset]")
283298
continue
284299

285300
# Treat as part of TINY input

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp