Add struct indexing parsing
This commit is contained in:
parent
c83d53ae53
commit
d9a1e8456d
@ -44,12 +44,13 @@ pub enum ExpressionKind {
|
|||||||
VariableName(String),
|
VariableName(String),
|
||||||
Literal(Literal),
|
Literal(Literal),
|
||||||
Array(Vec<Expression>),
|
Array(Vec<Expression>),
|
||||||
Index(Box<Expression>, u64),
|
ArrayIndex(Box<Expression>, u64),
|
||||||
|
StructIndex(Box<Expression>, String),
|
||||||
Binop(BinaryOperator, Box<Expression>, Box<Expression>),
|
Binop(BinaryOperator, Box<Expression>, Box<Expression>),
|
||||||
FunctionCall(Box<FunctionCallExpression>),
|
FunctionCall(Box<FunctionCallExpression>),
|
||||||
BlockExpr(Box<Block>),
|
BlockExpr(Box<Block>),
|
||||||
IfExpr(Box<IfExpression>),
|
IfExpr(Box<IfExpression>),
|
||||||
StructInit(StructInit),
|
StructExpression(StructExpression),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
@ -132,7 +133,7 @@ pub enum ReturnType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct StructInit {
|
pub struct StructExpression {
|
||||||
name: String,
|
name: String,
|
||||||
fields: Vec<(String, Expression)>,
|
fields: Vec<(String, Expression)>,
|
||||||
}
|
}
|
||||||
@ -150,7 +151,8 @@ pub struct VariableReference(pub VariableReferenceKind, pub TokenRange);
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum VariableReferenceKind {
|
pub enum VariableReferenceKind {
|
||||||
Name(String, TokenRange),
|
Name(String, TokenRange),
|
||||||
Index(Box<VariableReference>, u64),
|
ArrayIndex(Box<VariableReference>, u64),
|
||||||
|
StructIndex(Box<VariableReference>, String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -80,7 +80,7 @@ impl Parse for PrimaryExpression {
|
|||||||
(stream.peek(), stream.peek2())
|
(stream.peek(), stream.peek2())
|
||||||
{
|
{
|
||||||
Expression(
|
Expression(
|
||||||
Kind::StructInit(stream.parse()?),
|
Kind::StructExpression(stream.parse()?),
|
||||||
stream.get_range().unwrap(),
|
stream.get_range().unwrap(),
|
||||||
)
|
)
|
||||||
} else if let Some(token) = stream.next() {
|
} else if let Some(token) = stream.next() {
|
||||||
@ -88,7 +88,7 @@ impl Parse for PrimaryExpression {
|
|||||||
Token::Identifier(v) => {
|
Token::Identifier(v) => {
|
||||||
if let Some(Token::BraceOpen) = stream.peek() {
|
if let Some(Token::BraceOpen) = stream.peek() {
|
||||||
Expression(
|
Expression(
|
||||||
Kind::StructInit(stream.parse()?),
|
Kind::StructExpression(stream.parse()?),
|
||||||
stream.get_range().unwrap(),
|
stream.get_range().unwrap(),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
@ -134,11 +134,21 @@ impl Parse for PrimaryExpression {
|
|||||||
Err(stream.expected_err("expression")?)?
|
Err(stream.expected_err("expression")?)?
|
||||||
};
|
};
|
||||||
|
|
||||||
while let Ok(ValueIndex(idx)) = stream.parse() {
|
while let Ok(index) = stream.parse::<ValueIndex>() {
|
||||||
expr = Expression(
|
match index {
|
||||||
ExpressionKind::Index(Box::new(expr), idx),
|
ValueIndex::Array(ArrayValueIndex(idx)) => {
|
||||||
stream.get_range().unwrap(),
|
expr = Expression(
|
||||||
);
|
ExpressionKind::ArrayIndex(Box::new(expr), idx),
|
||||||
|
stream.get_range().unwrap(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
ValueIndex::Struct(StructValueIndex(name)) => {
|
||||||
|
expr = Expression(
|
||||||
|
ExpressionKind::StructIndex(Box::new(expr), name),
|
||||||
|
stream.get_range().unwrap(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(PrimaryExpression(expr))
|
Ok(PrimaryExpression(expr))
|
||||||
@ -415,9 +425,9 @@ impl Parse for VariableReference {
|
|||||||
stream.get_range().unwrap(),
|
stream.get_range().unwrap(),
|
||||||
);
|
);
|
||||||
|
|
||||||
while let Ok(ValueIndex(idx)) = stream.parse() {
|
while let Ok(ArrayValueIndex(idx)) = stream.parse() {
|
||||||
var_ref = VariableReference(
|
var_ref = VariableReference(
|
||||||
VariableReferenceKind::Index(Box::new(var_ref), idx),
|
VariableReferenceKind::ArrayIndex(Box::new(var_ref), idx),
|
||||||
stream.get_range().unwrap(),
|
stream.get_range().unwrap(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -429,7 +439,7 @@ impl Parse for VariableReference {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Parse for StructInit {
|
impl Parse for StructExpression {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
let Some(Token::Identifier(name)) = stream.next() else {
|
let Some(Token::Identifier(name)) = stream.next() else {
|
||||||
return Err(stream.expected_err("struct identifier")?);
|
return Err(stream.expected_err("struct identifier")?);
|
||||||
@ -439,9 +449,8 @@ impl Parse for StructInit {
|
|||||||
let fields = named_list.0.into_iter().map(|f| (f.0, f.1)).collect();
|
let fields = named_list.0.into_iter().map(|f| (f.0, f.1)).collect();
|
||||||
|
|
||||||
stream.expect(Token::BraceClose)?;
|
stream.expect(Token::BraceClose)?;
|
||||||
stream.expect(Token::Semi)?;
|
|
||||||
|
|
||||||
Ok(StructInit { name, fields })
|
Ok(StructExpression { name, fields })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -480,21 +489,51 @@ impl<T: Parse + std::fmt::Debug> Parse for NamedField<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ValueIndex(u64);
|
pub enum ValueIndex {
|
||||||
|
Array(ArrayValueIndex),
|
||||||
|
Struct(StructValueIndex),
|
||||||
|
}
|
||||||
|
|
||||||
impl Parse for ValueIndex {
|
impl Parse for ValueIndex {
|
||||||
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
|
match stream.peek() {
|
||||||
|
Some(Token::BracketOpen) => Ok(ValueIndex::Array(stream.parse()?)),
|
||||||
|
Some(Token::Dot) => Ok(ValueIndex::Struct(stream.parse()?)),
|
||||||
|
_ => Err(stream.expected_err("value or struct index")?),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct ArrayValueIndex(u64);
|
||||||
|
|
||||||
|
impl Parse for ArrayValueIndex {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
stream.expect(Token::BracketOpen)?;
|
stream.expect(Token::BracketOpen)?;
|
||||||
if let Some(Token::DecimalValue(idx)) = stream.next() {
|
if let Some(Token::DecimalValue(idx)) = stream.next() {
|
||||||
stream.expect(Token::BracketClose)?;
|
stream.expect(Token::BracketClose)?;
|
||||||
Ok(ValueIndex(idx))
|
Ok(ArrayValueIndex(idx))
|
||||||
} else {
|
} else {
|
||||||
return Err(stream.expected_err("array index (number)")?);
|
return Err(stream.expected_err("array index (number)")?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct StructValueIndex(String);
|
||||||
|
|
||||||
|
impl Parse for StructValueIndex {
|
||||||
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
|
stream.expect(Token::Dot)?;
|
||||||
|
if let Some(Token::Identifier(name)) = stream.next() {
|
||||||
|
Ok(StructValueIndex(name))
|
||||||
|
} else {
|
||||||
|
return Err(stream.expected_err("struct index (number)")?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Parse for BlockLevelStatement {
|
impl Parse for BlockLevelStatement {
|
||||||
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
|
||||||
use BlockLevelStatement as Stmt;
|
use BlockLevelStatement as Stmt;
|
||||||
|
@ -152,9 +152,12 @@ impl ast::VariableReferenceKind {
|
|||||||
(*range).into(),
|
(*range).into(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
ast::VariableReferenceKind::Index(var_ref, idx) => {
|
ast::VariableReferenceKind::ArrayIndex(var_ref, idx) => {
|
||||||
mir::IndexedVariableReferenceKind::Index(Box::new(var_ref.process()), *idx)
|
mir::IndexedVariableReferenceKind::Index(Box::new(var_ref.process()), *idx)
|
||||||
}
|
}
|
||||||
|
ast::VariableReferenceKind::StructIndex(variable_reference, _) => {
|
||||||
|
todo!("struct indexing into mir")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -194,12 +197,15 @@ impl ast::Expression {
|
|||||||
ast::ExpressionKind::Array(expressions) => {
|
ast::ExpressionKind::Array(expressions) => {
|
||||||
mir::ExprKind::Array(expressions.iter().map(|e| e.process()).collect())
|
mir::ExprKind::Array(expressions.iter().map(|e| e.process()).collect())
|
||||||
}
|
}
|
||||||
ast::ExpressionKind::Index(expression, idx) => mir::ExprKind::Index(
|
ast::ExpressionKind::ArrayIndex(expression, idx) => mir::ExprKind::Index(
|
||||||
Box::new(expression.process()),
|
Box::new(expression.process()),
|
||||||
mir::TypeKind::Vague(mir::VagueType::Unknown),
|
mir::TypeKind::Vague(mir::VagueType::Unknown),
|
||||||
*idx,
|
*idx,
|
||||||
),
|
),
|
||||||
ast::ExpressionKind::StructInit(struct_init) => todo!("implement struct init process"),
|
ast::ExpressionKind::StructExpression(struct_init) => {
|
||||||
|
todo!("implement struct init process")
|
||||||
|
}
|
||||||
|
ast::ExpressionKind::StructIndex(expression, _) => todo!("struct index expression"),
|
||||||
};
|
};
|
||||||
|
|
||||||
mir::Expression(kind, self.1.into())
|
mir::Expression(kind, self.1.into())
|
||||||
|
Loading…
Reference in New Issue
Block a user