Add parsing of casting
This commit is contained in:
parent
b2e9849504
commit
c4ab4ac0b3
@ -66,6 +66,7 @@ pub enum ExpressionKind {
|
|||||||
IfExpr(Box<IfExpression>),
|
IfExpr(Box<IfExpression>),
|
||||||
StructExpression(StructExpression),
|
StructExpression(StructExpression),
|
||||||
UnaryOperation(UnaryOperator, Box<Expression>),
|
UnaryOperation(UnaryOperator, Box<Expression>),
|
||||||
|
CastTo(Box<Expression>, Type),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -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))
|
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
|
/// This algorithm seems somewhat like magic to me. I understand it if I read
|
||||||
/// carefully, but it is difficult to read every single time.
|
/// carefully, but it is difficult to read every single time.
|
||||||
///
|
///
|
||||||
|
@ -256,6 +256,7 @@ impl ast::Expression {
|
|||||||
Box::new(expr.process(module_id)),
|
Box::new(expr.process(module_id)),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
ast::ExpressionKind::CastTo(expression, _) => todo!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
mir::Expression(kind, self.1.as_meta(module_id))
|
mir::Expression(kind, self.1.as_meta(module_id))
|
||||||
|
@ -24,6 +24,8 @@ pub enum Token {
|
|||||||
FnKeyword,
|
FnKeyword,
|
||||||
/// `pub`
|
/// `pub`
|
||||||
PubKeyword,
|
PubKeyword,
|
||||||
|
/// `as`
|
||||||
|
AsKeyword,
|
||||||
/// `->`
|
/// `->`
|
||||||
Arrow,
|
Arrow,
|
||||||
/// `if`
|
/// `if`
|
||||||
@ -124,6 +126,7 @@ impl ToString for Token {
|
|||||||
Token::False => String::from("false"),
|
Token::False => String::from("false"),
|
||||||
Token::Extern => String::from("extern"),
|
Token::Extern => String::from("extern"),
|
||||||
Token::Struct => String::from("struct"),
|
Token::Struct => String::from("struct"),
|
||||||
|
Token::AsKeyword => String::from("as"),
|
||||||
Token::Semi => String::from(';'),
|
Token::Semi => String::from(';'),
|
||||||
Token::Equals => String::from('='),
|
Token::Equals => String::from('='),
|
||||||
Token::Colon => 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,
|
"extern" => Token::Extern,
|
||||||
"pub" => Token::PubKeyword,
|
"pub" => Token::PubKeyword,
|
||||||
"struct" => Token::Struct,
|
"struct" => Token::Struct,
|
||||||
|
"as" => Token::AsKeyword,
|
||||||
_ => Token::Identifier(value),
|
_ => Token::Identifier(value),
|
||||||
};
|
};
|
||||||
variant
|
variant
|
||||||
|
Loading…
Reference in New Issue
Block a user