Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Moritz Sokoll
brainfuck
Commits
4599672a
Commit
4599672a
authored
Jun 30, 2021
by
Moritz Sokoll
🦀
Browse files
1.0.2
parent
217c83c3
Pipeline
#245
passed with stages
in 1 minute and 32 seconds
Changes
6
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
.gitlab-ci.yml
View file @
4599672a
...
...
@@ -12,7 +12,6 @@ build:
test
:
stage
:
test
script
:
-
cargo run -- -d test.bf
-
cargo run -- -d helloworld.bf
# vim: ts=2 sts=2 sw=2 expandtab
Cargo.toml
View file @
4599672a
[package]
name
=
"brainfuck"
version
=
"
0.
1.0"
version
=
"1.0
.2
"
authors
=
[
"Moritz Sokoll <moritz@sokoll.com>"
]
edition
=
"2018"
...
...
README.md
View file @
4599672a
...
...
@@ -4,10 +4,4 @@
[

](https://gitlab.sokoll.com/moritz/brainfuck/-/commits/master)
This interpreter is a project of mine for learning rust.
If I finish it, it will be uploaded to the AUR and possibly crates.io
## Usage
The last argument has to be a valid filename to use as an input file.
### Options
-
`-d`
,
`--debug`
: enable debug printing
-
`-h`
,
`--help`
: show help message
It is avaiable in the AUR as
[
brainfuck-interp
](
https://aur.archlinux.org/packages/brainfuck-interp
)
src/brainfuck.rs
View file @
4599672a
...
...
@@ -5,11 +5,15 @@ pub mod help {
let
mut
counter
:
i32
=
1
;
let
mut
pos
:
usize
=
open_pos
;
while
counter
>
0
{
pos
+=
1
;
match
chars
[
pos
]
{
'['
=>
counter
+=
1
,
']'
=>
counter
-=
1
,
_
=>
{}
if
pos
+
1
==
chars
.len
()
{
return
None
;
}
else
{
pos
+=
1
;
match
chars
[
pos
]
{
'['
=>
counter
+=
1
,
']'
=>
counter
-=
1
,
_
=>
{}
}
}
}
Some
(
pos
)
...
...
@@ -18,7 +22,12 @@ pub mod help {
pub
mod
interpreter
{
pub
use
crate
::
brainfuck
::
help
::
match_closing_paren
;
pub
use
std
::
io
::{
self
,
Write
};
pub
use
std
::
io
::{
self
,
Write
,
Error
,
ErrorKind
};
#[derive(Debug,
Clone)]
pub
struct
ProgramState
{
...
...
@@ -98,7 +107,7 @@ pub mod interpreter {
}
}
pub
fn
execute_statements
(
statements
:
&
Vec
<
char
>
,
inital_state
:
&
ProgramState
)
->
ProgramState
{
pub
fn
execute_statements
(
statements
:
&
Vec
<
char
>
,
inital_state
:
&
ProgramState
)
->
Result
<
ProgramState
,
Error
>
{
let
mut
new_state
:
ProgramState
=
inital_state
.clone
();
let
mut
new_statements
:
Vec
<
char
>
=
statements
.clone
();
while
new_statements
.len
()
!=
0
{
...
...
@@ -127,16 +136,14 @@ pub mod interpreter {
let
closing
:
usize
=
match
match_closing_paren
(
statement_string
,
0
)
{
Some
(
n
)
=>
n
,
None
=>
{
println!
(
"no closing bracket found!"
);
return
ProgramState
::
new
(
vec!
[
0
;
512
],
0
);
return
Err
(
Error
::
new
(
ErrorKind
::
Other
,
"no closing bracket found!"
));
}
};
if
closing
==
0
{
println!
(
"no closing bracket found!"
);
return
ProgramState
::
new
(
vec!
[
0
;
512
],
0
);
return
Err
(
Error
::
new
(
ErrorKind
::
Other
,
"no closing bracket found!"
));
}
while
new_state
.get_current
()
!=
0
{
new_state
=
execute_statements
(
&
new_statements
[
1
..
closing
]
.to_vec
(),
&
new_state
);
new_state
=
execute_statements
(
&
new_statements
[
1
..
closing
]
.to_vec
(),
&
new_state
)
.unwrap
()
;
}
let
mut
statements_left
:
Vec
<
char
>
=
Vec
::
new
();
if
closing
+
1
==
new_statements
.len
()
{
...
...
@@ -156,7 +163,7 @@ pub mod interpreter {
new_statements
.remove
(
0
);
}
new_state
Ok
(
new_state
)
}
}
...
...
@@ -170,6 +177,6 @@ pub mod cmd {
-h, --help: show this help message
-v, --version: print the version number
"
;
pub
const
VERSION
:
&
str
=
"brainfuck: v1.0.
1
"
;
pub
const
VERSION
:
&
str
=
"brainfuck: v1.0.
2
"
;
}
// vim: ts=4 sts=4 sw=4 expandtab
src/main.rs
View file @
4599672a
...
...
@@ -41,9 +41,15 @@ fn main() -> Result<(), Error> {
let
content
:
String
=
fs
::
read_to_string
(
fname
.as_str
())
?
;
let
statements
:
Vec
<
char
>
=
content
.chars
()
.collect
();
let
init_ptr
:
usize
=
0
;
let
init_mem
:
Vec
<
u8
>
=
vec!
[
0
;
16
];
let
init_mem
:
Vec
<
u8
>
=
vec!
[
0
;
512
];
let
state
:
ProgramState
=
ProgramState
::
new
(
init_mem
,
init_ptr
);
let
new_state
=
execute_statements
(
&
statements
,
&
state
);
let
new_state
=
match
execute_statements
(
&
statements
,
&
state
)
{
Ok
(
n
)
=>
n
,
Err
(
e
)
=>
{
println!
(
"error at runtime: {}"
,
e
);
return
Err
(
Error
::
new
(
std
::
io
::
ErrorKind
::
Other
,
"error at runtime"
));
}
};
if
debug
{
dbg!
(
new_state
);
}
...
...
test.bf
View file @
4599672a
+++++++
[
>
+++++++
<
-
]
>
-.
a comment to see if the interpreter cares about them
+++++++
[
>
+++++++
<
-
>
-.
a comment to see if the interpreter cares about them
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment