Fix: rename enterpreter to interpreter
This commit is contained in:
@@ -1,13 +1,18 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "enterpreter"
|
name = "interpreter"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
description = "Add your description here"
|
description = "Add your description here"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.10"
|
requires-python = ">=3.10"
|
||||||
dependencies = [
|
dependencies = ["colorama>=0.4.6", "cython>=3.0.12"]
|
||||||
"colorama>=0.4.6",
|
|
||||||
"cython>=3.0.12",
|
[project.scripts]
|
||||||
]
|
bf = "brainfuck.__main__:main"
|
||||||
|
brainfuck = "brainfuck.__main__:main"
|
||||||
|
|
||||||
[tool.uv]
|
[tool.uv]
|
||||||
package = true
|
package = true
|
||||||
|
|
||||||
|
[build-system]
|
||||||
|
requires = ["hatchling"]
|
||||||
|
build-backend = "hatchling.build"
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from sys import stdin, stdout
|
from sys import stdin, stdout
|
||||||
from os import PathLike
|
from os import PathLike
|
||||||
from enterpreter import Enterpreter, opcode
|
from interpreter import Interpreter, opcode
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
BrainFuckByteCode = {
|
BrainFuckByteCode = {
|
||||||
@@ -31,7 +32,7 @@ class BrainFuckByteCodeCompiler:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class BrainFuckEnterpreter(Enterpreter):
|
class BrainFuckInterpreter(Interpreter):
|
||||||
@opcode(BrainFuckByteCode[">"])
|
@opcode(BrainFuckByteCode[">"])
|
||||||
def forward(self):
|
def forward(self):
|
||||||
self._memory_pointer += 1
|
self._memory_pointer += 1
|
||||||
@@ -84,12 +85,12 @@ class BrainFuckEnterpreter(Enterpreter):
|
|||||||
return search_pointer
|
return search_pointer
|
||||||
|
|
||||||
|
|
||||||
def bf(program_path: PathLike, bits=8, mem=2**8) -> BrainFuckEnterpreter:
|
def bf(program_path: PathLike, bits=8, mem=None) -> BrainFuckInterpreter:
|
||||||
bf_c = BrainFuckByteCodeCompiler()
|
bf_c = BrainFuckByteCodeCompiler()
|
||||||
bf_c.load_file(program_path)
|
bf_c.load_file(program_path)
|
||||||
program = bf_c.compile()
|
program = bf_c.compile()
|
||||||
|
|
||||||
bf_e = BrainFuckEnterpreter(bits=bits, memsize=mem)
|
bf_e = BrainFuckInterpreter(bits=bits, memsize=mem)
|
||||||
bf_e.load_program(program)
|
bf_e.load_program(program)
|
||||||
bf_e.run()
|
bf_e.run()
|
||||||
|
|
||||||
@@ -97,7 +98,6 @@ def bf(program_path: PathLike, bits=8, mem=2**8) -> BrainFuckEnterpreter:
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
bf(Path("examples", "HelloWorld.bf"))
|
bf(Path("examples", "HelloWorld.bf"))
|
||||||
bf(Path("examples", "GameOfLife.bf"), bits=16, mem=2**16)
|
bf(Path("examples", "GameOfLife.bf"), bits=16, mem=2**16)
|
||||||
|
bf(Path("examples", "Mandlebrot.bf"), bits=16, mem=2**16)
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import argparse
|
||||||
|
from pathlib import Path
|
||||||
|
from .__brainfuck import bf
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Brainfuck interpreter using a custom compiler and virtual machine."
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"file", type=Path, help="Path to the Brainfuck program file (.bf)"
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-b",
|
||||||
|
"--bits",
|
||||||
|
type=int,
|
||||||
|
default=8,
|
||||||
|
help="Bit width of each memory cell (default: 8)",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-m", "--mem", type=int, default=None, help="Size of memory tape (default: 256)"
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
# Validate file existence
|
||||||
|
if not args.file.exists():
|
||||||
|
parser.error(f"File not found: {args.file}")
|
||||||
|
|
||||||
|
# Run the brainfuck program
|
||||||
|
bf(program_path=args.file, bits=args.bits, mem=args.mem)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
from .__enterpreter import Enterpreter, opcode
|
|
||||||
|
|
||||||
__all__ = ["Enterpreter", "opcode"]
|
|
||||||
3
src/interpreter/__init__.py
Normal file
3
src/interpreter/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
from .__interpreter import Interpreter, opcode
|
||||||
|
|
||||||
|
__all__ = ["Interpreter", "opcode"]
|
||||||
@@ -20,8 +20,9 @@ def opcode(opcode: Operation):
|
|||||||
return _helper
|
return _helper
|
||||||
|
|
||||||
|
|
||||||
class Enterpreter:
|
class Interpreter:
|
||||||
def __init__(self, bits=8, memsize=256) -> None:
|
def __init__(self, bits=8, memsize=None) -> None:
|
||||||
|
memsize = memsize or 2**bits
|
||||||
self.__bits = bits
|
self.__bits = bits
|
||||||
self.__memory_max_size = 2**self.__bits
|
self.__memory_max_size = 2**self.__bits
|
||||||
self.__memory_size = min(memsize, self.__memory_max_size)
|
self.__memory_size = min(memsize, self.__memory_max_size)
|
||||||
Reference in New Issue
Block a user