fromscratch-pycpu 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fromscratch_pycpu-0.1.0.dist-info/METADATA +232 -0
- fromscratch_pycpu-0.1.0.dist-info/RECORD +47 -0
- fromscratch_pycpu-0.1.0.dist-info/WHEEL +4 -0
- fromscratch_pycpu-0.1.0.dist-info/entry_points.txt +2 -0
- fromscratch_pycpu-0.1.0.dist-info/licenses/LICENSE +21 -0
- pycpu/__init__.py +11 -0
- pycpu/__main__.py +68 -0
- pycpu/_version.py +24 -0
- pycpu/arithmetic.py +176 -0
- pycpu/assembler.py +580 -0
- pycpu/circuit.py +359 -0
- pycpu/cockpit/__init__.py +8 -0
- pycpu/cockpit/app.py +545 -0
- pycpu/cockpit/code.py +161 -0
- pycpu/cockpit/examples/countdown.pebble +7 -0
- pycpu/cockpit/examples/divide-by-zero.pebble +7 -0
- pycpu/cockpit/examples/factorial.pebble +9 -0
- pycpu/cockpit/examples/greeting.pebble +4 -0
- pycpu/cockpit/examples/times-table.pebble +6 -0
- pycpu/cockpit/examples/tour.pebble +31 -0
- pycpu/cockpit/explain.py +660 -0
- pycpu/cockpit/page.html +1261 -0
- pycpu/cockpit/picture.py +470 -0
- pycpu/cockpit/session.py +778 -0
- pycpu/cockpit/walks/a-call-and-the-stack.toml +47 -0
- pycpu/cockpit/walks/many-instructions.toml +39 -0
- pycpu/cockpit/walks/subtracting-with-gates.toml +39 -0
- pycpu/cockpit/walks/where-n-lives.toml +39 -0
- pycpu/cockpit/walks/why-the-clock-ticks.toml +50 -0
- pycpu/cockpit/walks.py +518 -0
- pycpu/computer.py +175 -0
- pycpu/cpu.py +648 -0
- pycpu/cpu_parts.py +140 -0
- pycpu/fast.py +320 -0
- pycpu/isa.py +243 -0
- pycpu/logic.py +222 -0
- pycpu/memory.py +268 -0
- pycpu/proof.py +263 -0
- pycpu/py.typed +0 -0
- pycpu/simulator.py +361 -0
- pycpu/state.py +146 -0
- pycpu/translator/__init__.py +29 -0
- pycpu/translator/codegen.py +493 -0
- pycpu/translator/program.py +211 -0
- pycpu/translator/runtime.py +166 -0
- pycpu/translator/subset.py +688 -0
- pycpu/view.py +55 -0
pycpu/cockpit/code.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"""The code behind a part: the Python that built it, read live, for the Cockpit's code panel.
|
|
2
|
+
|
|
3
|
+
Think of a toy with a sticker on its back saying which page of the instruction booklet
|
|
4
|
+
built it. This module is that sticker, except it never copies the page: it opens the
|
|
5
|
+
booklet itself, the real source file, every time you ask. Because the code shown is read
|
|
6
|
+
from the module that is running, it cannot drift from the circuit it built.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import inspect
|
|
10
|
+
import textwrap
|
|
11
|
+
from collections.abc import Callable
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
from types import CodeType
|
|
14
|
+
|
|
15
|
+
import pycpu
|
|
16
|
+
from pycpu.cpu import GateMachine
|
|
17
|
+
|
|
18
|
+
DOCS_URL = "https://robcox2910.github.io/pycpu/concepts" # the public docs site; the one source
|
|
19
|
+
_PACKAGE_DIR = Path(pycpu.__file__).resolve().parent
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def root_for(package_dir: Path) -> Path:
|
|
23
|
+
"""Pick the folder that file paths are shown relative to.
|
|
24
|
+
|
|
25
|
+
In a checkout (`package_dir` is `<repo>/src/pycpu`, and `<repo>/pyproject.toml`
|
|
26
|
+
exists) that is the repo, so paths read `src/pycpu/...`. Installed from a wheel it is
|
|
27
|
+
the folder that holds the package, so paths read `pycpu/...`.
|
|
28
|
+
"""
|
|
29
|
+
checkout = package_dir.parents[1]
|
|
30
|
+
return checkout if (checkout / "pyproject.toml").is_file() else package_dir.parent
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
_REPO_ROOT = root_for(_PACKAGE_DIR)
|
|
34
|
+
|
|
35
|
+
# The concept page that explains each part: the one whose How It Works describes it.
|
|
36
|
+
PART_PAGES = {
|
|
37
|
+
"cpu.alu": "the-alu",
|
|
38
|
+
"cpu.registers": "registers",
|
|
39
|
+
"ram": "ram",
|
|
40
|
+
"cpu.halt": "what-happens-when-things-go-wrong",
|
|
41
|
+
"cpu.write_back": "fetch-decode-execute",
|
|
42
|
+
"cpu.address_mux": "fetch-decode-execute",
|
|
43
|
+
"cpu.next_pc": "fetch-decode-execute",
|
|
44
|
+
"cpu.next_sp": "the-stack",
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
# The title of each page the Cockpit links to: the part pages above and every walk's
|
|
48
|
+
# `see_also` page, as each page's own heading gives it.
|
|
49
|
+
PAGE_TITLES = {
|
|
50
|
+
"the-alu": "The ALU",
|
|
51
|
+
"registers": "Registers",
|
|
52
|
+
"ram": "RAM",
|
|
53
|
+
"what-happens-when-things-go-wrong": "What Happens When Things Go Wrong",
|
|
54
|
+
"fetch-decode-execute": "Fetch, Decode, Execute",
|
|
55
|
+
"the-stack": "The Stack",
|
|
56
|
+
"stepping-a-pebble-line": "Stepping a Pebble Line",
|
|
57
|
+
"variables-in-memory": "Variables in Memory",
|
|
58
|
+
"the-clock": "The Clock",
|
|
59
|
+
"how-a-function-call-really-works": "How a Function Call Really Works",
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def page_link(stem: str) -> dict[str, str]:
|
|
64
|
+
"""Return the `title` and `url` of a concept page on the docs server on your own computer.
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
stem: The page's file name without `.md`, such as `"the-alu"`.
|
|
68
|
+
|
|
69
|
+
Raises:
|
|
70
|
+
KeyError: If the page has no title in `PAGE_TITLES`.
|
|
71
|
+
|
|
72
|
+
"""
|
|
73
|
+
return {"title": PAGE_TITLES[stem], "url": f"{DOCS_URL}/{stem}/"}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _block(lines: list[str], scope: str) -> tuple[int, list[str]]:
|
|
77
|
+
"""Cut the `with c.scope("<scope>"):` block out of a function's lines.
|
|
78
|
+
|
|
79
|
+
Returns:
|
|
80
|
+
The offset of the block's first line within `lines`, and the block's lines
|
|
81
|
+
(the `with` line and everything indented deeper). When the function has no
|
|
82
|
+
such line the whole function: offset 0 and all its lines.
|
|
83
|
+
|
|
84
|
+
"""
|
|
85
|
+
# Edge cases this does not handle (none occur in the CPU's code today): a comment or a
|
|
86
|
+
# multi-line string dedented to the `with` line's level ends the block early, the first
|
|
87
|
+
# line that matches wins if a scope of the same name is nested earlier, and a `with`
|
|
88
|
+
# line with a trailing comment or an `as` name is not found (the whole function shows).
|
|
89
|
+
if not scope:
|
|
90
|
+
return 0, lines
|
|
91
|
+
opener = f'with c.scope("{scope}"):'
|
|
92
|
+
for start, line in enumerate(lines):
|
|
93
|
+
if line.strip() == opener:
|
|
94
|
+
indent = len(line) - len(line.lstrip())
|
|
95
|
+
end = start + 1
|
|
96
|
+
while end < len(lines):
|
|
97
|
+
text = lines[end]
|
|
98
|
+
if text.strip() and len(text) - len(text.lstrip()) <= indent:
|
|
99
|
+
break
|
|
100
|
+
end += 1
|
|
101
|
+
while not lines[end - 1].strip():
|
|
102
|
+
end -= 1
|
|
103
|
+
return start, lines[start:end]
|
|
104
|
+
return 0, lines
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _relative(function: Callable[..., object]) -> str:
|
|
108
|
+
"""Name the function's file relative to the repo root, or as it is when outside it."""
|
|
109
|
+
name = inspect.getsourcefile(function) or inspect.getfile(function)
|
|
110
|
+
try:
|
|
111
|
+
return Path(name).resolve().relative_to(_REPO_ROOT).as_posix()
|
|
112
|
+
except ValueError:
|
|
113
|
+
return name
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def built_by(gate: GateMachine, path: str) -> dict[str, object]:
|
|
117
|
+
"""Show the live source of the function that built the part `path`.
|
|
118
|
+
|
|
119
|
+
When the function builds more than one part, such as `_build_write_back`, only the
|
|
120
|
+
part's own `with c.scope(...)` block is kept, so the code shown is the code that
|
|
121
|
+
made this part. The block is named by the part's recorded scope, or else by the last
|
|
122
|
+
segment of its path. A part whose recorded scope is empty (the ALU, whose flags are
|
|
123
|
+
made after its block) or whose scope is opened somewhere else (the RAM's name is a
|
|
124
|
+
variable) gets the whole function.
|
|
125
|
+
|
|
126
|
+
Args:
|
|
127
|
+
gate: The gate machine whose builders were recorded as it was wired up.
|
|
128
|
+
path: The part's scope path, such as `"cpu.alu"`.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
`function` (a dotted name), `file` (relative to the repo), `line` (the first line
|
|
132
|
+
shown, 1-based), `code` (None if Python cannot find the source) and `page`
|
|
133
|
+
(`title` and `url` of the concept page that explains the part). If the source
|
|
134
|
+
is missing, `line` is the function's first line (0 when it has no code object).
|
|
135
|
+
|
|
136
|
+
Raises:
|
|
137
|
+
KeyError: If the part has no builder recorded.
|
|
138
|
+
|
|
139
|
+
"""
|
|
140
|
+
function, scope = gate.builders[path]
|
|
141
|
+
code: str | None = None
|
|
142
|
+
first = getattr(function, "__code__", None)
|
|
143
|
+
line = first.co_firstlineno if isinstance(first, CodeType) else 0
|
|
144
|
+
try:
|
|
145
|
+
lines, start = inspect.getsourcelines(function)
|
|
146
|
+
except OSError, TypeError:
|
|
147
|
+
pass
|
|
148
|
+
else:
|
|
149
|
+
offset, kept = _block(
|
|
150
|
+
[text.rstrip("\n") for text in lines],
|
|
151
|
+
path.rsplit(".", 1)[-1] if scope is None else scope,
|
|
152
|
+
)
|
|
153
|
+
code = textwrap.dedent("\n".join(kept))
|
|
154
|
+
line = start + offset
|
|
155
|
+
return {
|
|
156
|
+
"function": f"{function.__module__}.{function.__qualname__}",
|
|
157
|
+
"file": _relative(function),
|
|
158
|
+
"line": line,
|
|
159
|
+
"code": code,
|
|
160
|
+
"page": page_link(PART_PAGES[path]),
|
|
161
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# A tour of everything Pebble can do on PyCPU
|
|
2
|
+
fn fact(n) {
|
|
3
|
+
if n <= 1 {
|
|
4
|
+
return 1
|
|
5
|
+
}
|
|
6
|
+
return n * fact(n - 1)
|
|
7
|
+
}
|
|
8
|
+
fn describe(n) {
|
|
9
|
+
if n % 2 == 0 and n >= 2 {
|
|
10
|
+
print("even")
|
|
11
|
+
} else if n == 1 or not (n > 2) {
|
|
12
|
+
print("small")
|
|
13
|
+
} else {
|
|
14
|
+
print("odd")
|
|
15
|
+
}
|
|
16
|
+
return n // 2
|
|
17
|
+
}
|
|
18
|
+
print("Hello from Pebble!")
|
|
19
|
+
let total = 0
|
|
20
|
+
for i in range(1, 5) {
|
|
21
|
+
total = total + fact(i)
|
|
22
|
+
describe(i)
|
|
23
|
+
}
|
|
24
|
+
print(total)
|
|
25
|
+
let n = -total
|
|
26
|
+
while n < 0 {
|
|
27
|
+
n = n + 10
|
|
28
|
+
}
|
|
29
|
+
if n != 0 {
|
|
30
|
+
print(n)
|
|
31
|
+
}
|