parser: support double/float cast

This commit is contained in:
Ryan Chandler 2022-08-10 00:46:27 +01:00
parent 5a44de9650
commit a83f1b80bb
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA
4 changed files with 13 additions and 4 deletions

View File

@ -4,9 +4,11 @@ set -xe
dir=$(realpath $1)
cargo build --release
for file in $(find $dir -name "*.php")
do
cargo run -q -- $file
./target/release/phpast $file
if [ $? -ne 0 ]
then

View File

@ -643,6 +643,10 @@ impl Lexer {
self.col += 4;
self.skip(5);
TokenKind::IntCast
} else if self.try_read("float)") {
self.col += 6;
self.skip(7);
TokenKind::DoubleCast
} else {
TokenKind::LeftParen
}

View File

@ -274,6 +274,7 @@ pub enum CastKind {
Object,
Bool,
Int,
Double,
}
impl From<TokenKind> for CastKind {
@ -283,6 +284,7 @@ impl From<TokenKind> for CastKind {
TokenKind::ObjectCast => Self::Object,
TokenKind::BoolCast => Self::Bool,
TokenKind::IntCast => Self::Int,
TokenKind::DoubleCast => Self::Double,
_ => unreachable!()
}
}

View File

@ -1622,12 +1622,12 @@ impl Parser {
}
fn is_prefix(op: &TokenKind) -> bool {
matches!(op, TokenKind::Bang | TokenKind::Minus | TokenKind::StringCast | TokenKind::ObjectCast | TokenKind::BoolCast | TokenKind::IntCast)
matches!(op, TokenKind::Bang | TokenKind::Minus | TokenKind::StringCast | TokenKind::ObjectCast | TokenKind::BoolCast | TokenKind::IntCast | TokenKind::DoubleCast)
}
fn prefix_binding_power(op: &TokenKind) -> u8 {
match op {
TokenKind::StringCast | TokenKind::ObjectCast | TokenKind::BoolCast | TokenKind::IntCast => 101,
TokenKind::StringCast | TokenKind::ObjectCast | TokenKind::BoolCast | TokenKind::IntCast | TokenKind::DoubleCast => 101,
TokenKind::Minus => 100,
TokenKind::Bang => 99,
_ => unreachable!()
@ -1638,7 +1638,8 @@ fn prefix(op: &TokenKind, rhs: Expression) -> Expression {
match op {
TokenKind::Bang => Expression::BooleanNot { value: Box::new(rhs) },
TokenKind::Minus => Expression::Negate { value: Box::new(rhs) },
TokenKind::StringCast | TokenKind::ObjectCast | TokenKind::BoolCast | TokenKind::IntCast => Expression::Cast { kind: op.into(), value: Box::new(rhs) },
TokenKind::StringCast | TokenKind::ObjectCast | TokenKind::BoolCast | TokenKind::IntCast |
TokenKind::DoubleCast => Expression::Cast { kind: op.into(), value: Box::new(rhs) },
_ => unreachable!()
}
}