turing-py 2.2.1__tar.gz → 2.3.0__tar.gz

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.
Files changed (25) hide show
  1. {turing_py-2.2.1 → turing_py-2.3.0}/PKG-INFO +1 -1
  2. {turing_py-2.2.1 → turing_py-2.3.0}/pyproject.toml +1 -1
  3. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/__init__.py +5 -27
  4. turing_py-2.3.0/src/tmpy/dsl/loader.py +129 -0
  5. turing_py-2.2.1/src/tmpy/dsl/loader.py +0 -56
  6. {turing_py-2.2.1 → turing_py-2.3.0}/LICENSE +0 -0
  7. {turing_py-2.2.1 → turing_py-2.3.0}/README.md +0 -0
  8. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/alphabet/__init__.py +0 -0
  9. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/alphabet/input_alphabet.py +0 -0
  10. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/alphabet/symbol.py +0 -0
  11. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/alphabet/tape_alphabet.py +0 -0
  12. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/dsl/__init__.py +0 -0
  13. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/dsl/ast.py +0 -0
  14. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/dsl/lexer.py +0 -0
  15. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/dsl/parser.py +0 -0
  16. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/dsl/token.py +0 -0
  17. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/exception.py +0 -0
  18. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/machine/__init__.py +0 -0
  19. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/machine/machine.py +0 -0
  20. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/machine/states.py +0 -0
  21. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/machine/tape.py +0 -0
  22. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/transition/__init__.py +0 -0
  23. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/transition/direction.py +0 -0
  24. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/transition/transition.py +0 -0
  25. {turing_py-2.2.1 → turing_py-2.3.0}/src/tmpy/transition/transition_function.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: turing-py
3
- Version: 2.2.1
3
+ Version: 2.3.0
4
4
  Summary: Education Turing Machine simulator in Python
5
5
  License-File: LICENSE
6
6
  Author: Eric Santos
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "turing-py"
3
- version = "2.2.1"
3
+ version = "2.3.0"
4
4
  description = "Education Turing Machine simulator in Python"
