Skip to content

Commit 30aef4f

Browse files
authored
Merge pull request #427 from boriel/feature/add_prepro_concatenation
Feature/add prepro concatenation
2 parents 9b88dec + 6af277f commit 30aef4f

111 files changed

Lines changed: 2067 additions & 1216 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ dist/
1313
examples/*.bin
1414
examples/*.tzx
1515
scratch/
16-
.coverage/
16+
.coverage
1717
htmlcov/
1818
build/

src/api/errmsg.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@
1919
__all__ = ['error', 'warning']
2020

2121

22-
def msg_output(msg):
22+
def msg_output(msg: str) -> None:
2323
if msg in global_.error_msg_cache:
2424
return
2525

2626
OPTIONS.stderr.write("%s\n" % msg)
2727
global_.error_msg_cache.add(msg)
2828

2929

30-
def info(msg):
30+
def info(msg: str) -> None:
3131
if OPTIONS.Debug < 1:
3232
return
3333
OPTIONS.stderr.write("info: %s\n" % msg)
3434

3535

36-
def error(lineno, msg, fname: Optional[str] = None):
36+
def error(lineno: int, msg: str, fname: Optional[str] = None) -> None:
3737
""" Generic syntax error routine
3838
"""
3939
if fname is None:
@@ -51,7 +51,7 @@ def error(lineno, msg, fname: Optional[str] = None):
5151
global_.has_errors += 1
5252

5353

54-
def warning(lineno, msg, fname: Optional[str] = None):
54+
def warning(lineno: int, msg: str, fname: Optional[str] = None) -> None:
5555
""" Generic warning error routine
5656
"""
5757
if fname is None:
@@ -62,7 +62,7 @@ def warning(lineno, msg, fname: Optional[str] = None):
6262
global_.has_warnings += 1
6363

6464

65-
def warning_implicit_type(lineno, id_, type_=None):
65+
def warning_implicit_type(lineno: int, id_: str, type_: str = None):
6666
""" Warning: Using default implicit type 'x'
6767
"""
6868
if OPTIONS.strict:
@@ -75,19 +75,19 @@ def warning_implicit_type(lineno, id_, type_=None):
7575
warning(lineno, "Using default implicit type '%s' for '%s'" % (type_, id_))
7676

7777

78-
def warning_condition_is_always(lineno, cond=False):
78+
def warning_condition_is_always(lineno: int, cond: bool = False):
7979
""" Warning: Condition is always false/true
8080
"""
8181
warning(lineno, "Condition is always %s" % cond)
8282

8383

84-
def warning_conversion_lose_digits(lineno):
84+
def warning_conversion_lose_digits(lineno: int):
8585
""" Warning: Conversion may lose significant digits
8686
"""
8787
warning(lineno, 'Conversion may lose significant digits')
8888

8989

90-
def warning_empty_loop(lineno):
90+
def warning_empty_loop(lineno: int):
9191
""" Warning: Empty loop
9292
"""
9393
warning(lineno, 'Empty loop')
@@ -99,7 +99,7 @@ def warning_empty_if(lineno):
9999
warning(lineno, 'Useless empty IF ignored')
100100

101101

102-
# Emmits an optimization warning
102+
# Emits an optimization warning
103103
def warning_not_used(lineno, id_, kind='Variable'):
104104
if OPTIONS.optimization > 0:
105105
warning(lineno, "%s '%s' is never used" % (kind, id_))
@@ -109,71 +109,70 @@ def warning_not_used(lineno, id_, kind='Variable'):
109109
# Syntax error: Expected string instead of
110110
# numeric expression.
111111
# ----------------------------------------
112-
def syntax_error_expected_string(lineno, _type):
112+
def syntax_error_expected_string(lineno: str, _type: str):
113113
error(lineno, "Expected a 'string' type expression, got '%s' instead" % _type)
114114

115115

116116
# ----------------------------------------
117117
# Syntax error: FOR variable should be X
118118
# instead of Y
119119
# ----------------------------------------
120-
def syntax_error_wrong_for_var(lineno, x, y):
121-
error(lineno, "FOR variable should be '%s' instead of '%s'" %
122-
(x, y))
120+
def syntax_error_wrong_for_var(lineno: str, x: str, y: str):
121+
error(lineno, "FOR variable should be '%s' instead of '%s'" % (x, y))
123122

124123

125124
# ----------------------------------------
126125
# Syntax error: Initializer expression is
127126
# not constant
128127
# ----------------------------------------
129-
def syntax_error_not_constant(lineno):
128+
def syntax_error_not_constant(lineno: int):
130129
error(lineno, "Initializer expression is not constant.")
131130

132131

133132
# ----------------------------------------
134133
# Syntax error: Id is neither an array nor
135134
# a function
136135
# ----------------------------------------
137-
def syntax_error_not_array_nor_func(lineno, varname):
136+
def syntax_error_not_array_nor_func(lineno: int, varname: str):
138137
error(lineno, "'%s' is neither an array nor a function." % varname)
139138

140139

141140
# ----------------------------------------
142141
# Syntax error: Id is neither an array nor
143142
# a function
144143
# ----------------------------------------
145-
def syntax_error_not_an_array(lineno, varname):
144+
def syntax_error_not_an_array(lineno: int, varname: str):
146145
error(lineno, "'%s' is not an array (or has not been declared yet)" % varname)
147146

148147

149148
# ----------------------------------------
150149
# Syntax error: function redefinition type
151150
# mismatch
152151
# ----------------------------------------
153-
def syntax_error_func_type_mismatch(lineno, entry):
152+
def syntax_error_func_type_mismatch(lineno: int, entry):
154153
error(lineno, "Function '%s' (previously declared at %i) type mismatch" % (entry.name, entry.lineno))
155154

156155

157156
# ----------------------------------------
158157
# Syntax error: function redefinition parm.
159158
# mismatch
160159
# ----------------------------------------
161-
def syntax_error_parameter_mismatch(lineno, entry):
160+
def syntax_error_parameter_mismatch(lineno: int, entry):
162161
error(lineno, "Function '%s' (previously declared at %i) parameter mismatch" % (entry.name, entry.lineno))
163162

164163

165164
# ----------------------------------------
166165
# Syntax error: can't convert value to the
167166
# given type.
168167
# ----------------------------------------
169-
def syntax_error_cant_convert_to_type(lineno, expr_str, type_):
168+
def syntax_error_cant_convert_to_type(lineno: int, expr_str: str, type_: str):
170169
error(lineno, "Cant convert '%s' to type %s" % (expr_str, type_))
171170

172171

173172
# ----------------------------------------
174173
# Syntax error: is a SUB not a FUNCTION
175174
# ----------------------------------------
176-
def syntax_error_is_a_sub_not_a_func(lineno, name):
175+
def syntax_error_is_a_sub_not_a_func(lineno: int, name: str):
177176
error(lineno, "'%s' is SUBROUTINE not a FUNCTION" % name)
178177

179178

@@ -187,19 +186,19 @@ def syntax_error_undeclared_type(lineno: int, id_: str):
187186
# ----------------------------------------
188187
# Cannot assign a value to 'var'. It's not a variable
189188
# ----------------------------------------
190-
def syntax_error_cannot_assign_not_a_var(lineno, id_):
189+
def syntax_error_cannot_assign_not_a_var(lineno: int, id_: str):
191190
error(lineno, "Cannot assign a value to '%s'. It's not a variable" % id_)
192191

193192

194193
# ----------------------------------------
195194
# Cannot assign a value to 'var'. It's not a variable
196195
# ----------------------------------------
197-
def syntax_error_address_must_be_constant(lineno):
196+
def syntax_error_address_must_be_constant(lineno: int):
198197
error(lineno, 'Address must be a numeric constant expression')
199198

200199

201200
# ----------------------------------------
202201
# Cannot pass an array by value
203202
# ----------------------------------------
204-
def syntax_error_cannot_pass_array_by_value(lineno, id_):
203+
def syntax_error_cannot_pass_array_by_value(lineno: int, id_: str):
205204
error(lineno, "Array parameter '%s' must be passed ByRef" % id_)

src/api/global_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
# ----------------------------------------------------------------------
6868
# The current filename being processed (changes with each #include)
6969
# ----------------------------------------------------------------------
70-
FILENAME = '(stdin)' # name of current file being parsed
70+
FILENAME: str = '(stdin)' # name of current file being parsed
7171

7272
# ----------------------------------------------------------------------
7373
# Global Symbol Table

src/api/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from typing import Callable
1616
from typing import IO
1717
from typing import Iterable
18+
from typing import Union
1819

1920
from . import constants
2021
from . import global_
@@ -168,14 +169,14 @@ def get_final_value(symbol: symbols.SYMBOL) -> Any:
168169
return result
169170

170171

171-
def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
172+
def timeout(seconds: Union[Callable[[], int], int] = 10, error_message=os.strerror(errno.ETIME)):
172173
def decorator(func):
173174
def _handle_timeout(signum, frame):
174175
raise TimeoutError(error_message)
175176

176177
def wrapper(*args, **kwargs):
177178
signal.signal(signal.SIGALRM, _handle_timeout)
178-
signal.alarm(seconds)
179+
signal.alarm(seconds() if isinstance(seconds, Callable) else seconds)
179180
try:
180181
result = func(*args, **kwargs)
181182
finally:

src/libzxbc/zxblex.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@
1010
# ----------------------------------------------------------------------
1111

1212
import sys
13+
import re
14+
1315
from src.ply import lex
14-
from .keywords import KEYWORDS as reserved
1516
from src import api
1617
from src.api.errmsg import error
18+
from src.api import global_
19+
20+
from .keywords import KEYWORDS as reserved
1721

1822

1923
ASM = '' # Set to asm block when commenting
@@ -420,6 +424,16 @@ def t_asm_comment(t):
420424
r';.*'
421425

422426

427+
def t_asm_PREPROCLINE(t):
428+
r'\#[ \t]*[Ll][Ii][Nn][Ee][ \t]+([0-9]+)(?:[ \t]+"((?:[^"]|"")*)")?[ \t]*\r?\n'
429+
global ASM
430+
431+
ASM += t.value
432+
match = re.match('#[ \t]*[Ll][Ii][Nn][Ee][ \t]+([0-9]+)(?:[ \t]+"((?:[^"]|"")*)")?[ \t]*\r?\n', t.value)
433+
t.lexer.lineno = int(match.groups()[0])
434+
global_.FILENAME = match.groups()[1] or global_.FILENAME
435+
436+
423437
def t_asm_next(t):
424438
r'.'
425439
global ASM

0 commit comments

Comments
 (0)