“A language that doesn’t affect the way you think about programming is not worth knowing.” ― Alan J. Perlis

Welcome everyone, this is your host Nikhil Maan aka Sc0rpi0n101 and this week was one of the most chaotic weeks till now if not the most chaotic. I had to move places as the Uni opens next week, had the summer classes exams and had to convert the C parser to use SymPy’s Codegen AST from Python AST all in one week.

CHAOS!!

Chaos

This week has been absolute chaos, but I’m really excited for the next week. The Uni will open for students next week and the classes will resume. I ‘ve been on a hectic and irregular schedule for the past few weeks. So, it’ll be a good chance to get a regular schedule going with the classes starting. So, the last weekend and the start of this week was spent on moving and organizing my stuff at the new place. But its all settled now.

Also, as you already knew, I was taking a few summer classes, and since the summer break ends this week, I had finals for 2 classes and one lab this week. I had one of the tests on the 4th and the other one along with the lab on the 5th.

The C Parser Now

My Parser

The Parser now uses the Sympy Codegen AST nodes for the expressions. The parser can generate AST nodes for variable declarations, basic arithmetic assignments, Function declarations & prototypes with parameters, Function Calls and I’m developing a few more features that can be implemented soon.

If you’re interested in knowing how the function transformations work, you can check it out here

Parse

First, the return type is determined using the return type from the C function. It currently supports only void, int and float data types.

  if (c_ret_type == 'void'):
              ret_type = none
          elif(c_ret_type == 'int'):
              ret_type = type = IntBaseType(String('integer'))
          elif (c_ret_type == 'float'):
              ret_type = FloatBaseType(String('real'))
          else:
  raise NotImplementedError("Variable not yet supported")

Then we get the variables and the body of the function from the clang AST using the following code segment.

  while True:
                      decl = self.transform(child)
                      if (child.kind == cin.CursorKind.PARM_DECL):
                          param.append(decl)
                      elif (child.kind == cin.CursorKind.COMPOUND_STMT):
                          for val in decl:
                              body.append(val)
                      else:
  body.append(decl)

Then, based on the fact if the function had a body or not, we declare a Function Definition or a function prototype.

  if body == []:
              function = FunctionPrototype(
                  return_type = ret_type,
                  name = node.spelling,
                  parameters = param
              )
          else:
              function = FunctionDefinition(
                  return_type = ret_type,
                  name = node.spelling,
                  parameters = param,
                  body = body
  )

What More?

AST

There are still a lot of features that can be a part of the parser, some of which can be implemented with the current nodes available in Clang and the Codegen AST. Also, Assignments are missing from the function body because they create a different type of node in the Clang AST than the regular assignment nodes.

These features include some of the loops like for and while loops. There are nodes available for these in both the _Clang and codegen AST_s. They can be implemented without any other requirements.

The Printing/Output functions whose node are present in the codegen AST would require working with including files and requires more work. The codegen AST doesn’t have any nodes for Class declarations currently.

No Classes

The variable transformations presently only use the basic types for float and int. There are multiple types present in both the ASTs regarding different sizes and types of integers and floating points which can also be implemented.

The Missing Nodes

Missing Nodes

The codegen AST currently few of the nodes available in the Clang AST that can be implemented for code transformation. Some of the nodes include Type nodes for Strings/Characters, there is a standalone node to declare Strings, but there are no type nodes for that.

There are also no nodes related to classes currently like any type of Class Declaration, constructors, destructors, and any other related features.

The Meeting

No Meeting

The meeting this week was on July 4, which is the Independence Day for the United States of America. So, the meeting could not take place this time and had to be postponed.

Happy Independence Day to anyone reading this from the US. Hope you all had a great time and celebrations.

What’s Next?

Next Parser

The Parser in its current form, available here, requires documentation and docstrings on the functions and classes present to make it more readable. It also requires a little more setup with Travis and in the setup files to make it merge ready. So, the first job for today and tomorrow is to make sure the PR passes all the tests and is up to the SymPy standards in tests and documentation. Then, repeat the same for the Fortran Parser.

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