KWOC ’20 | Report

Shivam Pandey
4 min readJan 5, 2021

KWOC is an annual program organized by KOSS, IIT Kharagpur to promote the culture of open-source contribution at IIT Kharagpur. This is a report about my experience with KWOC ‘20

The registrations for KWOC happened in late November. After registration was done and the mentors had submitted a list of projects, we were asked to select any number of projects we would like to work on. Being someone who was acquainted with only basic Python, I was looking for a project which used Python. After shortlisting a number of Projects I was interested in, I finally settled on Sim-C.

Sim-C is somewhat of a compiler that takes in a file containing code written in Sim-C’s syntax and converts it into C code which can then be compiled and run by any C compiler. The idea behind Sim-C was to create a wrapper for C language, which is easy to use and has simple syntax (much like python). The project was already in production for a few months before KWOC, and a lot of progress had already been made.

For anyone interested in Sim-C, here is the Github link, and here is a link to its Documentation.

We had a meeting with the mentors for the project and were given a brief introduction to it. We were directed to read Documentation and go through the code for some of the important modules of the project. The project had three main scripts, the Lexical Analyzer, the Parser, and the Compiler. The lexical analyzer takes in the raw code from the .simc file and converts it into a list of tokens, where each token may signify a keyword, an identifier, a brace, comma, or other parts that make a program. Then this list was sent to the parser (which in my opinion was the heart of the project). Here, the token was looped over and a list of Op-codes was generated according to the arrangement of the tokens. The Op-codes were basically the final code in a condensed form. Then the compiler took these Op-codes and converted them into the final C code.

After going through the documentation and code, it was time to start contributing to the project. When I started, there were around 20 issues listed on the Github page which included bugs and new feature addition. Sim-C at this point did not support C structures, and a few issues were listed regarding the addition of support for structures. So this is what I decided to tackle.

The first thing I did, was to make changes to the code so that it recognizes the struct keyword. This just required adding the word struct to the list of keywords that the lexical_analyzer uses while generating tokens. Then I had to make changes to the parser to properly process the struct token and generate the correct Op-code. This was done by adding a new function to process this new token. The function was called when the struct token was found, and its task was to first of all search for any syntax errors, then create an opcode. Adding this to the code, I was able to get it to correctly output the struct declaration line in the C code.

The next step was to deal with instantiation of structure objects in the declaration, and then adding a semi-colon after the declaration and instantiation was done. This was done by, first of all, going to the end of the declaration, then looking for instantiated objects by searching the tokens. On finding the objects, they were added to the opcode, and a semi-colon was added afterward. In doing this, I uncovered a bug in Sim-C where it failed to correctly point of errors if the number of opening and closing braces was different, it also failed to correctly judge the end of scopes of functions and blocks. This bug remained hidden for the rest of the use cases, but when using structs, this was a major problem. Fortunately, this was easy to fix by maintaining a stack (or a count)of the braces used.

In the end, Sim-C now supports structure declaration and instantiation of structure objects.

In conclusion, working on this project was a great experience. I must say that the main difficulty of working on this project was not about writing code to do anything very complex but to write code that works properly in conjunction with the rest of the project. The idea was to take it slow and not break any of the functioning parts of the project. To do this I had to make sure that any changes to existing parts of the code did not hamper the functioning of the other uses of that section of code. And to make sure that the newly added code did not interfere with any other components.

All in all KWOC ’20 was pretty helpful in introducing me to the field of open-source.

--

--