1
0

Begin adding add-assign and subtract-assign

This commit is contained in:
Jeff 2024-03-08 12:39:35 -05:00
parent d99ebc0a44
commit f70c8f2b40
3 changed files with 40 additions and 6 deletions

View File

@ -9,14 +9,28 @@ use super::{AbstractTree, Action, Identifier, Statement, Type};
pub struct Assignment<'src> { pub struct Assignment<'src> {
identifier: Identifier, identifier: Identifier,
r#type: Option<Type>, r#type: Option<Type>,
operator: AssignmentOperator,
statement: Box<Statement<'src>>, statement: Box<Statement<'src>>,
} }
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub enum AssignmentOperator {
Assign,
AddAssign,
SubAssign,
}
impl<'src> Assignment<'src> { impl<'src> Assignment<'src> {
pub fn new(identifier: Identifier, r#type: Option<Type>, statement: Statement<'src>) -> Self { pub fn new(
identifier: Identifier,
r#type: Option<Type>,
operator: AssignmentOperator,
statement: Statement<'src>,
) -> Self {
Self { Self {
identifier, identifier,
r#type, r#type,
operator,
statement: Box::new(statement), statement: Box::new(statement),
} }
} }
@ -71,6 +85,7 @@ mod tests {
Assignment::new( Assignment::new(
Identifier::new("foobar"), Identifier::new("foobar"),
None, None,
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(ValueNode::Integer(42))), Statement::Expression(Expression::Value(ValueNode::Integer(42))),
) )
.run(&context) .run(&context)
@ -87,6 +102,7 @@ mod tests {
let validation = Assignment::new( let validation = Assignment::new(
Identifier::new("foobar"), Identifier::new("foobar"),
Some(Type::Boolean), Some(Type::Boolean),
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(ValueNode::Integer(42))), Statement::Expression(Expression::Value(ValueNode::Integer(42))),
) )
.validate(&Context::new()); .validate(&Context::new());

View File

@ -11,8 +11,16 @@ pub mod r#type;
pub mod value_node; pub mod value_node;
pub use self::{ pub use self::{
assignment::Assignment, block::Block, expression::Expression, identifier::Identifier, assignment::{Assignment, AssignmentOperator},
index::Index, logic::Logic, math::Math, r#loop::Loop, r#type::Type, statement::Statement, block::Block,
expression::Expression,
identifier::Identifier,
index::Index,
logic::Logic,
math::Math,
r#loop::Loop,
r#type::Type,
statement::Statement,
value_node::ValueNode, value_node::ValueNode,
}; };

View File

@ -222,10 +222,14 @@ pub fn parser<'src>() -> DustParser<'src> {
let assignment = identifier let assignment = identifier
.then(type_specification.clone().or_not()) .then(type_specification.clone().or_not())
.then_ignore(just(Token::Operator(Operator::Assign))) .then(choice((
just(Token::Operator(Operator::Assign)).to(AssignmentOperator::Assign),
just(Token::Operator(Operator::AddAssign)).to(AssignmentOperator::AddAssign),
just(Token::Operator(Operator::SubAssign)).to(AssignmentOperator::SubAssign),
)))
.then(statement.clone()) .then(statement.clone())
.map(|((identifier, r#type), statement)| { .map(|(((identifier, r#type), operator), statement)| {
Statement::Assignment(Assignment::new(identifier, r#type, statement)) Statement::Assignment(Assignment::new(identifier, r#type, operator, statement))
}) })
.boxed(); .boxed();
@ -406,6 +410,7 @@ mod tests {
Statement::Assignment(Assignment::new( Statement::Assignment(Assignment::new(
Identifier::new("foobar"), Identifier::new("foobar"),
None, None,
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(ValueNode::Integer(1))) Statement::Expression(Expression::Value(ValueNode::Integer(1)))
)), )),
); );
@ -418,6 +423,7 @@ mod tests {
Statement::Assignment(Assignment::new( Statement::Assignment(Assignment::new(
Identifier::new("foobar"), Identifier::new("foobar"),
Some(Type::Integer), Some(Type::Integer),
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(ValueNode::Integer(1))) Statement::Expression(Expression::Value(ValueNode::Integer(1)))
)), )),
); );
@ -430,6 +436,7 @@ mod tests {
Statement::Assignment(Assignment::new( Statement::Assignment(Assignment::new(
Identifier::new("foobar"), Identifier::new("foobar"),
Some(Type::Custom(Identifier::new("Foo"))), Some(Type::Custom(Identifier::new("Foo"))),
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(ValueNode::Enum( Statement::Expression(Expression::Value(ValueNode::Enum(
Identifier::new("Foo"), Identifier::new("Foo"),
Identifier::new("Bar") Identifier::new("Bar")
@ -445,6 +452,7 @@ mod tests {
Statement::Assignment(Assignment::new( Statement::Assignment(Assignment::new(
Identifier::new("foobar"), Identifier::new("foobar"),
Some(Type::List), Some(Type::List),
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(ValueNode::List(vec![]))) Statement::Expression(Expression::Value(ValueNode::List(vec![])))
)), )),
); );
@ -454,6 +462,7 @@ mod tests {
Statement::Assignment(Assignment::new( Statement::Assignment(Assignment::new(
Identifier::new("foobar"), Identifier::new("foobar"),
Some(Type::ListOf(Box::new(Type::Integer))), Some(Type::ListOf(Box::new(Type::Integer))),
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(ValueNode::List(vec![]))) Statement::Expression(Expression::Value(ValueNode::List(vec![])))
)), )),
); );
@ -463,6 +472,7 @@ mod tests {
Statement::Assignment(Assignment::new( Statement::Assignment(Assignment::new(
Identifier::new("foobar"), Identifier::new("foobar"),
Some(Type::ListExact(vec![Type::Integer, Type::String])), Some(Type::ListExact(vec![Type::Integer, Type::String])),
AssignmentOperator::Assign,
Statement::Expression(Expression::Value(ValueNode::List(vec![ Statement::Expression(Expression::Value(ValueNode::List(vec![
Expression::Value(ValueNode::Integer(42)), Expression::Value(ValueNode::Integer(42)),
Expression::Value(ValueNode::String("foo")) Expression::Value(ValueNode::String("foo"))