Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] LFortran code printing #16931

wants to merge 14 commits into
base: master

Add more (untested) visitor methods

- Float
- Rational
- Pow
- Function
  • Loading branch information...
anpandey committed Jun 3, 2019
commit 67a06a83d4aa239e8d319837d9ed7d11b67f7846
@@ -15,7 +15,10 @@ def __init__(self):
# process. Since this object is stateful, the intention is that each
# instance of this class is used only once.
self.translation_unit = builder.make_translation_unit()

# Build the types that should be used in conversion of reals
self.type_integer = builder.make_type_integer()
self.type_real = builder.make_type_real()

# Variables that will be used in the body of a function declaration.
self.variables = []
@@ -56,6 +59,34 @@ def _print_Integer(self, expr):
_print_One = _print_Integer
_print_Zero = _print_Integer

def _print_Float(self, expr):
# Floating point numbers in LFortran are represented as strings.
# LFortran supports the _dp suffix, so we default to double precision
# for everything
printed = CodePrinter._print_Float(self, expr)
return asr.Num("%s_dp" % printed, type=self.type_real)

def _print_Rational(self, expr):
p = asr.Num("%d_dp" % expr.p, type=self.type_real)
q = asr.Num("%d_dp" % expr.q, type=self.type_real)
return builder.make_binop(p, asr.Div(), q)

def _print_Pow(self, expr):
# FIXME: Results in a type mismatch when the base contains a variable,
# since variables default to integers
if expr.exp == -1:
one = asr.Num("1_dp", type=self.type_real)
base = self._print(expr.base)
return builder.make_binop(one, asr.Div(), base)
exp = self._print(expr.exp)
base = self._print(expr.base)
return builder.make_binop(base, asr.Pow(), exp)

def _print_Function(self, expr):
args = [self._print(x) for x in expr.args]
return asr.FuncCall(args=args)

def _print_Symbol(self, expr):
sym_name =

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.