14
14
import array
15
15
import re
16
16
17
+ try :
18
+ from typing import List , Tuple , Optional
19
+ except : # pylint: disable=bare-except
20
+ pass
21
+
17
22
splitter = re .compile (r",\s*|\s+(?:,\s*)?" ).split
18
23
mov_splitter = re .compile ("!|~|::" ).split
19
24
@@ -40,14 +45,16 @@ class Program: # pylint: disable=too-few-public-methods
40
45
41
46
"""
42
47
43
- def __init__ (self , text_program : str , * , build_debuginfo = False ) -> None :
48
+ debuginfo : Optional [Tuple [List [int ], str ]]
49
+
50
+ def __init__ (self , text_program : str , * , build_debuginfo : bool = False ) -> None :
44
51
"""Converts pioasm text to encoded instruction bytes"""
45
52
# pylint: disable=too-many-branches,too-many-statements,too-many-locals
46
- assembled = []
53
+ assembled : List [ int ] = []
47
54
program_name = None
48
55
labels = {}
49
56
linemap = []
50
- instructions = []
57
+ instructions : List [ str ] = []
51
58
sideset_count = 0
52
59
sideset_enable = 0
53
60
wrap = None
@@ -83,9 +90,9 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
83
90
84
91
max_delay = 2 ** (5 - sideset_count - sideset_enable ) - 1
85
92
assembled = []
86
- for instruction in instructions :
93
+ for instruction_str in instructions :
87
94
# print(instruction)
88
- instruction = splitter (instruction .strip ())
95
+ instruction = splitter (instruction_str .strip ())
89
96
delay = 0
90
97
if instruction [- 1 ].endswith ("]" ): # Delay
91
98
delay = int (instruction [- 1 ].strip ("[]" ), 0 )
@@ -242,14 +249,14 @@ def __init__(self, text_program: str, *, build_debuginfo=False) -> None:
242
249
else :
243
250
self .debuginfo = None
244
251
245
- def print_c_program (self , name , qualifier = "const" ):
252
+ def print_c_program (self , name : str , qualifier : str = "const" ) -> None :
246
253
"""Print the program into a C program snippet"""
247
- if self .debuginfo is None :
248
- linemap = None
249
- program_lines = None
250
- else :
254
+ if self .debuginfo :
251
255
linemap = self .debuginfo [0 ][:] # Use a copy since we destroy it
252
256
program_lines = self .debuginfo [1 ].split ("\n " )
257
+ else :
258
+ linemap = []
259
+ program_lines = []
253
260
254
261
print (
255
262
f"{ qualifier } int { name } _wrap = { self .pio_kwargs .get ('wrap' , len (self .assembled )- 1 )} ;"
@@ -290,7 +297,7 @@ def print_c_program(self, name, qualifier="const"):
290
297
print ()
291
298
292
299
293
- def assemble (program_text : str ) -> array .array :
300
+ def assemble (program_text : str ) -> " array.array[int]" :
294
301
"""Converts pioasm text to encoded instruction bytes
295
302
296
303
In new code, prefer to use the `Program` class so that the extra arguments
0 commit comments