git @ Cat's Eye Technologies UampirNexol / 5414cad
Edge in the direction we're headed. There will be a leap. Chris Pressey 10 days ago
2 changed file(s) with 16 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
1212
1313 import Language.UampirNexol.State
1414 import Language.UampirNexol.Expr
15 import Language.UampirNexol.Harvester (extractRoutines)
1516 import Language.UampirNexol.CodeGen
1617
1718 type Env = Map.Map Ident Expr
133134 error $ "unsupported for extraction: " ++ show other
134135
135136 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
137144
138145 checkAndExtract :: Expr -> Either String RoutineMap
139146 checkAndExtract e =
5454
5555 --
5656 -- 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.
5858 --
59 extractRoutines :: Expr -> (Expr, Map.Map Ident Expr)
59 extractRoutines :: Expr -> Map.Map Ident Expr
6060 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'
6367
6468 extractRoutinesM :: Expr -> ExtractorM Expr
6569 extractRoutinesM expr = case expr of
102106 return $ Extern e' t
103107
104108 _ -> 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