123 lines
2.8 KiB
Python
123 lines
2.8 KiB
Python
|
|
from lark import Lark, Visitor, Transformer, v_args, Token
|
|
|
|
from post import *
|
|
|
|
|
|
calc_grammar = """
|
|
?varlist: "[" [NAME ("," NAME)*] "]"
|
|
|
|
?start: statement*
|
|
|
|
?statement: NAME "=" sum -> assign_var
|
|
| avg "{" [NAME ("," NAME)*] "}" -> assign_avg_var
|
|
| varlist
|
|
|
|
?sum: product
|
|
| sum "+" product -> add
|
|
| sum "-" product -> sub
|
|
|
|
?product: atom
|
|
| product "*" atom -> mul
|
|
| product "/" atom -> div
|
|
|
|
?atom: NUMBER -> number
|
|
| "-" atom -> neg
|
|
| NAME -> var
|
|
| NAME "'" -> fluc
|
|
| "$" NAME -> env
|
|
| "(" sum ")"
|
|
| inlinefunc "(" sum ")" -> icall
|
|
| mathfunc "(" sum ")" -> fcall
|
|
| derivative "(" NAME ")" -> dnx
|
|
|
|
avg: "avg" [NAME]
|
|
|
|
?inlinefunc: "sqr" -> sqr
|
|
| "pow3" -> pow3
|
|
|
|
?mathfunc: "log" -> log
|
|
| "exp" -> exp
|
|
| "sqrt" -> sqrt
|
|
| "abs" -> abs
|
|
| "rxn_rate" -> rxn_rate
|
|
|
|
?derivative: "ddx" -> ddx
|
|
| "d2dx" -> d2dx
|
|
| "ddy" -> ddy
|
|
| "d2dy" -> d2dy
|
|
| "ddz" -> ddz
|
|
| "d2dz" -> d2dz
|
|
|
|
%import common.CNAME -> NAME
|
|
%import common.NUMBER
|
|
%import common.WS
|
|
|
|
%ignore WS
|
|
"""
|
|
|
|
|
|
class VersionInfo(object):
|
|
|
|
def __init__ (self, input_raw):
|
|
import os, datetime
|
|
import subprocess as sp
|
|
|
|
bd = 'character (len=*), parameter :: build_date="{}"'
|
|
bb = 'character (len=*), parameter :: build_base="{}"'
|
|
|
|
self.build_date = bd.format(datetime.datetime.today().ctime())
|
|
|
|
try:
|
|
self.build_base = bb.format(sp.check_output(["git", "rev-parse", "HEAD"]).strip())
|
|
except subprocess.CalledProcessError:
|
|
self.build_base = "None"
|
|
|
|
tinfo = 'character (len=*), parameter :: term_info={}'
|
|
|
|
self.lines = input_raw.split(os.linesep)
|
|
|
|
nl = len(self.lines)
|
|
|
|
self.term_info = tinfo.format("//char(10)//".join(map('"{}"'.format, self.lines)))
|
|
|
|
def fparams (self):
|
|
return "\n".join([self.build_date, self.build_base, self.term_info])
|
|
|
|
def test():
|
|
|
|
with open("resources/m_template.f90") as template_file:
|
|
mod_form = template_file.read()
|
|
|
|
with open("terms.input") as inputfile:
|
|
terms_raw = ((inputfile.read()))
|
|
|
|
vinfo = VersionInfo(terms_raw)
|
|
|
|
|
|
tree = Lark(calc_grammar, parser='lalr' ).parse(terms_raw)
|
|
|
|
ir1 = Stage1(tree)
|
|
|
|
ir2 = Stage2(ir1)
|
|
|
|
ir3 = Stage3(ir2)
|
|
|
|
ir4 = Stage4(ir3)
|
|
|
|
cdict = ir4.print_program()
|
|
|
|
cdict["module_data"] = vinfo.fparams() + "\n" + cdict["module_data"]
|
|
|
|
print mod_form.format( cdict )
|
|
|
|
# print mod_form.format( ir4.print_program() )
|
|
|
|
# print mod_form.format( ir3.print_program() )
|
|
|
|
# ir4.save_ir()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
test()
|