11use crate :: {
22 Expr , ExprVisitor , Literal , NodeAttributes , Operation , Query , QueryVisitor , Subject , Var ,
3- parser:: Record ,
3+ Where , parser:: Record ,
44} ;
55
6- trait IntoLiteral {
6+ pub trait IntoLiteral {
77 fn into_literal ( self ) -> Literal ;
88}
99
@@ -44,144 +44,66 @@ impl IntoLiteral for usize {
4444}
4545
4646pub enum Instr {
47- EnterScope ,
48- ExitScope ,
4947 Push ( Literal ) ,
50- EnterSource ,
51- ExitSource ,
5248 LoadVar ,
53- LoadSource ,
5449 Operation ( Operation ) ,
5550 Array ,
56- Rec ( usize ) ,
57- Call ( String ) ,
51+ Rec ,
52+ Call ,
5853}
5954
6055impl Instr {
61- pub fn push ( value : impl IntoLiteral ) -> Self {
56+ pub fn lit ( value : impl IntoLiteral ) -> Self {
6257 Instr :: Push ( value. into_literal ( ) )
6358 }
6459}
6560
66- pub enum SourceKind {
67- Events ,
68- Subject ,
69- Subquery ,
70- }
71-
72- pub struct LoadSource {
73- binding : String ,
74- scope : u64 ,
75- }
61+ pub fn codegen_where_clause ( where_clause : & Where ) -> Vec < Instr > {
62+ let mut state = ExprCodegen :: default ( ) ;
7663
77- pub fn codegen ( query : & Query ) -> Vec < Instr > {
78- let mut state = Codegen :: default ( ) ;
79-
80- query. dfs_post_order ( & mut state) ;
64+ where_clause. expr . dfs_post_order ( & mut state) ;
8165
8266 state. instrs
8367}
8468
8569#[ derive( Default ) ]
86- pub struct Codegen {
70+ pub struct ExprCodegen {
8771 instrs : Vec < Instr > ,
8872}
8973
90- impl QueryVisitor for Codegen {
91- type Inner < ' a > = ExprCodegen < ' a > ;
92-
93- fn enter_query ( & mut self , attrs : & NodeAttributes ) {
94- self . instrs . push ( Instr :: push ( attrs. scope ) ) ;
95- self . instrs . push ( Instr :: EnterScope ) ;
96- }
97-
98- fn enter_from ( & mut self , attrs : & NodeAttributes , _ident : & str ) {
99- self . instrs . push ( Instr :: push ( attrs. scope ) ) ;
100- self . instrs . push ( Instr :: EnterSource ) ;
101- }
102-
103- fn on_source_events ( & mut self , attrs : & NodeAttributes , ident : & str ) {
104- self . instrs . push ( Instr :: push ( attrs. scope ) ) ;
105- self . instrs . push ( Instr :: push ( ident) ) ;
106- self . instrs . push ( Instr :: push ( 0i64 ) ) ;
107- self . instrs . push ( Instr :: LoadSource ) ;
74+ impl ExprVisitor for ExprCodegen {
75+ fn on_literal ( & mut self , _attrs : & NodeAttributes , lit : & Literal ) {
76+ self . instrs . push ( Instr :: Push ( lit. clone ( ) ) ) ;
10877 }
10978
110- fn on_source_subject ( & mut self , attrs : & NodeAttributes , ident : & str , subject : & Subject ) {
111- self . instrs . push ( Instr :: push ( attrs. scope ) ) ;
112- self . instrs . push ( Instr :: push ( ident) ) ;
113- for seg in subject. inner . iter ( ) {
114- self . instrs . push ( Instr :: push ( seg) ) ;
79+ fn on_var ( & mut self , _attrs : & NodeAttributes , var : & Var ) {
80+ for seg in var. path . iter ( ) . rev ( ) {
81+ self . instrs . push ( Instr :: lit ( seg) ) ;
11582 }
11683
117- self . instrs . push ( Instr :: push ( subject . inner . len ( ) ) ) ;
84+ self . instrs . push ( Instr :: lit ( var . path . len ( ) + 1 ) ) ;
11885 self . instrs . push ( Instr :: Array ) ;
119- self . instrs . push ( Instr :: push ( 1i64 ) ) ;
120- self . instrs . push ( Instr :: LoadSource ) ;
121- }
122-
123- fn on_source_subquery ( & mut self , attrs : & NodeAttributes , ident : & str ) -> bool {
124- self . instrs . push ( Instr :: push ( attrs. scope ) ) ;
125- self . instrs . push ( Instr :: push ( ident) ) ;
126- self . instrs . push ( Instr :: push ( 2i64 ) ) ;
127- self . instrs . push ( Instr :: LoadSource ) ;
128-
129- true
130- }
131-
132- fn exit_from ( & mut self , attrs : & NodeAttributes , ident : & str ) {
133- self . instrs . push ( Instr :: push ( attrs. scope ) ) ;
134- self . instrs . push ( Instr :: ExitSource ) ;
135- }
136-
137- fn exit_query ( & mut self ) {
138- self . instrs . push ( Instr :: ExitScope ) ;
86+ self . instrs . push ( Instr :: lit ( var. name . as_str ( ) ) ) ;
87+ self . instrs . push ( Instr :: LoadVar ) ;
13988 }
14089
141- fn expr_visitor < ' a > ( & ' a mut self ) -> Self :: Inner < ' a > {
142- ExprCodegen { inner : self }
143- }
144- }
145-
146- pub struct ExprCodegen < ' a > {
147- inner : & ' a mut Codegen ,
148- }
149-
150- impl ExprVisitor for ExprCodegen < ' _ > {
151- fn on_literal ( & mut self , _attrs : & NodeAttributes , lit : & Literal ) {
152- self . inner . instrs . push ( Instr :: Push ( lit. clone ( ) ) ) ;
153- }
154-
155- fn on_var ( & mut self , attrs : & NodeAttributes , var : & Var ) {
156- self . inner . instrs . push ( Instr :: push ( attrs. scope ) ) ;
157-
158- self . inner . instrs . push ( Instr :: push ( var. name . as_str ( ) ) ) ;
159- for seg in & var. path {
160- self . inner . instrs . push ( Instr :: push ( seg) ) ;
90+ fn exit_record ( & mut self , _attrs : & NodeAttributes , record : & Record ) {
91+ for key in record. fields . keys ( ) {
92+ self . instrs . push ( Instr :: lit ( key) ) ;
16193 }
16294
163- self . inner . instrs . push ( Instr :: push ( var. path . len ( ) + 1 ) ) ;
164- self . inner . instrs . push ( Instr :: Array ) ;
165- self . inner . instrs . push ( Instr :: LoadVar ) ;
166- }
167-
168- fn enter_record_entry ( & mut self , _attrs : & NodeAttributes , key : & str , _expr : & Expr ) {
169- self . inner
170- . instrs
171- . push ( Instr :: Push ( Literal :: String ( key. to_string ( ) ) ) ) ;
172- }
173-
174- fn exit_record ( & mut self , _attrs : & NodeAttributes , record : & Record ) {
175- self . inner . instrs . push ( Instr :: Rec ( record. fields . len ( ) ) ) ;
95+ self . instrs . push ( Instr :: lit ( record. fields . len ( ) ) ) ;
96+ self . instrs . push ( Instr :: Rec ) ;
17697 }
17798
17899 fn exit_array ( & mut self , _attrs : & NodeAttributes , values : & [ Expr ] ) {
179- self . inner . instrs . push ( Instr :: push ( values. len ( ) ) ) ;
180- self . inner . instrs . push ( Instr :: Array ) ;
100+ self . instrs . push ( Instr :: lit ( values. len ( ) ) ) ;
101+ self . instrs . push ( Instr :: Array ) ;
181102 }
182103
183104 fn exit_app ( & mut self , _attrs : & NodeAttributes , name : & str , _params : & [ Expr ] ) {
184- self . inner . instrs . push ( Instr :: Call ( name. to_string ( ) ) ) ;
105+ self . instrs . push ( Instr :: lit ( name) ) ;
106+ self . instrs . push ( Instr :: Call ) ;
185107 }
186108
187109 fn exit_binary_op (
@@ -191,10 +113,10 @@ impl ExprVisitor for ExprCodegen<'_> {
191113 _lhs : & Expr ,
192114 _rhs : & Expr ,
193115 ) {
194- self . inner . instrs . push ( Instr :: Operation ( * op) ) ;
116+ self . instrs . push ( Instr :: Operation ( * op) ) ;
195117 }
196118
197119 fn exit_unary_op ( & mut self , _attrs : & NodeAttributes , op : & Operation , _expr : & Expr ) {
198- self . inner . instrs . push ( Instr :: Operation ( * op) ) ;
120+ self . instrs . push ( Instr :: Operation ( * op) ) ;
199121 }
200122}
0 commit comments