Skip to content

Commit bb25ca7

Browse files
committed
Add ability to use constants
1 parent df05b6d commit bb25ca7

2 files changed

Lines changed: 42 additions & 22 deletions

File tree

wdl/wavgen.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ def loadWDL(infile, outfile="/dev/null", verbose=1):
133133
pval = int(match.group(2))
134134
Parameters.update({pname: pval})
135135
continue
136-
# look for constants
137-
match = re.search(r"^constant\s+(\w+)\s*=\s*([+-]?\d+(?:\.\d*)?)\s*$", line)
136+
# look 'const' or 'constant' (case-insensitive), value may be float
137+
match = re.search(r"(?i)^(?:const(?:ant)?)\s+(\w+)\s*=\s*([+-]?\d+(?:\.\d*)?)\s*$", line)
138138
if match is not None:
139139
cname = match.group(1)
140140
cval = float(match.group(2))

wdl/wdlParser.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
import Lexer
5757
from Symbols import *
5858
import sys
59+
# Access constants collected by wavgen.py (fallback to empty if not present)
60+
try:
61+
from wavgen import Constants
62+
except Exception:
63+
Constants = {}
5964

6065
sys.dont_write_bytecode = True
6166

@@ -254,18 +259,23 @@ def lvds(slot_number):
254259

255260
lvds_chan = None
256261

257-
if found(NUMBER):
262+
if found(IDENTIFIER) and token.cargo in Constants:
263+
lvds_chan = str(int(float(Constants[token.cargo])))
264+
consume(IDENTIFIER)
265+
elif found(NUMBER):
258266
lvds_chan = token.cargo
259-
if module_name.upper() in {"LVDS"}:
260-
if not (1 <= int(lvds_chan) <= 16):
261-
error(
262-
f"LVDS channel {dq(lvds_chan)} outside range {{1:16}} for module: "
263-
f"{module_name.upper()}"
264-
)
265-
else:
266-
error(f"LVDSL is an invalid keyword for module: {module_name.upper()}")
267+
consume(NUMBER)
268+
else:
269+
error("Expected number or constant for LVDS channel")
267270

268-
consume(NUMBER)
271+
if module_name.upper() in {"LVDS"}:
272+
if not (1 <= int(lvds_chan) <= 16):
273+
error(
274+
f"LVDS channel {dq(lvds_chan)} outside range {{1:16}} for module: "
275+
f"{module_name.upper()}"
276+
)
277+
else:
278+
error(f"LVDSL is an invalid keyword for module: {module_name.upper()}")
269279

270280
# there can be an optional label, specified as token type=STRING
271281
if found(STRING):
@@ -1909,17 +1919,22 @@ def to():
19091919
global setLevel
19101920

19111921
consume("TO")
1912-
# could be a negative number...
1922+
# optional leading minus sign
19131923
if found("-"):
1914-
# if so, consume the sign and remember it
19151924
consume("-")
19161925
sign = -1.0
19171926
else:
19181927
sign = 1.0
1919-
if found(NUMBER):
1920-
# multiply the value by the sign from above (hehe)
1928+
1929+
# level can be a NUMBER or a defined constant
1930+
if found(IDENTIFIER) and token.cargo in Constants:
1931+
setLevel = str(sign * float(Constants[token.cargo]))
1932+
consume(IDENTIFIER)
1933+
elif found(NUMBER):
19211934
setLevel = str(sign * float(token.cargo))
1922-
consume(NUMBER)
1935+
consume(NUMBER)
1936+
else:
1937+
error("Expected level as number or defined constant after TO")
19231938

19241939

19251940
# -----------------------------------------------------------------------------
@@ -2191,6 +2206,7 @@ def generic_sequence(*sequenceName):
21912206
(found(IDENTIFIER))
21922207
and (token.cargo not in subroutines)
21932208
and (token.cargo not in paramNames)
2209+
and (token.cargo not in Constants)
21942210
):
21952211
error(
21962212
"(wdlParser.py::generic_sequence) undefined symbol "
@@ -2222,14 +2238,18 @@ def generic_sequence(*sequenceName):
22222238
consume(IDENTIFIER)
22232239
consume("(")
22242240
# if next token isn't a closing paren then assume
2225-
# it's a number or param
2241+
# it's a number, param, or constant
22262242
if not found(")"):
2227-
sequence_line += "(" + token.cargo # + ")"
2228-
# and if it's not a number then it must be a defined param
2229-
if not found(NUMBER) and token.cargo not in paramNames:
2243+
sequence_line += "(" + token.cargo # keep the name; const is emitted earlier
2244+
# validate: allow NUMBER, defined param, or defined constant
2245+
if (
2246+
not found(NUMBER)
2247+
and token.cargo not in paramNames
2248+
and token.cargo not in constNames
2249+
):
22302250
error(
22312251
"(wdlParser.py::generic_sequence) undefined "
2232-
"param " + token.show(align=False)
2252+
"param or constant " + token.show(align=False)
22332253
)
22342254
elif found("RETURN"):
22352255
consume("RETURN")

0 commit comments

Comments
 (0)