-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #347 from CSML-by-Clevy/feat/while_loop
Feat/while loop
- Loading branch information
Showing
15 changed files
with
191 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
start: | ||
do var = 0 | ||
while (var < 5) { | ||
say var | ||
do var = var + 1 | ||
} | ||
|
||
goto end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod actions; | ||
mod for_loop; | ||
mod while_loop; | ||
mod if_statement; | ||
|
||
pub use actions::match_actions; | ||
pub use for_loop::for_loop; | ||
pub use while_loop::while_loop; | ||
pub use if_statement::{evaluate_condition, solve_if_statement}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
csml_interpreter/src/interpreter/ast_interpreter/while_loop.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::data::{ | ||
ast::*, | ||
// hold::{ | ||
// hold_index_end_loop, hold_index_start_loop, hold_loop_decrs_index, hold_loop_incrs_index, | ||
// }, | ||
// primitive::tools::get_array, | ||
Data, MessageData, MSG, | ||
}; | ||
use crate::error_format::*; | ||
use crate::interpreter::{ | ||
ast_interpreter::if_statement::valid_condition, | ||
interpret_scope | ||
}; | ||
use crate::parser::ExitCondition; | ||
use std::sync::mpsc; | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// PUBLIC FUNCTION | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
pub fn while_loop( | ||
cond: &Expr, | ||
block: &Block, | ||
_range_interval: &Interval, | ||
mut msg_data: MessageData, | ||
data: &mut Data, | ||
sender: &Option<mpsc::Sender<MSG>>, | ||
) -> Result<MessageData, ErrorInfo> { | ||
// let literal = expr_to_literal(expr, false, None, data, &mut msg_data, sender)?; | ||
|
||
// TODO: hold | ||
// let mut array = get_array(literal, &data.context.flow, ERROR_FOREACH.to_owned())?; | ||
// let mut skip_value = 0; | ||
// let array = hold_index_start_loop(data, &mut array, &mut skip_value); | ||
|
||
while valid_condition(cond, data, &mut msg_data, sender) { | ||
// data.step_vars | ||
// .insert(ident.ident.to_owned(), elem.to_owned()); | ||
// if let Some(index) = index { | ||
// data.step_vars.insert( | ||
// index.ident.to_owned(), | ||
// PrimitiveInt::get_literal(for_loop_index as i64, elem.interval.to_owned()), | ||
// ); | ||
// }; | ||
|
||
// hold_loop_incrs_index(data, for_loop_index + skip_value); | ||
msg_data = msg_data + interpret_scope(block, data, sender)?; | ||
// hold_loop_decrs_index(data); | ||
|
||
|
||
match msg_data.exit_condition { | ||
Some(ExitCondition::Break) => { | ||
msg_data.exit_condition = None; | ||
break; | ||
} | ||
Some(ExitCondition::Continue) => msg_data.exit_condition = None, | ||
Some(_) => break, | ||
None => {} | ||
} | ||
} | ||
|
||
// hold_index_end_loop(data); | ||
// data.step_vars.remove(&ident.ident); | ||
// if let Some(index) = index { | ||
// data.step_vars.remove(&index.ident); | ||
// }; | ||
Ok(msg_data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use crate::data::{ | ||
ast::{Expr}, | ||
tokens::{Span, WHILE, L_PAREN, R_PAREN}, | ||
}; | ||
use crate::parser::operator::parse_operator; | ||
use crate::parser::{ | ||
parse_comments::comment, | ||
parse_scope::parse_scope, | ||
tools::{get_interval}, | ||
}; | ||
use nom::{ | ||
bytes::complete::tag, | ||
combinator::{cut}, | ||
error::ParseError, | ||
sequence::preceded, | ||
*, | ||
}; | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// PUBLIC FUNCTION | ||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
pub fn parse_while<'a, E>(s: Span<'a>) -> IResult<Span<'a>, Expr, E> | ||
where | ||
E: ParseError<Span<'a>>, | ||
{ | ||
let (s, _) = preceded(comment, tag(WHILE))(s)?; | ||
let (s, mut interval) = get_interval(s)?; | ||
|
||
let (s, _) = cut(preceded(comment, tag(L_PAREN)))(s)?; | ||
let (s, expr) = cut(parse_operator)(s)?; | ||
let (s, _) = cut(preceded(comment, tag(R_PAREN)))(s)?; | ||
|
||
let (s, block) = parse_scope(s)?; | ||
let (s, end) = get_interval(s)?; | ||
interval.add_end(end); | ||
|
||
Ok(( | ||
s, | ||
Expr::WhileExpr(Box::new(expr), block, interval), | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
mod support; | ||
|
||
use csml_interpreter::data::context::Context; | ||
use csml_interpreter::data::event::Event; | ||
use std::collections::HashMap; | ||
|
||
use crate::support::tools::format_message; | ||
use crate::support::tools::message_to_json_value; | ||
|
||
use serde_json::Value; | ||
|
||
#[test] | ||
fn ok_while_loop() { | ||
let data = | ||
r#" | ||
{ | ||
"messages":[ | ||
{"content":{ "text": "0" },"content_type":"text"}, | ||
{"content":{ "text": "1" },"content_type":"text"}, | ||
{"content":{ "text": "2" },"content_type":"text"}, | ||
{"content":{ "text": "3" },"content_type":"text"}, | ||
{"content":{ "text": "4" },"content_type":"text"} | ||
],"memories":[] | ||
} | ||
"#; | ||
let msg = format_message( | ||
Event::new("payload", "", serde_json::json!({})), | ||
Context::new(HashMap::new(), HashMap::new(), None, None, "start", "flow"), | ||
"CSML/basic_test/while_loops.csml", | ||
); | ||
|
||
let v1: Value = message_to_json_value(msg); | ||
let v2: Value = serde_json::from_str(data).unwrap(); | ||
|
||
assert_eq!(v1, v2) | ||
} |