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

Commit 0515d4f

Browse files
committed
conditional :D
1 parent dc52feb commit 0515d4f

File tree

10 files changed

+75
-21
lines changed

10 files changed

+75
-21
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "vuck"
3-
version = "0.1.0"
3+
version = "1.0.0"
44
authors = ["Josep Marcello <jspmarcello@live.com>"]
55
edition = "2018"
66

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Vuck
22

3+
## Apa itu Vuck? (Dalam bahasa Inggris)
4+
35
1. 1D, like brainfuck
46
1. The stack's elements are of `signed int` size (4 byte)
57
1. The stack's initially empty. Trying to access the stack/do operations on it will cause the
@@ -61,7 +63,7 @@
6163
T
6264
```
6365
64-
## Used characters
66+
### Rangkuman
6567
6668
| Character | Usage | notes |
6769
| ----------- | -------------------------------------------------- | ------------------------------------------------------------ |
@@ -83,3 +85,7 @@
8385
| `F` | the end of a loop block | goes back to `,` if the top of the stack is not 0 |
8486
| `\|` | the start of a conditional block | skips to `T` if the top of the stack is not 0 |
8587
| `T` | the end of a conditional block | |
88+
89+
## Pre-requisite
90+
91+
1. Rust

example/conditional.vuck

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
k0
2+
|
3+
p
4+
j
5+
k48
6+
k0
7+
|
8+
h
9+
p
10+
T
11+
T
12+
k10P
13+
j
14+
:q

example/hello.vuck

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ k108Pj
1212
k100Pj
1313
k33Pj
1414
k10Pj
15-
k3
1615

1716
:q

example/loop.vuck

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ k2
1919
k1-
2020
F
2121
j
22+
k48pk10Pjj
2223
k1-
2324
F
2425
j

src/interpreter/runner/helper.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl Runner {
1515
self.had_error = true;
1616
}
1717

18-
pub fn is_at_loop_end(&mut self) -> bool {
18+
pub fn is_at_loop_end(&self) -> bool {
1919
self.stack[self.ptr_idx as usize] == 0
2020
}
2121

@@ -40,16 +40,28 @@ impl Runner {
4040
i
4141
}
4242

43-
pub fn get_else_branch_idx(&mut self, toks: &[Token], start_idx: usize) -> usize {
43+
pub fn get_conditional_end_idx(&mut self, toks: &[Token], start_idx: usize) -> usize {
4444
let mut i = start_idx;
4545
let rest_of_code = &toks[start_idx..];
46+
let mut current_depth = 0;
47+
4648
for tok in rest_of_code {
4749
i += 1;
4850
if *tok.get_tok_type() == TokenType::ConditionalEnd {
49-
break;
51+
if current_depth == 0 {
52+
break;
53+
} else {
54+
current_depth -= 1;
55+
}
56+
} else if *tok.get_tok_type() == TokenType::ConditionalStart {
57+
current_depth += 1;
5058
}
5159
}
5260

5361
i
5462
}
63+
64+
pub fn should_skip_if(&self) -> bool {
65+
self.stack[self.ptr_idx as usize] != 0
66+
}
5567
}

src/interpreter/runner/mod.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,33 @@ impl Runner {
8989
return Ok(());
9090
}
9191
}
92-
TokenType::ConditionalStart => println!("Masuk ke conditional"),
93-
TokenType::ConditionalElse => println!("Branch else"),
92+
TokenType::ConditionalStart => {
93+
if self.is_stack_empty() {
94+
self.error(tok, "Tidak bisa memulai conditional jika stack kosong");
95+
return Ok(());
96+
}
97+
98+
// cari akhir di mana
99+
let start_idx = i + 1;
100+
let end_idx = self.get_conditional_end_idx(toks, start_idx);
101+
102+
if end_idx < start_idx {
103+
continue;
104+
}
105+
106+
if !self.should_skip_if() {
107+
let _ = self.the_actual_runner_lol(&toks[(start_idx - 1)..end_idx]);
108+
}
109+
110+
let mut tmp_tok = tok_iter.next().unwrap();
111+
while *tmp_tok.get_tok_type() != TokenType::ConditionalEnd || i != (end_idx - 1)
112+
{
113+
tmp_tok = tok_iter.next().unwrap();
114+
i += 1;
115+
}
116+
i += 1;
117+
}
94118
TokenType::ConditionalEnd => {
95-
println!("Akhir conditional");
96119
return Ok(());
97120
}
98121
TokenType::Number => self.error(tok, "Kok tiba-tiba angka, mas/mba!"),

src/interpreter/scanner.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,6 @@ impl Scanner {
104104
self.count_loop -= 1;
105105
self.add_token(TokenType::LoopEnd, None)
106106
}
107-
':' => {
108-
if self.peek() == 'q' {
109-
self.advance();
110-
self.has_seen_eof = true;
111-
self.add_token(TokenType::Eof, None)
112-
} else {
113-
Interpreter::error(self.line, self.column, "Karakter/perintah invalid");
114-
Ok(())
115-
}
116-
}
117107
'|' => {
118108
self.count_conditional += 1;
119109
self.add_token(TokenType::ConditionalStart, None)
@@ -128,6 +118,16 @@ impl Scanner {
128118
self.column = 1;
129119
Ok(())
130120
}
121+
':' => {
122+
if self.peek() == 'q' {
123+
self.advance();
124+
self.has_seen_eof = true;
125+
self.add_token(TokenType::Eof, None)
126+
} else {
127+
Interpreter::error(self.line, self.column, "Karakter/perintah invalid");
128+
Ok(())
129+
}
130+
}
131131
_ => {
132132
Interpreter::error(self.line, self.column, "Karakter/perintah invalid");
133133
Ok(())

src/interpreter/token/token_type.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ pub enum TokenType {
2828
// conditionals
2929
ConditionalStart,
3030
ConditionalEnd,
31-
ConditionalElse,
3231

3332
// literal
3433
Number,

0 commit comments

Comments
 (0)