Skip to content

Commit cfed5b0

Browse files
committed
Updated readme, adjusted examples, cleaned main and added simple lambda test on parser_expr.rs
1 parent 9028460 commit cfed5b0

File tree

5 files changed

+72
-499
lines changed

5 files changed

+72
-499
lines changed

README.md

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ A single `end` closes the entire if-chain. The `elif` and `else` keywords introd
125125
```text
126126
var i = 0;
127127
while i < 5:
128-
print(i);
128+
var _ = print_line(to_string(i));
129129
i = i + 1;
130-
end
130+
end;
131131
132132
for x in [1, 2, 3]:
133-
print(x);
134-
end
133+
var _ = print_line(to_string(x));
134+
end;
135135
```
136136

137137
- `break` exits the innermost loop immediately.
@@ -153,7 +153,7 @@ def factorial(n: Int) -> Int:
153153
end;
154154
155155
val result = factorial(5);
156-
assertrue(result == 120, "5! should be 120");
156+
asserttrue(result == 120, "5! should be 120");
157157
```
158158

159159
> **Syntax note:** Block statements (`if`, `while`, `for`, `def`) require a semicolon after the closing `end` when followed by additional statements at the same level.
@@ -163,10 +163,12 @@ assertrue(result == 120, "5! should be 120");
163163
Anonymous functions can be assigned to variables or passed as arguments.
164164

165165
```text
166-
val add = lambda (a: Int, b: Int) -> Int: a + b end;
166+
val add = lambda (a: Int, b: Int) -> Int: return a + b end;
167167
val sum = add(2, 3);
168168
```
169169

170+
> **Current limitation:** Lambdas are parsed correctly but not yet fully implemented in the interpreter.
171+
170172
---
171173

172174
## Standard Library (Metabuiltins)
@@ -232,6 +234,8 @@ RPython provides two monadic types for representing optional or fallible values:
232234

233235
ADTs can be declared with multiple constructors. Pattern matching is not yet implemented; values are constructed and passed around opaquely.
234236

237+
> **Current limitation:** ADT declarations are parsed as types but cannot yet be declared as top-level statements. The syntax shown below is the planned syntax; it is not yet functional.
238+
235239
```text
236240
data Shape:
237241
| Circle Int
@@ -290,6 +294,32 @@ src/
290294
- **`interpreter/statement_execute.rs`** — Executes statements; handles loops with `break`/`continue`.
291295
- **`interpreter/expression_eval.rs`** — Evaluates expressions; dispatches metabuiltin calls.
292296
- **`stdlib/standard_library.rs`** — Implements 13 metabuiltins.
297+
- **`pretty_print/`** — AST-to-source formatter (see below).
298+
299+
### Pretty Printer
300+
301+
The `pretty_print` module converts parsed AST nodes back into readable RPython source code. It is primarily used for:
302+
303+
- **Testing round-trip correctness:** parse → pretty-print → compare.
304+
- **Debugging:** inspect how the parser understood your code.
305+
- **Future tooling:** code formatters, IDE integrations.
306+
307+
The pretty printer is **not invoked during normal program execution**. It is available via the `prelude` module for programmatic use:
308+
309+
```rust
310+
use r_python::prelude::{pretty, ToDoc};
311+
// pretty(80, &statement.to_doc()) → formatted String
312+
```
313+
314+
Optional feature flags enable performance instrumentation:
315+
316+
```bash
317+
# Timing metrics
318+
cargo run --features pp-timing --example pp_timing
319+
320+
# Profile counters
321+
cargo run --features pp-profile --example pp_bench
322+
```
293323

294324
---
295325

@@ -347,6 +377,12 @@ The interpreter reads from stdin and writes to stdout, making it suitable for au
347377
5. **Limited error messages:** parser and type checker errors are functional but not always user-friendly.
348378
6. **No tail-call optimization:** deep recursion may overflow the stack.
349379
7. **Maybe/Result constructors:** `Just()`, `Nothing`, `Ok()`, `Err()` cannot be parsed from source code yet.
380+
8. **Small standard library:** only a small set of metabuiltins is available (basic I/O, conversions, simple string/list helpers); there are no rich libraries for math, dates/times, networking, etc.
381+
9. **No exceptions:** there is no `try`/`catch` mechanism or exception hierarchy; errors are represented via `Maybe`/`Result` types or abort execution with an error message.
382+
10. **No objects or methods:** there are no classes, interfaces, or method calls; programs are written with functions, lists/tuples, and algebraic data types.
383+
11. **No concurrency or async:** the language has no built-in support for threads, async/await, or parallel execution.
384+
12. **Interpreter-only, unoptimized:** execution is performed by a tree-walking interpreter without bytecode/JIT or optimization passes, so performance is below production-grade VMs/compilers.
385+
13. **Minimal tooling:** beyond the CLI and internal pretty printer, there is no dedicated debugger, formatter binary, or IDE integration yet.
350386

351387
---
352388

examples/factorial.rpy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def factorial(n: Int) -> Int:
2+
if n <= 1:
3+
return 1;
4+
else:
5+
return n * factorial(n - 1);
6+
end;
7+
end;
8+
9+
val result = factorial(11);
10+
asserttrue(result == 39916800, "factorial(11) should be 39916800");
11+
var _ = print(result);

examples/hello_io.rpy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ var _ = print("5 == 5: ");
3333
var _ = print_line(5 == 5);
3434
var _ = print("10 > 3: ");
3535
var _ = print_line(10 > 3);
36+
var _ = print("2 < 1: ");
37+
var _ = print_line(2 < 1);
3638
var _ = print("True and True: ");
3739
var _ = print_line(True and True);
3840
var _ = print("False or True: ");
3941
var _ = print_line(False or True);
42+
var _ = print("not True: ");
43+
var _ = print_line(not True);
4044
var _ = print("not False: ");
4145
var _ = print_line(not False);
4246

0 commit comments

Comments
 (0)