Skip to content

Commit 62376a7

Browse files
committed
IF works! Turing complete!
1 parent 15941d4 commit 62376a7

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
2-
untracked_assets
2+
untracked_assets
3+
.DS_Store

LANGUAGE_DOC.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
### Logic (uninplemented)
1616

17-
`IF [x] [loc] (antiLoc)` - he current location will jump to the execution point `loc` if the value of `x` is `1`. If `x` is `0`, it will continue reading OR (if `antiLoc` is passed), jump to the execution point `antiLoc`.
17+
`IF [x] [loc] [antiLoc]` - The current location will jump to the execution point `loc` if the value of `x` is `1`. If `x` is `0`, it will jump to the execution point `antiLoc`.
1818

1919
- `WBOOL ([a] (c) [b]) [loc]` - Runs the opperation `b` on `a` (and if not `NOT` `c`). Writes the value of that to the location `loc`. For example:
2020

@@ -33,7 +33,7 @@ To use memory points use the `{[x]}` in-line command.
3333

3434
#### Logic example
3535

36-
`IF {1} AND {2} 1 2`
36+
`IF 3 1 2`
3737
Explaination: Get the value at `1` and `2`, run `AND` on them. If the result is `1`, jump to execution point `1`, if the result is `0`, jump to execution point `2`.
3838

3939
#### Nesting logic

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
![Turing Complete Status](https://img.shields.io/badge/turing%20complete-mostly-yellow?style=for-the-badge)
1+
![Turing Complete Status](https://img.shields.io/badge/turing%20complete-complete-green?style=for-the-badge)
22

33
![Turing Logo](assets/turing_fully_transparent_white_logo.png)
44

src/main.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ fn main() {
2323
let jump_regex: Regex = Regex::new(r"(?<=JUMP )\d*").unwrap();
2424
let write_current_regex: Regex = Regex::new(r"(?<=WRITE )\d*").unwrap();
2525
let write_selected_regex: Regex = Regex::new(r"(?<=WRITE (0|1) )\d*").unwrap();
26+
// If
27+
let if_first_param_regex: Regex = Regex::new(r"(?<=IF )\d*").unwrap();
28+
let if_second_param_regex: Regex = Regex::new(r"(?<=\d )\d*").unwrap();
29+
let if_third_param_regex: Regex = Regex::new(r"\d+(?! )").unwrap();
2630

2731
// Booleans
2832
let boolean_multiparam_second_param_regex: Regex =
@@ -196,6 +200,29 @@ fn main() {
196200
let value = !first_value;
197201
memory_line[final_param] = value;
198202
}
203+
} else if line.contains(&"IF") {
204+
let memory_value_string = if_first_param_regex.find(&line).unwrap().unwrap().as_str();
205+
let memory_value = memory_value_string.parse::<usize>().unwrap();
206+
let byte = memory_line[memory_value];
207+
if byte == true {
208+
let jump_index_string =
209+
if_second_param_regex.find(&line).unwrap().unwrap().as_str();
210+
let jump_index = jump_index_string.parse::<usize>().unwrap();
211+
current_line = jump_list[jump_index];
212+
} else if byte == false {
213+
if if_third_param_regex.is_match(&line).unwrap() {
214+
let jump_index_string =
215+
if_third_param_regex.find(&line).unwrap().unwrap().as_str();
216+
let jump_index = jump_index_string.parse::<usize>().unwrap();
217+
218+
current_line = jump_list[jump_index];
219+
}
220+
} else {
221+
panic!(
222+
"The value of position {} in not a byte. This MAY be a problem with your code.",
223+
memory_value
224+
);
225+
}
199226
}
200227

201228
// Uncomment for debugging

src/test/if.trng

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
INIT 8
2+
23
WRITE 1 0
34
WRITE 1 2
45

5-
WBOOL (2 AND 0) 1
6+
WBOOL (7 NOT) 3
67

7-
IF 1 1
8+
IF 3 1 2
9+
LOC 2
810
BIT PRINT 7
911
END
1012

1113
LOC 1
1214
BIT PRINT 2
1315
END
1416

17+
// Expected result: `1`
18+

0 commit comments

Comments
 (0)