Skip to content
This repository was archived by the owner on Jul 12, 2021. It is now read-only.

Commit dc52feb

Browse files
committed
clean up, simplify conditionals
1 parent c56ec68 commit dc52feb

File tree

5 files changed

+44
-91
lines changed

5 files changed

+44
-91
lines changed

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,13 @@
5353
1. Conditionals:
5454
1. Mark the start of the conditional with:
5555
```
56-
.
56+
|
5757
```
5858
1. Mark the end of the conditional with:
59+
5960
```
6061
T
6162
```
62-
1. Mark the else branch with:
63-
```
64-
|
65-
```
6663
6764
## Used characters
6865
@@ -84,6 +81,5 @@
8481
| `P` | prints the ASCII character of the top of the stack | Resets the pointer |
8582
| `,` | the start of a loop block | |
8683
| `F` | the end of a loop block | goes back to `,` if the top of the stack is not 0 |
87-
| `.` | the start of a conditional block | skips to `\|` if the top of the stack is not 0 |
84+
| `\|` | the start of a conditional block | skips to `T` if the top of the stack is not 0 |
8885
| `T` | the end of a conditional block | |
89-
| `\|` | the else arm of a conditional block | |

src/interpreter/runner/executor.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{Runner, Token, TokenType};
1+
use super::{Runner, Token};
22
use std::{io, usize};
33

44
#[allow(unused_macros)]
@@ -98,38 +98,4 @@ impl Runner {
9898
self.stack.push(res);
9999
self.reset_pointer();
100100
}
101-
102-
pub fn get_loop_end_idx(&mut self, toks: &[Token], start_idx: usize) -> usize {
103-
let mut i = start_idx;
104-
let rest_of_code = &toks[start_idx..];
105-
let mut current_depth = 0;
106-
107-
for tok in rest_of_code {
108-
i += 1;
109-
if *tok.get_tok_type() == TokenType::LoopEnd {
110-
if current_depth == 0 {
111-
break;
112-
} else {
113-
current_depth -= 1;
114-
}
115-
} else if *tok.get_tok_type() == TokenType::LoopStart {
116-
current_depth += 1;
117-
}
118-
}
119-
120-
i
121-
}
122-
123-
pub fn get_else_branch_idx(&mut self, toks: &[Token], start_idx: usize) -> usize {
124-
let mut i = start_idx;
125-
let rest_of_code = &toks[start_idx..];
126-
for tok in rest_of_code {
127-
i += 1;
128-
if *tok.get_tok_type() == TokenType::ConditionalEnd {
129-
break;
130-
}
131-
}
132-
133-
i
134-
}
135101
}

src/interpreter/runner/helper.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{Runner, Token};
1+
use super::{Runner, Token, TokenType};
22
use crate::Interpreter;
33

