@@ -60,6 +60,7 @@ pub fn parse_instruction(
6060 ast:: Instruction :: Draw { .. } => Box :: new ( Noop ) ,
6161 ast:: Instruction :: Print { value } => Box :: new ( Print { value : lvar ( value) } ) ,
6262 ast:: Instruction :: PrintChar { value } => Box :: new ( PrintChar { value : lvar ( value) } ) ,
63+ ast:: Instruction :: Format { value } => Box :: new ( Format { value : lvar ( value) } ) ,
6364
6465 // operations
6566 ast:: Instruction :: Set { to, from } => Box :: new ( Set {
@@ -143,10 +144,12 @@ impl Print {
143144
144145impl SimpleInstruction for Print {
145146 fn execute ( & self , state : & mut ProcessorState , _: & LogicVM ) {
146- if state. printbuffer . len ( ) < MAX_TEXT_BUFFER {
147- let value = self . value . get ( state) ;
148- state. append_printbuffer ( & Print :: to_string ( & value) ) ;
147+ if state. printbuffer . len ( ) >= MAX_TEXT_BUFFER {
148+ return ;
149149 }
150+
151+ let value = self . value . get ( state) ;
152+ state. append_printbuffer ( & Print :: to_string ( & value) ) ;
150153 }
151154}
152155
@@ -156,13 +159,53 @@ struct PrintChar {
156159
157160impl SimpleInstruction for PrintChar {
158161 fn execute ( & self , state : & mut ProcessorState , _: & LogicVM ) {
159- if state. printbuffer . len ( ) < MAX_TEXT_BUFFER {
160- // TODO: content emojis
161- if let LValue :: Number ( c) = self . value . get ( state) {
162- // Java converts from float to char via int, not directly
163- state. printbuffer . push ( c. floor ( ) as u32 as u16 ) ;
162+ if state. printbuffer . len ( ) >= MAX_TEXT_BUFFER {
163+ return ;
164+ }
165+
166+ // TODO: content emojis
167+ if let LValue :: Number ( c) = self . value . get ( state) {
168+ // Java converts from float to char via int, not directly
169+ state. printbuffer . push ( c. floor ( ) as u32 as u16 ) ;
170+ }
171+ }
172+ }
173+
174+ struct Format {
175+ value : LVar ,
176+ }
177+
178+ impl SimpleInstruction for Format {
179+ fn execute ( & self , state : & mut ProcessorState , _: & LogicVM ) {
180+ if state. printbuffer . len ( ) >= MAX_TEXT_BUFFER {
181+ return ;
182+ }
183+
184+ let mut placeholder_index = MAX_TEXT_BUFFER ;
185+ let mut placeholder_number = 10 ;
186+
187+ for ( i, vals) in state. printbuffer . windows ( 3 ) . enumerate ( ) {
188+ let & [ left, c, right] = vals else {
189+ unreachable ! ( )
190+ } ;
191+ if left == ( '{' as u16 ) && right == ( '}' as u16 ) {
192+ let n = ( c as i32 ) - ( '0' as i32 ) ;
193+ if ( 0 ..=9 ) . contains ( & n) && n < placeholder_number {
194+ placeholder_number = n;
195+ placeholder_index = i;
196+ }
164197 }
165198 }
199+
200+ if placeholder_index == MAX_TEXT_BUFFER {
201+ return ;
202+ }
203+
204+ let value = self . value . get ( state) ;
205+ state. printbuffer . splice (
206+ placeholder_index..placeholder_index + 3 ,
207+ ProcessorState :: encode_utf16 ( & Print :: to_string ( & value) ) ,
208+ ) ;
166209 }
167210}
168211
0 commit comments