-
Notifications
You must be signed in to change notification settings - Fork 2
Refactor I/O functions for cross-platform support and improve memory … #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hsiang086
wants to merge
1
commit into
Alex-Velez:main
Choose a base branch
from
hsiang086:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 +1,5 @@ | ||
| use crate::error::BitBitError; | ||
| use std::io::Write; | ||
| use std::io::{self, Write}; | ||
|
|
||
| pub const BASE: usize = 256; | ||
| #[cfg(target_pointer_width = "16")] | ||
|
|
@@ -33,7 +33,7 @@ pub fn shift_left(pointer: &mut usize, amount: usize) -> Result<(), BitBitError> | |
| /// Output character at pointer | ||
| #[inline] | ||
| pub fn output(memory: &Vec<u8>, pointer: &usize, amount: usize) -> Result<(), BitBitError> { | ||
| // print ascii | ||
| // print ASCII character at the pointer position | ||
| for _ in 0..amount { | ||
| putch(memory[*pointer] as core::ffi::c_char); | ||
| } | ||
|
|
@@ -49,7 +49,7 @@ pub fn output(memory: &Vec<u8>, pointer: &usize, amount: usize) -> Result<(), Bi | |
| #[inline] | ||
| pub fn input(memory: &mut Vec<u8>, pointer: &usize, amount: usize) { | ||
| for _ in 0..amount { | ||
| // get 1 character input | ||
| // get 1 character input and store it in the memory at the pointer position | ||
| (*memory)[*pointer] = getch() as u8; | ||
| } | ||
| } | ||
|
|
@@ -84,22 +84,46 @@ pub fn decrement(cell: &mut u8, amount: usize) { | |
| } | ||
| } | ||
|
|
||
| // I/O for brainfuck | ||
| // Cross-platform I/O for brainfuck | ||
| #[cfg(target_os = "windows")] | ||
| extern "C" { | ||
| fn _getch() -> core::ffi::c_char; | ||
| fn _putch(c_char: core::ffi::c_char) -> core::ffi::c_void; | ||
| } | ||
|
|
||
| // Read 1 byte input | ||
| #[cfg(target_os = "windows")] | ||
| #[inline] | ||
| fn getch() -> core::ffi::c_char { | ||
| // SAFETY: trust C | ||
| // SAFETY: call Windows-specific `_getch` | ||
| unsafe { _getch() } | ||
| } | ||
|
|
||
| // Write 1 byte output | ||
| #[cfg(target_os = "windows")] | ||
| #[inline] | ||
| fn putch(c_char: core::ffi::c_char) -> core::ffi::c_void { | ||
| // SAFETY: trust C | ||
| // SAFETY: call Windows-specific `_putch` | ||
| unsafe { _putch(c_char) } | ||
| } | ||
|
|
||
| // Alternative implementation for non-Windows platforms | ||
| #[cfg(not(target_os = "windows"))] | ||
| #[inline] | ||
| fn getch() -> core::ffi::c_char { | ||
| use std::io::Read; | ||
| // Read a single byte from stdin | ||
| io::stdin() | ||
| .bytes() | ||
| .next() | ||
| .and_then(|result| result.ok()) | ||
| .unwrap_or(0) as core::ffi::c_char | ||
| } | ||
|
|
||
| // Alternative implementation for non-Windows platforms | ||
| #[cfg(not(target_os = "windows"))] | ||
| #[inline] | ||
| fn putch(c_char: core::ffi::c_char) { | ||
| print!("{}", c_char as u8 as char); | ||
| io::stdout().flush().unwrap(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Flushing is already handled in |
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although this
getchfunction uses the stand rust library, which is nice to not depend on external C functions, it has a different functionality from the original implementation. The intended implementation forgetchshould terminate once a single byte has been read, but your implementation allows for multiple characters to be typed in the terminal and requires a\ncharacter or enter key to be pressed before the first byte is read.Ex: This code should terminate and only show a single character in the terminal after 1 key press.
,.