Skip to content

Commit c7f2d11

Browse files
committed
Add warning when a value in DB > 255
It will be truncated for being byte.
1 parent 6432cb2 commit c7f2d11

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

src/api/errmsg.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ def warning_unreachable_code(lineno: int, fname: Optional[str] = None):
186186
def warning_function_should_return_a_value(lineno: int, func_name: str, fname: Optional[str] = None):
187187
warning(lineno, f"Function '{func_name}' should return a value", fname=fname)
188188

189+
190+
@register_warning('200')
191+
def warning_value_will_be_truncated(lineno: int, fname: Optional[str] = None):
192+
warning(lineno, "Value will be truncated", fname=fname)
193+
189194
# endregion
190195

191196
# region [Syntax Errors]

src/libzxbasm/asmparse.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,26 @@
1313

1414
import os
1515
import re
16-
from . import asmlex, basic
16+
1717
import src.ply.yacc as yacc
18+
import src.api.utils
19+
20+
from src import outfmt
21+
from src.api import errmsg
1822

19-
from .asmlex import tokens # noqa
20-
from .asm import AsmInstruction, Error
2123
from src.ast import Ast
2224
from src.ast.tree import NotAnAstError
2325
from src.api.debug import __DEBUG__
2426
from src.api.config import OPTIONS
2527
from src.api.errmsg import error
2628
from src.api.errmsg import warning
2729
from src.api import global_ as gl
28-
import src.api.utils
2930
from src.libzxbpp import zxbpp
30-
from .. import outfmt
31+
32+
from . import asmlex, basic
33+
from .asmlex import tokens # noqa
34+
from .asm import AsmInstruction, Error
35+
3136

3237
LEXER = asmlex.Lexer()
3338

@@ -144,6 +149,8 @@ def bytes(self):
144149
return tuple([0] * N) # ??
145150

146151
args = self.argval()
152+
if args[1] > 255:
153+
errmsg.warning_value_will_be_truncated(self.lineno)
147154
num = args[1] & 0xFF
148155
return tuple([num] * args[0])
149156

@@ -165,7 +172,10 @@ def argval(self):
165172
return [None]
166173

167174
if self.asm in ('DEFB', 'DEFS', 'DEFW'):
168-
return tuple([x.eval() if isinstance(x, Expr) else x for x in self.arg])
175+
result = tuple([x.eval() if isinstance(x, Expr) else x for x in self.arg])
176+
if self.asm == 'DEFB' and any(x > 255 for x in result):
177+
errmsg.warning_value_will_be_truncated(self.lineno)
178+
return result
169179

170180
self.arg = tuple([x if not isinstance(x, Expr) else x.eval() for x in self.arg])
171181
if gl.has_errors:

tests/functional/db256.asm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ORG 32768
2+
3+
DB 0, 1, 2, 256
4+
DB 1, TEST
5+
DS 10
6+
DS 10, 256
7+
8+
TEST:
9+
10+

tests/functional/db256.bin

26 Bytes
Binary file not shown.

tests/functional/test_errmsg.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ line_err.bas:5: error: Variable 'q' already declared at line_err.bas:1
181181
>>> process_file('let_expr_type_crash.bas')
182182
let_expr_type_crash.bas:3: error: Syntax Error. Unexpected token 's' <ID>
183183
let_expr_type_crash.bas:8: error: Function 'editStringFN' takes 0 parameters, not 3
184+
>>> process_file('db256.asm')
185+
db256.asm:3: warning: [W200] Value will be truncated
186+
db256.asm:4: warning: [W200] Value will be truncated
187+
db256.asm:6: warning: [W200] Value will be truncated
184188

185189
# Test warning silencing
186190
>>> process_file('mcleod3.bas', ['-S', '-q', '-O --expect-warnings=2'])

0 commit comments

Comments
 (0)