Add array indexing
This commit is contained in:
parent
8f95d445c0
commit
df6b5ef34b
@ -9,5 +9,5 @@ fn main() -> u16 {
|
||||
|
||||
let list = array();
|
||||
|
||||
return heehoo;
|
||||
return list[0];
|
||||
}
|
||||
|
@ -39,6 +39,7 @@ pub enum ExpressionKind {
|
||||
VariableName(String),
|
||||
Literal(Literal),
|
||||
Array(Vec<Expression>),
|
||||
Index(Box<Expression>, u64),
|
||||
Binop(BinaryOperator, Box<Expression>, Box<Expression>),
|
||||
FunctionCall(Box<FunctionCallExpression>),
|
||||
BlockExpr(Box<Block>),
|
||||
|
@ -63,7 +63,7 @@ impl Parse for PrimaryExpression {
|
||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||
use ExpressionKind as Kind;
|
||||
|
||||
let expr = if let Ok(exp) = stream.parse() {
|
||||
let mut expr = if let Ok(exp) = stream.parse() {
|
||||
Expression(
|
||||
Kind::FunctionCall(Box::new(exp)),
|
||||
stream.get_range().unwrap(),
|
||||
@ -114,6 +114,20 @@ impl Parse for PrimaryExpression {
|
||||
} else {
|
||||
Err(stream.expected_err("expression")?)?
|
||||
};
|
||||
|
||||
while let Some(Token::BracketOpen) = stream.peek() {
|
||||
stream.next(); // Consume BracketOpen
|
||||
if let Some(Token::DecimalValue(idx)) = stream.next() {
|
||||
stream.expect(Token::BracketClose)?;
|
||||
expr = Expression(
|
||||
ExpressionKind::Index(Box::new(expr), idx),
|
||||
stream.get_range().unwrap(),
|
||||
);
|
||||
} else {
|
||||
return Err(stream.expected_err("array index (number)")?);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(PrimaryExpression(expr))
|
||||
}
|
||||
}
|
||||
|
@ -150,6 +150,7 @@ impl ast::Expression {
|
||||
mir::ExprKind::If(mir::IfExpression(Box::new(cond), then_block, else_block))
|
||||
}
|
||||
ast::ExpressionKind::Array(expressions) => todo!(),
|
||||
ast::ExpressionKind::Index(expression, _) => todo!(),
|
||||
};
|
||||
|
||||
mir::Expression(kind, self.1.into())
|
||||
|
Loading…
Reference in New Issue
Block a user