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
59 changes: 34 additions & 25 deletions lib/python/parse.x
Original file line number Diff line number Diff line change
Expand Up @@ -3796,7 +3796,8 @@
; a `def` binds its function under the method's NAME STRING, since Python
; looks methods up by name at call time and the symbol is only how x would
; have bound it; a decorated def binds deco(fn); `NAME = value` binds the
; value; and a bare expression -- a docstring, a print -- is evaluated for
; value; a nested `class` binds its class as a def binds its function; and a
; bare expression -- a docstring, a print -- is evaluated for
; its effect. Those are the statements a class body takes here. The body's
; own expressions read what it has bound so far (%py-class-level).

Expand All @@ -3806,30 +3807,28 @@
(let ((t (%py-async-strip (%py-skip-nl toks))))
(match
((null? t) (pair (python-reverse acc) t))
; A DECORATED METHOD is the same entry with a call around its function:
; the decorators are collected, the def is parsed as it always was, and
; what is bound is deco(fn) rather than fn.
; A DECORATED METHOD OR CLASS is the same entry with a call around its
; value: the decorators are collected, the def or class is parsed as it
; always was, and what is bound is deco(value) rather than value.
((%py-op-is? (first t) "@")
(let ((ds (%py-decos-of t ())))
(let ((t2 (%py-async-strip (rest ds))))
(if (not (%py-kw? (if (null? t2) () (first t2)) "def"))
(Err raise (lit syntax) "a decorator must be followed by a def" ())
(let ((nm (if (null? (rest t2)) () (first (rest t2)))))
(if (not (eq? (%py-label nm) (lit tok-name)))
(Err raise (lit syntax) "expected a method name after def" ())
(let ((r (%py-def (rest t2))))
(self (rest r)
(pair
(%py-class-bind (%py-val nm)
(%py-wrap-decos (first ds) (first (rest (rest (first r))))))
acc)))))))))
(if (if (%py-kw? (if (null? t2) () (first t2)) "def") #f
(not (%py-kw? (if (null? t2) () (first t2)) "class")))
(Err raise (lit syntax) "a decorator must be followed by a def or a class" ())
(let ((r (%py-class-entry-of t2)))
(self (rest r)
(pair
(%py-class-bind (first (first r))
(%py-wrap-decos (first ds) (rest (first r))))
acc)))))))
((%py-kw? (first t) "pass") (self (rest t) acc))
; a name the body declares nonlocal is the enclosing function's, and
; what the body binds to it goes there (%py-class-bind); the
; declaration itself compiles to nothing
((%py-kw? (first t) "nonlocal")
(self (rest (%py-line-of (rest t) ())) acc))
((not (%py-kw? (first t) "def"))
((if (%py-kw? (first t) "def") #f (not (%py-kw? (first t) "class")))
(if (if (eq? (%py-label (first t)) (lit tok-name))
(%py-op-is? (if (null? (rest t)) () (first (rest t))) "=")
#f)
Expand All @@ -3838,12 +3837,21 @@
(let ((v (%py-exprlist t)))
(self (rest v) (pair (first v) acc)))))
(#t
(let ((nm (if (null? (rest t)) () (first (rest t)))))
(if (not (eq? (%py-label nm) (lit tok-name)))
(Err raise (lit syntax) "expected a method name after def" ())
(let ((r (%py-def (rest t))))
(self (rest r)
(pair (%py-class-bind (%py-val nm) (first (rest (rest (first r))))) acc))))))))))
(let ((r (%py-class-entry-of t)))
(self (rest r) (pair (%py-class-bind (first (first r)) (rest (first r))) acc))))))))

; A `def` or a `class` in a class body, T at its keyword: ((NAME . VALUE) .
; rest), the name as text and the form its statement would bind to it -- a
; nested class is a row of the namespace as a method is.
(def %py-class-entry-of
(fn (_ t)
(let ((nm (if (null? (rest t)) () (first (rest t))))
(def? (%py-kw? (first t) "def")))
(if (not (eq? (%py-label nm) (lit tok-name)))
(Err raise (lit syntax)
(if def? "expected a method name after def" "expected a class name after class") ())
(let ((r (if def? (%py-def (rest t)) (%py-class-stmt (rest t)))))
(pair (pair (%py-val nm) (first (rest (rest (first r))))) (rest r)))))))

(def %py-crow-form
(fn (_ name form) (list (lit %py-crow-put!) (lit %py-crows) name form)))
Expand Down Expand Up @@ -3882,13 +3890,14 @@
((%py-str-seen? (first xs) ys) (self (rest xs) ys))
(#t (pair (first xs) (self (rest xs) ys))))))

; The names a class body binds at its own level, as text: each `def NAME`,
; decorated or not, and each `NAME =`. A def's body is its own and skipped.
; The names a class body binds at its own level, as text: each `def NAME` and
; `class NAME`, decorated or not, and each `NAME =`. A def's or a nested
; class's body is its own and skipped.
(def %py-class-level-names
(fn (self toks acc)
(match
((null? toks) acc)
((%py-kw? (first toks) "def")
((if (%py-kw? (first toks) "def") #t (%py-kw? (first toks) "class"))
(let ((n (if (null? (rest toks)) () (first (rest toks)))))
(self (%py-skip-def (rest toks) 0)
(if (eq? (%py-label n) (lit tok-name)) (pair (%py-val n) acc) acc))))
Expand Down
13 changes: 13 additions & 0 deletions tests/specs/73-class-body-and-edges.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ global class global ('closure', 'closure!')
in body deco
global global deco:m 5 2 [2, 4]
```

### a class body holds a class: plain, decorated, one line, read by the body and its methods

```python
(python-run "def deco75(c):\n c.tagged = True\n return c\n\nclass A75:\n \"\"\"outer\"\"\"\n class B75:\n y = 2\n def f(self):\n return \"B75.f\"\n @deco75\n class C75(B75):\n z = 4\n class D75: pass\n w = B75.y + 1\n def make(self):\n return self.B75().f()\n\nprint(A75.B75.y, A75.w, A75.C75.z, A75.C75.y, A75.C75.tagged, A75().make())\nprint(A75.B75.__name__, A75.D75.__name__)\nprint(sorted(k for k in vars(A75) if not k.startswith(\"__\")))\nx75 = 10\nclass E75:\n x75 = 1\n class F75:\n g = [x75]\nprint(E75.F75.g)\n")
```
---
```output
2 3 4 2 True B75.f
B75 D75
['B75', 'C75', 'D75', 'make', 'w']
[10]
```
Loading