e2e98196b60696e2e33749442126e0000d82c563
[sdk] / compiler / libec2 / src / externals.ec
1 import "statements"
2
3 public class ASTDeclaration : ASTStmtOrDecl
4 {
5 public:
6    DeclarationType type;
7    // ASTSpecifier extStorage;
8    // Symbol symbol;
9    // AccessMode declMode;
10
11    ASTDeclaration ::parse(SpecsList specs, InitDeclList decls)
12    {
13       if(peekToken().type == ';')
14          readToken();
15       return DeclarationInit { specifiers = specs, declarators = decls };
16    }
17 }
18
19 public class DeclarationInit : ASTDeclaration
20 {
21 public:
22    SpecsList specifiers;
23    InitDeclList declarators;
24
25    void print()
26    {
27       if(specifiers)
28       {
29          specifiers.print();
30          if(declarators) Print(" ");
31       }
32       if(declarators) declarators.print();
33       Print(";");
34    }
35 }
36
37 public class DeclarationInstance : ASTDeclaration
38 {
39    ASTInstantiation inst;
40 }
41
42 public class DeclarationDefine : ASTDeclaration
43 {
44    ASTIdentifier id;
45    ASTExpression exp;
46 }
47
48 public class ASTFunctionDefinition : ASTNode
49 {
50 public:
51    SpecsList specifiers;
52    ASTDeclarator declarator;
53    List<ASTDeclaration> oldStyleDeclarations;
54    StmtCompound body;
55
56    void print()
57    {
58       // PrintLn("");
59       printIndent();
60       if(specifiers) 
61       {
62          for(s : specifiers)
63             s.print();
64          Print(" ");
65       }
66       if(declarator)
67          declarator.print();
68       PrintLn("");
69       if(body)
70          body.print();
71    }
72
73    ASTFunctionDefinition ::parse(SpecsList specs, InitDeclList decls)
74    {
75       ASTFunctionDefinition function { };
76       ASTDeclarator decl = (decls && decls[0]) ? decls[0].declarator : null;
77       if(decl && decls[0]) decls[0].declarator = null;
78       delete decls;
79       function.specifiers = specs;
80       function.declarator = decl;
81       function.body = StmtCompound::parse();
82       return function;
83    }
84    /*
85    Class _class;
86    OldList attached;    // For IDE
87    AccessMode declMode;
88
89    Type type;
90    Symbol propSet;
91
92    int tempCount;
93    bool propertyNoThis; // Not used yet; might use to support both this = and return syntax for conversion properties
94    */
95 };
96
97 /*
98    union
99    {
100       ASTFunctionDefinition function;
101       SpecClass _class;
102       ASTDeclaration declaration;
103       String importString;
104       ASTIdentifier id;
105       DBTableDef table;
106    };
107 };
108 */
109
110 /*
111 class External
112 {
113    ImportType importType;
114 }
115 */
116
117 public class AST : ASTList<ASTNode>
118 {
119    ASTNode ::ParseExternalDeclaration()
120    {
121       SpecsList specs = SpecsList::parse();
122       InitDeclList decls = InitDeclList::parse();
123
124       if(peekToken().type == '{')
125          return ASTFunctionDefinition::parse(specs, decls);
126       else if(specs || decls)
127          return ASTDeclaration::parse(specs, decls);
128       else
129       {
130          readToken(); // Error
131          return null;
132       }
133    }
134 public:
135    AST ::parse()
136    {
137       AST ast = null;
138       while(peekToken().type)
139       {
140          ASTNode n = ParseExternalDeclaration();
141          if(n)
142          {
143             if(!ast) ast = { };
144             ast.Add(n);
145          }
146       }
147       return ast;
148    }
149
150    void print()
151    {
152       for(n : this)
153       {
154          n.print();
155          PrintLn("");
156       }
157    }
158 }