An observation about Python
During my quest to make a compiler for a new language, I'm finding a number of times when I need exactly one of some interesting data structure. The symbol table is a perfect example. There's only one symbol table. The symbol table is a complex structure with many parts. No other data structure in the program is quite like a symbol table.
My first inclination was to write a class for the symbol table, create one instance somewhere, and then make that instance available to whatever needs it. This is the only way to proceed in Java or C++, but Python offers a better way. That way is the module.
In Python, I should write a module called "symbol table". Actually, I'll have to give it a name Python would be happy with, but that's beside the point. This works out quite well. Module imports work just like objects. Module-level functions work just like methods. The syntax is identical. Finally, the import system makes sure that there is exactly one symbol table.
Pascal anticipated this need with its "unit" structure. You can sort of emulate Pascal units in C, but you don't get the nice deterministic start-up and shut-down features unless you explicitly code for them as part of the main() function in your C program. Pascal and Python both put those concerns where they belong: the separated unit itself.
By the way, are you wondering about this crazy idea of writing a compiler in an interpreted language? Why not? The language should be a vehicle, not an obstacle. Python gets in my way less than C does. I could work in C++ instead, but I'm doing a lot with character strings right now. Despite the extensive support for strings in the standard template library, C++ string handling still sucks rocks. Eventually, the compiler will bootstrap. NASM is the current target language, and I'm hijacking the C standard library for basic services at the moment. Therefore, porting to other Intel operating systems is almost trivial. Other CPU architectures, as well as optimization, should probably best be supported by targeting GCC's RTL intermediate language, but that's a long time off.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home