Skip to content

Commit c93e1a2

Browse files
committed
Add table parsing wip
1 parent 3aeaea4 commit c93e1a2

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/ast.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::BTreeMap;
2+
13
use crate::vm::VirtualMachine;
24

35
#[derive(Debug, Clone, PartialEq, PartialOrd)]
@@ -27,6 +29,7 @@ pub enum Expression {
2729
NumberLiteral(f64),
2830
BooleanLiteral(bool),
2931
StringLiteral(String),
32+
TableLiteral(BTreeMap<Expression, Expression>),
3033
NilLiteral,
3134
IdentifierExpression(String),
3235
BinaryExpression(Box<Expression>, String, Box<Expression>),
@@ -158,6 +161,7 @@ impl Expression {
158161
_ => Err(format!("Function '{}' not found", function_name)),
159162
}
160163
}
164+
Expression::TableLiteral(btree_map) => todo!(),
161165
}
162166
}
163167
}

src/lex.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub enum Token {
2929
RightParen,
3030
LeftBracket,
3131
RightBracket,
32+
LeftSquareBracket,
33+
RightSquareBracket,
3234
Assigment,
3335
Dot,
3436
Comma,
@@ -161,12 +163,19 @@ impl<'a> Lexer<'a> {
161163
'{' => {
162164
tokens.push(Token::LeftBracket);
163165
self.advance();
164-
165166
}
166167
'}' => {
167168
tokens.push(Token::RightBracket);
168169
self.advance();
169170
}
171+
'[' => {
172+
tokens.push(Token::LeftSquareBracket);
173+
self.advance();
174+
}
175+
']' => {
176+
tokens.push(Token::RightSquareBracket);
177+
self.advance();
178+
}
170179
'<' => {
171180
if Some('=') == self.input.clone().next() {
172181
tokens.push(Token::LessThanOrEqual);

src/parser.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::BTreeMap;
2+
13
use crate::{
24
ast::{Expression, Statement},
35
lex::{self, Lexer, LiteralType},
@@ -287,6 +289,15 @@ impl<'a> Parser<'a> {
287289
Err("Expected ')'".to_string())
288290
}
289291
}
292+
lex::Token::LeftBracket => {
293+
let table_literal = self.parse_table(tokens)?;
294+
295+
if let Some(lex::Token::RightBracket) = tokens.next() {
296+
Ok(table_literal)
297+
} else {
298+
Err("Expected '}'".to_string())
299+
}
300+
}
290301
lex::Token::Literal(LiteralType::Number(number)) => {
291302
Ok(Expression::NumberLiteral(number))
292303
}
@@ -423,4 +434,16 @@ impl<'a> Parser<'a> {
423434
loop_condition,
424435
})
425436
}
437+
438+
fn parse_table(
439+
&mut self,
440+
tokens: &mut std::iter::Peekable<std::vec::IntoIter<lex::Token>>,
441+
) -> Result<Expression, String> {
442+
tokens.next();
443+
444+
// while tokens.peek() != Some(&lex::Token::RightBracket) {
445+
// tokens.next()
446+
// }
447+
unimplemented!();
448+
}
426449
}

0 commit comments

Comments
 (0)