-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduceMethods.hs
67 lines (58 loc) · 2.17 KB
/
reduceMethods.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module ReduceMethods where
import Nodes
import Scope
import Data.Void
import EitherUtility
import Debug.Trace
import Data.List
import Control.Monad
import qualified Text.Megaparsec as P
-- A pass that turns methods into functions
methodFun p@(ProgramNode ps pos) = ProgramNode (getNodes $ map mFun ps) pos where
mFun (MethodNode id args pos) =
Just $ DeclNode id (
FuncDefNode
(Just id)
args
(
SequenceIfNode (
getNodes $ map (removeDef . convMethod (extractString id)) ps ++
map (convMethod $ extractString id) (filter getDefKey ps)
) pos
)
Prelude.False
pos
) pos where
ns = getNodes $ map (convMethod $ extractString id) ps
mFun a = Just a
getNodes :: [Maybe Node] -> [Node]
getNodes xs =
map (\(Just a) -> a) (
filter (
\x ->
case x of
Just _ -> True
Nothing -> False
) xs
)
getDefKey :: Node -> Bool
getDefKey (NewMethodNode fid@(IdentifierNode id _) (IdentifierNode "def" _) te pos) = True
getDefKey _ = False
removeDef :: Maybe Node -> Maybe Node
removeDef (Just (IfNode (IdentifierNode "def" _) _ _ _)) = Nothing
removeDef a = a
convMethod :: String -> Node -> Maybe Node
convMethod rid (NewMethodNode (IdentifierNode id _) ce te pos) = if rid == id then Just $ IfNode ce te Nothing pos else Nothing
convMethod _ _ = Nothing
-- Remove all the method nodes
removeNewMethods :: Scope -> Node -> Node
removeNewMethods sc (ProgramNode dcs pos) = ProgramNode ls pos where
noNothings n =
case n of
(NewMethodNode id _ _ pos) -> not $ StringPos (extractString id) pos `existsIn` sc
n -> True
ls = filter noNothings dcs
-- IO for method fun
runMethodFun :: Scope -> Either (P.ParseErrorBundle String Data.Void.Void) Node -> Either String Node
runMethodFun sc (Left e) = Left $ P.errorBundlePretty e
runMethodFun sc (Right n) = Right $ methodFun n