compiler/libec2: Support for instantiations
[sdk] / compiler / libec2 / src / classes.ec
1 import "externals"
2
3 public class ASTMemberInit : ASTNode
4 {
5 public:
6    Location realLoc;
7    List<ASTIdentifier> identifiers;
8    ASTInitializer initializer;
9
10    // COMPILE DATA
11    bool used;
12    bool variable;
13    bool takeOutExp;
14
15    ASTMemberInit ::parse()
16    {
17       ASTMemberInit init { };
18       if(peekToken().type == IDENTIFIER)
19       {
20          int a = pushAmbiguity();
21          while(true)
22          {
23             ASTIdentifier id = ASTIdentifier::parse();
24             if(id)
25             {
26                if(!init.identifiers) init.identifiers = { };
27                init.identifiers.Add(id);
28                if(peekToken().type != '.')
29                   break;
30                else
31                   readToken();
32             }
33          }
34          if(peekToken().type == '=')
35          {
36             clearAmbiguity();
37             readToken();
38          }
39          else
40             popAmbiguity(a);
41       }
42       init.initializer = InitExp::parse();
43       return init;
44    }
45
46    void print()
47    {
48       if(identifiers)
49       {
50          Iterator<ASTIdentifier> it { identifiers };
51          while(it.Next())
52          {
53             it.data.print();
54             if(identifiers.GetNext(it.pointer))
55                Print(".");
56          }
57          Print(" = ");
58       }
59       if(initializer)
60          initializer.print();
61    }
62 };
63
64 public class MemberInitList : ASTList<ASTMemberInit>
65 {
66    MemberInitList ::parse()
67    {
68       MemberInitList list = (MemberInitList)ASTList::parse(class(MemberInitList), ASTMemberInit::parse, ',');
69       if(peekToken().type == ';')
70          readToken();
71       return list;
72    }
73 }
74
75 public class ASTPropertyDef : ASTNode
76 {
77    SpecsList specifiers;
78    ASTDeclarator declarator;
79    ASTIdentifier id;
80    ASTStatement getStmt;
81    ASTStatement setStmt;
82    ASTStatement issetStmt;
83    Symbol symbol;
84    bool conversion;
85    bool isWatchable;
86    ASTExpression category;
87 };
88
89 public class ClassDefList : ASTList<ASTClassDef>
90 {
91    ClassDefList ::parse()
92    {
93       return (ClassDefList)ASTList::parse(class(ClassDefList), ASTClassDef::parse, 0);
94    }
95
96    void printSep()
97    {
98    }
99 }
100
101 public class ASTClassDef : ASTNode
102 {
103 public:
104    AccessMode memberAccess;
105
106    // IDE
107    void * object;
108
109    ASTClassDef ::parse()
110    {
111       SpecsList specs = null;
112       InitDeclList decls = null;
113       int a = -1;
114       ASTDeclarator decl;
115
116       peekToken();
117       if(nextToken.type == '}')
118          return null;
119
120       if(nextToken.type == IDENTIFIER)
121          a = pushAmbiguity();
122
123       specs = SpecsList::parse();
124       decls = InitDeclList::parse();
125       peekToken();
126       decl = GetFuncDecl(decls && decls[0] ? decls[0].declarator : null);
127       if(decl)
128       {
129          if(a > -1) clearAmbiguity();
130          return ClassDefFunction::parse(specs, decls);
131       }
132       else if((specs || decls) && (nextToken.type != '.' && nextToken.type != '='))
133       {
134          if(a > -1) clearAmbiguity();
135          return ClassDefDeclaration::parse(specs, decls);
136       }
137       else if(a > -1)
138       {
139          ClassDefInitialization init;
140          popAmbiguity(a);
141
142          init = ClassDefInitialization::parse();
143          if(init)
144             return init;
145       }
146       readToken(); // Error
147       return null;
148    }
149 };
150
151 public class ClassDefClassPropertyValue : ASTClassDef
152 {
153    ASTIdentifier id;
154    ASTInitializer initializer;
155 }
156
157 public class ClassDefDeclaration : ASTClassDef
158 {
159    ASTDeclaration decl;
160
161    ClassDefDeclaration ::parse(SpecsList specs, InitDeclList decls)
162    {
163       ASTDeclaration decl = ASTDeclaration::parse(specs, decls);
164       if(decl)
165       {
166          return { decl = decl };
167       }
168       return null;
169    }
170
171    void print()
172    {
173       printIndent();
174       if(decl) decl.print();
175       PrintLn("");
176    }
177 }
178
179 public class ASTClassFunction : ASTFunctionDefinition
180 {
181 public:
182    Class _class;
183    //List attached;    // For IDE
184    AccessMode declMode;
185    
186    // COMPILING DATA
187    Type type;
188    Symbol propSet;
189
190    bool isVirtual;
191    bool isConstructor, isDestructor;
192    bool dontMangle;
193    // int id, idCode;
194
195    ASTClassFunction ::parse(SpecsList specs, InitDeclList decls)
196    {
197       return (ASTClassFunction)ASTFunctionDefinition::parse(specs, decls);
198    }
199 };
200
201 public class ClassDefFunction : ASTClassDef
202 {
203    ASTClassFunction function;
204
205    ClassDefFunction ::parse(SpecsList specs, InitDeclList decls)
206    {
207       ASTClassFunction function = ASTClassFunction::parse(specs, decls);
208       if(function)
209          return { function = function };
210       return null;
211    }
212
213    void print()
214    {
215       if(function) function.print();
216    }
217 }
218
219 public class ClassDefInitialization : ASTClassDef
220 {
221    MemberInitList defValues;
222
223    ClassDefInitialization ::parse()
224    {
225       MemberInitList defValues = MemberInitList::parse();
226       if(defValues)
227          return { defValues = defValues };
228       return null;
229    }
230
231    void print()
232    {
233       if(defValues)
234       {
235          printIndent();
236          defValues.print();
237          PrintLn(";");
238       }
239    }
240 }
241
242 public class ClassDefProperty : ASTClassDef
243 {
244    ASTPropertyDef propertyDef;
245 }
246
247 public class ClassDefPropertyWatch : ASTClassDef
248 {
249    ASTPropertyWatch propertyWatch;
250 }
251
252 public class ClassDefDesigner : ASTClassDef
253 {
254    String designer;
255 }
256
257 public class ClassDefDefaultProperty : ASTClassDef
258 {
259    ASTIdentifier defaultProperty;
260 }