Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add *=, /=, %= #426

Merged
merged 2 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions csml_interpreter/src/data/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ pub enum AssignType {
Assignment,
AdditionAssignment,
SubtractionAssignment,
MultiplicationAssignment,
DivisionAssignment,
RemainderAssignment,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
3 changes: 3 additions & 0 deletions csml_interpreter/src/data/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub const AND: &str = "&&";

pub const SUBTRACTION_ASSIGNMENT: &str = "-=";
pub const ADDITION_ASSIGNMENT: &str = "+=";
pub const MULTIPLY_ASSIGNMENT: &str = "*=";
pub const DIVISION_ASSIGNMENT: &str = "/=";
pub const REMAINDER_ASSIGNMENT: &str = "%=";

pub const GREATER_THAN_EQUAL: &str = ">=";
pub const LESS_THAN_EQUAL: &str = "<=";
Expand Down
89 changes: 38 additions & 51 deletions csml_interpreter/src/interpreter/ast_interpreter/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,62 +220,49 @@ pub fn match_actions(

let (lit, name, mem_type, path) = get_var_info(old, None, data, &mut msg_data, sender)?;

match assign_type {
let primitive = match assign_type {
AssignType::AdditionAssignment => {
let primitive = lit.primitive.clone() + new_value.primitive;

match primitive {
Ok(primitive) => {
new_value = Literal {
content_type: new_value.content_type,
interval: new_value.interval,
additional_info: None,
secure_variable: false,
primitive,
};
}
Err(err) => {
new_value = PrimitiveString::get_literal(&err, lit.interval);
MSG::send_error_msg(
&sender,
&mut msg_data,
Err(gen_error_info(
Position::new(new_value.interval, &new_scope_data.context.flow),
err,
)),
);
}
}
Some(lit.primitive.clone() + new_value.primitive.clone())
}
AssignType::SubtractionAssignment => {
let primitive = lit.primitive.clone() - new_value.primitive;

match primitive {
Ok(primitive) => {
new_value = Literal {
content_type: new_value.content_type,
interval: new_value.interval,
additional_info: None,
secure_variable: false,
primitive,
};
}
Err(err) => {
new_value = PrimitiveString::get_literal(&err, lit.interval);

MSG::send_error_msg(
&sender,
&mut msg_data,
Err(gen_error_info(
Position::new(new_value.interval, &new_scope_data.context.flow),
err,
)),
);
}
}
Some(lit.primitive.clone() - new_value.primitive.clone())
}
AssignType::DivisionAssignment => {
Some(lit.primitive.clone() / new_value.primitive.clone())
}
AssignType::MultiplicationAssignment => {
Some(lit.primitive.clone() * new_value.primitive.clone())
}
AssignType::RemainderAssignment => {
Some(lit.primitive.clone() % new_value.primitive.clone())
}
_ => {}
AssignType::Assignment => None,
};

match primitive {
Some(Ok(primitive)) => {
new_value = Literal {
content_type: new_value.content_type,
interval: new_value.interval,
additional_info: None,
secure_variable: false,
primitive,
};
}
Some(Err(err)) => {
new_value = PrimitiveString::get_literal(&err, lit.interval);
MSG::send_error_msg(
&sender,
&mut msg_data,
Err(gen_error_info(
Position::new(new_value.interval, &new_scope_data.context.flow),
err,
)),
);
}
None => {}
}

//TODO: refacto memory update system

let (new_value, update) = if let MemoryType::Constant = mem_type {
Expand Down
38 changes: 33 additions & 5 deletions csml_interpreter/src/parser/parse_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ where
Ok((rest, AssignType::SubtractionAssignment))
}

fn division_assignment<'a, E>(s: Span<'a>) -> IResult<Span<'a>, AssignType, E>
where
E: ParseError<Span<'a>> + ContextError<Span<'a>>,
{
let (rest, ..) = tag(DIVISION_ASSIGNMENT)(s)?;
Ok((rest, AssignType::DivisionAssignment))
}

fn multiplication_assignment<'a, E>(s: Span<'a>) -> IResult<Span<'a>, AssignType, E>
where
E: ParseError<Span<'a>> + ContextError<Span<'a>>,
{
let (rest, ..) = tag(MULTIPLY_ASSIGNMENT)(s)?;
Ok((rest, AssignType::MultiplicationAssignment))
}

fn remainder_assignment<'a, E>(s: Span<'a>) -> IResult<Span<'a>, AssignType, E>
where
E: ParseError<Span<'a>> + ContextError<Span<'a>>,
{
let (rest, ..) = tag(REMAINDER_ASSIGNMENT)(s)?;
Ok((rest, AssignType::RemainderAssignment))
}

fn assignment<'a, E>(s: Span<'a>) -> IResult<Span<'a>, AssignType, E>
where
E: ParseError<Span<'a>> + ContextError<Span<'a>>,
Expand Down Expand Up @@ -74,7 +98,14 @@ where

let (s, assign_type) = preceded(
comment,
alt((addition_assignment, subtraction_assignment, assignment)),
alt((
remainder_assignment,
division_assignment,
multiplication_assignment,
addition_assignment,
subtraction_assignment,
assignment,
)),
)(s)?;
let (s, expr) = preceded(comment, parse_operator)(s)?;

Expand Down Expand Up @@ -338,10 +369,7 @@ where

let (s, ..) = get_tag(name, HOLD_SECURE)(s)?;

Ok((
s,
Expr::ObjectExpr(ObjectType::HoldSecure(inter)),
))
Ok((s, Expr::ObjectExpr(ObjectType::HoldSecure(inter))))
}

fn parse_break<'a, E>(s: Span<'a>) -> IResult<Span<'a>, Expr, E>
Expand Down