File tree Expand file tree Collapse file tree 3 files changed +25
-5
lines changed
Expand file tree Collapse file tree 3 files changed +25
-5
lines changed Original file line number Diff line number Diff line change 1- local t = { 1 , 2 , 3 }
1+ local t = { 123 , 2 , 3 }
22
3- print (" Table exists" )
3+ print (t [1 ])
4+ print (t [2 ])
5+ print (t [3 ])
Original file line number Diff line number Diff line change @@ -43,6 +43,7 @@ pub enum Expression {
4343 IdentifierExpression ( String ) ,
4444 BinaryExpression ( Box < Expression > , String , Box < Expression > ) ,
4545 FunctionCall ( String , Vec < Expression > ) ,
46+ IndexOperator ( Box < Expression > , Box < Expression > ) ,
4647}
4748
4849#[ derive( Debug , Clone , PartialEq , PartialOrd ) ]
@@ -177,6 +178,18 @@ impl Expression {
177178 }
178179 Ok ( EvalValue :: Table ( table) )
179180 }
181+ Expression :: IndexOperator ( table, index) => {
182+ let table_value = table. execute ( _g) ?;
183+ let index_value = index. execute ( _g) ?;
184+
185+ match table_value {
186+ EvalValue :: Table ( table) => {
187+ Ok ( table. get ( & index_value) . cloned ( ) . unwrap_or ( EvalValue :: Nil ) )
188+ }
189+ EvalValue :: Nil => Ok ( EvalValue :: Nil ) ,
190+ _ => Err ( "Cannot index non-table value" . to_string ( ) )
191+ }
192+ }
180193 }
181194 }
182195}
Original file line number Diff line number Diff line change @@ -324,6 +324,13 @@ impl<'a> Parser<'a> {
324324 tokens. next ( ) ;
325325
326326 Ok ( Expression :: FunctionCall ( identifier, arguments) )
327+ } else if tokens. peek ( ) == Some ( & lex:: Token :: LeftSquareBracket ) {
328+ tokens. next ( ) ;
329+ let index = self . parse_expression ( tokens) ?;
330+
331+ self . expect ( tokens, lex:: Token :: RightSquareBracket ) ?;
332+
333+ Ok ( Expression :: IndexOperator ( Box :: new ( Expression :: IdentifierExpression ( identifier) ) , Box :: new ( index) ) )
327334 } else {
328335 Ok ( Expression :: IdentifierExpression ( identifier) )
329336 }
@@ -439,7 +446,6 @@ impl<'a> Parser<'a> {
439446 & mut self ,
440447 tokens : & mut std:: iter:: Peekable < std:: vec:: IntoIter < lex:: Token > > ,
441448 ) -> Result < Expression , String > {
442- tokens. next ( ) ;
443449
444450 let mut table_structure = BTreeMap :: new ( ) ;
445451 let mut in_table_index = 1 ;
@@ -454,8 +460,7 @@ impl<'a> Parser<'a> {
454460 } // Skip comma
455461 _ => {
456462 let element = self . parse_expression ( tokens) ?;
457- table_structure
458- . insert ( Expression :: NumberLiteral ( in_table_index as f64 ) , element) ;
463+ table_structure. insert ( Expression :: NumberLiteral ( in_table_index as f64 ) , element) ;
459464 in_table_index += 1 ;
460465 }
461466 }
You can’t perform that action at this time.
0 commit comments