Merge pull request #314 from HigherOrderCO/add-pow-operation

Add exponentiation operator
This commit is contained in:
Nicolas Abril 2024-05-16 11:07:57 +02:00 committed by GitHub
commit 530f8d777f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 7 deletions

View File

@ -238,7 +238,7 @@ impl fmt::Display for Op {
Op::AND => write!(f, "&"),
Op::OR => write!(f, "|"),
Op::XOR => write!(f, "^"),
Op::POW => todo!(),
Op::POW => write!(f, "**"),
Op::LOG => todo!(),
Op::ATN => todo!(),
}

View File

@ -994,6 +994,8 @@ pub trait ParserCommons<'a>: Parser<'a> {
Op::ADD
} else if self.try_consume_exactly("-") {
Op::SUB
} else if self.try_consume_exactly("**") {
Op::POW
} else if self.try_consume_exactly("*") {
Op::MUL
} else if self.try_consume_exactly("/") {
@ -1025,6 +1027,8 @@ pub trait ParserCommons<'a>: Parser<'a> {
Op::ADD
} else if self.starts_with("-") {
Op::SUB
} else if self.starts_with("**") {
Op::POW
} else if self.starts_with("*") {
Op::MUL
} else if self.starts_with("/") {

View File

@ -8,12 +8,17 @@ use crate::{
};
use TSPL::Parser;
const PREC: &[&[Op]] =
&[&[Op::OR], &[Op::XOR], &[Op::AND], &[Op::EQL, Op::NEQ], &[Op::LTN], &[Op::GTN], &[Op::ADD, Op::SUB], &[
Op::MUL,
Op::DIV,
Op::REM,
]];
const PREC: &[&[Op]] = &[
&[Op::OR],
&[Op::XOR],
&[Op::AND],
&[Op::EQL, Op::NEQ],
&[Op::LTN],
&[Op::GTN],
&[Op::ADD, Op::SUB],
&[Op::MUL, Op::DIV, Op::REM],
&[Op::POW],
];
pub struct PyParser<'i> {
pub input: &'i str,