I am developing a Graphics package in Java that allows the user to draw triangles, squares, circles, etc. and group/move/manipulate them in order to make animated stick-figure movies. I have an interface that allows users to add figures with GUI buttons, drop-downs, mouse-clicks and the like, and also a text area that users can enter commands like
add new triangle((0,0),(50,0),(50,50)). I am writing a lexer/parser to make sense of these commands and execute them. I have a Triangle class and a Point class and an ArrayList of Triangle objects called
triangles. The command text above is fairly easy to parse, but some of the potential commands are more complicated and time-consuming. The example above would have the effect of the following code being executed (again this is a simple example. The more complex examples would involve multiple, harder-to-parse commands)...
triangles.add(new Triangle(new Point(0,0), new Point(50,0), new Point(50,50));
What I'd like to know is if it is somehow possible for the user to type the above into the text area, hit the Submit button, and then instead of having to write the parsing code myself, call the
javac command and have it compile the code, then execute it somehow. I've been thinking of ways to do it, but they all seem rather inefficient. I had the thought of creating a new .java text file that created a class that contained a main function containing the line of code above, then execute a shell command to compile that class using
javac, then another shell command that executed it using the
java command. If this was C++, I could compile it into a DLL and then load it from my main process or thread or use some shared memory so Serialization might not be required, but I don't how to do that in Java, so I was thinking of Serializing that Triangle and sending that Serialized Triangle data to my main program via a socket or something.
I feel confident that if there is a way to do what I want, it's not by doing it the way above. Is there a better way to do it? Can it be done at all? Should I just stop wanting to do it and simply resign myself to creating my own language and run-time parser?