1919import static ko .carbonel .json .SerializeClass .AST ;
2020import static ko .carbonel .json .SerializeClass .TS ;
2121
22- // TODO: Static access for methods. ! Done
23- // TODO: Separate literals ! Done
24-
2522public final class TinyRustSymbolTable implements SymbolTable {
2623 public static final String OBJECT = "Object" ;
2724 static final String STR = "Str" ;
@@ -34,10 +31,10 @@ public final class TinyRustSymbolTable implements SymbolTable {
3431 public final List <ClassEntry > classEntries ;
3532 @ SerializeClass ({TS , AST })
3633 public final MethodEntry main ;
37- public MethodEntry activeMethod = new MethodEntry (this , null );
38- public ClassEntry activeClass = new ClassEntry (this );
3934 private final Stack <StatementEntry > statementStack = new Stack <>();
4035 private final Stack <ExpressionEntry > expressionStack = new Stack <>();
36+ public MethodEntry activeMethod = new MethodEntry (this , null );
37+ public ClassEntry activeClass = new ClassEntry (this );
4138
4239 public TinyRustSymbolTable () {
4340 this .classEntries = new ArrayList <>();
@@ -115,20 +112,6 @@ public void consolidate() {
115112 activeClass = null ;
116113 }
117114
118- private void populatePredefined () {
119- classEntries .add (new ClassEntry (this , new TinyRustLexerToken (TinyRustTokenType .INT , I_32 ), false , true ));
120- classEntries .add (new ClassEntry (this , new TinyRustLexerToken (TinyRustTokenType .BOOL , BOOL ), false , true ));
121- classEntries .add (new ClassEntry (this , new TinyRustLexerToken (TinyRustTokenType .CHR , CHAR ), false , true ));
122- classEntries .add (new ClassEntry (this , new TinyRustLexerToken (TinyRustTokenType .STR , STR ), false , true ));
123- classEntries .add (new ClassEntry (this , new TinyRustLexerToken (TinyRustTokenType .CLASS_ID , OBJECT ), true , false ));
124- populateStdlib ();
125- }
126-
127- private void populateStdlib () {
128- // TODO: Add stdlib classes
129- classEntries .add (new ClassEntry (this , new TinyRustLexerToken (TinyRustTokenType .CLASS_ID , IO ), false , false ));
130- }
131-
132115 @ CalledByParser
133116 public void newIf () {
134117 IfStatementEntry newIf = new IfStatementEntry ();
@@ -140,7 +123,7 @@ public void newIf() {
140123 public void ifBlock () {
141124 IfStatementEntry active = (IfStatementEntry ) statementStack .peek ();
142125 ExpressionEntry condition = expressionStack .pop ();
143- active .setCondition (condition );
126+ active .setIfCondition (condition );
144127 Logger .output ("If condition set" );
145128 }
146129
@@ -163,53 +146,47 @@ public void ifEnd() {
163146
164147 @ CalledByParser
165148 public void newReturn () {
166- ReturnStatementEntry newReturn = new ReturnStatementEntry ();
167- statementStack .push (newReturn );
149+ newStatement (new ReturnStatementEntry ());
150+ }
151+
152+ private void newStatement (StatementEntry statement ) {
153+ statementStack .push (statement );
168154 }
169155
170156 @ CalledByParser
171157 public void newExpr () {
172- ExpressionStatementEntry newExpr = new ExpressionStatementEntry ();
173- statementStack .push (newExpr );
158+ newStatement (new ExpressionStatementEntry ());
174159 }
175160
176161 @ CalledByParser
177162 public void newWhile () {
178- WhileStatementEntry newWhile = new WhileStatementEntry ();
179- statementStack .push (newWhile );
163+ newStatement (new WhileStatementEntry ());
180164 }
181165
182166 @ CalledByParser
183167 public void newAssign () {
184- AssignStatementEntry newAssign = new AssignStatementEntry ();
185- statementStack .push (newAssign );
168+ newStatement (new AssignStatementEntry ());
186169 }
187170
188171 public void assignLHS () {
189172 AssignStatementEntry active = (AssignStatementEntry ) statementStack .peek ();
190- active .setLhs (expressionStack .pop ());
173+ active .setAssignLHS (expressionStack .pop ());
191174 }
192175
193176 public void assignRHS () {
194177 AssignStatementEntry active = (AssignStatementEntry ) statementStack .peek ();
195- active .setRhs (expressionStack .pop ());
178+ active .setAssignRHS (expressionStack .pop ());
196179 }
197180
198181 @ CalledByParser
199182 public void newEmpty () {
200- EmptyStatementEntry newEmpty = new EmptyStatementEntry ();
201- statementStack .push (newEmpty );
183+ newStatement (new EmptyStatementEntry ());
202184 }
203185
204186 @ CalledByParser
205187 public void whileBody () {
206188 WhileStatementEntry active = (WhileStatementEntry ) statementStack .peek ();
207- active .setCondition (expressionStack .pop ());
208- }
209-
210- @ CalledByParser
211- public void end () {
212- Logger .output ("TODO" ); // FIXME
189+ active .setWhileCondition (expressionStack .pop ());
213190 }
214191
215192 /**
@@ -233,11 +210,6 @@ public void expBinComplete() {
233210 Logger .output ("Binary expression: " + nw );
234211 }
235212
236- @ CalledByParser
237- public void expLit (TinyRustLexerToken lit ) {
238- expLit (List .of (lit ));
239- }
240-
241213 @ CalledByParser
242214 public void expLit (List <TinyRustLexerToken > lit ) {
243215 if (lit .size () != 1 ) throw new RuntimeException ("Literal size is not 1" );
@@ -307,14 +279,6 @@ public void expArrayAccess() {
307279 expressionStack .push (arr );
308280 }
309281
310- @ CalledByParser
311- public void expChain () {
312- ChainedExpressionEntry chain = new ChainedExpressionEntry ();
313- chain .setChain (expressionStack .pop ());
314- chain .setBase (expressionStack .pop ());
315- expressionStack .push (chain );
316- }
317-
318282 @ CalledByParser
319283 public void newBlock () {
320284 BlockStatementEntry block = new BlockStatementEntry ();
@@ -330,31 +294,6 @@ public void endBlock() {
330294 });
331295 }
332296
333- private <T extends StatementEntry > void popStatementUntil (Class <T > clazz , BiConsumer <T , List <StatementEntry >> callback ) {
334- popUntil (clazz , callback , statementStack , it -> false );
335- }
336-
337- /**
338- * Pop until the top of the stack is an instance of clazz, then call callback with the top of the stack cast to clazz and the popped statements
339- *
340- * @param <T> The class to pop until
341- * @param <S> The type of the stack
342- * @param clazz The class to pop until
343- * @param callback The callback to call
344- * @param stack The stack to pop from
345- * @param predicate A predicate to test the popped statements - if true, the popped statements are added to the list anyway
346- */
347- private <T extends S , S > void popUntil (Class <T > clazz , BiConsumer <T , List <S >> callback , Stack <S > stack , Predicate <T > predicate ) {
348- List <S > statementList = new ArrayList <>();
349- while (!(clazz .isInstance (stack .peek ())) || predicate .test (clazz .cast (stack .peek ())))
350- statementList .add (0 , stack .pop ());
351- callback .accept (clazz .cast (stack .peek ()), statementList );
352- }
353-
354- private <T extends ExpressionEntry > void popExpressionUntil (Class <T > clazz , BiConsumer <T , List <ExpressionEntry >> callback , Predicate <T > predicate ) {
355- popUntil (clazz , callback , expressionStack , predicate );
356- }
357-
358297 @ CalledByParser
359298 public void endExpr () {
360299 ExpressionStatementEntry exp = (ExpressionStatementEntry ) statementStack .peek ();
@@ -389,7 +328,7 @@ public void endWhile() {
389328 @ CalledByParser
390329 public void endReturn (){
391330 ReturnStatementEntry ret = new ReturnStatementEntry ();
392- ret .setExpression (expressionStack .pop ());
331+ ret .setReturnExpression (expressionStack .pop ());
393332 statementStack .push (ret );
394333 Logger .output ("Return: " + ret );
395334 }
@@ -450,4 +389,43 @@ public void expNewArray(){
450389 exp .setType (type .fieldName ());
451390 expressionStack .push (exp );
452391 }
392+
393+ private void populatePredefined () {
394+ classEntries .add (new ClassEntry (this , new TinyRustLexerToken (TinyRustTokenType .INT , I_32 ), false , true ));
395+ classEntries .add (new ClassEntry (this , new TinyRustLexerToken (TinyRustTokenType .BOOL , BOOL ), false , true ));
396+ classEntries .add (new ClassEntry (this , new TinyRustLexerToken (TinyRustTokenType .CHR , CHAR ), false , true ));
397+ classEntries .add (new ClassEntry (this , new TinyRustLexerToken (TinyRustTokenType .STR , STR ), false , true ));
398+ classEntries .add (new ClassEntry (this , new TinyRustLexerToken (TinyRustTokenType .CLASS_ID , OBJECT ), true , false ));
399+ populateStdlib ();
400+ }
401+
402+ private void populateStdlib () {
403+ // TODO: Add stdlib classes
404+ classEntries .add (new ClassEntry (this , new TinyRustLexerToken (TinyRustTokenType .CLASS_ID , IO ), false , false ));
405+ }
406+
407+ private <T extends StatementEntry > void popStatementUntil (Class <T > clazz , BiConsumer <T , List <StatementEntry >> callback ) {
408+ popUntil (clazz , callback , statementStack , it -> false );
409+ }
410+
411+ /**
412+ * Pop until the top of the stack is an instance of clazz, then call callback with the top of the stack cast to clazz and the popped statements
413+ *
414+ * @param <T> The class to pop until
415+ * @param <S> The type of the stack
416+ * @param clazz The class to pop until
417+ * @param callback The callback to call
418+ * @param stack The stack to pop from
419+ * @param predicate A predicate to test the popped statements - if true, the popped statements are added to the list anyway
420+ */
421+ private <T extends S , S > void popUntil (Class <T > clazz , BiConsumer <T , List <S >> callback , Stack <S > stack , Predicate <T > predicate ) {
422+ List <S > statementList = new ArrayList <>();
423+ while (!(clazz .isInstance (stack .peek ())) || predicate .test (clazz .cast (stack .peek ())))
424+ statementList .add (0 , stack .pop ());
425+ callback .accept (clazz .cast (stack .peek ()), statementList );
426+ }
427+
428+ private <T extends ExpressionEntry > void popExpressionUntil (Class <T > clazz , BiConsumer <T , List <ExpressionEntry >> callback , Predicate <T > predicate ) {
429+ popUntil (clazz , callback , expressionStack , predicate );
430+ }
453431}
0 commit comments