Skip to main content

Part 1 of writting an experimental code analyzer and sort of interpreter for C#

The purpose of this C# analyzer is to see all possible execution paths from any entry point inside the code. Currently the entry point can be only a method.

There are multiple ways to deal with this. The approach would be to rewrite all existing code and rewrite all conditional statement so that they are true all the time. This approach has numerous disadvantages.

Firstly, it will actually execute the code and any calls to external resources such as databases will be executed. Since the code has been altered, there is also no guarantee that it won't produce exceptions at runtime. And for really big code bases, it will be incredibly slow and it will require actual compilation of the modified code which may take even more time.

My approach is to write a C# interpreter with some special conditions that will go through all possible execution paths. In order to go through all the execution paths, our interpreter will need to ignore all the conditional statements and jump directly in the code that is executed when a given statement is true.

Still this is not enough, for example if we have an "if" statement and in each branch of it we assign a new variable to the same reference, then what should happen? If we overwrite the value of that reference each time we assign something to it, then if someone calls a method on that reference, it might ignore some methods of the variables that were previously replaced. So we need to modify the reference assignment behavior and the actual references themselves. The references in the code become collections of variables and when we assign a new value to it, we add another variable inside the collection.

So a variable or better said reference in our interpreter will look like this:



























Since now a reference contains multiple variables, the way we call methods is also changed.
If we have multiple variables inside a reference, what methods do we call? We actually need to call the method on all the variables contained in that reference.


Above is just a part of the code, but we can see that iterate through all the variables that are stored in the reference, get the accessed method and then call the method with the variable. All methods by default have an extra parameter to them, the object on which the method is called. This parameter is passed on the last line of the code above. Inside the method call it will become the "this" variable.

Comments

Popular posts from this blog

Some software development common sense ideas

 I haven't really written here in a long time so it's time to write some new things. These are just some random common sense things that seemed to short to write about individually but seem really interesting and useful to me or other people 1. There is nothing fixed in software development, all things vary by circumstances and time Remember the documentation that didn't seem that important when you started the project, well after a couple of years one the application has grown and become really complicated, no one actually knows everything about the application anymore. So now you really need that documentation. What happens if you suddenly need much more people to develop the application because of some explosive growth? Without documentation, new developers will just look at the application like they look at a painting. This actually happened to me. Maybe in the beginning of a project, a technology really helped you a lot but as the project grew, it started making things...

Some things which are often blindly applied and followed by developers which are not always good

This is probably one of the most controversial things that I have written so far but I am just tired of hearing about these things and discussing these things. Other developers that I know share part of my feelings. I would rather hear more about how people built things, overcame challenges or what new interesting ideas and concepts they implemented. Those things are really interesting and innovative, not hearing about the same theoretical things over and over again. I can just read and learn those things from 100 sources on the internet. Firstly, one of the most discussed and promoted things is agile/scrum development. I think I have been through 5-8 workshops about agile development methodology. And each time, some things differed. There is no 100% standard approach to this. Everyone uses their own version of this development methodology and seem to argue a lot that their approach is right and everyone else is doing it wrong. You go to an interview, this will be one of the first 10 t...

Some things about doing presentations that I learned recently

Lately I had to do more presentations ranging from technical ones about various technologies to other presentation about projects that I work on or even tools that I made for developers and testers. I didn't learn a big list of things from them but rather a couple of really important things to keep in mind when doing a presentation. To begin with, as a presenter your purpose is to make people understand what you are explaining first and foremost. I have seen all too many experienced developers trying to impress the audience when presenting something. They use a lot of pompous language, terms and jargon to make things seem more complicated than they really are. This is just to feed their own ego, to send a subliminal message to the crowd over the lines of: "Look at me how good I am, I managed to understand and apply these things which sound really complicated when I tell them". Imagine if they used a more simple language and a language which is less scary and intimid...