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