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

Commit c56ec68

Browse files
committed
benerin nested loop
1 parent e3f9afe commit c56ec68

File tree

4 files changed

+50
-35
lines changed

4 files changed

+50
-35
lines changed

example/hello.vuck

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ k108Pj
1212
k100Pj
1313
k33Pj
1414
k10Pj
15+
k3
16+
1517
:q

example/loop.vuck

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
k3
1+
k1
22
,
3-
k69420p
4-
k10P
5-
jj
6-
k1-
3+
k48Pk10Pjjk1-
74
F
85
j
9-
k10P
10-
j
116

12-
k3
7+
k2
138
,
149
k2
1510
,
16-
k12345p
17-
k10P
18-
jj
11+
k2
12+
,
13+
k12345p
14+
k10P
15+
jj
16+
k1-
17+
F
18+
j
1919
k1-
2020
F
21+
j
2122
k1-
2223
F
2324
j

src/interpreter/runner/executor.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ impl Runner {
8080
let (first, second) = {
8181
if self.stack.len() < 2 {
8282
self.error(tok, "Jumlah operand di stack kurang");
83+
return;
8384
}
8485
let right = self.stack.pop().unwrap();
8586
let left = self.stack.pop().unwrap();
@@ -99,12 +100,32 @@ impl Runner {
99100
}
100101

101102
pub fn get_loop_end_idx(&mut self, toks: &[Token], start_idx: usize) -> usize {
102-
// cari ujung loopnya dlu di mana
103103
let mut i = start_idx;
104104
let rest_of_code = &toks[start_idx..];
105+
let mut current_depth = 0;
106+
105107
for tok in rest_of_code {
106108
i += 1;
107109
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 {
108129
break;
109130
}
110131
}

src/interpreter/runner/mod.rs

Lines changed: 14 additions & 23 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_loop: isize,
1716
count_conditional: isize,
1817
}
1918

@@ -32,16 +31,10 @@ impl Runner {
3231
return Ok(()); // kaborr
3332
}
3433

35-
{
36-
// println!("Token [");
37-
// for (i, tok) in toks.into_iter().enumerate() {
38-
// println!("\t{}: \t{:?}", i, tok);
39-
// }
40-
// println!("]");
41-
}
42-
4334
// run
44-
self.the_actual_runner_lol(toks)
35+
let _ = self.the_actual_runner_lol(toks);
36+
37+
Ok(())
4538
}
4639

4740
fn the_actual_runner_lol(&mut self, toks: &[Token]) -> io::Result<()> {
@@ -75,32 +68,31 @@ impl Runner {
7568
}
7669

7770
// cari akhir di mana
78-
// eksekusi sampe stack-nya 0
7971
let start_idx = i + 1;
8072
let end_idx = self.get_loop_end_idx(toks, start_idx);
8173

82-
self.count_loop += 1;
83-
84-
let banyak_muter_nya = self.stack[self.ptr_idx as usize];
85-
for _ in 0..banyak_muter_nya {
74+
// nge-run kode loop
75+
// eksekusi sampe stack-nya 0
76+
while !self.is_at_loop_end() {
8677
let _ = self.the_actual_runner_lol(&toks[(start_idx - 1)..end_idx]);
8778
}
8879

89-
// skip to end
80+
// skip to loop end
9081
let mut tmp_tok = tok_iter.next().unwrap();
91-
while *tmp_tok.get_tok_type() != TokenType::LoopEnd {
82+
while *tmp_tok.get_tok_type() != TokenType::LoopEnd || i != (end_idx - 1) {
9283
tmp_tok = tok_iter.next().unwrap();
9384
i += 1;
9485
}
86+
i += 1;
9587
}
9688
TokenType::LoopEnd => {
97-
if self.count_loop > 0 && self.is_at_loop_end() {
98-
self.count_loop -= 1;
89+
if self.is_at_loop_end() {
90+
return Ok(());
9991
}
10092
}
101-
TokenType::ConditionalStart => todo!(),
102-
TokenType::ConditionalEnd => todo!(),
103-
TokenType::ConditionalElse => todo!(),
93+
TokenType::ConditionalStart => println!("Masuk ke conditional"),
94+
TokenType::ConditionalElse => println!("Branch else"),
95+
TokenType::ConditionalEnd => println!("Akhir conditional"),
10496
TokenType::Number => self.error(tok, "Kok tiba-tiba angka, mas/mba!"),
10597
}
10698

@@ -116,7 +108,6 @@ pub fn run(source: &str) -> io::Result<()> {
116108
stack: vec![],
117109
ptr_idx: -1,
118110
had_error: HAD_ERROR.load(Ordering::SeqCst),
119-
count_loop: 0,
120111
count_conditional: 0,
121112
};
122113

0 commit comments

Comments
 (0)