@@ -293,16 +293,34 @@ function call(tokens: readonly Token[]): [(undefined | ParseError), (undefined |
293293}
294294
295295function if_then_else ( tokens : readonly Token [ ] ) : [ ( undefined | ParseError ) , ( undefined | _IfThenElse ) , Token [ ] ] {
296- const [ first , ...rest ] = tokens ;
296+ let error : undefined | ParseError = undefined ;
297+ let ast : undefined | _Binding | _Atomic | _Call = undefined ;
298+ const first = tokens [ 0 ] ;
299+ let [ ...rest ] = tokens ;
300+ let condition : undefined | _Atomic ;
301+ let then_branch : undefined | _Block ;
302+ let else_branch : undefined | _Block ;
297303
298- // TODO
299- // _IfThenElse = {token: number, tag: '_IfThenElse', condition: _Atomic, then_branch: _Block, else_branch: _Block} ;
304+ [ error , ast , rest ] = consume ( rest , 'IF' ) ;
305+ if ( error ) return [ error , ast , [ ... tokens ] ] ;
300306
301- return [ {
302- tag : 'ParseError' ,
303- token : first . id ,
304- message : `Expected ?????. Got '${ first . value } ' of type '${ first . lexeme } ' instead.` ,
305- } , undefined , [ ...tokens ] ] ;
307+ [ error , condition , rest ] = atomic ( rest ) ;
308+ if ( error ) return [ error , ast , [ ...tokens ] ] ;
309+
310+ [ error , ast , rest ] = consume ( rest , 'THEN' ) ;
311+ if ( error ) return [ error , ast , [ ...tokens ] ] ;
312+
313+ [ error , then_branch , rest ] = block ( rest ) ;
314+ if ( error ) return [ error , ast , [ ...tokens ] ] ;
315+
316+ [ error , ast , rest ] = consume ( rest , 'ELSE' ) ;
317+ if ( error ) return [ error , ast , [ ...tokens ] ] ;
318+
319+ [ error , else_branch , rest ] = block ( rest ) ;
320+ if ( error ) return [ error , ast , [ ...tokens ] ] ;
321+
322+ const result : _IfThenElse = { token : first . id , tag : '_IfThenElse' , condition : ( condition as _Atomic ) , then_branch : ( then_branch as _Block ) , else_branch : ( else_branch as _Block ) } ;
323+ return [ undefined , result , rest ] ;
306324}
307325
308326function binding ( tokens : readonly Token [ ] ) : [ ( undefined | ParseError ) , ( undefined | _Binding ) , Token [ ] ] {
0 commit comments