functions are objects: closures carry a member table (_doc_)

- CAT_TYPES_FUNCTION_Closure gains an attrs table; get/set_attribute
  give closures real members, so a function can hold _doc_ and any other
  attribute like a table
- cat_types_function_attrs() accessor; attributes() enumerates a
  closure's members; help() reads a function's _doc_
- compiler: generalise dotted assignment to chains (name.a.b = v), so a
  method can be documented (Calc.add._doc_ = ...)
- tests/functions_as_objects.cat + 7 assertions (84 total, all green)
- README: 'funcoes tambem sao objetos' + reflection sections
This commit is contained in:
Minus committed 2026-11-16 03:50:54 +00:00
1 parent 1203acd64b
commit e0bf102f2a
7 files changed
+183 -18

No files matched your search

+31
View File
@@ -275,6 +275,37 @@ Gramática da assinatura: `RET(ARG,ARG,...)`, com tipos `void`, `bool`,
Requer `-lffi -ldl` (já no Makefile).
## Funções também são objetos
Uma closure tem sua própria tabela de membros, então carrega `_doc_` (e
qualquer outro atributo) como uma tabela:
```lua
function add(a, b) return a + b end
add._doc_ = "add(a, b) -- soma dois numeros."
help(add) -- lê a doc da função
-- documentando um método (atribuição encadeada)
class Calc
function add(self, a, b) return a + b end
end
Calc.add._doc_ = "add(a, b) on Calc."
```
## Reflection: `attributes()` e `help()`
`attributes(obj)` lista os nomes dos membros de um objeto (como o `dir()`
do Python), seguindo a herança via `__index`; devolve uma **lista**
iterável. `help(obj)` imprime a documentação de um valor, lida do seu
atributo `_doc_`:
```lua
local p = {x = 0, y = 0}
p._doc_ = "A 2D point."
for a in attributes(p) do print(a) end -- x, y, _doc_
help(p) -- A 2D point.
```
## cpk — Cat Package Manager
O primeiro programa em catscript. Lê o `package.toml` de um pacote