while-machine
While Abstract Machine implemented in python.
While Machine is an abstract machine that is turing complete and can compute any partial recursive function. Find out more here https://en.wikipedia.org/wiki/Abstract_machine
Its simpler brother is called Random Access Machine and you can find its implementation here https://github.com/GabrieleMaurina/random-access-machine
Install
Run inside terminal:
python -m pip install while-machine
Usage
To execute a ram source file:
python -m while_machine <while source file> <integer input>
To import it in your script:
from while_machine import WhileMachine
data = 4
program = 'begin inc 0 inc 0 end'
wm = WhileMachine()
result = wm.compute(program, data)
print(result) # 2
How it works
A while machine has 21 registers numbered from 0, 1, 2..., the input is loaded on register 1 before the execution starts, the output is taken from register 0 when the execution ends.
Instructions:
A while machine supports only 4 basic instructions:
Increment register k by 1:
inc k
Decrement register k by 1:
dec k
Set register k to zero:
zero k
While register k is not zero, do the following command:
while k
Commands can be grouped with:
begin
...
...
end
Example
An example program that will double whatever input you give to the machine:
while 1
begin
inc 0
inc 0
dec 1
end
RAM compiler
It is possible to compile a While program into a Ram program using the method compile_ram of the class WhileMachine. You can find an implementation of the ram machine here https://github.com/GabrieleMaurina/random-access-machine
Here is an example of how to compile from While to RAM:
from while_machine import WhileMachine
import sys
def main():
wm = WhileMachine()
with open(sys.argv[1],'r') as while_prog, open(sys.argv[2],'w') as ram_prog:
while_prog = while_prog.read()
compiled = wm.compile_ram(while_prog)
ram_prog.write(compiled)
if __name__ == '__main__':
main()
Metadata
Release files for while-machine 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| while-machine-1.0.2.tar.gz | 3.1 kB | Without provenance | Details for while-machine-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| while_machine-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for while_machine-1.0.2-py3-none-any.whl |
Total release size: 6.3 kB
Release files / while-machine-1.0.2.tar.gz
| Download URL | while-machine-1.0.2.tar.gz |
|---|---|
| Size | 3.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2df910f87b54c0ffde22851d33e4c5d4c7a42d9dca0a1534e3bcbd8fe8e7daeb
|
|
BLAKE2b-256 checksum How to use checksums |
43d066bf48c9922ace92f510d46004007104dc599aa4d80b295aadfb22e60c87
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.7
|
Release files / while_machine-1.0.2-py3-none-any.whl
| Download URL | while_machine-1.0.2-py3-none-any.whl |
|---|---|
| Size | 3.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3181dddbc869f7d9c0b75171b56d9fe236a6f9bc523f885882beed7f359b1626
|
|
BLAKE2b-256 checksum How to use checksums |
27590e89f719e8c37c620dd7719ae749475bdd6780075a326b7907ca326b3ce6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/54.1.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.7
|
Security for while-machine 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.