1- use crate :: {
2- interp :: JmpWhen , program :: ProgramDetails , types :: CelByteCode , ByteCode , CelError , CelValue ,
3- CelValueDyn , Program ,
4- } ;
1+ mod preresolved ;
2+
3+ use crate :: { program :: ProgramDetails , types :: CelByteCode , ByteCode , CelValue , Program } ;
4+ pub use preresolved :: { PreResolvedByteCode , PreResolvedCodePoint } ;
55
66#[ derive( Debug , Clone ) ]
77pub enum NodeValue {
8- Bytecode ( CelByteCode ) ,
8+ Bytecode ( PreResolvedByteCode ) ,
99 ConstExpr ( CelValue ) ,
1010}
1111
@@ -19,9 +19,8 @@ pub struct CompiledProg {
1919macro_rules! compile {
2020 ( $bytecode: expr, $const_expr: expr, $( $child : ident) ,+) => {
2121 {
22- use crate :: compiler:: compiled_prog:: NodeValue ;
22+ use crate :: compiler:: compiled_prog:: { NodeValue , PreResolvedByteCode } ;
2323 use crate :: program:: ProgramDetails ;
24- use crate :: types:: CelByteCode ;
2524
2625 let mut new_details = ProgramDetails :: new( ) ;
2726
@@ -40,7 +39,7 @@ macro_rules! compile {
4039 }
4140 }
4241 ( $( $child, ) +) => {
43- let mut new_bytecode = CelByteCode :: new( ) ;
42+ let mut new_bytecode = PreResolvedByteCode :: new( ) ;
4443
4544 $(
4645 new_bytecode. extend( $child. into_bytecode( ) . into_iter( ) ) ;
@@ -61,9 +60,13 @@ macro_rules! compile {
6160}
6261
6362impl CompiledProg {
63+ pub fn new ( inner : NodeValue , details : ProgramDetails ) -> Self {
64+ Self { inner, details }
65+ }
66+
6467 pub fn empty ( ) -> CompiledProg {
6568 CompiledProg {
66- inner : NodeValue :: Bytecode ( CelByteCode :: new ( ) ) ,
69+ inner : NodeValue :: Bytecode ( PreResolvedByteCode :: new ( ) ) ,
6770 details : ProgramDetails :: new ( ) ,
6871 }
6972 }
@@ -77,18 +80,35 @@ impl CompiledProg {
7780
7881 pub fn with_bytecode ( bytecode : CelByteCode ) -> CompiledProg {
7982 CompiledProg {
80- inner : NodeValue :: Bytecode ( bytecode) ,
83+ inner : NodeValue :: Bytecode ( bytecode. into ( ) ) ,
8184 details : ProgramDetails :: new ( ) ,
8285 }
8386 }
8487
85- pub fn with_code_points ( bytecode : Vec < ByteCode > ) -> CompiledProg {
88+ pub fn with_code_points ( bytecode : Vec < PreResolvedCodePoint > ) -> CompiledProg {
8689 CompiledProg {
87- inner : NodeValue :: Bytecode ( bytecode. into ( ) ) ,
90+ inner : NodeValue :: Bytecode ( bytecode. into_iter ( ) . collect ( ) ) ,
8891 details : ProgramDetails :: new ( ) ,
8992 }
9093 }
9194
95+ pub fn details ( & self ) -> & ProgramDetails {
96+ & self . details
97+ }
98+
99+ pub fn into_parts ( self ) -> ( NodeValue , ProgramDetails ) {
100+ ( self . inner , self . details )
101+ }
102+
103+ pub fn append_if_bytecode ( & mut self , b : impl IntoIterator < Item = PreResolvedCodePoint > ) {
104+ match & mut self . inner {
105+ NodeValue :: Bytecode ( bytecode) => {
106+ bytecode. extend ( b) ;
107+ }
108+ NodeValue :: ConstExpr ( _) => { /* do nothing */ }
109+ }
110+ }
111+
92112 pub fn with_const ( val : CelValue ) -> CompiledProg {
93113 CompiledProg {
94114 inner : NodeValue :: ConstExpr ( val) ,
@@ -122,7 +142,7 @@ impl CompiledProg {
122142 . into_iter ( )
123143 . map ( |c| c. inner . into_bytecode ( ) . into_iter ( ) )
124144 . flatten ( )
125- . chain ( bytecode. into_iter ( ) )
145+ . chain ( bytecode. into_iter ( ) . map ( |b| b . into ( ) ) )
126146 . collect ( ) ,
127147 )
128148 } ;
@@ -149,10 +169,13 @@ impl CompiledProg {
149169 } ,
150170 None => CompiledProg {
151171 inner : NodeValue :: Bytecode (
152- [ ByteCode :: Push ( c1) , ByteCode :: Push ( c2) ]
153- . into_iter ( )
154- . chain ( bytecode. into_iter ( ) )
155- . collect ( ) ,
172+ [
173+ PreResolvedCodePoint :: Bytecode ( ByteCode :: Push ( c1) ) ,
174+ PreResolvedCodePoint :: Bytecode ( ByteCode :: Push ( c2) ) ,
175+ ]
176+ . into_iter ( )
177+ . chain ( bytecode. into_iter ( ) . map ( |b| b. into ( ) ) )
178+ . collect ( ) ,
156179 ) ,
157180 details : new_details,
158181 } ,
@@ -162,7 +185,7 @@ impl CompiledProg {
162185 c1. into_bytecode ( )
163186 . into_iter ( )
164187 . chain ( c2. into_bytecode ( ) . into_iter ( ) )
165- . chain ( bytecode. into_iter ( ) )
188+ . chain ( bytecode. into_iter ( ) . map ( |b| b . into ( ) ) )
166189 . collect ( ) ,
167190 ) ,
168191 details : new_details,
@@ -174,7 +197,7 @@ impl CompiledProg {
174197 let mut details = self . details ;
175198 details. add_source ( source) ;
176199
177- Program :: new ( details, self . inner . into_bytecode ( ) )
200+ Program :: new ( details, self . inner . into_bytecode ( ) . resolve ( ) )
178201 }
179202
180203 pub fn add_ident ( mut self , ident : & str ) -> CompiledProg {
@@ -205,82 +228,6 @@ impl CompiledProg {
205228 r
206229 }
207230
208- pub fn into_turnary (
209- mut self ,
210- true_clause : CompiledProg ,
211- false_clause : CompiledProg ,
212- ) -> CompiledProg {
213- self . details . union_from ( true_clause. details ) ;
214- self . details . union_from ( false_clause. details ) ;
215-
216- if let NodeValue :: ConstExpr ( i) = self . inner {
217- if i. is_err ( ) {
218- CompiledProg {
219- inner : NodeValue :: ConstExpr ( i) ,
220- details : self . details ,
221- }
222- } else {
223- if cfg ! ( feature = "type_prop" ) {
224- if i. is_truthy ( ) {
225- CompiledProg {
226- inner : true_clause. inner ,
227- details : self . details ,
228- }
229- } else {
230- CompiledProg {
231- inner : false_clause. inner ,
232- details : self . details ,
233- }
234- }
235- } else {
236- if let CelValue :: Bool ( b) = i {
237- if b {
238- CompiledProg {
239- inner : true_clause. inner ,
240- details : self . details ,
241- }
242- } else {
243- CompiledProg {
244- inner : false_clause. inner ,
245- details : self . details ,
246- }
247- }
248- } else {
249- CompiledProg {
250- inner : NodeValue :: ConstExpr ( CelValue :: from_err ( CelError :: Value (
251- format ! ( "{} cannot be converted to bool" , i. as_type( ) ) ,
252- ) ) ) ,
253- details : self . details ,
254- }
255- }
256- }
257- }
258- } else {
259- let true_clause_bytecode = true_clause. inner . into_bytecode ( ) ;
260- let false_clause_bytecode = false_clause. inner . into_bytecode ( ) ;
261- CompiledProg {
262- inner : NodeValue :: Bytecode (
263- self . inner
264- . into_bytecode ( )
265- . into_iter ( )
266- . chain (
267- [ ByteCode :: JmpCond {
268- when : JmpWhen :: False ,
269- dist : ( true_clause_bytecode. len ( ) as u32 ) + 1 , // +1 to jmp over the next jump
270- leave_val : false ,
271- } ]
272- . into_iter ( ) ,
273- )
274- . chain ( true_clause_bytecode. into_iter ( ) )
275- . chain ( [ ByteCode :: Jmp ( false_clause_bytecode. len ( ) as u32 ) ] . into_iter ( ) )
276- . chain ( false_clause_bytecode. into_iter ( ) )
277- . collect ( ) ,
278- ) ,
279- details : self . details ,
280- }
281- }
282- }
283-
284231 #[ inline]
285232 pub fn bytecode_len ( & self ) -> usize {
286233 match self . inner {
@@ -289,7 +236,7 @@ impl CompiledProg {
289236 }
290237 }
291238
292- pub fn into_bytecode ( self ) -> CelByteCode {
239+ pub fn into_unresolved_bytecode ( self ) -> PreResolvedByteCode {
293240 self . inner . into_bytecode ( )
294241 }
295242
@@ -310,10 +257,10 @@ impl NodeValue {
310257 matches ! ( * self , NodeValue :: ConstExpr ( _) )
311258 }
312259
313- pub fn into_bytecode ( self ) -> CelByteCode {
260+ pub fn into_bytecode ( self ) -> PreResolvedByteCode {
314261 match self {
315262 NodeValue :: Bytecode ( b) => b,
316- NodeValue :: ConstExpr ( c) => CelByteCode :: from_code_point ( ByteCode :: Push ( c) ) ,
263+ NodeValue :: ConstExpr ( c) => [ ByteCode :: Push ( c) ] . into_iter ( ) . collect ( ) ,
317264 }
318265 }
319266}
0 commit comments