Consolidate build info module generation into code_gen.py and delete build_info_gen.py
This commit is contained in:
parent
4c732f3880
commit
e9524d645b
3 changed files with 54 additions and 77 deletions
|
|
@ -1,74 +0,0 @@
|
|||
import sys
|
||||
import os
|
||||
import datetime
|
||||
import subprocess as sp
|
||||
|
||||
# Add the directory containing this script to sys.path for local imports
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
sys.path.insert(0, script_dir)
|
||||
|
||||
from lark import Lark
|
||||
import code_gen
|
||||
import post
|
||||
|
||||
def escape_fortran_string(s):
|
||||
# Escape double quotes by doubling them
|
||||
escaped = s.replace('"', '""')
|
||||
# Split by newline and format as Fortran concatenation with char(10)
|
||||
lines = escaped.splitlines()
|
||||
if not lines:
|
||||
return '""'
|
||||
formatted_lines = [f'"{line}"' for line in lines]
|
||||
return ' // char(10) // &\n '.join(formatted_lines)
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python3 build_info_gen.py <termspec_file>")
|
||||
sys.exit(1)
|
||||
|
||||
terms_file = sys.argv[1]
|
||||
with open(terms_file, 'r', encoding='utf-8') as f:
|
||||
terms_raw = f.read()
|
||||
|
||||
# Generate Version Info
|
||||
vinfo = code_gen.VersionInfo(terms_raw)
|
||||
build_info_str = vinfo.fparams()
|
||||
|
||||
# Generate LaTeX equations
|
||||
parser = Lark(code_gen.calc_grammar,
|
||||
parser='lalr',
|
||||
lexer_callbacks={
|
||||
'ESCAPED_STRING': code_gen.tok_to_str,
|
||||
'INT': code_gen.tok_to_int,
|
||||
'BOOL': code_gen.tok_to_bool
|
||||
})
|
||||
tree = parser.parse(terms_raw)
|
||||
ir1 = post.Stage1(tree)
|
||||
ir2 = post.Stage2(ir1)
|
||||
ir3 = post.Stage3(ir2)
|
||||
ir4 = post.Stage4(ir3)
|
||||
|
||||
latex_lines = ["{"]
|
||||
for avg in ir4.averaged.values():
|
||||
latex_lines.append(f' "{avg.name}" : r"${avg.latex}$",')
|
||||
latex_lines.append("}")
|
||||
latex_str = "\n".join(latex_lines)
|
||||
|
||||
# Format for Fortran
|
||||
fortran_build_info = escape_fortran_string(build_info_str)
|
||||
fortran_latex = escape_fortran_string(latex_str)
|
||||
|
||||
# Write m_build_info.f90
|
||||
fortran_code = f"""module m_build_info
|
||||
implicit none
|
||||
character(len=*), parameter :: build_info_str = &
|
||||
{fortran_build_info}
|
||||
character(len=*), parameter :: latex_equations_str = &
|
||||
{fortran_latex}
|
||||
end module m_build_info
|
||||
"""
|
||||
# Print to stdout so it can be redirected
|
||||
print(fortran_code)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
@ -13,6 +13,7 @@ parser.add_argument("term_file", help="name of file containing postprocessing te
|
|||
parser.add_argument("-b", "--build-info", action="store_true", help="print build_info instead of code")
|
||||
parser.add_argument("-x", "--print-latex", action="store_true", help="print latex equation instead of code")
|
||||
parser.add_argument("-p", "--print-report", action="store_true", help="print code generation report instead of code")
|
||||
parser.add_argument("-m", "--make-build-module", action="store_true", help="print build_info Fortran module instead of code")
|
||||
args = parser.parse_args()
|
||||
|
||||
calc_grammar = """
|
||||
|
|
@ -182,6 +183,54 @@ def test(terms_raw, report=False, latex=False):
|
|||
|
||||
|
||||
|
||||
def escape_fortran_string(s):
|
||||
"""Escapes string content for standard Fortran source formatting."""
|
||||
escaped = s.replace('"', '""')
|
||||
lines = escaped.splitlines()
|
||||
if not lines:
|
||||
return '""'
|
||||
formatted_lines = [f'"{line}"' for line in lines]
|
||||
return ' // char(10) // &\n '.join(formatted_lines)
|
||||
|
||||
|
||||
def generate_build_info_module(terms_raw):
|
||||
"""Generates a complete standard Fortran module containing build info and LaTeX equations."""
|
||||
vinfo = VersionInfo(terms_raw)
|
||||
build_info_str = vinfo.fparams()
|
||||
|
||||
parser = Lark(calc_grammar,
|
||||
parser='lalr',
|
||||
lexer_callbacks={
|
||||
'ESCAPED_STRING': tok_to_str,
|
||||
'INT': tok_to_int,
|
||||
'BOOL': tok_to_bool
|
||||
})
|
||||
tree = parser.parse(terms_raw)
|
||||
ir1 = Stage1(tree)
|
||||
ir2 = Stage2(ir1)
|
||||
ir3 = Stage3(ir2)
|
||||
ir4 = Stage4(ir3)
|
||||
|
||||
latex_lines = ["{"]
|
||||
for avg in ir4.averaged.values():
|
||||
latex_lines.append(f' "{avg.name}" : r"${avg.latex}$",')
|
||||
latex_lines.append("}")
|
||||
latex_str = "\n".join(latex_lines)
|
||||
|
||||
fortran_build_info = escape_fortran_string(build_info_str)
|
||||
fortran_latex = escape_fortran_string(latex_str)
|
||||
|
||||
fortran_code = f"""module m_build_info
|
||||
implicit none
|
||||
character(len=*), parameter :: build_info_str = &
|
||||
{fortran_build_info}
|
||||
character(len=*), parameter :: latex_equations_str = &
|
||||
{fortran_latex}
|
||||
end module m_build_info
|
||||
"""
|
||||
print(fortran_code)
|
||||
|
||||
|
||||
def build_info(terms_raw):
|
||||
"""Generates and prints build information for the current code generation run.
|
||||
|
||||
|
|
@ -200,7 +249,9 @@ if __name__ == '__main__':
|
|||
with open(args.term_file) as inputfile:
|
||||
terms_raw = ((inputfile.read()))
|
||||
|
||||
if args.build_info:
|
||||
if args.make_build_module:
|
||||
generate_build_info_module(terms_raw)
|
||||
elif args.build_info:
|
||||
build_info(terms_raw)
|
||||
else:
|
||||
test(terms_raw, report=args.print_report, latex=args.print_latex)
|
||||
|
|
|
|||
|
|
@ -69,5 +69,5 @@ test_compact : Compact.o test_compact.o
|
|||
|
||||
test_compact.o : Compact.o
|
||||
|
||||
m_build_info.f90 : code_gen/build_info_gen.py ${TERMSPEC} code_gen/code_gen.py code_gen/post.py
|
||||
${PYTHON} code_gen/build_info_gen.py ${TERMSPEC} > $@
|
||||
m_build_info.f90 : ${TERMSPEC} code_gen/code_gen.py code_gen/post.py
|
||||
${PYTHON} code_gen/code_gen.py -m ${TERMSPEC} > $@
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue