Added Variable Definition and Assignment to LKit
Friday, July 13th, 2012Today I slugged through a bunch of code to get the variable definition and assignment into LKit. It was a lot of grief, but in the end the tools I'm using (GCC, GDB) are more than up to the task and I got things working just the way I wanted.
The idea was that I wanted to be able to define and update variables in the language with the simple syntax:
(set x 6) (set y (+ 1 2 3))
where the first just assigns the value of 6 to x, and the second assigns the expression (+ 1 2 3) to y. The second is by far the more interesting in that it's a way to define a complex expression to a variable and it'll be evaluated once, and after that, it'll use that value over and over in the calculations. At least that's the idea.
In order to get it working I had to do a little messing around with the definition of the variable. Initially, we had a simple subclass relationship between the value and the variable, where the variable just added in the concept of a name. But what I really needed was to be able to have any value in a variable, and in order to do that, I needed to be able to hold onto a pointer to a value, and that was a bit tricky.
What I needed to do was to add in a pointer to a value in the variable and then look to see if it's set in the eval() method. If so, then call eval() on that pointer's contents as opposed to the super class's eval() method. Then, because I had this pointer, I had to manage the memory on it, and that meant that I needed to make a few more constructors to make everything fit in as it should.
In the end, I had a pointer double-free problem, and GDB helped me find it by pointing out where it was occurring, and from that, I was able to infer what was going on. Standard debugging, but I will certainly admit that it had me fooled for a while, until I was able to figure out what the sequence of events were that lead to the logic problem.