• Announcements

Archived

This topic is now archived and is closed to further replies.

Interpreting C++

Recommended Posts

For a while, I''ve been wondering why people claim that Java is great for scripting. I still don''t know why. Google returns 2500 links for "C++ Interpreter". So... Why aren''t we using these, instead of having to learn Java or designing a custom scripting language? Cédric

Share on other sites
There are good reasons why, while C++ is a kickass language for compilation, it makes a crappy scripting language.

1. POINTERS. A throwback to C which can improve efficiency and make elegant code in compiled programs, these should never be used in something like a scripting language. They are unsafe and obfuscating.

2. CLASSES. C++, burdened with C compatibility, has a very complex and ponderous OO syntax. Java is much simpler in this regard.

3. STATIC ARRAYS. These are just silly in a scripted program, since they have to be emulated with dynamic arrays anyway. No dynamic resizing; no bounds checking; no safety.

4. LACK OF GARBAGE COLLECTION. Makes sense for a compiled system, but in a scripting language you really want a system to avoid memory leaks, since you won''t be doing processor intensive stuff.

5. UGLY LINKING SEMANTICS. From the simple stuff like multiple inclusion guards to the complex and subtle stuff like automatic template instantiation across object files, C/C++ compiling and linking semantics suck large hairy sweaty volleyballs.

6. STUPID EXCEPTION SEMANTICS. Is there anyone who DOESN''T dislike the dodgy and incomplete support for exceptions in the C++ standard? ''nuff said.

6. NO REASON TO. The whole thing about scripting languages is that, since they are interpreted, they can do things that compiled languages can''t, like have simple syntax, reflection, etc. C++ doesn''t do any of these, simply because it CAN''T. And to add this stuff in would be to further complicated an already large and complex syntax.

To sum up: No thank you.

Don''t listen to me. I''ve had too much coffee.

Share on other sites
quote:
Original post by cedricl

Are you sure they are not all pointing to "How to write an
interpreter for language XYZ in C++"?

Kami no Itte ga ore ni zettai naru!

Share on other sites
Ch is the only C++ interpretter I know of.

Careful when you say C++ can''t have reflection - I think it may already be in the language, it''s just hiding A few simple additions would make a compile-time reflection system easy, and we already have code to translate compile-time type information into data structures that can be iterated at run-time.

Worst case scenario at this time, is that you have to add an additional pass to the compilation process using a tool such as OpenC++ that outputs the requiesite typelist defintions. From there it''s all downhill using the existing meta-templates in loki.

Share on other sites
A List of C/C++ Interpreters by CUJ.

Sneftel: Good points. However:
quote:
1. POINTERS. A throwback to C which can improve efficiency and make elegant code in compiled programs, these should never be used in something like a scripting language. They are unsafe and obfuscating.

They are also unsafe in compiled C/C++. Scripters don''t have to use them if they don''t know what they''re doing.
quote:
2. CLASSES. C++, burdened with C compatibility, has a very complex and ponderous OO syntax. Java is much simpler in this regard.

Same comments as above. Maybe is it simpler for you to write a class in Java, but it''s not for me, because I don''t know Java. Do we really want non-programmers to start writing classes in their scripts?
quote:
6. NO REASON TO. The whole thing about scripting languages is that, since they are interpreted, they can do things that compiled languages can''t, like have simple syntax, reflection, etc. C++ doesn''t do any of these, simply because it CAN''T. And to add this stuff in would be to further complicated an already large and complex syntax.

My most important point is that C++ is much more intuitive to C++ programmers. Furthermore, I don''t know how to interface Java with C++.

Also, there is an idea that prompted me to write this post. I realized yesterday that, with not a whole lof of effort, I could get free run-time function calls, like

void g(int x, int y){}
RegisterFunction(g);
Interpret("g(10, 20)");

RegisterFunction would be a macro using the tokenizer thingy to get the name of the function in a string, and then templates, functors, and std::map do the rest.

I had to write scripting languages twice, and both times, it didn''t support much more than functions and simple assignments. Both could have been handled by an "expression interpreter" like the one described above.

So, does anyone know why I wouldn''t write such library? Is it already done? Am I wasting my time because there is a simpler and better alternative?

As a side note, I could make a search for "reflection", but somehow, I doubt this word''s use is limited to programming. Could anyone summarize for me what reflection is? Thanks.

Cédric

Share on other sites
Reflection is used to inspect the type system at runtime. You can use it on a given object, and enumerate all it''s methods and data members. It also lets you create objects and invoke methods by name.
The following is a C# example(taken from the docs) which shows the public and protected methods methods of a given class:

  public static void Main()          {            Type myType =(typeof(MyTypeClass));            // Get the methods with the Access-Specifier as public.            MethodInfo[] myArrayMethodInfo = myType.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly);            Console.WriteLine("\nThe Number of Public Methods are :"+myArrayMethodInfo.Length);            Console.WriteLine("The Names of the Public Methods are :");            // Display all the methods.            DisplayMethodInfo(myArrayMethodInfo);            // Get the methods information of all ''NonPublic''.            MethodInfo[] myArrayMethodInfo1 = myType.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly);            Console.WriteLine("\nThe Number of Protected Methods are :"+myArrayMethodInfo1.Length);            Console.WriteLine("The Names of the Protected methods are :");            // Display all the methods information.            DisplayMethodInfo(myArrayMethodInfo1);               }         public static void DisplayMethodInfo(MethodInfo[] myArrayMethodInfo)         {            // Display  the information of all the Methods.            for(int i=0;i<myArrayMethodInfo.Length;i++)            {               MethodInfo myMethodInfo = (MethodInfo)myArrayMethodInfo[i];               Console.WriteLine("\nName of the Method is :"+myMethodInfo.Name);            }         }

Share on other sites
quote:
Original post by cedricl
[Pointers] are also unsafe in compiled C/C++. Scripters don''t have to use them if they don''t know what they''re doing.

But in normal C++, and especially in C, you really do need them to achieve most things. A language without such a requirement makes those things easier.

quote:
My most important point is that C++ is much more intuitive to C++ programmers. Furthermore, I don''t know how to interface Java with C++.

I think the idea with scripting languages is that they offer a very different set of benefits to those offered by C or C++, but additionally that a good programmer would have little trouble learning a new language. In fact, with some scripting languages, even a poor or totally novice programmer could pick them up quite quickly. (Python being one such language.)

Such as you mentioning that writing a class might be too difficult and unnecessary for a non-programmer. But if a scripting language made it simple for them, then why shouldn''t they use it?

Reflective languages are basically those where the program is data too. This means you can do things such as query an object for a list of functions it supports (allowing for something similar to C++''s templates without the code bloat since there can be run-time checks), and sometimes even alter a single object''s list of methods at run-time. (For example, this allows you to implement the Strategy pattern without using a hierarchy of classes.)

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

Share on other sites
quote:
Original post by Kylotan
and sometimes even alter a single object''s list of methods at run-time.

I have seen the term "expando objects" being used in reference to this. I believe JScript.NET supports it, and probably plain vanilla ECMAScript as well.

• Partner Spotlight

• Forum Statistics

• Total Topics
627654
• Total Posts
2978450

• 10
• 12
• 22
• 13
• 33