Checking in miniswig: It's a flex/bison based parser that is able to parse
[supertux.git] / tools / miniswig / tree.h
1 #ifndef __TREE_H__
2 #define __TREE_H__
3
4 #include <vector>
5 #include <string>
6 #include <iostream>
7
8 class AtomicType {
9 public:
10     std::string name;
11     virtual ~AtomicType()
12     { }
13
14     virtual void write_c(std::ostream& out)
15     {
16         out << name;
17     }
18 };
19
20 class BasicType : public AtomicType {
21 public:
22     static BasicType VOID;
23     static BasicType BOOL;
24     static BasicType CHAR;
25     static BasicType SHORT;
26     static BasicType INT;
27     static BasicType LONG;
28     static BasicType FLOAT;
29     static BasicType DOUBLE;
30
31 private:
32     BasicType(const std::string& name)
33     { 
34         this->name = name;
35     }                                     
36 };
37
38 class Type {
39 public:
40     Type() 
41         : atomic_type(0), _const(false), _static(false), pointer(0), ref(0)
42     { }
43
44     void write_c_type(std::ostream& out)
45     {
46         if(_static)
47             out << "static ";        
48         if(_const)
49             out << "const ";
50         atomic_type->write_c(out);
51         for(int i = 0; i < pointer; ++i)
52             out << "*";
53         for(int i = 0; i < ref; ++i)
54             out << "&";
55     }
56
57     bool is_void() const
58     {
59         if(atomic_type == &BasicType::VOID && pointer == 0)
60             return true;
61         return false;
62     }
63
64     AtomicType* atomic_type;
65     bool _const;
66     bool _static;
67     // number of '*' in the type declaration...
68     int pointer;
69     // number of '&' in the type declaration...
70     int ref;
71 };
72
73 class StringType : public AtomicType {
74 public:
75     StringType()
76     {
77         this->name = "string";
78         assert(_instance == 0);
79         _instance = this;
80     }
81     virtual ~StringType()
82     {
83         assert(_instance == this);
84         _instance = 0;
85     }
86
87     static StringType* instance()
88     {
89         return _instance;
90     }
91
92     virtual void write_c(std::ostream& out)
93     {
94         out << "std::string";
95     }
96
97 private:
98     static StringType* _instance;   
99 };
100
101 class Parameter {
102 public:
103     std::string name;
104     Type type;
105 };
106
107 class ClassMember {
108 public:
109     virtual ~ClassMember()
110     { }
111
112     enum Visbility {
113         PUBLIC,
114         PROTECTED,
115         PRIVATE
116     };
117     Visbility visibility;
118 };
119
120 class Function : public ClassMember {
121 public:
122     enum FuncType {
123         FUNCTION,
124         CONSTRUCTOR,
125         DESTRUCTOR
126     };
127     FuncType type;
128     std::string name;
129     Type return_type;
130     std::vector<Parameter> parameters;
131 };
132
133 class Class : public AtomicType {
134 public:
135     ~Class() {
136         for(std::vector<ClassMember*>::iterator i = members.begin();
137                 i != members.end(); ++i)
138             delete *i;
139     }
140     
141     std::vector<ClassMember*> members;
142 };
143
144 class Namespace {
145 public:
146     std::string name;
147 };
148
149 class CompilationUnit {
150 public:
151     ~CompilationUnit() {
152         for(std::vector<Function*>::iterator i = functions.begin();
153                 i != functions.end(); ++i)
154             delete *i;
155         for(std::vector<AtomicType*>::iterator i = types.begin();
156                 i != types.end(); ++i)
157             delete *i;
158         for(std::vector<Namespace*>::iterator i = namespaces.begin();
159                 i != namespaces.end(); ++i)
160             delete *i;
161     }
162  
163     std::vector<Function*> functions;
164     std::vector<AtomicType*> types;
165     std::vector<Namespace*> namespaces;
166 };
167
168 #endif
169