Skip to content

Commit eec47b6

Browse files
authored
Merge pull request #541 from boriel/feature/user_macros_in_include_filenames
Allow #include macro
2 parents 11ef813 + 643e012 commit eec47b6

7 files changed

Lines changed: 93 additions & 14 deletions

File tree

src/parsetab/tabs.dbm.bak

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'zxbpp', (0, 73193)
2-
'asmparse', (73216, 271961)
3-
'zxnext_asmparse', (345600, 302573)
4-
'zxbparser', (648192, 703160)
1+
'zxbpp', (0, 76970)
2+
'asmparse', (77312, 271961)
3+
'zxnext_asmparse', (349696, 302573)
4+
'zxbparser', (652288, 703160)

src/parsetab/tabs.dbm.dat

4 KB
Binary file not shown.

src/parsetab/tabs.dbm.dir

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'zxbpp', (0, 73193)
2-
'asmparse', (73216, 271961)
3-
'zxnext_asmparse', (345600, 302573)
4-
'zxbparser', (648192, 703160)
1+
'zxbpp', (0, 76970)
2+
'asmparse', (77312, 271961)
3+
'zxnext_asmparse', (349696, 302573)
4+
'zxbparser', (652288, 703160)

src/zxbpp/prepro/operators.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# -*- coding: utf-8 -*-
22
# Operators implemented in the preprocessor
33

4-
5-
from src.zxbpp import prepro
64
from src.zxbpp.prepro.macrocall import MacroCall
5+
from src.zxbpp.prepro import DefinesTable
76

87

98
class Concatenation(MacroCall):
@@ -13,12 +12,12 @@ class Concatenation(MacroCall):
1312
Out of a macro body, ID1 and ID2 are expanded normally and "##" is
1413
also output as is.
1514
"""
16-
def __init__(self, fname: str, lineno: int, table: 'prepro.DefinesTable', left: MacroCall, right: MacroCall):
15+
def __init__(self, fname: str, lineno: int, table: DefinesTable, left: MacroCall, right: MacroCall):
1716
super().__init__(fname=fname, lineno=lineno, table=table, id_='')
1817
self.left = left
1918
self.right = right
2019

21-
def __call__(self, symbolTable: 'prepro.DefinesTable' = None) -> str:
20+
def __call__(self, symbolTable: DefinesTable = None) -> str:
2221
return self.left(symbolTable).rstrip() + self.right(symbolTable).lstrip()
2322

2423

@@ -27,7 +26,7 @@ class Stringizing(MacroCall):
2726
macrocall into a BASIC string (double quotes " as delimiters, escaped as
2827
doubled-double quote 'Hello "dear"' => 'Hello ""dear""').
2928
"""
30-
def __init__(self, fname: str, lineno: int, table: 'prepro.DefinesTable', macro_call: MacroCall):
29+
def __init__(self, fname: str, lineno: int, table: DefinesTable, macro_call: MacroCall):
3130
super().__init__(fname=fname, lineno=lineno, table=table, id_='')
3231
self.macro_call = macro_call
3332

@@ -36,5 +35,5 @@ def stringize(s: str) -> str:
3635
s = s.replace('"', '""')
3736
return f'"{s}"'
3837

39-
def __call__(self, symbolTable: 'prepro.DefinesTable' = None) -> str:
38+
def __call__(self, symbolTable: DefinesTable = None) -> str:
4039
return self.stringize(self.macro_call(symbolTable))

src/zxbpp/zxbpp.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import sys
1515
import os
1616
import argparse
17+
import re
1718

1819
from dataclasses import dataclass
1920

@@ -64,6 +65,10 @@
6465
# Enabled to FALSE if IFDEF failed
6566
ENABLED: bool = True
6667

68+
# Defines Regexp to match filenames
69+
RE_LOCAL_FIRST_FILENAME = re.compile('^"([^"]|"")*"$')
70+
RE_GLOBAL_FIRST_FILENAME = re.compile('^<[^>]+>$')
71+
6772

6873
class IfDef(NamedTuple):
6974
enabled: bool
@@ -382,6 +387,23 @@ def p_include_fname(p):
382387
p.lexer.next_token = '_ENDFILE_'
383388

384389

390+
def p_include_macro(p):
391+
""" include : INCLUDE expr
392+
"""
393+
global_fist = RE_GLOBAL_FIRST_FILENAME.match(p[2])
394+
local_first = RE_LOCAL_FIRST_FILENAME.match(p[2])
395+
if global_fist is None and local_first is None:
396+
error(p.lineno(1), f"invalid filename {p[2]}")
397+
p[0] = []
398+
return
399+
400+
if ENABLED:
401+
p[0] = include_file(p[2][1:-1], p.lineno(2), local_first=local_first is not None)
402+
else:
403+
p[0] = []
404+
p.lexer.next_token = '_ENDFILE_'
405+
406+
385407
def p_include_once(p):
386408
""" include_once : INCLUDE ONCE STRING
387409
"""

tests/functional/prepro64.bi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
#define a <pos.bas>
3+
4+
#include a
5+
6+

tests/functional/prepro64.out

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#line 1 "prepro64.bi"
2+
3+
#line 3 "prepro64.bi"
4+
5+
#line 1 "/zxbasic/src/arch/zx48k/library/pos.bas"
6+
7+
8+
9+
10+
11+
12+
13+
14+
15+
16+
17+
18+
19+
#line 15 "/zxbasic/src/arch/zx48k/library/pos.bas"
20+
21+
#pragma push(case_insensitive)
22+
#pragma case_insensitive = true
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
function FASTCALL pos as ubyte
33+
asm
34+
push namespace core
35+
PROC
36+
37+
call __LOAD_S_POSN
38+
ld <pos.bas>, e
39+
40+
ENDP
41+
pop namespace
42+
end asm
43+
end function
44+
45+
#pragma pop(case_insensitive)
46+
#require "sposn.asm"
47+
48+
49+
#line 45 "/zxbasic/src/arch/zx48k/library/pos.bas"
50+
#line 5 "prepro64.bi"
51+
52+

0 commit comments

Comments
 (0)