@@ -79,83 +79,112 @@ pub fn ksm_to_binary(ksm: KSM) -> Result<Vec<u8>, BinaryError> {
7979 Ok ( encoded)
8080}
8181
82- fn parse_ksm ( mut ksm : KSM ) -> Result < Vec < u8 > , BinaryError > {
82+ fn parse_ksm ( ksm : KSM ) -> Result < Vec < u8 > , BinaryError > {
83+ let ksm_main = ksm. main ;
84+ let ksm_functions = ksm. functions ;
85+
8386 let mut code = vec ! [ b'k' , b'\x03' , b'X' , b'E' ] ;
8487
8588 let mut args = vec ! [ b'%' , b'A' , b'\x00' /* To be filled later, this is `numArgIndexBytes` */ ] ;
8689
87- for ref mut inst in & mut ksm. main {
90+ let mut new_main = Vec :: new ( ) ;
91+
92+ for inst in ksm_main {
93+ #[ cfg( feature = "slow_dev_debugging" ) ]
94+ super :: LOG . debug ( & format ! ( "Main code arg parsing: {:?}" , & inst) ) ;
95+
8896 match inst {
8997 KSMInstructions :: Push ( lit) => {
9098 let idx = args. len ( ) ;
9199 args. append ( & mut to_bin ( & lit) ) ;
92- * * inst = KSMInstructions :: Push ( KSMLit :: ArgIndex ( idx) ) ;
100+ new_main . push ( KSMInstructions :: Push ( KSMLit :: ArgIndex ( idx) ) ) ;
93101 } ,
94102 KSMInstructions :: Store ( lit) => {
95103 let idx = args. len ( ) ;
96104 args. append ( & mut to_bin ( & lit) ) ;
97- * * inst = KSMInstructions :: Store ( KSMLit :: ArgIndex ( idx) ) ;
105+ new_main . push ( KSMInstructions :: Store ( KSMLit :: ArgIndex ( idx) ) ) ;
98106 } ,
99107 KSMInstructions :: Jump ( lit1, lit2) => {
100108 let idx1 = args. len ( ) ;
101109 args. append ( & mut to_bin ( & lit1) ) ;
102110 let idx2 = args. len ( ) ;
103111 args. append ( & mut to_bin ( & lit2) ) ;
104- * * inst = KSMInstructions :: Jump ( KSMLit :: ArgIndex ( idx1) , KSMLit :: ArgIndex ( idx2) ) ;
112+ new_main . push ( KSMInstructions :: Jump ( KSMLit :: ArgIndex ( idx1) , KSMLit :: ArgIndex ( idx2) ) ) ;
105113 } ,
106114 KSMInstructions :: BranchFalse ( lit1, lit2) => {
107115 let idx1 = args. len ( ) ;
108116 args. append ( & mut to_bin ( & lit1) ) ;
109117 let idx2 = args. len ( ) ;
110118 args. append ( & mut to_bin ( & lit2) ) ;
111- * * inst = KSMInstructions :: BranchFalse ( KSMLit :: ArgIndex ( idx1) , KSMLit :: ArgIndex ( idx2) ) ;
119+ new_main . push ( KSMInstructions :: BranchFalse ( KSMLit :: ArgIndex ( idx1) , KSMLit :: ArgIndex ( idx2) ) ) ;
112120 } ,
113121 KSMInstructions :: Call ( lit1, lit2) => {
114122 let idx1 = args. len ( ) ;
115123 args. append ( & mut to_bin ( & lit1) ) ;
116124 let idx2 = args. len ( ) ;
117125 args. append ( & mut to_bin ( & lit2) ) ;
118- * * inst = KSMInstructions :: Call ( KSMLit :: ArgIndex ( idx1) , KSMLit :: ArgIndex ( idx2) ) ;
126+ new_main . push ( KSMInstructions :: Call ( KSMLit :: ArgIndex ( idx1) , KSMLit :: ArgIndex ( idx2) ) ) ;
119127 } ,
120128 _ => { }
121129 }
122130 }
123131
132+ let ksm_main = new_main;
133+
124134 let mut func_name_idx = Vec :: new ( ) ;
125- let mut tmp_ksm_functions = ksm . functions . clone ( ) ;
135+ let mut new_functions = Vec :: new ( ) ;
126136
127- for & mut ( ref func_name, ref mut func_inst) in & mut tmp_ksm_functions {
128- func_name_idx. push ( ( args. len ( ) , func_name) ) ;
137+ for ( func_name, func_inst) in ksm_functions {
138+ func_name_idx. push ( ( args. len ( ) , func_name. clone ( ) ) ) ;
129139 args. append ( & mut to_bin ( & KSMLit :: String ( func_name. to_string ( ) ) ) ) ;
140+
141+ let mut new_func_code = Vec :: new ( ) ;
142+
130143 for ref mut inst in func_inst {
144+ #[ cfg( feature = "slow_dev_debugging" ) ]
145+ super :: LOG . debug ( & format ! ( "Func ({:?}) arg parsing: {:?}" , & func_name, & inst) ) ;
146+
131147 match inst {
132148 KSMInstructions :: Push ( lit) => {
133149 let idx = args. len ( ) ;
134150 args. append ( & mut to_bin ( & lit) ) ;
135- * * inst = KSMInstructions :: Push ( KSMLit :: ArgIndex ( idx) ) ;
151+ new_func_code . push ( KSMInstructions :: Push ( KSMLit :: ArgIndex ( idx) ) ) ;
136152 } ,
137153 KSMInstructions :: Store ( lit) => {
138154 let idx = args. len ( ) ;
139155 args. append ( & mut to_bin ( & lit) ) ;
140- * * inst = KSMInstructions :: Store ( KSMLit :: ArgIndex ( idx) ) ;
156+ new_func_code . push ( KSMInstructions :: Store ( KSMLit :: ArgIndex ( idx) ) ) ;
141157 } ,
142158 KSMInstructions :: Jump ( lit1, lit2) => {
143- let idx = args. len ( ) ;
159+ let idx1 = args. len ( ) ;
144160 args. append ( & mut to_bin ( & lit1) ) ;
161+ let idx2 = args. len ( ) ;
145162 args. append ( & mut to_bin ( & lit2) ) ;
146- * * inst = KSMInstructions :: Jump ( KSMLit :: ArgIndex ( idx ) , KSMLit :: ArgIndex ( idx + 1 ) ) ;
163+ new_func_code . push ( KSMInstructions :: Jump ( KSMLit :: ArgIndex ( idx1 ) , KSMLit :: ArgIndex ( idx2 ) ) ) ;
147164 } ,
148165 KSMInstructions :: BranchFalse ( lit1, lit2) => {
149- let idx = args. len ( ) ;
166+ let idx1 = args. len ( ) ;
150167 args. append ( & mut to_bin ( & lit1) ) ;
168+ let idx2 = args. len ( ) ;
151169 args. append ( & mut to_bin ( & lit2) ) ;
152- * * inst = KSMInstructions :: BranchFalse ( KSMLit :: ArgIndex ( idx) , KSMLit :: ArgIndex ( idx + 1 ) ) ;
170+ new_func_code. push ( KSMInstructions :: BranchFalse ( KSMLit :: ArgIndex ( idx1) , KSMLit :: ArgIndex ( idx2) ) ) ;
171+ } ,
172+ KSMInstructions :: Call ( lit1, lit2) => {
173+ let idx1 = args. len ( ) ;
174+ args. append ( & mut to_bin ( & lit1) ) ;
175+ let idx2 = args. len ( ) ;
176+ args. append ( & mut to_bin ( & lit2) ) ;
177+ new_func_code. push ( KSMInstructions :: Call ( KSMLit :: ArgIndex ( idx1) , KSMLit :: ArgIndex ( idx2) ) ) ;
153178 } ,
154179 _ => { }
155180 }
156181 }
182+
183+ new_functions. push ( ( func_name. clone ( ) , new_func_code) ) ;
157184 }
158185
186+ let ksm_functions = new_functions;
187+
159188 let num_arg_index_bytes: u8 = match args. len ( ) {
160189 0 => 0x00 ,
161190 ..255 => 0x01 ,
@@ -171,13 +200,19 @@ fn parse_ksm(mut ksm: KSM) -> Result<Vec<u8>, BinaryError> {
171200 super :: LOG . debug ( & format ! ( "ARGS: {args:?}" ) ) ;
172201
173202 #[ cfg( feature = "slow_dev_debugging" ) ]
174- super :: LOG . debug ( & format ! ( "Main code after arg parsing: {:?}" , & ksm. main) ) ;
203+ super :: LOG . debug ( & format ! ( "Main code after arg parsing: {:?}" , & ksm_main) ) ;
204+
205+ #[ cfg( feature = "slow_dev_debugging" ) ]
206+ super :: LOG . debug ( & format ! ( "Funcs after arg parsing: {:?}" , & ksm_functions) ) ;
175207
176208 code. append ( & mut args) ;
177209
178210 let mut main_code = vec ! [ b'%' , b'M' ] ;
179211
180- for inst in ksm. main {
212+ for inst in ksm_main {
213+ #[ cfg( feature = "slow_dev_debugging" ) ]
214+ super :: LOG . debug ( & format ! ( "Parsing main instruction: {:?}" , & inst) ) ;
215+
181216 match inst {
182217 KSMInstructions :: NOP => main_code. push ( b'\x33' ) ,
183218 KSMInstructions :: Add => main_code. push ( b'\x3C' ) ,
@@ -226,12 +261,12 @@ fn parse_ksm(mut ksm: KSM) -> Result<Vec<u8>, BinaryError> {
226261
227262 let mut func_code = vec ! [ b'%' , b'F' ] ;
228263
229- for ( func_name, func_inst) in ksm . functions {
264+ for ( func_name, func_inst) in ksm_functions {
230265 func_code. push ( b'\xF0' ) ;
231266 func_code. append ( & mut {
232267 let mut i = 0 ;
233268 for ( idx, name) in & func_name_idx {
234- if name == & & func_name {
269+ if name == & func_name {
235270 i = * idx;
236271 }
237272 }
@@ -253,6 +288,9 @@ fn parse_ksm(mut ksm: KSM) -> Result<Vec<u8>, BinaryError> {
253288 } ) ;
254289
255290 for inst in func_inst {
291+ #[ cfg( feature = "slow_dev_debugging" ) ]
292+ super :: LOG . debug ( & format ! ( "Parsing function ({:?}) instruction: {:?}" , & func_name, & inst) ) ;
293+
256294 match inst {
257295 KSMInstructions :: NOP => func_code. push ( b'\x33' ) ,
258296 KSMInstructions :: Add => func_code. push ( b'\x3C' ) ,
0 commit comments