Skip to content

Commit 44d14fb

Browse files
committed
Merge branch 'cli-docs' of https://github.com/JeffersGlass/spy into cli-docs
2 parents e5c2d13 + 4d99cac commit 44d14fb

46 files changed

Lines changed: 751 additions & 290 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/docs-preview-build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ on:
1616
pull_request:
1717
paths:
1818
- 'docs/**'
19+
- 'spy/cli/**' # Rebuild cli docs when cli changes
1920
- '.github/workflows/docs**'
2021
types:
2122
- opened

.github/workflows/docs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
- 'docs/**'
1010
paths:
1111
- 'docs/**'
12+
- 'spy/cli/**' # Rebuild cli docs when cli changes
1213
workflow_dispatch: # Allows manual triggering
1314

1415
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages

docs/mkdocs.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,23 @@ markdown_extensions:
4444
alternate_style: true
4545
- toc:
4646
permalink: true
47+
toc_depth: 3
4748
- def_list
48-
- mkdocs-typer
49+
50+
plugins:
51+
- mkdocs-typer2
4952

5053
nav:
5154
- Home: index.md
5255
- contributing.md
5356
- Language features:
5457
- howto/main_function.md
55-
- howto/cli.md
56-
- API Reference:
58+
- howto/generics.md
59+
- Reference:
5760
- api_reference/python_builtins.md
5861
- api_reference/spy_builtins.md
5962
- api_reference/spy_module.md
63+
- api_reference/cli.md
6064
- Unsorted notes:
6165
- Low-level memory model: llmem.md
6266

docs/src/api_reference/cli.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
title: SPy CLI
2+
____
3+
4+
::: mkdocs-typer2
5+
:module: spy.cli.cli
6+
:name: spy
7+
:engine: legacy
8+
:pretty: false

docs/src/howto/cli.md

Lines changed: 0 additions & 11 deletions
This file was deleted.

