@@ -9,6 +9,7 @@ use crate::{
99 } ,
1010 prelude:: lookup:: TYPE ,
1111 types:: Type ,
12+ value:: Value ,
1213} ;
1314
1415pub mod opcode {
@@ -190,12 +191,12 @@ impl CompileTimeEnv {
190191pub struct ExprByteCode {
191192 version : [ u8 ; 4 ] ,
192193 codes : Vec < u8 > ,
193- strings : Vec < String > ,
194+ constants : Vec < Value > ,
194195 types : Vec < Type > ,
195196}
196197
197198impl ExprByteCode {
198- pub fn new ( codes : Vec < u8 > , strings : Vec < String > , types : Vec < Type > ) -> Self {
199+ pub fn new ( codes : Vec < u8 > , constants : Vec < Value > , types : Vec < Type > ) -> Self {
199200 let version_bytes = get_version_bytes ( ) ;
200201 let version_bytes_from_codes = & codes[ 0 ..4 ] ;
201202
@@ -209,7 +210,7 @@ impl ExprByteCode {
209210 Self {
210211 version : version_bytes,
211212 codes,
212- strings ,
213+ constants ,
213214 types,
214215 }
215216 }
@@ -226,8 +227,8 @@ impl ExprByteCode {
226227 self . codes . get ( index)
227228 }
228229
229- pub fn strings ( & self ) -> & [ String ] {
230- & self . strings
230+ pub fn constants ( & self ) -> & [ Value ] {
231+ & self . constants
231232 }
232233
233234 pub fn types ( & self ) -> & [ Type ] {
@@ -246,21 +247,21 @@ pub fn get_version_bytes() -> [u8; 4] {
246247
247248/// Compile an [`ast::Expr`] into [`ExprByteCode`]
248249pub fn compile ( expr : & mut ExprS , env : & CompileTimeEnv ) -> ExprResult < ExprByteCode > {
249- let mut strings : Vec < String > = vec ! [ ] ;
250+ let mut constants : Vec < Value > = vec ! [ ] ;
250251 let mut types: Vec < Type > = vec ! [ ] ;
251252 let mut codes = vec ! [ ] ;
252253
253254 codes. extend ( get_version_bytes ( ) ) ;
254255
255- codes. extend ( compile_expr ( expr, env, & mut strings , & mut types) ?) ;
256+ codes. extend ( compile_expr ( expr, env, & mut constants , & mut types) ?) ;
256257
257- Ok ( ExprByteCode :: new ( codes, strings , types) )
258+ Ok ( ExprByteCode :: new ( codes, constants , types) )
258259}
259260
260261fn compile_expr (
261262 ( expr, span) : & mut ExprS ,
262263 env : & CompileTimeEnv ,
263- strings : & mut Vec < String > ,
264+ constants : & mut Vec < Value > ,
264265 types : & mut Vec < Type > ,
265266) -> ExprResult < Vec < u8 > > {
266267 use opcode:: * ;
@@ -272,12 +273,18 @@ fn compile_expr(
272273
273274 match expr {
274275 Expr :: String ( string) => {
275- if let Some ( index) = strings. iter ( ) . position ( |x| x == & string. 0 ) {
276+ if let Some ( index) = constants. iter ( ) . position ( |x| {
277+ if let Value :: String ( string_constant) = x {
278+ string_constant == & string. 0
279+ } else {
280+ false
281+ }
282+ } ) {
276283 codes. push ( CONSTANT ) ;
277284 codes. push ( index as u8 ) ;
278285 } else {
279- strings . push ( string. 0 . clone ( ) ) ;
280- let index = strings . len ( ) - 1 ;
286+ constants . push ( Value :: String ( string. 0 . clone ( ) ) ) ;
287+ let index = constants . len ( ) - 1 ;
281288 codes. push ( CONSTANT ) ;
282289 codes. push ( index as u8 ) ;
283290 }
@@ -356,7 +363,7 @@ fn compile_expr(
356363 }
357364 }
358365 Expr :: Call ( expr_call) => {
359- let callee_bytecode = compile_expr ( & mut expr_call. callee , env, strings , types) ?;
366+ let callee_bytecode = compile_expr ( & mut expr_call. callee , env, constants , types) ?;
360367
361368 if let Some ( _op) = callee_bytecode. first ( )
362369 && let Some ( lookup) = callee_bytecode. get ( 1 )
@@ -433,7 +440,7 @@ fn compile_expr(
433440 codes. extend ( callee_bytecode) ;
434441
435442 for arg in expr_call. args . iter_mut ( ) {
436- match compile_expr ( arg, env, strings , types) {
443+ match compile_expr ( arg, env, constants , types) {
437444 Ok ( arg_bytecode) => {
438445 codes. extend ( arg_bytecode) ;
439446 }
0 commit comments