Skip to content

Commit 4dded94

Browse files
committed
allow assembler to skip comment removal to avoid removing comments twice
Since the preprocessor was introduced, which already removes comments, the assembler does not need to remove comments anymore in the usual case. The assembler still retains the ability to remove comments (enabled by default) in case it is used without the preprocessor. The `remove_comments` argument to the `assemble()` method can be used to control whether comments will be removed during assembly or not.
1 parent d76fd26 commit 4dded94

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

esp32_ulp/__main__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
def src_to_binary(src):
1212
assembler = Assembler()
13-
assembler.assemble(src)
13+
assembler.assemble(src, remove_comments=False) # comments already removed by preprocessor
1414
garbage_collect('before symbols export')
1515
addrs_syms = assembler.symbols.export()
1616
for addr, sym in addrs_syms:

esp32_ulp/assemble.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
from . import opcodes
6-
from .nocomment import remove_comments
6+
from .nocomment import remove_comments as do_remove_comments
77
from .util import garbage_collect
88

99
TEXT, DATA, BSS = 'text', 'data', 'bss'
@@ -283,8 +283,8 @@ def assembler_pass(self, lines):
283283
raise ValueError('Unknown opcode or directive: %s' % opcode)
284284
self.finalize_sections()
285285

286-
def assemble(self, text):
287-
lines = remove_comments(text)
286+
def assemble(self, text, remove_comments=True):
287+
lines = do_remove_comments(text) if remove_comments else text.splitlines()
288288
self.init(1) # pass 1 is only to get the symbol table right
289289
self.assembler_pass(lines)
290290
self.symbols.set_bases(self.compute_bases())

tests/assemble.py

+19
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,24 @@ def test_assemble_evalulate_expressions():
157157
assert a.symbols.get_sym('entry') == (REL, TEXT, 0)
158158

159159

160+
def test_assemble_optional_comment_removal():
161+
line = " move r1, 123 # comment"
162+
163+
a = Assembler()
164+
165+
# first assemble as normal (comments will be removed by default)
166+
a.assemble(line)
167+
168+
# now assemble with comment removal disabled
169+
try:
170+
a.assemble(line, remove_comments=False)
171+
except ValueError as e:
172+
raised = True
173+
else:
174+
raised = False
175+
assert raised
176+
177+
160178
def test_symbols():
161179
st = SymbolTable({}, {}, {})
162180
for entry in [
@@ -218,4 +236,5 @@ def test_symbols():
218236
test_assemble_global()
219237
test_assemble_uppercase_opcode()
220238
test_assemble_evalulate_expressions()
239+
test_assemble_optional_comment_removal()
221240
test_symbols()

0 commit comments

Comments
 (0)