Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 98 additions & 20 deletions python/repl.x
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,104 @@
(if (eq? lines (lit cancel)) lines (Str8 join "\n" lines)))
first-line)))

; --- where an entry failed ---------------------------------------------------
;
; An error is reported against the entry that raised it, in the layout of a
; CPython traceback's last frame: the line the entry began on, its text, and
; a caret under each character of it, ahead of the error's own line. The
; entry is the whole of what is known: a token carries no position and an
; emitted form no line. So an entry of one line is marked, and a block is
; shown by its header with no carets, since they would point at a line that
; may not be the one that failed.
;
; Lines are counted where they are read with no editor -- a program on
; standard input -- so the number is the program's own. An entry typed at
; the editor is line 1, as each is at CPython's prompt.
(def %py-lines-read (pair 0 ()))
(def %py-entry-line (pair 1 ()))
(def %py-entry-now (pair "" ()))

(def %py-read-counted
(fn (_ prompt)
(let ((line (python-read prompt)))
(if (if (symbol? line) #f (not (guard (_ #f) (Line available?))))
(%set-first! %py-lines-read (+ (first %py-lines-read) 1))
())
line)))

; An entry begins: its first line, and the line it is on.
(def %py-entry-begin!
(fn (_ line)
(%set-first! %py-entry-now line)
(%set-first! %py-entry-line
(if (= (first %py-lines-read) 0) 1 (first %py-lines-read)))))

; Past the last byte of S's first N that is not a blank or a carriage return.
(def %py-text-end
(fn (self s n)
(if (> n 0)
(let ((b (python-code-at s (- n 1))))
(if (match ((= b 32) #t) ((= b 9) #t) (#t (= b 13))) (self s (- n 1)) n))
0)))

; The characters in bytes I to N of S: every byte but a UTF-8 continuation.
(def %py-chars
(fn (self s i n acc)
(if (>= i n)
acc
(let ((b (python-code-at s i)))
(self s (+ i 1) n (if (if (>= b 128) (< b 192) #f) acc (+ acc 1)))))))

(def %py-carets
(fn (self n acc) (if (> n 0) (self (- n 1) (Str8 append acc "^")) acc)))

(def %py-entry-report
(fn (_ entry)
(let ((nl (Str8 index-of "\n" entry)))
(let ((text (if (null? nl) entry (Str8 sub 0 nl entry))))
(let ((n (%py-text-end text (python-byte-len text))))
(let ((i (%py-past-blanks text 0 n)))
(do
(display " File \"<stdin>\", line ")
(display (first %py-entry-line))
(newline)
(display " ")
(display (Str8 sub i (- n i) text))
(newline)
(if (null? nl)
(do
(display " ")
(display (%py-carets (%py-chars text i n 0) ""))
(newline))
()))))))))

; What both loops print for an error: where, then what. A report that
; cannot be drawn does not take the error's own line with it.
(def %py-entry-error
(fn (_ err)
(do
(guard (_ ()) (%py-entry-report (first %py-entry-now)))
(display "Error: ")
(display err)
(newline))))

; One entry, from its first line: the rest of it read, then evaluated, and
; an error from either reported where the entry is (%py-entry-error).
(def %py-entry-run
(fn (_ line more)
(do
(%py-entry-begin! line)
(guard (%py-err (%py-entry-error %py-err))
(let ((entry (%py-read-entry line more)))
(unless (eq? entry (lit cancel))
(do
(%set-first! %py-entry-now entry)
(%py-repl-eval entry))))))))

; The loop's own way to the next line: the editor when there is a terminal,
; the byte reader otherwise (python/line.x), the prompt drawn either way.
(def %py-more-from-loop
(fn (_) (python-read "... ")))
(fn (_) (%py-read-counted "... ")))

; --- the loop ----------------------------------------------------------------
(def %python-repl ())
Expand All @@ -225,7 +319,7 @@
(fn (_)
; Through the line editor when there is a terminal (python/line.x),
; which draws the prompt itself; through the byte reader otherwise.
(let ((line (python-read ">>> ")))
(let ((line (%py-read-counted ">>> ")))
(match
((eq? line (lit eof)) (%seq (newline) (Sys exit 0)))
; ctrl-c abandons the line being typed.
Expand All @@ -235,14 +329,7 @@
(Sys exit 0))
(#t
(%seq
(guard (%py-err
(%seq
(display "Error: ")
(%seq
(display %py-err)
(newline))))
(let ((entry (%py-read-entry line %py-more-from-loop)))
(unless (eq? entry (lit cancel)) (%py-repl-eval entry))))
(%py-entry-run line %py-more-from-loop)
(%python-repl-loop)))))))

; --- the editor's way in -----------------------------------------------------
Expand All @@ -261,16 +348,7 @@
(match
((= (Str8 length line) 0) ())
((if (Str8 =? line "quit()") #t (Str8 =? line "exit()")) (Sys exit 0))
(#t
(let ((entry (%py-read-entry line %py-more-from-editor)))
(unless (eq? entry (lit cancel))
(guard (%py-err
(%seq
(display "Error: ")
(%seq
(display %py-err)
(newline))))
(%py-repl-eval entry))))))))
(#t (%py-entry-run line %py-more-from-editor)))))

; Python's own spelling of the switch: lang("x"). The name arrives as a
; Python str, code points, and Lang wants bytes.
Expand Down
2 changes: 2 additions & 0 deletions tests/gen-harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ cat > "$OUT" <<EOX
; The privates the specs read, each through its module.
(def %py-class-of (eval (lit %py-class-of) (module python/core)))
(def %py-dset (eval (lit %py-dset) (module python/core)))
(def %py-entry-run (eval (lit %py-entry-run) (module python/core)))
(def %py-eval-line (eval (lit %py-eval-line) (module python/core)))
(def %py-f-exact (eval (lit %py-f-exact) (module python/core)))
(def %py-fmtfield (eval (lit %py-fmtfield) (module python/core)))
Expand All @@ -85,6 +86,7 @@ cat > "$OUT" <<EOX
(def %py-jit-compile! (eval (lit %py-jit-compile!) (module python/tokens)))
(def %py-jit-number-labels (eval (lit %py-jit-number-labels) (module python/tokens)))
(def %py-jit-threshold (eval (lit %py-jit-threshold) (module python/tokens)))
(def %py-lines-read (eval (lit %py-lines-read) (module python/core)))
(def %py-mkclass (eval (lit %py-mkclass) (module python/core)))
(def %py-number-label! (eval (lit %py-number-label!) (module python/tokens)))
(def %py-opens-block? (eval (lit %py-opens-block?) (module python/core)))
Expand Down
24 changes: 24 additions & 0 deletions tests/golden/shapes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# shapes.py -- one of each layout the tokenizer and the parser decide
import math

class Shape:
def __init__(self, name, sides=0):
self.name = name
self.sides = sides

def describe(self):
return f"{self.name}: {self.sides} sides"

def area(kind, *dims):
if kind == "circle":
return math.pi * dims[0] ** 2
elif kind == "rect":
return dims[0] * dims[1]
else:
raise ValueError("unknown " + kind)

shapes = [Shape(n, s) for n, s in (("triangle", 3), ("square", 4))]
total = 0
for s in shapes:
total += s.sides # counted
print(total, {s.name: s.sides for s in shapes}, area("rect", 2, 0x10))
76 changes: 76 additions & 0 deletions tests/specs/91-repl-editor.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,86 @@ left of the cursor, so a stand-in with that method serves here as it does in
```
---
```output
File "<stdin>", line 1
1 +
^^^
Error: syntax: unexpected end of input in expression
still here
```

## where an entry failed

An error is reported against the entry that raised it: the line the entry
began on, its text, and a caret under each character of it. The entry is the
whole of what is known, so a block is shown by its header and carries no
carets.

### an error raised while the line runs is marked the same way

```python
(do
(%py-eval-line "q91 = [1, 2][7]")
(display "still here") (newline))
```
---
```output
File "<stdin>", line 1
q91 = [1, 2][7]
^^^^^^^^^^^^^^^
Error: index: list index out of range
still here
```

### indentation and trailing blanks are not marked, and a character is one caret

```python
(%py-entry-run "é91 = 1 + " (fn (_) (lit eof)))
```
---
```output
File "<stdin>", line 1
é91 = 1 +
^^^^^^^^^
Error: syntax: unexpected end of input in expression
```

### a block is shown by its header, with no carets

```python
(do
(def %more91 (pair (list " 1 +" "") ()))
(%py-entry-run "for i91 in [1]:"
(fn (_)
(let ((l (first (first %more91))))
(do (%set-first! %more91 (rest (first %more91))) l)))))
```
---
```output
File "<stdin>", line 1
for i91 in [1]:
Error: syntax: unexpected end of input in expression
```

### a program on standard input is reported by its own line

The byte loop counts the lines it reads, and an entry begins on the line it
read last.

```python
(do
(%set-first! %py-lines-read 6)
(%py-entry-run "1 +" (fn (_) (lit eof)))
(%set-first! %py-lines-read 0)
())
```
---
```output
File "<stdin>", line 6
1 +
^^^
Error: syntax: unexpected end of input in expression
```

## the registry

### install registers "python" in the platform's registry
Expand Down
Loading
Loading