File tree Expand file tree Collapse file tree 3 files changed +39
-3
lines changed
Expand file tree Collapse file tree 3 files changed +39
-3
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,6 @@ use serde::Deserialize;
66#[ derive( Default , Debug , Deserialize ) ]
77pub enum VerbosityLevel {
88 /// No messages logged.
9- #[ default]
109 None ,
1110
1211 /// All messages are logged.
@@ -16,6 +15,7 @@ pub enum VerbosityLevel {
1615 Medium ,
1716
1817 /// Only high priority messages are logged.
18+ #[ default]
1919 High ,
2020}
2121
Original file line number Diff line number Diff line change 1+ use std:: fmt;
2+
13/// Token that is parsed by a [Parser] from source input and can be consumed by an [Interpreter].
24#[ derive( Debug , Clone ) ]
35pub enum Token {
@@ -22,3 +24,25 @@ pub enum Token {
2224 /// Signals the [Interpreter] to read a byte from `stdin` and store it at the currently pointed at location.
2325 Read ,
2426}
27+
28+ impl fmt:: Display for Token {
29+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
30+ match self {
31+ Self :: Inc => write ! ( f, "Inc" ) ,
32+ Self :: Dec => write ! ( f, "Dec" ) ,
33+ Self :: MoveRight => write ! ( f, "MoveRght" ) ,
34+ Self :: MoveLeft => write ! ( f, "MoveLeft" ) ,
35+ Self :: Loop ( vec) => {
36+ write ! ( f, "Loop[" ) ?;
37+
38+ for ele in vec {
39+ write ! ( f, "{}," , ele) ?;
40+ }
41+
42+ return write ! ( f, "]" ) ;
43+ }
44+ Self :: Print => write ! ( f, "Print" ) ,
45+ Self :: Read => write ! ( f, "Read" ) ,
46+ }
47+ }
48+ }
Original file line number Diff line number Diff line change @@ -13,13 +13,17 @@ pub enum ProgramError {
1313
1414 /// Error when performing an I/O operation.
1515 IOError ,
16+
17+ /// Error when performing illegal operation
18+ IllegalOperation
1619}
1720
1821impl fmt:: Display for ProgramError {
1922 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2023 match self {
2124 ProgramError :: OutOfBounds => write ! ( f, "Attempted to move pointer past end of buffer" ) ,
2225 ProgramError :: IOError => write ! ( f, "Error when attempting to perform IO operation" ) ,
26+ ProgramError :: IllegalOperation => write ! ( f, "Operation is illegal" )
2327 }
2428 }
2529}
@@ -106,8 +110,16 @@ impl Interpreter {
106110 /// Processes the given [Token] and returns the result of its computation.
107111 fn process_token ( & self , token : Token ) -> Result < ( ) , ProgramError > {
108112 match token {
109- Token :: Inc => self . data . borrow_mut ( ) . inc ( ) ,
110- Token :: Dec => self . data . borrow_mut ( ) . dec ( ) ,
113+ Token :: Inc => {
114+ self . data . borrow_mut ( ) . inc ( ) ;
115+ }
116+ Token :: Dec => {
117+ if self . data . borrow ( ) . get ( ) == 0 {
118+ return Err ( ProgramError :: IllegalOperation ) ;
119+ }
120+
121+ self . data . borrow_mut ( ) . dec ( ) ;
122+ }
111123 Token :: MoveLeft => {
112124 if self . data . borrow ( ) . pointer ( ) == 0 {
113125 return Err ( ProgramError :: OutOfBounds ) ;
You can’t perform that action at this time.
0 commit comments