1
0
This commit is contained in:
Jeff 2024-03-29 15:03:38 -04:00
parent d7a5586bc9
commit e1002b21d9
2 changed files with 23 additions and 21 deletions

View File

@ -24,7 +24,7 @@ impl<'src> Display for Token<'src> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self { match self {
Token::Boolean(boolean) => write!(f, "{boolean}"), Token::Boolean(boolean) => write!(f, "{boolean}"),
Token::BuiltInFunction(built_in_identifier) => write!(f, "{built_in_identifier}"), Token::BuiltInFunction(built_in_function) => write!(f, "{built_in_function}"),
Token::Integer(integer) => write!(f, "{integer}"), Token::Integer(integer) => write!(f, "{integer}"),
Token::Float(float) => write!(f, "{float}"), Token::Float(float) => write!(f, "{float}"),
Token::String(string) => write!(f, "{string}"), Token::String(string) => write!(f, "{string}"),
@ -297,7 +297,7 @@ pub fn lexer<'src>() -> impl Parser<
)) ))
.map(Token::Keyword); .map(Token::Keyword);
let built_in_identifier = choice(( let built_in_function = choice((
just(BuiltInFunction::ReadLine.name()).to(BuiltInFunction::ReadLine), just(BuiltInFunction::ReadLine.name()).to(BuiltInFunction::ReadLine),
just(BuiltInFunction::Sleep.name()).to(BuiltInFunction::Sleep), just(BuiltInFunction::Sleep.name()).to(BuiltInFunction::Sleep),
just(BuiltInFunction::WriteLine.name()).to(BuiltInFunction::WriteLine), just(BuiltInFunction::WriteLine.name()).to(BuiltInFunction::WriteLine),
@ -313,7 +313,7 @@ pub fn lexer<'src>() -> impl Parser<
identifier, identifier,
control, control,
operator, operator,
built_in_identifier, built_in_function,
)) ))
.map_with(|token, state| (token, state.span())) .map_with(|token, state| (token, state.span()))
.padded() .padded()

View File

