-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
534 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Mandelbrot Set | ||
-------------- | ||
Optimized: took 5.399 seconds with 1024 bytes of memory at pointer location 512 (no auto-allocation) | ||
Unoptimized: took 13.773 seconds with 1024 bytes of memory at pointer location 512 (no auto-allocation) | ||
|
||
Hello World | ||
----------- | ||
Optimized: took 0.002 seconds with 1024 bytes of memory at pointer location 512 (no auto-allocation) | ||
Unoptimized: took 0.012 seconds with 1024 bytes of memory at pointer location 512 (no auto-allocation) | ||
|
||
Factorial | ||
--------- | ||
TERMINATION AFTER 1 SECOND | ||
|
||
Optimized: reached "354996793146960497053355363383973425965094809743694491885455534984190204750249968830591340591162785093141951525209177997501478084577063512837513105442388103085116949108248219929177667335850225156399124325817472036634653562449665740610033707601842063277098323069015230061026956365247457276593902258859903874498560000000000000000000000000000000000000000000000" with auto-allocation | ||
Unoptimized: reaced "68514381077363375931297585133106871211263298280533036933892918251948709516798243984304128734094417522976396644365371353517785270323373257977640029350380903895427571177891906446331289795819093455185030994882772103070488137552785487937736505567155518212479976352319939401778202578492759254382623135959961447778222080000000000000000000000000000000000000000000000" with auto-allocation |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "bs_bin" | ||
version = "2.0.0" | ||
version = "2.5.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,87 +1,71 @@ | ||
use crate::types::Instruction; | ||
use crate::utils::{BrainsuckError, BrainsuckErrorType}; | ||
use crate::{ | ||
types::Instruction as I, | ||
utils::{BrainsuckError, BrainsuckErrorType} | ||
}; | ||
use std::{io::{self, Read}, vec}; | ||
|
||
use std::io::{self, Read}; | ||
pub fn alloc(memory: &mut Vec<u8>, amount: usize) { | ||
memory.resize(memory.len() + amount, 0); | ||
} | ||
|
||
pub fn interpret( | ||
instructions: &[Instruction], | ||
memory: &mut Vec<u8>, | ||
memory_pointer: &mut usize, | ||
auto_alloc: bool, | ||
repl_mode: bool, | ||
) { | ||
for inst in instructions { | ||
if *memory_pointer - 1 > memory.len() || *memory_pointer - 1 > memory.len() + 1 { | ||
if auto_alloc { | ||
memory.resize(memory.len() + 512, 0); | ||
} else { | ||
BrainsuckError::throw_error( | ||
"Memory overflow!\nHelp: Give program more memory.".to_owned(), | ||
BrainsuckErrorType::MemoryError, | ||
repl_mode, | ||
); | ||
} | ||
} | ||
|
||
match inst { | ||
Instruction::IncrementPointer => match memory.get(*memory_pointer + 1) { | ||
Some(_v) => *memory_pointer += 1, | ||
None => BrainsuckError::throw_error( | ||
"Memory overflow!\nHelp: Give program more memory.".to_owned(), | ||
BrainsuckErrorType::MemoryError, | ||
!repl_mode, | ||
), | ||
}, | ||
Instruction::DecrementPointer => match memory.get(*memory_pointer + 1) { | ||
Some(_v) => *memory_pointer -= 1, | ||
None => BrainsuckError::throw_error( | ||
"Memory overflow!\nHelp: Give program more memory.".to_owned(), | ||
BrainsuckErrorType::MemoryError, | ||
!repl_mode, | ||
), | ||
}, | ||
Instruction::Increment => match memory.get(*memory_pointer - 1) { | ||
Some(_v) => memory[*memory_pointer] += 1, | ||
None => BrainsuckError::throw_error( | ||
"Memory overflow!\nHelp: Give program more memory.".to_owned(), | ||
BrainsuckErrorType::MemoryError, | ||
!repl_mode, | ||
), | ||
}, | ||
Instruction::Decrement => match memory.get(*memory_pointer - 1) { | ||
Some(_v) => memory[*memory_pointer] -= 1, | ||
None => BrainsuckError::throw_error( | ||
"Memory overflow!\nHelp: Give program more memory.".to_owned(), | ||
BrainsuckErrorType::MemoryError, | ||
!repl_mode, | ||
), | ||
}, | ||
Instruction::Write => { | ||
if repl_mode { | ||
print!("\n{}\n\n", memory[*memory_pointer] as char) | ||
} else { | ||
print!("{}", memory[*memory_pointer] as char) | ||
} | ||
} | ||
Instruction::Read => { | ||
let mut input_byte: [u8; 1] = [0; 1]; | ||
pub fn interpret(instructions: &Vec<I>, memory: &mut Vec<u8>, pointer: &mut usize, repl_mode: bool, auto_allocate: bool) { | ||
|
||
for (_idx, instr) in instructions.iter().enumerate() { | ||
match instr { | ||
I::IncrementPointer => if *pointer + 1 > memory.len() - 1 { | ||
if auto_allocate { | ||
alloc(memory, 1); | ||
*pointer += 1; | ||
} | ||
else { | ||
BrainsuckError::throw_error_with_help( | ||
format!("Memory owerflow at {}", pointer), | ||
"Give program more memory or\nenable auto-allocation".to_string(), | ||
BrainsuckErrorType::MemoryError, | ||
!repl_mode, | ||
); | ||
} | ||
} else { | ||
*pointer += 1; | ||
} | ||
|
||
match io::stdin().read_exact(&mut input_byte) { | ||
Ok(()) => (), | ||
Err(_e) => BrainsuckError::throw_error( | ||
"Can't read input form stdin".to_owned(), | ||
BrainsuckErrorType::IOError, | ||
repl_mode, | ||
), | ||
} | ||
I::DecrementPointer => if (*pointer as i32) - 1 < 0 { | ||
BrainsuckError::throw_error_with_help( | ||
"Memory pointer can't be negative".to_string(), | ||
format!("Decrementing pointer by 1\nat pointer location {}", pointer), | ||
BrainsuckErrorType::MemoryError, | ||
true, | ||
); | ||
} else { | ||
*pointer -= 1; | ||
} | ||
|
||
memory[*memory_pointer] = input_byte[0]; | ||
} | ||
Instruction::Loop(loop_instructions) => { | ||
while memory[*memory_pointer] != 0 { | ||
interpret(loop_instructions, memory, memory_pointer, true, false) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
I::Add => if (memory[*pointer] as i32) + 1 > 255 { | ||
memory[*pointer] = 0; | ||
} else { | ||
memory[*pointer] += 1; | ||
}, | ||
|
||
I::Subtract => if (memory[*pointer] as i32) - 1 < 0 { | ||
memory[*pointer] = 255; | ||
} else { | ||
memory[*pointer] -= 1; | ||
}, | ||
|
||
I::Write => print!("{}", memory[*pointer] as char), | ||
|
||
I::Read => { | ||
let mut input = vec![0; 1]; | ||
io::stdin().read(&mut input).unwrap(); | ||
memory[*pointer] = input[0]; | ||
}, | ||
|
||
I::Loop(loop_instructions) => { | ||
while memory[*pointer] != 0 { | ||
interpret(&loop_instructions, memory, pointer, repl_mode, auto_allocate); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.