Skip to content

Commit

Permalink
add support for for ... with f, stop, initial { ... }
Browse files Browse the repository at this point in the history
  • Loading branch information
Maiori44 committed Dec 8, 2023
1 parent 55a943b commit 776f2dd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
15 changes: 12 additions & 3 deletions core/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,21 +642,30 @@ impl<'a> Compiler<'a> {
}
FOR_FUNC_LOOP {
iterators,
expr,
exprs: (expr, stop, initial),
code,
line,
} => {
let expr = self.compile_expression(scope, expr)?;
let iterators = self.compile_identifiers(iterators)?;
let debug = self.compile_debug_line(line, scope, true);
let code = self.compile_code_block(scope, "do", code)?;
let code = self.compile_code_block(scope, " do", code)?;
format_clue!(
debug,
"for ",
iterators,
" in ",
expr,
" ",
if let Some(stop) = stop {
String::from(", ") + &self.compile_expression(scope, stop)?
} else {
String::new()
},
if let Some(initial) = initial {
String::from(", ") + &self.compile_expression(scope, initial)?
} else {
String::new()
},
code,
debug,
"end",
Expand Down
36 changes: 27 additions & 9 deletions core/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ pub enum ComplexToken {
/// The iterator of the for loop.
iterators: Vec<String>,

/// The expression of the for loop
/// The expression(s) of the for loop
/// for..in loops: for k,v in pairs(t) do end
/// for..of loops: for k,v in ipairs(t) do end
/// for..with loops: for k,v in custom_iter(t) do end
expr: Expression,
/// for..with loops: for k,v in custom_iter[, stop[, initial]] do end
exprs: (Expression, Option<Expression>, Option<Expression>),

/// The code block of the for loop.
code: CodeBlock,
Expand Down Expand Up @@ -2268,15 +2268,15 @@ impl<'a> ParserInfo<'a> {
})
} else {
let iterators = self.build_identifier_list();
let expr = match self.advance().kind() {
let exprs = match self.advance().kind() {
OF => {
let mut expr = vec_deque![SYMBOL(String::from("pairs("))];
expr.append(&mut self.build_expression(
Some((CURLY_BRACKET_OPEN, "{")),
Some("Missing table to iterate")
));
expr.push_back(SYMBOL(String::from(")")));
expr
(expr, None, None)
}
IN => {
let mut expr = vec_deque![SYMBOL(String::from("ipairs("))];
Expand All @@ -2285,9 +2285,27 @@ impl<'a> ParserInfo<'a> {
Some("Missing array to iterate")
));
expr.push_back(SYMBOL(String::from(")")));
expr
(expr, None, None)
}
WITH => self.build_expression(Some((CURLY_BRACKET_OPEN, "{")), Some("Missing iterator")),
WITH => {
let expr = self.build_expression(None, Some("Missing iterator"));
self.current -= 1;
if self.advance_if(COMMA) {
let stop = self.build_expression(None, Some("Missing stop value"));
self.current -= 1;
if self.advance_if(COMMA) {
let initial = self.build_expression(
Some((CURLY_BRACKET_OPEN, "{")),
Some("Missing initial value")
);
(expr, Some(stop), Some(initial))
} else {
(expr, Some(stop), None)
}
} else {
(expr, None, None)
}
},
_ => {
let t = self.peek(0);
self.expected(
Expand All @@ -2298,13 +2316,13 @@ impl<'a> ParserInfo<'a> {
t.range(),
None
);
Expression::new()
(Expression::new(), None, None)
}
};
let code = self.build_loop_block();
self.expr.push_back(FOR_FUNC_LOOP {
iterators,
expr,
exprs,
code,
line,
});
Expand Down

0 comments on commit 776f2dd

Please sign in to comment.