Edge in the direction we're headed. There will be a leap.
Chris Pressey
10 days ago
12 | 12 |
|
13 | 13 |
import Language.UampirNexol.State
|
14 | 14 |
import Language.UampirNexol.Expr
|
|
15 |
import Language.UampirNexol.Harvester (extractRoutines)
|
15 | 16 |
import Language.UampirNexol.CodeGen
|
16 | 17 |
|
17 | 18 |
type Env = Map.Map Ident Expr
|
|
133 | 134 |
error $ "unsupported for extraction: " ++ show other
|
134 | 135 |
|
135 | 136 |
extract :: Expr -> Env -> RoutineMap
|
136 | |
extract expr env = execCodeGen "main" $ extractM expr env
|
|
137 |
extract expr env =
|
|
138 |
let
|
|
139 |
_routineExprMap = extractRoutines expr
|
|
140 |
in
|
|
141 |
-- FIXME: extract code for each of the routine exprs in routineExprMap.
|
|
142 |
-- that might mean we don't do the routine switching stuff in this monad.
|
|
143 |
execCodeGen "main" $ extractM expr env
|
137 | 144 |
|
138 | 145 |
checkAndExtract :: Expr -> Either String RoutineMap
|
139 | 146 |
checkAndExtract e =
|
54 | 54 |
|
55 | 55 |
--
|
56 | 56 |
-- Main function to extract routines from an expression
|
57 | |
-- Returns the transformed expression and a map of routine names to their expressions
|
|
57 |
-- Returns a map of routine names to their expressions.
|
58 | 58 |
--
|
59 | |
extractRoutines :: Expr -> (Expr, Map.Map Ident Expr)
|
|
59 |
extractRoutines :: Expr -> Map.Map Ident Expr
|
60 | 60 |
extractRoutines expr =
|
61 | |
let (expr', st) = runState (extractRoutinesM expr) initialExtractorState
|
62 | |
in (expr', routineMap st)
|
|
61 |
let
|
|
62 |
(expr', st) = runState (extractRoutinesM expr) initialExtractorState
|
|
63 |
r = routineMap st
|
|
64 |
r' = Map.insert "main" expr' r
|
|
65 |
in
|
|
66 |
r'
|
63 | 67 |
|
64 | 68 |
extractRoutinesM :: Expr -> ExtractorM Expr
|
65 | 69 |
extractRoutinesM expr = case expr of
|
|
102 | 106 |
return $ Extern e' t
|
103 | 107 |
|
104 | 108 |
_ -> return expr
|
105 | |
|
106 | |
--
|
107 | |
-- Helper function to get the extracted routines as a list
|
108 | |
--
|
109 | |
getExtractedRoutines :: Map.Map Ident Expr -> [(Ident, Expr)]
|
110 | |
getExtractedRoutines = Map.toList
|