@ -266,8 +266,6 @@ pub fn parser<'src>(
) )
}); });
use Operator::*;
let structure_field = identifier let structure_field = identifier
.clone() .clone()
.then_ignore(just(Token::Operator(Operator::Assign))) .then_ignore(just(Token::Operator(Operator::Assign)))
@ -321,9 +319,13 @@ pub fn parser<'src>(
)); ));
let logic_math_indexes_and_function_calls = atom.pratt(( let logic_math_indexes_and_function_calls = atom.pratt((
prefix(2, just(Token::Operator(Not)), |_, expression, span| { prefix(
2,
just(Token::Operator(Operator::Not)),
|_, expression, span| {
Expression::Logic(Box::new(Logic::Not(expression)).with_position(span)) Expression::Logic(Box::new(Logic::Not(expression)).with_position(span))
}), },
),
postfix( postfix(
2, 2,
expression.clone().delimited_by( expression.clone().delimited_by(
@ -375,14 +377,14 @@ pub fn parser<'src>(
), ),
infix( infix(
left(1), left(1),
just(Token::Operator(Equal)), just(Token::Operator(Operator::Equal)),
|left, _, right, span| { |left, _, right, span| {
Expression::Logic(Box::new(Logic::Equal(left, right)).with_position(span)) Expression::Logic(Box::new(Logic::Equal(left, right)).with_position(span))
}, },
), ),
infix( infix(
left(1), left(1),
just(Token::Operator(NotEqual)), just(Token::Operator(Operator::NotEqual)),
|left, _, right, span| { |left, _, right, span| {
Expression::Logic( Expression::Logic(
Box::new(Logic::NotEqual(left, right)).with_position(span), Box::new(Logic::NotEqual(left, right)).with_position(span),
@ -391,21 +393,21 @@ pub fn parser<'src>(
), ),
infix( infix(
left(1), left(1),
just(Token::Operator(Greater)), just(Token::Operator(Operator::Greater)),
|left, _, right, span| { |left, _, right, span| {
Expression::Logic(Box::new(Logic::Greater(left, right)).with_position(span)) Expression::Logic(Box::new(Logic::Greater(left, right)).with_position(span))
}, },
), ),
infix( infix(
left(1), left(1),
just(Token::Operator(Less)), just(Token::Operator(Operator::Less)),
|left, _, right, span| { |left, _, right, span| {
Expression::Logic(Box::new(Logic::Less(left, right)).with_position(span)) Expression::Logic(Box::new(Logic::Less(left, right)).with_position(span))
}, },
), ),
infix( infix(
left(1), left(1),
just(Token::Operator(GreaterOrEqual)), just(Token::Operator(Operator::GreaterOrEqual)),
|left, _, right, span| { |left, _, right, span| {
Expression::Logic( Expression::Logic(
Box::new(Logic::GreaterOrEqual(left, right)).with_position(span), Box::new(Logic::GreaterOrEqual(left, right)).with_position(span),
@ -414,7 +416,7 @@ pub fn parser<'src>(
), ),
infix( infix(
left(1), left(1),
just(Token::Operator(LessOrEqual)), just(Token::Operator(Operator::LessOrEqual)),
|left, _, right, span| { |left, _, right, span| {
Expression::Logic( Expression::Logic(
Box::new(Logic::LessOrEqual(left, right)).with_position(span), Box::new(Logic::LessOrEqual(left, right)).with_position(span),
@ -423,49 +425,49 @@ pub fn parser<'src>(
), ),
infix( infix(
left(1), left(1),
just(Token::Operator(And)), just(Token::Operator(Operator::And)),
|left, _, right, span| { |left, _, right, span| {
Expression::Logic(Box::new(Logic::And(left, right)).with_position(span)) Expression::Logic(Box::new(Logic::And(left, right)).with_position(span))
}, },
), ),
infix( infix(
left(1), left(1),
just(Token::Operator(Or)), just(Token::Operator(Operator::Or)),
|left, _, right, span| { |left, _, right, span| {
Expression::Logic(Box::new(Logic::Or(left, right)).with_position(span)) Expression::Logic(Box::new(Logic::Or(left, right)).with_position(span))
}, },
), ),
infix( infix(
left(1), left(1),
just(Token::Operator(Add)), just(Token::Operator(Operator::Add)),
|left, _, right, span| { |left, _, right, span| {
Expression::Math(Box::new(Math::Add(left, right)).with_position(span)) Expression::Math(Box::new(Math::Add(left, right)).with_position(span))
}, },
), ),
infix( infix(
left(1), left(1),
just(Token::Operator(Subtract)), just(Token::Operator(Operator::Subtract)),
|left, _, right, span| { |left, _, right, span| {
Expression::Math(Box::new(Math::Subtract(left, right)).with_position(span)) Expression::Math(Box::new(Math::Subtract(left, right)).with_position(span))
}, },
), ),
infix( infix(
left(2), left(2),
just(Token::Operator(Multiply)), just(Token::Operator(Operator::Multiply)),
|left, _, right, span| { |left, _, right, span| {
Expression::Math(Box::new(Math::Multiply(left, right)).with_position(span)) Expression::Math(Box::new(Math::Multiply(left, right)).with_position(span))
}, },
), ),
infix( infix(
left(2), left(2),
just(Token::Operator(Divide)), just(Token::Operator(Operator::Divide)),
|left, _, right, span| { |left, _, right, span| {
Expression::Math(Box::new(Math::Divide(left, right)).with_position(span)) Expression::Math(Box::new(Math::Divide(left, right)).with_position(span))
}, },
), ),
infix( infix(
left(1), left(1),
just(Token::Operator(Modulo)), just(Token::Operator(Operator::Modulo)),
|left, _, right, span| { |left, _, right, span| {
Expression::Math(Box::new(Math::Modulo(left, right)).with_position(span)) Expression::Math(Box::new(Math::Modulo(left, right)).with_position(span))
}, },