Skip to content

Commit

Permalink
Break and continue (#18)
Browse files Browse the repository at this point in the history
# Pull Request

<!-- Provide a general summary of your changes in the title above -->
<!-- Optional fields can be removed if not applicable -->

## Description

Add `break` and `continue` syntax to the compiler. The interpreter shall
no receive updates for now.

<!-- Briefly describe the purpose of this pull request. -->

## Changes Made

<!-- Summarize the changes you've made in this pull request. -->

`break` allows us to exit a loo and `continue` to skip the rest of the
loop and go back to the `while` condition. They compile just to `Jump`
instructions.

## Related Issue <!-- Optional -->

<!-- Link to the related issue, e.g., "Closes #123" or "Fixes #456" -->
Closes #4 


## Checklist

- [x] I have self-reviewed my code
- [x] Code follows project's style guidelines
- [x] Tests added and passing
- [x] Documentation updated (if needed)
  • Loading branch information
Yag000 authored Aug 29, 2023
2 parents 86deca0 + 7004800 commit e35ca6a
Show file tree
Hide file tree
Showing 13 changed files with 655 additions and 66 deletions.
15 changes: 14 additions & 1 deletion docs/MONKEY.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ if (a == 1) {

### Loops

While loops have been implemented, but for now keywords such as `break` and `continue` have not yet been implemented.
While loops have been implemented.

```monkey
let a = 1;
Expand All @@ -232,6 +232,19 @@ while (a < 4) {
}
```

You can also use `break` and `continue` inside a loop.

```monkey
let a = 1;
while (a < 4) {
if (a == 2) {
break;
}
puts(a);
let a = a + 1;
}
```

## Comments

For now comments are not supported ( not a huge loss :) )
Expand Down
141 changes: 130 additions & 11 deletions src/compiler/compiler_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1191,17 +1191,6 @@ pub mod tests {

#[test]
fn test_while_statements() {
let input = r#"
while (true){
("yes");
}
"#
.to_string();

println!("{input}");
println!("{:?}", parse(&input));
println!("{}", parse(&input));

let tests = vec![CompilerTestCase {
input: r#"
while (true){
Expand All @@ -1224,4 +1213,134 @@ pub mod tests {

run_compiler(tests);
}

#[test]
fn test_break_in_while() {
let tests = vec![CompilerTestCase {
input: r#"
while (true){
break;
}
"#
.to_string(),
expected_constants: vec![],
expected_instructions: flatten_instructions(vec![
Opcode::True.make(vec![]), // 000
Opcode::JumpNotTruthy.make(vec![10]), // 001
Opcode::Jump.make(vec![10]), // 004
Opcode::Jump.make(vec![0]), // 007
// 010
]),
}];

run_compiler(tests);
}

#[test]
fn test_nested_breaks_in_while() {
let tests = vec![CompilerTestCase {
input: r#"
while (true){
while (true){
break;
}
break;
}
"#
.to_string(),
expected_constants: vec![],
expected_instructions: flatten_instructions(vec![
Opcode::True.make(vec![]), // 000
Opcode::JumpNotTruthy.make(vec![20]), // 001
Opcode::True.make(vec![]), // 004
Opcode::JumpNotTruthy.make(vec![14]), // 005
Opcode::Jump.make(vec![14]), // 008
Opcode::Jump.make(vec![4]), // 011
Opcode::Jump.make(vec![20]), // 014
Opcode::Jump.make(vec![0]), // 017
// 020
]),
}];

run_compiler(tests);
}
#[test]
fn test_continue_in_while() {
let tests = vec![CompilerTestCase {
input: r#"
while (true){
continue;
}
"#
.to_string(),
expected_constants: vec![],
expected_instructions: flatten_instructions(vec![
Opcode::True.make(vec![]), // 000
Opcode::JumpNotTruthy.make(vec![10]), // 001
Opcode::Jump.make(vec![0]), // 004
Opcode::Jump.make(vec![0]), // 007
// 010
]),
}];

run_compiler(tests);
}

#[test]
fn test_nested_continue_in_while() {
let tests = vec![CompilerTestCase {
input: r#"
while (true){
while (true){
continue;
}
continue;
}
"#
.to_string(),
expected_constants: vec![],
expected_instructions: flatten_instructions(vec![
Opcode::True.make(vec![]), // 000
Opcode::JumpNotTruthy.make(vec![20]), // 001
Opcode::True.make(vec![]), // 004
Opcode::JumpNotTruthy.make(vec![14]), // 005
Opcode::Jump.make(vec![4]), // 008
Opcode::Jump.make(vec![4]), // 011
Opcode::Jump.make(vec![0]), // 014
Opcode::Jump.make(vec![0]), // 017
// 020
]),
}];

run_compiler(tests);
}

#[test]
fn test_continue_and_break_in_while() {
let tests = vec![CompilerTestCase {
input: r#"
while (true){
while (true){
continue;
}
break;
}
"#
.to_string(),
expected_constants: vec![],
expected_instructions: flatten_instructions(vec![
Opcode::True.make(vec![]), // 000
Opcode::JumpNotTruthy.make(vec![20]), // 001
Opcode::True.make(vec![]), // 004
Opcode::JumpNotTruthy.make(vec![14]), // 005
Opcode::Jump.make(vec![4]), // 008
Opcode::Jump.make(vec![4]), // 011
Opcode::Jump.make(vec![20]), // 014
Opcode::Jump.make(vec![0]), // 017
// 020
]),
}];

run_compiler(tests);
}
}
Loading

0 comments on commit e35ca6a

Please sign in to comment.