docs/src/howto/generics.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
title: Generic Functions and Types
2+
---
3+
<!-- See https://github.com/spylang/spy/pull/519 and https://github.com/spylang/spy/pull/448-->
4+
5+
### Generic Functions
6+
7+
Functions decorated with [@blue.generic](../api_reference/spy_builtins.md#bluegeneric) are called with `[]` brackets instead of parentheses. These *may* be used anywhere, but they are intended to help create functions that look like [PEP 695](https://peps.python.org/pep-0695/) functions with type parameters:
8+
9+
```python
10+
@blue.generic
11+
def add(T):
12+
def impl(x:T, y: T) -> T:
13+
return x + y
14+
return impl
15+
16+
def main() -> None:
17+
print(add[i32](1, 2))
18+
print(add[str]('hello ', 'world'))
19+
```
20+
21+
Like all functions marked `@blue`, the generic function is guaranteed to be executed at compile-time. We can see in the redshifted version of the above code that `add()` no longer appears, but the two specialized versions of it remain:
22+
23+
<!-- Colorful code formatted by ansi2html -->
24+
<style type="text/css">
25+
.ansi2html-content { display: block; white-space: pre-wrap; word-wrap: break-word; font-size: .85em; padding:1.1em; corner-radius: 0.1em}
26+
.ansi32 { color: #00aa00; }
27+
.ansi33 { color: #aa5500; }
28+
.ansi34 { color: #0000aa; }
29+
.ansi35 { color: #E850A8; }
30+
</style>
31+
<div class="body_background" style="background-color:rgb(245, 245, 245);">
32+
<pre class="ansi2html-content">
33+
<span class="ansi34">def</span> main() -&gt; <span class="ansi34">None</span>:
34+
<span class="ansi35">`_print::println[i32]::p`</span>(<span class="ansi35">`t::add[i32]::impl`</span>(<span class="ansi33">1</span>, <span class="ansi33">2</span>))
35+
<span class="ansi35">`_print::println[str]::p`</span>(<span class="ansi35">`t::add[str]::impl`</span>(<span class="ansi32">'hello '</span>, <span class="ansi32">'world'</span>))
36+
37+
<span class="ansi34">def</span> <span class="ansi35">`t::add[i32]::impl`</span>(x: <span class="ansi35">i32</span>, y: <span class="ansi35">i32</span>) -&gt; <span class="ansi35">i32</span>:
38+
<span class="ansi34">return</span> x + y
39+
40+
<span class="ansi34">def</span> <span class="ansi35">`t::add[str]::impl`</span>(x: <span class="ansi35">str</span>, y: <span class="ansi35">str</span>) -&gt; <span class="ansi35">str</span>:
41+
<span class="ansi34">return</span> <span class="ansi35">`operator::str_add`</span>(x, y)
42+
</pre>
43+
</div>
44+
45+
### Generic Class Syntax
46+
47+
`@struct` classes may also be created with one or more parameters in `[]` brackets. This is different from passing superclasses inside of `()` parentheses; rather, this is syntactic sugar for a generic function with an inner `@struct` class than can make use of those parameters:
48+
49+
```py
50+
@struct
51+
class MyList[T]:
52+
inner: list[T]
53+
other_param_1: ...
54+
other_param_2: ...
55+
56+
# ↑ is syntactic sugar for ↓
57+
58+
@blue.generic
59+
def MyList(T):
60+
@struct
61+
class Self:
62+
inner: list[T]
63+
other_param_1: ...
64+
other_param_2: ...
65+
66+
return Self
67+
```
68+
69+
In use, this looks like:
70+
71+
```py
72+
@struct
73+
class MyNamedList[T]:
74+
name: str
75+
data: list[T]
76+
77+
def main() -> None:
78+
my_int_list = MyNamedList[i32]("profits", [])
79+
my_int_list.data.append(1_000_000)
80+
81+
my_str_list = MyNamedList[str]("words", ["hello", "world"])
82+
my_str_list.data.extend(["and", "goodbye"])
83+
```
84+
85+
For a larger example, see the `myarray` example [on GitHub](https://github.com/spylang/spy/blob/main/examples/myarray.spy) or [run it in the SPy Playground](https://spylang.github.io/spy/#code=eJydVcFO40AMvecrrHBpdiGiwHKoFsRKcOCCkOCyQlVkEqed3XQmmplQytevJ5OmkzZIK6oeEnv8bL9nT-I4jp6XwgD_lSRQJdglwUoZC1i8ocypAHrHVV2Rcd6nxw0YBSXqNIruLQjnWZG0pg18RSPy9iDCgiRpkQNqjRuwm5qi6EFZ4oNowVjd5JxEE9RoDKd53cAbVg3nmYiUUshVLahI0ijmIqNSqxU00mBJLqnSFhZ5hlWl8mP3VFsdRdGNh43yijHhl8t8ixZfbp9_P97NZxHwryK5sMsZiPOz9j3HGnNhNzuLsLQysw61i2XweEvWtjOfhQ1_GubLbKTF3LLdNAvUUCqu6OaVO0q7gKigclfUpAVOfFHbwtuCWtgnqkrvGyt6vPDPi3ceTbbRsgUOlXC6SVz14udK5nyUWslcd1itcWMgdoFxJ8cY09NiwPMRo3F0J7TDcQYJH6TVSe4mbK2xrknzEKhGFm3yOMuqKstiqJWQlnQKz2xlWhusoGDWHNCSsD5ppUdLRdpm83F93_vSdxQcgeS-Z4yg6bhlgtvVmkytZMEjrEYYGPaWeiCnZJZJWmfZJNAmgZPrPeWOuO-uGcZfoy78qvi1cC3tRK7gqp_qwxYm0yQ4mvq0HOEfQtd2Mkad7YCEiTx818YuheAzp_3beikqngb4uR3F3hPCvoj5IGqLI-A7THtrMIhplq3wLzkWq2RHrZsLWUwMnzj2t8IM_Lo4gh_4qpoNWTMey83AroEyoOn6KiRmWP0RV2TEB68BUcE30QkUqnmtyA9DFzKIcMKHJAeUf4OzYfvl3mnmZ5h-BHB6kO0z1cLAZK8rvkE3fHsLY4Vc-IvhQJmhVoHKW-bGaw3UDrQ_OHqovVesH8Iea_9AQEbYYnQwyS99pa6YdlbGt6R_duUES7wg64B4BP24id0mtxz_36SJbsLGSNMoDMG9LOj9Tmv-LPQOvwghg0FhZrQw3oev7cKXKhwu9htf_K62FQo52UuPWVkptE7U7Y1ZXl7wpXWadH4hB17uZT65CJ0vp_N-9jvL1FnOQsuZs5yHlnNnuQgsaXd9_EhGjJfeyF9nZoQ_Rxrlgti6I6TWfH7SgYt5Ev0DKx6k_A==)
86+
87+
### `__origin__`
88+
89+
The `__origin__` attribute of SPy objects carries information about the generic function which created them, if any. When `blue.generic` defines a `type` or `function` and returns it, the returned object has it's `__origin__` is set to the generic function:
90+
91+
```py
92+
@blue.generic
93+
def adder(T):
94+
@struct
95+
class impl:
96+
...
97+
98+
return impl
99+
100+
101+
def main() -> None:
102+
assert adder[T].__origin__ is adder
103+
```
104+
105+
This is a straightforward way to identify that, for example, `MyList[T]` is a 'specialised' version of `MyList` on the type `T`.
106+
107+
(The default value for `__origin__` is `None`. If the object returned by a generic function already has a non-`None` origin, that origin will *not* be overwritten.)
108+
109+
The `__origin__` functions identically with the Generic Class syntax described above:
110+
111+
```py
112+
@struct
113+
class MyList[T]:
114+
inner: list[T]
115+
116+
def main() -> None:
117+
assert MyList[i32].__origin__ is MyList
118+
```

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ dependencies = [
1717
"wasmtime==8.0.1; sys_platform != 'emscripten'",
1818
"ziglang==0.13.0; sys_platform != 'emscripten'",
1919
"mkdocs-typer>=0.0.3",
20+
"mkdocs-typer2[mkdocs]>=0.3.0",
2021
]
2122

2223
[project.optional-dependencies]

spy/backend/c/cmodwriter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ def init_c(self) -> None:
172172
spy_list_str lst = spy_list_str_new();
173173
for (int i = 0; i < argc; i++) {
174174
size_t length = strlen(argv[i]);
175-
spy_Str *s = spy_str_alloc(length);
176-
char *buf = (char *)s->utf8;
175+
spy_StrObject *s = spy_str_alloc(length);
176+
char *buf = (char *)spy_StrObject_UTF8(s);
177177
memcpy(buf, argv[i], length);
178178
lst = spy_list_str_push(lst, s);
179179
}

spy/backend/c/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def __init__(self, vm: SPyVM) -> None:
136136
self._d[B.w_f32] = C_Type("float")
137137
self._d[B.w_complex128] = C_Type("spy_Complex128")
138138
self._d[B.w_bool] = C_Type("bool")
139-
self._d[B.w_str] = C_Type("spy_Str *")
139+
self._d[B.w_str] = C_Type("spy_StrObject *")
140140
self._d[RB.w_RawBuffer] = C_Type("spy_RawBuffer *")
141141
self._d[JSFFI.w_JsRef] = C_Type("JsRef")
142142
self._d[POSIX.w__FILE] = C_Type("FILE *")

spy/backend/c/cstructwriter.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ def emit_StructType(self, fqn: FQN, w_st: W_StructType) -> None:
124124
)
125125
return
126126

127+
# _str::StrObject is already declared in str.h as spy_StrObject. Just emit a
128+
# typedef.
129+
if str(w_st.fqn) == "_str::StrObject":
130+
self.tbh_fwdecl.wl(
131+
f"typedef spy_StrObject {c_st}; /* alias of spy_StrObject */"
132+
)
133+
return
134+
127135
human_fqn = w_st.fqn.human_name(self.ctx.vm)
128136
self.tbh_fwdecl.wl(f"typedef struct {c_st} {c_st}; /* {human_fqn} */")
129137

@@ -144,6 +152,18 @@ def emit_PtrType(self, fqn: FQN, w_ptrtype: W_PtrType) -> None:
144152
c_ptrtype = C_Type(w_ptrtype.fqn.c_name)
145153
w_itemT = w_ptrtype.w_itemT
146154
c_itemT = self.ctx.w2c(w_itemT)
155+
156+
# some ptr types are predeclared manually in libspy. Make sure to keep the code
157+
# generated here and the code manually written there always in sync.
158+
if str(w_ptrtype.fqn) in (
159+
"unsafe::gc_ptr[u8]",
160+
"unsafe::gc_ptr[_str::StrObject]",
161+
):
162+
self.tbh_fwdecl.wl(
163+
f"// {c_ptrtype}: skip as it's already pre-declared by libspy"
164+
)
165+
return
166+
147167
self.tbh_fwdecl.wb(f"""
148168
typedef struct {c_ptrtype} {{
149169
{c_itemT} *p;

0 commit comments

Comments
 (0)