Crafting Interpreters: Jlox — Scanning

Ukpa Uchechi
3 min readJan 4, 2024

--

Yay completed the first chapter, the scanner. Contemplating how to write this chapter, which will eventually set a benchmark for others, one is to through the code, which I think may not be necessary as different languages can be used. Well since this is a mini documentation. I will just talk about challenges faced and perhaps lessons learned.

I initially used replit to write the code, which I mentioned in my last article concerning this. One mistake I made was not to test the code in different stages, so I coded everything and when it was time to run it, the code didn’t compile talk more of running. The vs code I was avoiding, had to go back in good faith and start debugging.

After many tweaks, it compiled and ran, but it wasn’t working as expected, posted my code on Reddit and got some feedback and further tools to use to help debugging. If you have ever used C, you know one of the most common errors is memory error. I saw a YouTube video about using Valgrind to check memory leaks, I downloaded that and yup it was plenty lol.

This time I just decided to start again but this time, testing and running the code at different stages, started seeing and fixing the memory leaks. But that didn’t mean there were no more issues to encounter.

One of the common ways I debugged in JavaScript was to litter my code with console.logs, Ah the famous console log if you know you know, and of course, I took that bad habit with me here, littering my whole code with printfs. But I wanted to learn how to use the vs code debugger, something I was running away from, because it just looked hard.

One thing I have internalized is that, things that are not familiar look hard, so yeah I gave it a couple of days to familiarize myself with, and after a while, I picked it up. I also saw about the C/C++ GDB and gave it a try too, it worked better for me than the vs code debugger, and boy did it save me so much stress and time I would have spent pulling out my hair.

Posted my code on Reddit to get some code feedback, got a few, and implemented them, which also helped simplify my code and dropped the lines of code. Getting feedbacks is really helpful.

All in All, going through it with a different language is great, when things don’t go as expected you have to figure out why that is, and in the cause of that you clearly understand what each line of the code is doing.

This is the source code for anyone interested https://replit.com/@ukpauchechi/jlox#scanner.c

If you have any feedback, be kind to let me know in the comment section, Thank you.

Now to the next chapter :)

--

--

Ukpa Uchechi
Ukpa Uchechi

Written by Ukpa Uchechi

A Tech Enthusiastic and lover, who loves teaching and learning.

No responses yet