Add parsing of casting

This commit is contained in:
Sofia 2025-07-21 21:23:37 +03:00
parent b2e9849504
commit c4ab4ac0b3
4 changed files with 23 additions and 0 deletions

View File

@ -66,6 +66,7 @@ pub enum ExpressionKind {
IfExpr(Box<IfExpression>),
StructExpression(StructExpression),
UnaryOperation(UnaryOperator, Box<Expression>),
CastTo(Box<Expression>, Type),
}
#[derive(Debug, Clone)]

View File

@ -233,10 +233,27 @@ impl Parse for PrimaryExpression {
}
}
while let Ok(as_cast) = stream.parse::<AsCast>() {
expr = Expression(
ExpressionKind::CastTo(Box::new(expr), as_cast.0),
stream.get_range().unwrap(),
);
}
Ok(PrimaryExpression(expr))
}
}
#[derive(Debug)]
pub struct AsCast(Type);
impl Parse for AsCast {
fn parse(mut stream: TokenStream) -> Result<Self, Error> {
stream.expect(Token::AsKeyword)?;
Ok(AsCast(stream.parse()?))
}
}
/// This algorithm seems somewhat like magic to me. I understand it if I read
/// carefully, but it is difficult to read every single time.
///

View File

@ -256,6 +256,7 @@ impl ast::Expression {
Box::new(expr.process(module_id)),
),
},
ast::ExpressionKind::CastTo(expression, _) => todo!(),
};
mir::Expression(kind, self.1.as_meta(module_id))

View File

@ -24,6 +24,8 @@ pub enum Token {
FnKeyword,
/// `pub`
PubKeyword,
/// `as`
AsKeyword,
/// `->`
Arrow,
/// `if`
@ -124,6 +126,7 @@ impl ToString for Token {
Token::False => String::from("false"),
Token::Extern => String::from("extern"),
Token::Struct => String::from("struct"),
Token::AsKeyword => String::from("as"),
Token::Semi => String::from(';'),
Token::Equals => String::from('='),
Token::Colon => String::from(':'),
@ -286,6 +289,7 @@ pub fn tokenize<T: Into<String>>(to_tokenize: T) -> Result<Vec<FullToken>, Error
"extern" => Token::Extern,
"pub" => Token::PubKeyword,
"struct" => Token::Struct,
"as" => Token::AsKeyword,
_ => Token::Identifier(value),
};
variant