@@ -156,6 +156,13 @@ impl CompilerContext {
156156 self . symbol_table . contains ( symbol)
157157 }
158158
159+ pub fn get_symbol_type ( & self , symbol_name : & str ) -> Option < Option < DataType > > {
160+ self . symbol_table
161+ . iter ( )
162+ . find ( |x| x. name == symbol_name)
163+ . map ( |x| x. data_type . clone ( ) )
164+ }
165+
159166 pub fn create_ast_graph ( & mut self , from : AstPtr ) -> Result < ( ) , CompilerError > {
160167 self . ast
161168 . graph_ast (
@@ -167,66 +174,85 @@ impl CompilerContext {
167174 }
168175}
169176
170- #[ derive( Clone , Debug ) ]
171- pub enum SymbolTableElement {
172- VarDeclaration ( String , DataType , usize ) ,
173- IntLiteral ( TokenIntLiteral ) ,
174- FloatLiteral ( TokenFloatLiteral ) ,
175- StringLiteral ( TokenStringLiteral ) ,
177+ #[ derive( Default ) ]
178+ pub struct SymbolTableElement {
179+ pub name : String ,
180+ pub data_type : Option < DataType > ,
181+ pub value : Option < String > ,
182+ pub length : Option < usize > ,
176183}
177184
178185impl Display for SymbolTableElement {
179186 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
180- match self {
181- Self :: FloatLiteral ( float) => write ! (
182- f,
183- "{}|CONST_FLOAT|{}|{}" ,
184- float. original,
185- float. original,
186- float. original. len( )
187- ) ?,
188- Self :: IntLiteral ( int) => {
189- write ! ( f, "{}|CONST_INT|{}|{}" , int, int, int. to_string( ) . len( ) ) ?
190- }
191- Self :: StringLiteral ( string) => {
192- write ! ( f, "{}|CONST_STRING|{}|{}" , string, string, string. len( ) ) ?
193- }
194- Self :: VarDeclaration ( token, r#type, length) => {
195- write ! ( f, "{}|{}|-|{}" , token, r#type, length) ?
196- }
197- } ;
198- Ok ( ( ) )
187+ let name = & self . name ;
188+ let data_type = self
189+ . data_type
190+ . as_ref ( )
191+ . map ( |r#type| r#type. to_string ( ) )
192+ . unwrap_or_else ( || String :: from ( "-" ) ) ;
193+ let value = self
194+ . value
195+ . as_ref ( )
196+ . cloned ( )
197+ . unwrap_or_else ( || String :: from ( "-" ) ) ;
198+ let length = self
199+ . length
200+ . map ( |length| length. to_string ( ) )
201+ . unwrap_or_else ( || String :: from ( "-" ) ) ;
202+
203+ write ! ( f, "{name}|{data_type}|{value}|{length}" )
199204 }
200205}
201206
202207impl PartialEq for SymbolTableElement {
203208 fn eq ( & self , other : & Self ) -> bool {
204- match ( self , other) {
205- ( Self :: FloatLiteral ( token0) , Self :: FloatLiteral ( token1) ) => token0 == token1,
206- ( Self :: IntLiteral ( token0) , Self :: IntLiteral ( token1) ) => token0 == token1,
207- ( Self :: StringLiteral ( token0) , Self :: StringLiteral ( token1) ) => token0 == token1,
208- ( Self :: VarDeclaration ( token0, _, _) , Self :: VarDeclaration ( token1, _, _) ) => {
209- token0 == token1
210- }
211- _ => false ,
212- }
209+ self . name == other. name
213210 }
214211}
215212
213+ impl Eq for SymbolTableElement { }
214+
216215impl From < TokenIntLiteral > for SymbolTableElement {
217216 fn from ( value : TokenIntLiteral ) -> Self {
218- Self :: IntLiteral ( value)
217+ let mut name = String :: with_capacity ( value. original . len ( ) + 1 ) ;
218+ name. push ( '_' ) ;
219+ name. push_str ( & value. original ) ;
220+
221+ Self {
222+ name,
223+ data_type : None ,
224+ value : Some ( value. original ) ,
225+ length : None ,
226+ }
219227 }
220228}
221229
222230impl From < TokenFloatLiteral > for SymbolTableElement {
223231 fn from ( value : TokenFloatLiteral ) -> Self {
224- Self :: FloatLiteral ( value)
232+ let mut name = String :: with_capacity ( value. original . len ( ) + 1 ) ;
233+ name. push ( '_' ) ;
234+ name. push_str ( & value. original ) ;
235+
236+ Self {
237+ name : value. original . clone ( ) ,
238+ data_type : None ,
239+ value : Some ( value. original ) ,
240+ length : None ,
241+ }
225242 }
226243}
227244
228245impl From < TokenStringLiteral > for SymbolTableElement {
229246 fn from ( value : TokenStringLiteral ) -> Self {
230- Self :: StringLiteral ( value)
247+ let mut name = String :: with_capacity ( value. len ( ) + 1 ) ;
248+ name. push ( '_' ) ;
249+ name. push_str ( & value) ;
250+
251+ Self {
252+ name,
253+ data_type : None ,
254+ length : Some ( value. len ( ) ) ,
255+ value : Some ( value) ,
256+ }
231257 }
232258}
0 commit comments