What I am trying to do is to use Python to parse script bob.ps
and output bob.py
and bob.cpp
depending on user input.
lets say we had bob.ps
which is python-like simple language
#comment
use ShowBase
# Load the environment model.
environ = loadModel 'cube'
# Reparent the model to render.
render environ
run
User would need to run python script with commands like : $ python main.py -py -c++ and it would result generating following python and c++ scripts:
from direct.showbase.ShowBase import ShowBase
class MyApp(ShowBase):
def __init__(self):
ShowBase.__init__(self)
# Load the environment model.
self.environ = self.loader.loadModel("models/environment")
# Reparent the model to render.
self.environ.reparentTo(self.render)
app = MyApp()
app.run()
and c++
#include "pandaFramework.h"
#include "pandaSystem.h"
int main(int argc, char *argv[]) {
// Load the window and set its title.
PandaFramework framework;
framework.open_framework(argc, argv);
framework.set_window_title("My Panda3D Window");
WindowFramework *window = framework.open_window();
// Load the environment model.
NodePath environ = window->load_model(framework.get_models(), "models/environment");
// Reparent the model to render.
environ.reparent_to(window->get_render());
// Run the engine.
framework.main_loop();
// Shut down the engine when done.
framework.close_framework();
return (0);
}
I spent some time browsing the internet for answers. What I found out is that I need to parse bob.ps and use lexer. I tried to mess little bit with PLY-3.4 and it doesn't exactly do what I want to accomplish. I don't need/want to execute code while parsing instead my intention is only to generate equivalent python/c++ code.
What would be best approach to this problem, is there any module/book/article/tutorial on this specific subject? I really hit the wall and don't know where to look. Any help is highly appreciated.
What you want to do here is a bit like a compiler. You want to compile "ps" to python or cpp.
Standard tools for that are lex and yacc and there is plenty of litterature on that everywhere.