🌐 AI搜索 & 代理 主页
Skip to content

Commit 92a07e4

Browse files
committed
Minor code changes: add comment to tiny_parser.py explaining function_call expression optimization, and reformat listed functions to easily read them in sorted name order; general rewrite/reword of sections of the TINY README.md
1 parent 508e041 commit 92a07e4

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

examples/tiny/README.md

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ illustrative scripts, using the TINY language.
1111

1212
## Running the REPL
1313

14-
The TINY project also includes an interactive Read–Eval–Print Loop (REPL) for quickly
14+
The TINY project includes an interactive Read–Eval–Print Loop (REPL) for quickly
1515
trying out statements and functions.
1616

1717
- Start the REPL:
@@ -26,7 +26,6 @@ trying out statements and functions.
2626
- Useful keys/behavior:
2727
- Press Ctrl-C while typing to cancel the current partial input and return to a fresh prompt.
2828
- Press Ctrl-C during a long-running execution to interrupt it and return to the prompt.
29-
- The REPL always prints a newline after executing a statement block.
3029

3130
- Built-in REPL commands (typed at an empty prompt):
3231
- `quit` — exit the REPL
@@ -51,12 +50,36 @@ trying out statements and functions.
5150
>>> int x := 5;
5251
>>> write factorial(x);
5352
120
53+
>>> list
54+
[variables]
55+
x = 5 : int
56+
[functions]
57+
abs(float a) : float
58+
cos(float rad_) : float
59+
deg(float rad) : float
60+
div(int a, int b) : int
61+
exp(float x) : float
62+
factorial(int n) : int
63+
hypot(float x, float y) : float
64+
mod(int a, int b) : int
65+
pi() : float
66+
pow(float x, int y) : float
67+
rad(float deg) : float
68+
round(float a, int n) : float
69+
sgn(float x) : int
70+
sin(float rad_) : float
71+
sqrt(float a) : float
72+
tan(float rad_) : float
73+
>>> quit
5474
```
5575

5676
- Errors and debugging:
57-
- By default, non-runtime exceptions raised while executing TINY statements are shown concisely as `ExceptionType: message` without a traceback.
58-
- The same concise behavior applies when importing files with `import`/`reimport` (I/O and other errors print `ExceptionType: message`).
59-
- Turn on verbose debugging with `debug on` to display full Python tracebacks for exceptions during execution. Use `debug off` to return to concise error messages.
77+
- By default, exceptions raised while executing TINY statements are shown concisely as
78+
`ExceptionType: message` without a traceback.
79+
- The same concise behavior applies when importing files with `import`/`reimport` (I/O and other errors
80+
print `ExceptionType: message`).
81+
- Turn on verbose debugging with `debug on` to display full Python tracebacks for exceptions during execution.
82+
Use `debug off` to return to concise error messages.
6083
- In debug mode, file import errors will also show full Python tracebacks.
6184

6285
For a fuller walkthrough of REPL features and development notes, see
@@ -65,23 +88,33 @@ For a fuller walkthrough of REPL features and development notes, see
6588
## Project Structure
6689

6790
- tiny_parser.py
68-
- Defines the TINY language grammar using pyparsing and exposes `parse_tiny(text)` to parse source into `ParseResults`.
91+
- Defines the TINY language grammar using pyparsing and exposes `parse_tiny(text)` to parse source into internal
92+
parser results.
93+
- The parser tags each statement group with a `type` tag (for example: `main_decl`, `decl_stmt`, `assign_stmt`,
94+
`if_stmt`, `repeat_stmt`, `read_stmt`, `write_stmt`, `return_stmt`, `call_stmt`), which is used in `tiny_ast.py`
95+
to instantiate the appropriate executable AST node subclass.
6996
- Independent of execution; focused purely on syntax and result structuring.
70-
- Allows for testing the parser in isolation from any integration or implementation
71-
components.
97+
- Allows for testing the parser in isolation from any integration or implementation components.
7298

7399
- tiny_ast.py
74-
- Declares the abstract base `TinyNode` and node subclasses for each TINY statement type (for example: `main_decl`, `decl_stmt`, `assign_stmt`, `if_stmt`, `repeat_stmt`, `read_stmt`, `write_stmt`, `return_stmt`, `call_stmt`).
100+
- Declares the abstract base `TinyNode` and node subclasses for each TINY statement type.
75101
- Nodes wrap parser results and implement `execute(engine)`; nodes that contain bodies pre-build their child nodes.
76102

77103
- tiny_engine.py
78-
- Implements `TinyEngine`, the runtime responsible for variable scopes (stack frames plus globals), text I/O, expression evaluation, and function invocation.
104+
- Implements `TinyEngine`, the runtime responsible for variable scopes (stack frames plus globals), text I/O,
105+
expression evaluation, and function invocation.
79106
- Provides APIs used by AST nodes: declare/assign variables, evaluate expressions, read/write output, call functions.
80107

81108
- tiny_run.py
82109
- CLI entry point to parse and run a `.tiny` program.
83110
- Registers top-level functions/globals, builds the `main` function node, and executes it using `TinyEngine`.
84-
- Converts the parser's `ParseResults` into an executable hierarchy of `TinyNode` objects, using each statement group's `type` tag to instantiate the correct `TinyNode` subclass.
111+
- Converts the parser's internal results into an executable hierarchy of `TinyNode` objects, using each statement
112+
group's `type` tag to instantiate the correct `TinyNode` subclass.
113+
114+
- tiny_repl.py
115+
- CLI entry point to start the interactive TINY REPL.
116+
- Implements the REPL's command-line interface and REPL-specific logic.
117+
- Uses `tiny_parser.py`, `tiny_ast.py`, and `tiny_engine.py` to parse and execute TINY statements.
85118

86119
- samples/
87120
- Sample TINY programs (for example: `hello.tiny`, `hello_5.tiny`, `factorial.tiny`).
@@ -102,7 +135,8 @@ For a fuller walkthrough of REPL features and development notes, see
102135

103136
Grammar outline: see `docs/grammar.md` and `docs/tiny_parser_diagram.html`
104137

105-
Pyparsing best practices were used to prompt the AI on preferred usages of pyparsing. Accessible using the command `python -m pyparsing.ai.show_best_practices`
138+
Pyparsing best practices were used to prompt the AI on preferred usages of pyparsing. Accessible using the command
139+
`python -m pyparsing.ai.show_best_practices`
106140

107141
## Reference
108142
- TINY language definition: https://a7medayman6.github.io/Tiny-Compiler/Language-Description.html

examples/tiny/tiny_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
+ FunctionName("name")
8585
+ LPAREN
8686
+ (
87+
# fast evaluation of empty arg list, since it is common, and the recursive expr
88+
# parser can be expensive
8789
RPAREN
8890
| pp.DelimitedList(expr)("args") + RPAREN
8991
)

examples/tiny/tiny_repl.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from pathlib import Path
2121
import sys
2222
import traceback
23-
from typing import Iterable
2423

2524
import pyparsing as pp
2625

@@ -206,7 +205,7 @@ def _print_functions() -> None:
206205
sigs = engine.get_function_signatures()
207206
for name in names:
208207
fn_ret_type, fn_params = sigs[name]
209-
print(f" {fn_ret_type} {name}({', '.join(' '.join(p) for p in fn_params)})")
208+
print(f" {name}({', '.join(' '.join(p) for p in fn_params)}) : {fn_ret_type}")
210209

211210
if lower in ("list", "list vars"):
212211
_print_vars()

0 commit comments

Comments
 (0)