@@ -214,24 +214,13 @@ compile env (List [Atom "expand", _body]) copts = do
214
214
val <- Language.Scheme.Macro. expand env False _body Language.Scheme.Core. apply
215
215
compileScalar (" return $ " ++ astToHaskellStr val) copts
216
216
217
- compile env (List (Atom " let-syntax" : List _bindings : _body)) copts@ ( CompileOptions tfnc uvar uargs nfnc) = do
217
+ compile env (List (Atom " let-syntax" : List _bindings : _body)) copts = do
218
218
-- TODO: check if let-syntax has been rebound?
219
219
bodyEnv <- liftIO $ extendEnv env []
220
220
_ <- Language.Scheme.Macro. loadMacros env bodyEnv Nothing False _bindings
221
221
-- Expand whole body as a single continuous macro, to ensure hygiene
222
222
expanded <- Language.Scheme.Macro. expand bodyEnv False (List _body) Language.Scheme.Core. apply
223
-
224
- -- TODO: should be able to consolidate with the one from macroEval
225
- -- also, need to use for the other 'expand' call below
226
- vars <- Language.Scheme.Macro. getDivertedVars bodyEnv
227
- case vars of
228
- [] -> func bodyEnv expanded copts
229
- _ -> do
230
- Atom symNext <- _gensym " afterDivert"
231
- diverted <- compileDivertedVars symNext bodyEnv vars copts
232
- rest <- func bodyEnv expanded $ CompileOptions symNext uvar uargs nfnc
233
- return $ [diverted] ++ rest
234
-
223
+ divertVars bodyEnv expanded copts func
235
224
where
236
225
func bodyEnv' expanded' copts' = do
237
226
case expanded' of
@@ -244,9 +233,12 @@ compile env (List (Atom "letrec-syntax" : List _bindings : _body)) copts = do
244
233
_ <- Language.Scheme.Macro. loadMacros bodyEnv bodyEnv Nothing False _bindings
245
234
-- Expand whole body as a single continuous macro, to ensure hygiene
246
235
expanded <- Language.Scheme.Macro. expand bodyEnv False (List _body) Language.Scheme.Core. apply
247
- case expanded of
248
- List e -> compile bodyEnv (List $ Atom " begin" : e) copts
249
- e -> compile bodyEnv e copts
236
+ divertVars bodyEnv expanded copts func
237
+ where
238
+ func bodyEnv' expanded' copts' = do
239
+ case expanded' of
240
+ List e -> compile bodyEnv' (List $ Atom " begin" : e) copts'
241
+ e -> compile bodyEnv' e copts'
250
242
251
243
compile env (List [Atom " define-syntax" , Atom keyword,
252
244
(List [Atom " er-macro-transformer" ,
@@ -659,17 +651,32 @@ compile _ badForm _ = throwError $ BadSpecialForm "Unrecognized special form" ba
659
651
mcompile :: Env -> LispVal -> CompOpts -> IOThrowsError [HaskAST ]
660
652
mcompile env lisp copts = mfunc env lisp compile copts
661
653
mfunc :: Env -> LispVal -> (Env -> LispVal -> CompOpts -> IOThrowsError [HaskAST ]) -> CompOpts -> IOThrowsError [HaskAST ]
662
- mfunc env lisp func copts@ (CompileOptions tfnc uvar uargs nfnc) = do
663
-
664
- transformed <- Language.Scheme.Macro. macroEval env lisp Language.Scheme.Core. apply
665
-
654
+ mfunc env lisp func copts = do
655
+ expanded <- Language.Scheme.Macro. macroEval env lisp Language.Scheme.Core. apply
656
+ divertVars env expanded copts func
657
+
658
+ -- | Do the actual insertion of diverted variables back to the
659
+ -- compiled program.
660
+ divertVars ::
661
+ Env ->
662
+ -- ^ Current compile Environment
663
+ LispVal ->
664
+ -- ^ Lisp code after macro expansion
665
+ CompOpts ->
666
+ -- ^ Compiler options
667
+ (Env -> LispVal -> CompOpts -> IOThrowsError [HaskAST ]) ->
668
+ -- ^ Continuation to call into after vars are diverted
669
+ IOThrowsError [HaskAST ]
670
+ -- ^ Code generated by the continuation, along with the code
671
+ -- added to divert vars to the compiled program
672
+ divertVars env expanded copts@ (CompileOptions tfnc uvar uargs nfnc) func = do
666
673
vars <- Language.Scheme.Macro. getDivertedVars env
667
674
case vars of
668
- [] -> func env transformed copts
675
+ [] -> func env expanded copts
669
676
_ -> do
670
677
Atom symNext <- _gensym " afterDivert"
671
678
diverted <- compileDivertedVars symNext env vars copts
672
- rest <- func env transformed $ CompileOptions symNext uvar uargs nfnc
679
+ rest <- func env expanded $ CompileOptions symNext uvar uargs nfnc
673
680
return $ [diverted] ++ rest
674
681
675
682
-- | Take a list of variables diverted into env at compile time, and
0 commit comments