74 lines
2.2 KiB
Python
74 lines
2.2 KiB
Python
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()
|