"Introduction"
I am relatively new to C ++. I went through all the basic things and managed to build 2-3 simple translators for my programming languages.
The first thing that gave and still gives me a headache: Implementing the type system of my language in C ++
Think about it: Ruby, Python, PHP and Co. have many built-in types, which are obviously implemented in C. So I first tried to make it possible to give a value in my language of three possible types: Int, String and Nil.
I came up with this:
enum ValueType { Int, String, Nil }; class Value { public: ValueType type; int intVal; string stringVal; };
Yes, wow, I know. It was very slow to go through this class, because you had to call the row allocator all the time.
The next time I tried something like this:
enum ValueType { Int, String, Nil }; extern string stringTable[255]; class Value { public: ValueType type; int index; };
I would save all the lines in stringTable and write their position in index . If the Value type was Int , I just stored the integer in index , it would not make sense at all using the int index to access another int or?
In any case, the above said gave me a headache too. After some time, access to the row from the table here, a link to it there and its copying there turned over over my head - I lost control. I had to impose a translator.
Now: Good, so C and C ++ are statically typed.
How do the main implementations of the above languages process different types in their programs (fixnums, bignums, nums, string, array, resources, ...)?
What to do to get maximum speed with many different types available?
How do solutions compare to my simplified versions above?