“On two occasions I have been asked [by members of Parliament!]: ‘Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?’ I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.”
— Charles Babbage

Evaluation feedback

Welcome everyone, this is your host Nikhil Maan aka Sc0rpi0n101 and this week was the last week of Phase-1 and the evaluations for the first phase. So, this post also marks the end of the first phase of the project.

The First Phase


May 27 - June 28 marked the first phase for the project and GSoC 2019. It has been 5 weeks of work and coding and there has been significant progress in the project. I’ve managed to create basic parsers for C and Fortran, more details in the following sections. It’s been a great experience. I got to meet a lot of interesting people, learn a lot of things and created something that can actually help the community. For anyone that’s interested in the working of the parsers, stay tuned, I have a detailed explaination for some of the code segments how some of the conversions work. I’d be updating you all with more details in the upcoming blogs once the parser are merged.

The Evaluation


The Phase 1 evaluations had to be completed between June 24-28. I had to submit an evalution for the mentors and the mentors had to submit an evalution of my work to Google. I would only be allowed to continue this project to the second phase if I was given a pass grade in the evaluation. I also had to tell Google about the Organization and the Mentors, how resposnsive and helpful they have been with the prject. How are the communications between me and the mentor and how the project progress has been.

The Fortran Parser


The Fortran Parser takes the LFortran ASR and converts that into the Python AST which is then compiled to give out Python code as a string. The original aim of the parser was to return SymPy expression, not python code. So, the parser is still a step behind and needs to be fixed in that aspect, but it works as for converting the ASR into AST.

You can check out the current parser at #17054

For anyone that’s interested in working of the parsers.Here’s an instance of code for converting Assignments works as follows :

if isinstance(node.target, asr.Variable):
   target = node.target
   value = node.value
   if isinstance(value, asr.Variable):
        new_node = Assign(
            targets = [
                   id = target.name,
                   ctx = Store()
            value = Name(
               id = value.name,
               ctx = Store()
   elif (type(value) == asr.BinOp):
        exp_ast = call_visitor_func(value)
        for expr in exp_ast.body:
            new_node = Assign(
               targets = [
                        id = target.name,
                        ctx = Store()
               ], value = expr
        raise NotImplementedError("Numeric assignments not supported")
   raise NotImplementedError("Arrays not supported")

The convesion only supports single variable assignment. So, the code section runs if the target is a variable. Then, if the value is a Variable, then a new assignment object is created, with the target as a Name node to store the data and the value as a name node to load the data. If the value is a binary operation (BinOp), the visitor function is called on the value to get the pythonic nodes for the binary operation, the exp_ast then stores the list of nodes returned by the visitor function. Then, a new assignment object is created using the target and the returned node.

No Parsing

So, the parser currently creates Python AST nodes and hence generates python code. But, now the plan is to use the codegen AST nodes from SymPy so that it generates SymPy expression.

The C Parser


I have worked on the C parser so that it uses the codegen nodes already and generates SymPy expression. The parser uses Clang’s AST to extract the C code, then the parser uses that to generate codegen AST nodes. Here’s a code segment from the transformation function for variable declaration:

   children = node.get_children()
   prev_child = None
   child = next(children)
   #ignoring namespace and type details for the variable
   while child.kind == CursorKind.NAMESPACE_REF:
        prev_child = child
        child = next(children)

   while child.kind == CursorKind.TYPE_REF:
        prev_child = child
        child = next(children)
   args = self.transform(child)
   return Variable(node.spelling).as_Declaration(type = args[0], value = args[1])

except StopIteration:
   return Variable(node.spelling).as_Declaration()

If the variable declaration contains a value assigned to the variable, then the try block is executed. The except block is executed when the variable is only declared with no value assigned to it. Then a new Variable object is created as declaration and returned. Inside the try block, the namespace and type information are ignored if any. Then, a new Variable object is created as declaration with the type information and the value to be assigned to it.

The Meeting

Project Meeting

This week the meeting took place as expected on Thursday, June 28. The features that are currently available in LFortran and Clang which can be currently used for the parsers and the features that still need further development before being useful were discussed. The plans for the upcoming week were discussed. A summary of the meeting is :

  • The features available in the LFortran ASR
  • Plans for conversion of the Fortran ASR to use codegen AST nodes
  • Looking for similarities in both the parsers to be used in a superclass [not priority]
  • Steps to get the PR merge ready
  • Handling the optional dependencies required by the parsers
  • Extending Documentation to include working on submodules having external dependencies

The Next Phase

Next Phase

The plan now is to update both the Pull Requests with both the parsers using SymPy’s codegen AST nodes so that the parsers generate SymPy expressions instead of python code. Then, getting the parser merge ready with all the documentation, testing, and dependencies set up. Then, the parser will be expanded upon including being able to parse all the other features available and to keep them updated as the ASR features get updated.

The Drupal Camp blog couldnot be completed as I had some work due to the evaluation work. The blog will be updated soon.

Subscribe to the blog if you don’t want to miss any important updates