44
impl Runner {
@@ -18,4 +18,38 @@ impl Runner {
1818
pub fn is_at_loop_end(&mut self) -> bool {
1919
self.stack[self.ptr_idx as usize] == 0
2020
}
21+
22+
pub fn get_loop_end_idx(&mut self, toks: &[Token], start_idx: usize) -> usize {
23+
let mut i = start_idx;
24+
let rest_of_code = &toks[start_idx..];
25+
let mut current_depth = 0;
26+
27+
for tok in rest_of_code {
28+
i += 1;
29+
if *tok.get_tok_type() == TokenType::LoopEnd {
30+
if current_depth == 0 {
31+
break;
32+
} else {
33+
current_depth -= 1;
34+
}
35+
} else if *tok.get_tok_type() == TokenType::LoopStart {
36+
current_depth += 1;
37+
}
38+
}
39+
40+
i
41+
}
42+
43+
pub fn get_else_branch_idx(&mut self, toks: &[Token], start_idx: usize) -> usize {
44+
let mut i = start_idx;
45+
let rest_of_code = &toks[start_idx..];
46+
for tok in rest_of_code {
47+
i += 1;
48+
if *tok.get_tok_type() == TokenType::ConditionalEnd {
49+
break;
50+
}
51+
}
52+
53+
i
54+
}
2155
}

src/interpreter/runner/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ struct Runner {
1313
stack: Vec<i32>,
1414
ptr_idx: i32,
1515
had_error: bool,
16-
count_conditional: isize,
1716
}
1817

1918
impl Runner {
@@ -92,7 +91,10 @@ impl Runner {
9291
}
9392
TokenType::ConditionalStart => println!("Masuk ke conditional"),
9493
TokenType::ConditionalElse => println!("Branch else"),
95-
TokenType::ConditionalEnd => println!("Akhir conditional"),
94+
TokenType::ConditionalEnd => {
95+
println!("Akhir conditional");
96+
return Ok(());
97+
}
9698
TokenType::Number => self.error(tok, "Kok tiba-tiba angka, mas/mba!"),
9799
}
98100

@@ -108,7 +110,6 @@ pub fn run(source: &str) -> io::Result<()> {
108110
stack: vec![],
109111
ptr_idx: -1,
110112
had_error: HAD_ERROR.load(Ordering::SeqCst),
111-
count_conditional: 0,
112113
};
113114

114115
runner.run(source)

src/interpreter/scanner.rs

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,10 @@ pub struct Scanner {
2020
has_seen_eof: bool,
2121

2222
// count loop and conditional. Should be zero at the end of parsing
23-
// in_loop: bool,
24-
// in_conditional: bool,
2523
count_loop: isize,
2624
count_conditional: isize,
2725
}
2826

29-
// const RESERVED_CHARACTERS: &[char] = &[
30-
// 'l', 'h', 'j', 'k', '+', '-', '*', '/', '%', 'i', 'I', 'p', 'P', ':', 'q', ',', 'F', '.', 'T',
31-
// '|', ' ', '\t', '\r', '\n',
32-
// ];
33-
3427
impl Scanner {
3528
pub fn new(source: &str) -> Self {
3629
Self {
@@ -41,8 +34,6 @@ impl Scanner {
4134
line: 1,
4235
column: 0,
4336
has_seen_eof: false,
44-
// in_loop: false,
45-
// in_conditional: false,
4637
count_conditional: 0,
4738
count_loop: 0,
4839
}
@@ -106,24 +97,10 @@ impl Scanner {
10697
'p' => self.add_token(TokenType::WriteNumber, None),
10798
'P' => self.add_token(TokenType::WriteAscii, None),
10899
',' => {
109-
// if self.in_loop {
110-
// Interpreter::error(
111-
// self.line,
112-
// self.column,
113-
// "Tidak boleh ada loop di dalam loop",
114-
// );
115-
// return Ok(());
116-
// }
117-
// self.in_loop = true;
118100
self.count_loop += 1;
119101
self.add_token(TokenType::LoopStart, None)
120102
}
121103
'F' => {
122-
// if !self.in_loop {
123-
// Interpreter::error(self.line, self.column, "Sedang tidak ada di dalam loop");
124-
// return Ok(());
125-
// }
126-
// self.in_loop = false;
127104
self.count_loop -= 1;
128105
self.add_token(TokenType::LoopEnd, None)
129106
}
@@ -137,35 +114,14 @@ impl Scanner {
137114
Ok(())
138115
}
139116
}
140-
'.' => {
141-
// if self.in_conditional {
142-
// Interpreter::error(
143-
// self.line,
144-
// self.column,
145-
// "Tidak boleh ada conditonal di dalam conditional",
146-
// );
147-
// return Ok(());
148-
// }
149-
// self.in_conditional = true;
117+
'|' => {
150118
self.count_conditional += 1;
151119
self.add_token(TokenType::ConditionalStart, None)
152120
}
153121
'T' => {
154-
// if !self.in_conditional {
155-
// Interpreter::error(self.line, self.column, "Tidak sedang di dalam conditonal");
156-
// return Ok(());
157-
// }
158-
// self.in_conditional = false;
159122
self.count_conditional -= 1;
160123
self.add_token(TokenType::ConditionalEnd, None)
161124
}
162-
'|' => {
163-
if self.count_conditional == 0 {
164-
Interpreter::error(self.line, self.column, "Tidak sedang di dalam conditional");
165-
return Ok(());
166-
}
167-
self.add_token(TokenType::ConditionalElse, None)
168-
}
169125
' ' | '\t' | '\r' => Ok(()),
170126
'\n' => {
171127
self.line += 1;

0 commit comments

Comments
 (0)