Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request boriel-basic#638 from boriel/feature/add_options_t…
Browse files Browse the repository at this point in the history
…o_INCBIN

Feature/add options to incbin
  • Loading branch information
boriel committed Dec 6, 2022
2 parents 02081fe + 31df7f3 commit 756b001
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*.yml text

*.png binary
*.bin binary

*.bas linguist-vendored
*.asm linguist-vendored

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ scratch/
htmlcov/
build/
venv/
.pypi-token
3 changes: 3 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ repos:
rev: v2.3.0
hooks:
- id: check-yaml
exclude: \.bin$
- id: end-of-file-fixer
exclude: \.bin$
- id: trailing-whitespace
exclude: \.bin$
- repo: https://github.com/psf/black
rev: 22.1.0
hooks:
Expand Down
6 changes: 3 additions & 3 deletions src/parsetab/tabs.dbm.bak
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'zxbpp', (0, 66998)
'asmparse', (67072, 220672)
'zxnext_asmparse', (287744, 245099)
'zxbparser', (532992, 641182)
'asmparse', (67072, 222500)
'zxnext_asmparse', (289792, 246933)
'zxbparser', (537088, 641182)
Binary file modified src/parsetab/tabs.dbm.dat
Binary file not shown.
6 changes: 3 additions & 3 deletions src/parsetab/tabs.dbm.dir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'zxbpp', (0, 66998)
'asmparse', (67072, 220672)
'zxnext_asmparse', (287744, 245099)
'zxbparser', (532992, 641182)
'asmparse', (67072, 222500)
'zxnext_asmparse', (289792, 246933)
'zxbparser', (537088, 641182)
34 changes: 32 additions & 2 deletions src/zxbasm/asmparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from src.api import global_ as gl
from src.api.config import OPTIONS
from src.api.debug import __DEBUG__
from src.api.errmsg import error
from src.api.errmsg import error, warning
from src.zxbasm import asmlex, basic
from src.zxbasm import global_ as asm_gl
from src.zxbasm.asm import Asm, Container
Expand Down Expand Up @@ -383,19 +383,49 @@ def p_align(p):


def p_incbin(p):
"""asm : INCBIN STRING"""
"""asm : INCBIN STRING
| INCBIN STRING COMMA expr
| INCBIN STRING COMMA expr COMMA expr
"""

try:
fname = zxbpp.search_filename(p[2], p.lineno(2), local_first=True)
if not fname:
p[0] = None
return

with src.api.utils.open_file(fname, "rb") as f:
filecontent = f.read()

except IOError:
error(p.lineno(2), "cannot read file '%s'" % p[2])
p[0] = None
return

offset = 0
length = None

if len(p) > 4:
offset = p[4].eval()

if len(p) > 6:
length = p[6].eval()
if length < 1:
error(p.lineno(5), "INCBIN length must be greater than 0")

if offset < 0:
offset = len(filecontent) + offset
if offset < 0 or offset >= len(filecontent):
error(p.lineno(4), "INCBIN offset is out of range")

if length is None:
length = len(filecontent) - offset

if offset + length > len(filecontent):
excess = len(filecontent) - (offset + length)
warning(p.lineno(5), f"INCBIN length if beyond file length by {excess} bytes")

filecontent = filecontent[offset : offset + length]
p[0] = Asm(p.lineno(1), "DEFB", filecontent)


Expand Down
3 changes: 3 additions & 0 deletions tests/functional/incbin2.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

; Tries to include a non binary file
incbin "incbin2.asm", 16
2 changes: 2 additions & 0 deletions tests/functional/incbin2.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ude a non binary file
incbin "incbin2.asm", 16
3 changes: 3 additions & 0 deletions tests/functional/incbin3.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

; Tries to include a non binary file
incbin "incbin3.asm", 16, 2
1 change: 1 addition & 0 deletions tests/functional/incbin3.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ud
3 changes: 3 additions & 0 deletions tests/functional/incbin4.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

; Tries to include a non binary file
incbin "incbin4.asm", -5, 2
1 change: 1 addition & 0 deletions tests/functional/incbin4.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5,
3 changes: 3 additions & 0 deletions tests/functional/incbin5.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

; Tries to include a non binary file
incbin "incbin5.asm", -6
1 change: 1 addition & 0 deletions tests/functional/incbin5.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
", -6
2 changes: 2 additions & 0 deletions tests/functional/incbin6.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

incbin "incbin6.asm", -60, 2

0 comments on commit 756b001

Please sign in to comment.