5
5
  authors = [
6
6
  {name = "Eric Santos",email = "ericshantos13@gmail.com"}
@@ -1,13 +1,4 @@
1
- from .alphabet import Alphabet, Symbol, TapeAlphabet
2
- from .dsl import (
3
- Lexer,
4
- MachineLoader,
5
- MachineNode,
6
- Parser,
7
- Token,
8
- TokenType,
9
- TransitionNode,
10
- )
1
+ from . import alphabet, dsl, machine, transition
11
2
  from .exception import (
12
3
  AlphabetError,
13
4
  BlankSymbolError,
@@ -24,25 +15,12 @@ from .exception import (
24
15
  TransitionNotDefinedError,
25
16
  TuringMachineError,
26
17
  )
27
- from .machine import States, Tape, TuringMachine
28
- from .transition import Direction, Transition, TransitionFunction
29
18
 
30
19
  __all__ = [
31
- "TransitionFunction",
32
- "Transition",
33
- "Alphabet",
34
- "TapeAlphabet",
35
- "Symbol",
36
- "TuringMachine",
37
- "States",
38
- "Tape",
39
- "Direction",
40
- "MachineNode",
41
- "TransitionNode",
42
- "Lexer",
43
- "MachineLoader",
44
- "Parser",
45
- "Token",
20
+ "alphabet",
21
+ "dsl",
22
+ "machine",
23
+ "transition",
46
24
  "TokenType",
47
25
  "BlankSymbolError",
48
26
  "TuringMachineError",
@@ -0,0 +1,129 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ @author: Eric Santos <ericshantos13@gmail.com>
4
+
5
+ Loader for the Turing machine DSL.
6
+ """
7
+
8
+ from pathlib import Path
9
+
10
+ from ..alphabet import Alphabet, Symbol, TapeAlphabet
11
+ from ..machine import States, TuringMachine
12
+ from ..transition import Direction, Transition, TransitionFunction
13
+ from .ast import MachineNode
14
+ from .lexer import Lexer
15
+ from .parser import Parser
16
+ from .token import Token
17
+
18
+ move_map = {"L": Direction.LEFT, "R": Direction.RIGHT, "S": Direction.STAY}
19
+
20
+
21
+ class MachineLoader:
22
+
23
+ @classmethod
24
+ def load(cls, path: str | Path) -> TuringMachine:
25
+
26
+ text = cls._read_file(path)
27
+ tokens = cls._lex(text)
28
+ ast = cls._parse(tokens)
29
+
30
+ symbols, blank = cls._collect_symbols(ast)
31
+
32
+ alphabet, tape_alphabet = cls._build_alphabets(symbols, blank)
33
+ states = cls._build_states(ast)
34
+ delta = cls._build_delta(ast)
35
+
36
+ return TuringMachine(states, alphabet, tape_alphabet, delta)
37
+
38
+ @classmethod
39
+ def _collect_symbols(cls, ast: MachineNode) -> tuple[set[Symbol], Symbol]:
40
+ symbols: set[Symbol] = set()
41
+
42
+ for t in ast.transitions:
43
+ symbols.add(Symbol(t.read))
44
+ symbols.add(Symbol(t.write))
45
+
46
+ if ast.blank is None:
47
+ raise ValueError("Blank symbol not defined")
48
+
49
+ blank = Symbol(ast.blank)
50
+
51
+ return symbols, blank
52
+
53
+ @classmethod
54
+ def _build_alphabets(cls, symbols: set[Symbol], blank: Symbol) -> tuple[Alphabet, TapeAlphabet]:
55
+ input_alphabet = Alphabet(*[s for s in symbols if s != blank])
56
+ tape_alphabet = TapeAlphabet(blank, *symbols)
57
+
58
+ return input_alphabet, tape_alphabet
59
+
60
+ @classmethod
61
+ def _build_states(cls, ast: MachineNode) -> States:
62
+ states = set()
63
+
64
+ for t in ast.transitions:
65
+ states.add(t.state)
66
+ states.add(t.next_state)
67
+
68
+ if ast.start is None:
69
+ raise ValueError("Start state not defined")
70
+
71
+ if ast.accept is None:
72
+ raise ValueError("Accept state not defined")
73
+
74
+ states.add(ast.start)
75
+ states.add(ast.accept)
76
+
77
+ return States(
78
+ *states,
79
+ initial_state=ast.start,
80
+ final_states={ast.accept},
81
+ )
82
+
83
+ @classmethod
84
+ def _build_delta(cls, ast: MachineNode):
85
+ transitions = []
86
+
87
+ for node in ast.transitions:
88
+ transitions.append(
89
+ Transition(
90
+ node.state,
91
+ Symbol(node.read),
92
+ node.next_state,
93
+ Symbol(node.write),
94
+ move_map[node.move],
95
+ )
96
+ )
97
+
98
+ return TransitionFunction(*transitions)
99
+
100
+ @classmethod
101
+ def _read_file(cls, path: str | Path) -> str:
102
+
103
+ with open(path, "r") as f:
104
+ return f.read()
105
+
106
+ @classmethod
107
+ def _lex(cls, text: str) -> list[Token]:
108
+
109
+ lexer = Lexer(text)
110
+
111
+ return lexer.tokenize()
112
+
113
+ @classmethod
114
+ def _parse(cls, tokens: list[Token]) -> MachineNode:
115
+
116
+ parser = Parser(tokens)
117
+
118
+ return parser.parse()
119
+
120
+ @classmethod
121
+ def _build_transitions(cls, ast: MachineNode) -> list[Transition]:
122
+
123
+ transitions = []
124
+
125
+ for node in ast.transitions:
126
+
127
+ transitions.append(Transition(node.state, Symbol(node.read), node.next_state, Symbol(node.write), node.move))
128
+
129
+ return transitions
@@ -1,56 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- """
3
- @author: Eric Santos <ericshantos13@gmail.com>
4
-
5
- Loader for the Turing machine DSL.
6
- """
7
-
8
- from ..alphabet import Symbol
9
- from ..transition import Transition
10
- from .lexer import Lexer
11
- from .parser import MachineNode, Parser
12
-
13
-
14
- class MachineLoader:
15
-
16
- def load(self, path: str):
17
- text = self._read_file(path)
18
-
19
- tokens = self._lex(text)
20
-
21
- ast = self._parse(tokens)
22
-
23
- transitions = self._build_transitions(ast)
24
-
25
- return transitions
26
-
27
- def _read_file(self, path: str) -> str:
28
-
29
- with open(path, "r") as f:
30
- return f.read()
31
-
32
- from tmpy.dsl.lexer import Lexer
33
-
34
- def _lex(self, text: str):
35
-
36
- lexer = Lexer(text)
37
-
38
- return lexer.tokenize()
39
-
40
- def _parse(self, tokens):
41
-
42
- parser = Parser(tokens)
43
-
44
- return parser.parse()
45
-
46
- def _build_transitions(self, machine_ast: MachineNode) -> list[Transition]:
47
-
48
- transitions = []
49
-
50
- for node in machine_ast.transitions:
51
-
52
- t = Transition(node.state, Symbol(node.read), node.next_state, Symbol(node.write), node.move)
53
-
54
- transitions.append(t)
55
-
56
- return transitions
File without changes
File without changes
File without changes