@@ -19,7 +19,7 @@ use parser::{Command, Identifier, Redirection};
1919
2020use crate :: {
2121 ir:: {
22- Instruction , Label , MaybeImm , NonLocal , Reg ,
22+ Instruction , Label , NonLocal , Reg ,
2323 lower:: { Bytecode , Code } ,
2424 } ,
2525 types:: Value ,
@@ -32,7 +32,6 @@ pub enum ExecMode {
3232 Posix ,
3333}
3434
35- #[ derive( Debug ) ]
3635pub struct Interpreter < ' a > {
3736 arena : & ' a Bump ,
3837 bc : Bytecode < ' a > ,
@@ -121,20 +120,25 @@ impl<'a> Consts<'a> {
121120impl Interpreter < ' _ > {
122121 pub fn run ( & mut self ) {
123122 macro_rules! rx {
124- ( $self: expr, $dest: expr, $src: ident, $e: expr) => { {
125- rx!( $self, $src) ;
123+ ( $self: expr, $dest: expr, $src: ident: $ty: ident, $e: expr) => { {
124+ rx!( $self, $src: $ty) ;
125+ $self. registers. write( $dest, $e) ;
126+ } } ;
127+ ( $self: expr, $dest: expr, $lhs: ident: $tyl: ident, $rhs: ident: $tyr: ident, $e: expr) => { {
128+ rx!( $self, $lhs: $tyl, $rhs: $tyr) ;
126129 $self. registers. write( $dest, $e) ;
127130 } } ;
128- ( $self: expr, $( $src: ident) ,+) => {
129- $( let $src = match $src {
130- MaybeImm :: Reg ( src) => $self. registers. get( src) ,
131- MaybeImm :: Rec ( _) => todo!( ) ,
132- MaybeImm :: Imm ( src) => & Value :: Int ( src. into( ) ) ,
133- MaybeImm :: ImmCnt ( src) => & $self. consts. 0 . get_index( src as _) . unwrap( ) . clone( ) ,
134- MaybeImm :: ImmUserVar ( src) => {
135- & $self. symbols. lookup_user_scalar( NonLocal ( src. into( ) ) ) . clone( )
131+ ( $self: expr, $( $src: ident: $ty: ident) ,+) => {
132+ use $crate:: ir:: ArgTy ;
133+ $( let $src = match $ty {
134+ ArgTy :: Reg => $self. registers. get( unsafe { $src. reg } ) ,
135+ ArgTy :: Rec => todo!( ) ,
136+ ArgTy :: Imm => & Value :: Int ( unsafe { $src. imm } as _) ,
137+ ArgTy :: Cnt => & $self. consts. 0 . get_index( unsafe { $src. sym. 0 } as _) . unwrap( ) . clone( ) ,
138+ ArgTy :: UsVal => {
139+ & $self. symbols. lookup_user_scalar( unsafe { $src. sym } ) . clone( )
136140 }
137- MaybeImm :: ImmBuiltinVar ( _ ) => todo!( ) ,
141+ _ => todo!( )
138142 } ; ) +
139143 } ;
140144 ( $self: expr, $dest: expr, $lhs: ident, $rhs: ident, $e: expr) => { {
@@ -144,92 +148,102 @@ impl Interpreter<'_> {
144148 }
145149 while let Some ( & instr) = self . bc . code . get ( self . program_counter ) {
146150 match instr {
147- Instruction :: Record ( _) => todo ! ( ) ,
148- Instruction :: Negation ( ( dest, src) ) => {
149- rx ! ( self , dest, src, Value :: b2f( !src. to_bool( ) ) ) ;
151+ Instruction :: Record { dest : _, arg : _, ty : _ } => todo ! ( ) ,
152+ Instruction :: Negation { dest, arg, ty } => {
153+ rx ! ( self , dest, arg: ty, Value :: b2f( !arg. to_bool( ) ) ) ;
154+ }
155+ Instruction :: ToInt { dest, arg, ty } => {
156+ rx ! ( self , dest, arg: ty, Value :: Float ( arg. to_num( ) . trunc( ) ) ) ;
157+ }
158+ Instruction :: Negative { dest, arg, ty } => {
159+ rx ! ( self , dest, arg: ty, Value :: Float ( -arg. to_num( ) ) ) ;
160+ }
161+ Instruction :: Copy { dest, arg, ty } => rx ! ( self , dest, arg: ty, arg. clone( ) ) ,
162+ Instruction :: Eq { dest, lhs, rhs, tyl, tyr } => {
163+ rx ! ( self , dest, lhs: tyl, rhs: tyr, Value :: b2f( lhs == rhs) ) ;
150164 }
151- Instruction :: ToInt ( ( dest, src ) ) => {
152- rx ! ( self , dest, src , Value :: Float ( src . to_num ( ) . trunc ( ) ) ) ;
165+ Instruction :: NEq { dest, lhs , rhs , tyl , tyr } => {
166+ rx ! ( self , dest, lhs : tyl , rhs : tyr , Value :: b2f ( lhs != rhs ) ) ;
153167 }
154- Instruction :: Negative ( ( dest, src ) ) => {
155- rx ! ( self , dest, src , Value :: Float ( -src . to_num ( ) ) ) ;
168+ Instruction :: Gt { dest, lhs , rhs , tyl , tyr } => {
169+ rx ! ( self , dest, lhs : tyl , rhs : tyr , Value :: b2f ( lhs > rhs ) ) ;
156170 }
157- Instruction :: Copy ( ( dest, src) ) => rx ! ( self , dest, src, src. clone( ) ) ,
158- Instruction :: Eq ( ( dest, lhs, rhs) ) => {
159- rx ! ( self , dest, lhs, rhs, Value :: b2f( lhs == rhs) ) ;
171+ Instruction :: Lt { dest, lhs, rhs, tyl, tyr } => {
172+ rx ! ( self , dest, lhs: tyl, rhs: tyr, Value :: b2f( lhs < rhs) ) ;
160173 }
161- Instruction :: NEq ( ( dest, lhs, rhs) ) => {
162- rx ! ( self , dest, lhs, rhs, Value :: b2f( lhs ! = rhs) ) ;
174+ Instruction :: LtE { dest, lhs, rhs, tyl , tyr } => {
175+ rx ! ( self , dest, lhs: tyl , rhs: tyr , Value :: b2f( lhs < = rhs) ) ;
163176 }
164- Instruction :: Gt ( ( dest, lhs, rhs) ) => {
165- rx ! ( self , dest, lhs, rhs, Value :: b2f( lhs > rhs) ) ;
177+ Instruction :: GtE { dest, lhs, rhs, tyl , tyr } => {
178+ rx ! ( self , dest, lhs: tyl , rhs: tyr , Value :: b2f( lhs >= rhs) ) ;
166179 }
167- Instruction :: Lt ( ( dest, lhs, rhs) ) => {
168- rx ! ( self , dest, lhs, rhs, Value :: b2f( lhs < rhs) ) ;
180+ Instruction :: And { dest : _, lhs : _, rhs : _, tyr : _, tyl : _ } => todo ! ( ) ,
181+ Instruction :: Or { dest : _, lhs : _, rhs : _, tyr : _, tyl : _ } => todo ! ( ) ,
182+ Instruction :: Matches { dest : _, lhs : _, rhs : _, tyr : _, tyl : _ } => todo ! ( ) ,
183+ Instruction :: MatchesNot { dest : _, lhs : _, rhs : _, tyr : _, tyl : _ } => todo ! ( ) ,
184+ Instruction :: Add { dest, lhs, rhs, tyl, tyr } => {
185+ rx ! ( self , dest, lhs: tyl, rhs: tyr, lhs + rhs) ;
169186 }
170- Instruction :: LtE ( ( dest, lhs, rhs) ) => {
171- rx ! ( self , dest, lhs, rhs, Value :: b2f ( lhs <= rhs) ) ;
187+ Instruction :: Subtract { dest, lhs, rhs, tyl , tyr } => {
188+ rx ! ( self , dest, lhs: tyl , rhs: tyr , lhs - rhs) ;
172189 }
173- Instruction :: GtE ( ( dest, lhs, rhs) ) => {
174- rx ! ( self , dest, lhs, rhs, Value :: b2f ( lhs >= rhs) ) ;
190+ Instruction :: Multiply { dest, lhs, rhs, tyl , tyr } => {
191+ rx ! ( self , dest, lhs: tyl , rhs: tyr , lhs * rhs) ;
175192 }
176- Instruction :: And ( ( _dest, _lhs, _rhs) ) => todo ! ( ) ,
177- Instruction :: Or ( ( _dest, _lhs, _rhs) ) => todo ! ( ) ,
178- Instruction :: Matches ( ( _dest, _lhs, _rhs) ) => todo ! ( ) ,
179- Instruction :: MatchesNot ( ( _dest, _lhs, _rhs) ) => todo ! ( ) ,
180- Instruction :: Add ( ( dest, lhs, rhs) ) => rx ! ( self , dest, lhs, rhs, lhs + rhs) ,
181- Instruction :: Subtract ( ( dest, lhs, rhs) ) => rx ! ( self , dest, lhs, rhs, lhs - rhs) ,
182- Instruction :: Multiply ( ( dest, lhs, rhs) ) => rx ! ( self , dest, lhs, rhs, lhs * rhs) ,
183- Instruction :: Divide ( ( dest, lhs, rhs) ) => rx ! ( self , dest, lhs, rhs, lhs / rhs) ,
184- Instruction :: Raise ( ( dest, lhs, rhs) ) => rx ! ( self , dest, lhs, rhs, lhs ^ rhs) ,
185- Instruction :: Modulo ( ( dest, lhs, rhs) ) => rx ! ( self , dest, lhs, rhs, lhs % rhs) ,
186- Instruction :: Concat ( ( dest, lhs, rhs) ) => {
187- rx ! ( self , lhs, rhs) ;
193+ Instruction :: Divide { dest, lhs, rhs, tyl, tyr } => {
194+ rx ! ( self , dest, lhs: tyl, rhs: tyr, lhs / rhs) ;
195+ }
196+ Instruction :: Raise { dest, lhs, rhs, tyl, tyr } => {
197+ rx ! ( self , dest, lhs: tyl, rhs: tyr, lhs ^ rhs) ;
198+ }
199+ Instruction :: Modulo { dest, lhs, rhs, tyl, tyr } => {
200+ rx ! ( self , dest, lhs: tyl, rhs: tyr, lhs % rhs) ;
201+ }
202+ Instruction :: Concat { dest, lhs, rhs, tyl, tyr } => {
203+ rx ! ( self , lhs: tyl, rhs: tyr) ;
188204 let mut buf =
189205 StdVec :: with_capacity ( lhs. string_size_hint ( ) + rhs. string_size_hint ( ) ) ;
190206 lhs. write_string ( & mut buf) ;
191207 rhs. write_string ( & mut buf) ;
192208 self . registers . write ( dest, Value :: String ( buf. into ( ) ) ) ;
193209 }
194- Instruction :: LoadUserScalar ( ( dest, src) ) => {
195- let val = self . symbols . lookup_user_scalar ( src) ;
196- self . registers . write ( dest, val. clone ( ) ) ;
197- }
198- Instruction :: LoadUserArray ( ( _dest, _start, _end, _src) ) => todo ! ( ) ,
199- Instruction :: LoadUserMDimArray ( ( _dest, _start, _end, _place) ) => todo ! ( ) ,
200- Instruction :: LoadBuiltinScalar ( ( _dest, _src) ) => todo ! ( ) ,
201- Instruction :: LoadBuiltinArray ( ( _dest, _src, _start, _end) ) => todo ! ( ) ,
202- Instruction :: LoadConst ( ( dest, src) ) => {
203- let val = self . consts . 0 . get_index ( src. 0 as _ ) . unwrap ( ) . clone ( ) ;
204- self . registers . write ( dest, val) ;
210+ Instruction :: LoadA { dest : _, ty_place : _, start : _, end : _, var : _ } => todo ! ( ) ,
211+ Instruction :: StoreS { dest, ty_place, var, arg, ty } => {
212+ rx ! ( self , arg: ty) ;
213+ match ty_place {
214+ ArgTy :: UsVal => self . symbols . write_user_val ( var, arg. clone ( ) ) ,
215+ ArgTy :: IsVal => todo ! ( ) ,
216+ _ => unreachable ! ( ) ,
217+ }
218+ self . registers . write ( dest, arg. clone ( ) ) ;
205219 }
206- Instruction :: StoreUserScalar ( ( dest, imm, src) ) => {
207- rx ! ( self , imm) ;
208- self . symbols . write_user_val ( src, imm. clone ( ) ) ;
209- self . registers . write ( dest, imm. clone ( ) ) ;
220+ Instruction :: StoreR { dest : _, src : _, arg : _, ty : _, tys : _ } => {
221+ todo ! ( )
210222 }
211- Instruction :: StoreUserArray ( ( _dest, _src, _start, _end) ) => todo ! ( ) ,
212- Instruction :: StoreUserMDimArray ( ( _dest, _start, _end, _place) ) => todo ! ( ) ,
213- Instruction :: StoreRecord ( _) => todo ! ( ) ,
214- Instruction :: StoreBuiltinScalar ( ( _dest, _imm, _src) ) => todo ! ( ) ,
215- Instruction :: StoreBuiltinArray ( ( _dest, _src, _start, _end) ) => todo ! ( ) ,
216- Instruction :: IntrinsicCall ( ( _dest, _start, _end, _fun) ) => todo ! ( ) ,
217- Instruction :: OutputCall ( ( start, end, fun, redir) ) => {
218- self . intrinsic_print ( start, end, fun, redir) ;
223+ Instruction :: StoreA {
224+ dest : _,
225+ ty_place : _,
226+ start : _,
227+ end : _,
228+ var : _,
229+ arg : _,
230+ } => todo ! ( ) ,
231+ Instruction :: IntrinsicCall { dest : _, start : _, end : _, name : _ } => todo ! ( ) ,
232+ Instruction :: OutputCall { start, end, cmd, redir } => {
233+ self . intrinsic_print ( start, end, cmd, redir) ;
219234 }
220- Instruction :: UserCall ( ( _dest , _start , _end , _fun ) ) => todo ! ( ) ,
221- Instruction :: IndirectCall ( ( _dest , _start , _end , _fun ) ) => todo ! ( ) ,
222- Instruction :: Jump ( Label ( label) ) => {
235+ Instruction :: UserCall { dest : _ , start : _ , end : _ , name : _ } => todo ! ( ) ,
236+ Instruction :: IndirectCall { dest : _ , start : _ , end : _ , name : _ , ty : _ } => todo ! ( ) ,
237+ Instruction :: Jump { to : Label ( label) } => {
223238 self . program_counter = label as _ ;
224239 continue ;
225240 }
226- Instruction :: Return ( _src) => todo ! ( ) ,
227- Instruction :: Branch ( ( src, Label ( true_to) , Label ( false_to) ) ) => {
228- rx ! ( self , src) ;
229- if src. to_bool ( ) {
230- self . program_counter = true_to as _ ;
241+ Instruction :: Return { arg : _, ty : _ } => todo ! ( ) ,
242+ Instruction :: Branch { then_label, else_label, condition } => {
243+ if self . registers . get ( condition) . to_bool ( ) {
244+ self . program_counter = then_label. 0 as _ ;
231245 } else {
232- self . program_counter = false_to as _ ;
246+ self . program_counter = else_label . 0 as _ ;
233247 }
234248 continue ;
235249 }
0 commit comments