From a83f1b80bb03759620bb8c03ffe3cc523e5e018b Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Wed, 10 Aug 2022 00:46:27 +0100 Subject: [PATCH] parser: support double/float cast --- meta/run_on_files.sh | 4 +++- trunk_lexer/src/lexer.rs | 4 ++++ trunk_parser/src/ast.rs | 2 ++ trunk_parser/src/parser/mod.rs | 7 ++++--- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/meta/run_on_files.sh b/meta/run_on_files.sh index 71ac5e4..4f6b97f 100755 --- a/meta/run_on_files.sh +++ b/meta/run_on_files.sh @@ -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 diff --git a/trunk_lexer/src/lexer.rs b/trunk_lexer/src/lexer.rs index 2819aea..8a4e887 100644 --- a/trunk_lexer/src/lexer.rs +++ b/trunk_lexer/src/lexer.rs @@ -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 } diff --git a/trunk_parser/src/ast.rs b/trunk_parser/src/ast.rs index e99b9c3..adbdf06 100644 --- a/trunk_parser/src/ast.rs +++ b/trunk_parser/src/ast.rs @@ -274,6 +274,7 @@ pub enum CastKind { Object, Bool, Int, + Double, } impl From for CastKind { @@ -283,6 +284,7 @@ impl From for CastKind { TokenKind::ObjectCast => Self::Object, TokenKind::BoolCast => Self::Bool, TokenKind::IntCast => Self::Int, + TokenKind::DoubleCast => Self::Double, _ => unreachable!() } } diff --git a/trunk_parser/src/parser/mod.rs b/trunk_parser/src/parser/mod.rs index b47b707..415f6be 100644 --- a/trunk_parser/src/parser/mod.rs +++ b/trunk_parser/src/parser/mod.rs @@ -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!() } }