182 lines
4.1 KiB
Python
182 lines
4.1 KiB
Python
#
|
|
# This example shows how to write a basic calculator with variables.
|
|
#
|
|
|
|
from lark import Lark, Transformer, v_args
|
|
|
|
|
|
try:
|
|
input = raw_input # For Python2 compatibility
|
|
except NameError:
|
|
pass
|
|
|
|
|
|
|
|
calc_grammar = """
|
|
?varlist: "[" [NAME ("," NAME)*] "]"
|
|
|
|
?start: NAME "=" sum -> assign_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
|
|
| "(" sum ")"
|
|
| mathfunc "(" sum ")" -> fcall
|
|
| derivative "(" NAME ")" -> dnx
|
|
|
|
?mathfunc: "log" -> log
|
|
| "exp" -> exp
|
|
| "sqrt" -> sqrt
|
|
|
|
?derivative: "dx" -> dx
|
|
| "d2x" -> d2x
|
|
|
|
%import common.CNAME -> NAME
|
|
%import common.NUMBER
|
|
%import common.WS
|
|
|
|
%ignore WS
|
|
"""
|
|
|
|
real_array_decl = "real*8, allocatable, dimension(:,:,:) :: {}"
|
|
real_array_alloc = "allocate({0}(nxp,nyp,nzp), stat=ierr) ; {0} = 0."
|
|
real_array_free = "deallocate({})"
|
|
|
|
real_array_loop = """
|
|
do k = 1, nzp
|
|
do j = 1, nyp
|
|
do i = 1, nxp
|
|
{0[0]}(i,j,k) = {0[1]}
|
|
end do
|
|
end do
|
|
end do
|
|
"""
|
|
|
|
|
|
@v_args(inline=True) # Affects the signatures of the methods
|
|
class CalculateTree(Transformer):
|
|
|
|
number = float
|
|
|
|
def __init__(self):
|
|
self.primary = []
|
|
self.derived = {}
|
|
self.averaged = {}
|
|
self.derivatives = {}
|
|
|
|
def varlist(self, *args):
|
|
for arg in args:
|
|
self.primary.append(arg.value)
|
|
return self.primary
|
|
|
|
def assign_var(self, name, value):
|
|
self.derived[name.value] = value
|
|
return "{} = {}".format(name, value)
|
|
|
|
def var(self, name):
|
|
return name + "(i,j,k)"
|
|
|
|
def dnx (self, a, b):
|
|
signature = "{}_{}".format(a, b)
|
|
|
|
self.derivatives[signature] = (a, b)
|
|
|
|
return "{}_{}".format(a, b) + "(i,j,k)"
|
|
|
|
def fcall (self, a, b):
|
|
return "( {} ( {} ) )".format(a, b)
|
|
|
|
def neg(self, value):
|
|
return "( - {} )".format(value)
|
|
|
|
def add(self, a, b):
|
|
return "( {} + {} )".format(a, b)
|
|
|
|
def sub(self, a, b):
|
|
return "( {} - {} )".format(a, b)
|
|
|
|
def mul(self, a, b):
|
|
return "( {} * {} )".format(a, b)
|
|
|
|
def div(self, a, b):
|
|
return "( {} / {} )".format(a, b)
|
|
|
|
log = lambda self : "log"
|
|
exp = lambda self : "exp"
|
|
dx = lambda self : "dx"
|
|
d2x = lambda self : "d2x"
|
|
sqrt = lambda self : "sqrt"
|
|
|
|
def array_decl (self):
|
|
return "\n".join(
|
|
map(real_array_decl.format, self.derived.iterkeys())
|
|
+ map(real_array_decl.format, self.derivatives.iterkeys())
|
|
)
|
|
|
|
def array_init (self):
|
|
return "\n".join(
|
|
map(real_array_alloc.format, self.derived.iterkeys())
|
|
+ map(real_array_alloc.format, self.derivatives.iterkeys())
|
|
)
|
|
|
|
def array_final (self):
|
|
return "\n".join(
|
|
map(real_array_free.format, self.derived.iterkeys())
|
|
+ map(real_array_free.format, self.derivatives.iterkeys())
|
|
)
|
|
|
|
def array_ci (self):
|
|
return "\n".join(map(real_array_loop.format, self.derived.iteritems()))
|
|
|
|
def module_dict (self):
|
|
md = {}
|
|
md["module_name"] = "terms"
|
|
md["module_data"] = self.array_decl()
|
|
md["module_init"] = self.array_init()
|
|
md["module_finalize"] = self.array_final()
|
|
md["module_ci"] = self.array_ci()
|
|
|
|
return md
|
|
|
|
|
|
tf=CalculateTree()
|
|
|
|
calc_parser = Lark(calc_grammar, parser='lalr' , transformer=tf)
|
|
calc = calc_parser.parse
|
|
|
|
import sys
|
|
|
|
def main():
|
|
while True:
|
|
try:
|
|
s = input('> ')
|
|
except EOFError:
|
|
break
|
|
print(calc(s))
|
|
|
|
|
|
def test():
|
|
|
|
with open("resources/m_template.f90") as template_file:
|
|
mod_form = template_file.read()
|
|
|
|
with open("terms.input") as inputfile:
|
|
for line in inputfile:
|
|
if len(line.strip()) > 0:
|
|
(calc(line))
|
|
|
|
|
|
print mod_form.format(tf.module_dict())
|
|
|
|
if __name__ == '__main__':
|
|
test()
|
|
# main()
|