- More work on scripting interface
[supertux.git] / tools / miniswig / main.cpp
1 #include <iostream>
2 #include <fstream>
3 #include <vector>
4 #include <string>
5 #include "tree.h"
6 #include "globals.h"
7 #include "create_wrapper.h"
8
9 extern int yyparse();
10 extern int yylex();
11
12 CompilationUnit* unit = 0;
13 std::istream* input = 0;
14 std::string inputfile;
15 std::string selected_namespace;
16 std::string modulename = "wrapper";
17
18 void usage()
19 {
20     std::cout << "Usage: miniswig --input FILE --output-cpp FILE --output-hpp FILE [--module NAME] [--select-namespace NAME]\n";
21     std::cout << "\n";
22 }
23
24 int main(int argc, char** argv)
25 {
26     std::string outputcpp;
27     std::string outputhpp;
28     for(int i = 0; i < argc; ++i) {
29         if(strcmp(argv[i], "--module") == 0) {
30             if(i+1 >= argc) {
31                 std::cerr << "Need to specify a module name.\n";
32                 usage();
33                 return 1;
34             }
35             modulename = argv[++i];
36         } else if(strcmp(argv[i], "--input") == 0) {
37             if(i+1 >= argc) {
38                 std::cerr << "Need to specify input file name.\n";
39                 usage();
40                 return 1;
41             }
42             inputfile = argv[++i];
43         } else if(strcmp(argv[i], "--output-cpp") == 0) {
44             if(i+1 >= argc) {
45                 std::cerr << "Need to specifiy output cpp name.\n";
46                 usage();
47                 return 1;
48             }
49             outputcpp = argv[++i];
50         } else if(strcmp(argv[i], "--output-hpp") == 0) {
51             if(i+1 >= argc) {
52                 std::cerr << "Need to specify output hpp name.\n";
53                 usage();
54                 return 1;
55             }
56             outputhpp = argv[++i];
57         } else if(strcmp(argv[i], "--select-namespace") == 0) {
58             if(i+1 >= argc) {
59                 std::cerr << "Need to specify a namespace.\n";
60                 usage();
61                 return 1;
62             }
63             selected_namespace = argv[++i];
64         } else if(argv[i][0] == '-') {
65             std::cerr << "Unknown option '" << argv[i] << "'.\n";
66             usage();
67             return 1;
68         } else {
69         }
70     }
71     if(inputfile == "" || outputcpp == "" || outputhpp == "") {
72         std::cerr << "Not all options specified.\n";
73         usage();
74         return 1;
75     }
76     
77     try {
78         input = new std::ifstream(inputfile.c_str());
79         if(!input->good()) {
80             std::cerr << "Couldn't open file '" << input << "' for reading.\n";
81             return 1;
82         }
83         unit = new CompilationUnit();
84         Namespace* std_namespace = new Namespace();
85         std_namespace->name = "std";
86         std_namespace->types.push_back(new StringType());
87         unit->namespaces.push_back(std_namespace);
88         
89         yyparse();
90
91         std::ofstream cppout(outputcpp.c_str());
92         if(!cppout.good()) {
93             std::cerr << "Couldn't open file '" << outputcpp << "' for writing.\n";
94             return 1;
95         }
96         std::ofstream hppout(outputhpp.c_str());
97         if(!hppout.good()) {
98             std::cerr << "Couldn't open file '" << outputhpp << "' for writing.\n";
99             return 1;
100         }
101
102         Namespace* ns = unit;
103         if(selected_namespace != "") {
104             ns = ns->findNamespace(selected_namespace);
105         }
106
107         WrapperCreator creator(cppout, hppout);
108         creator.create_wrapper(ns);
109     } catch(std::exception& e) {
110         std::cerr << e.what() << "\n";
111         return 1;
112     }
113
114     return 0;
115 }
116