-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.hs
85 lines (78 loc) · 3.63 KB
/
nodes.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module Nodes where
import Data.List
import Data.Maybe
import Text.Megaparsec
import Text.Megaparsec.Char
data Node =
StringNode String SourcePos
| NumNode String SourcePos
| IdentifierNode String SourcePos
| BinOpNode Node String Node SourcePos
| UnaryExpr String Node SourcePos
| IfNode Node Node (Maybe Node) SourcePos
| SequenceIfNode [Node] SourcePos
| ProgramNode [Node] SourcePos
| ListNode [Node] SourcePos
| TupleNode [Node] SourcePos
| CallNode Node [Node] SourcePos
| DeclNode Node Node SourcePos
| FuncDefNode (Maybe Node) [Node] Node Bool SourcePos
| MethodNode Node [Node] SourcePos
| NewMethodNode Node Node Node SourcePos
| StructInstanceNode Node [Node] Bool SourcePos
| StructDefNode Node [Node] Bool (Maybe Node) SourcePos
| SumTypeNode [Node] SourcePos
| DeStructure [Node] SourcePos
| DataNode String SourcePos
| BoolNode String SourcePos
| FromStruct Node
| WhereNode Node [Node] SourcePos
| StrictNode Node
| MultipleDefinitionNode [Node]
| ExternalNode Node [Node] SourcePos
| TypeRefNode Node SourcePos
deriving (Eq)
instance Show Node where
show (StringNode str _) = "\"" ++ str ++ "\""
show (SequenceIfNode ifs _) = "[" ++ intercalate ", " (map show ifs) ++ "]"
show (NumNode n _) = n
show (IdentifierNode id _) = id
show (CallNode callee args _) = show callee ++ "(" ++ intercalate ", " (map show args) ++ ")"
show (WhereNode e ds _) = show e ++ "{" ++ intercalate ", " (map show ds) ++"}"
show (MultipleDefinitionNode ds) = "start:\n" ++ intercalate "\n" (map show ds) ++ "\nend"
show (ProgramNode arr _) = intercalate "\n" (map show arr)
show (ListNode xs _) = "[" ++ intercalate ", " (map show xs) ++ "]"
show (TupleNode arr _) = "(" ++ intercalate ", " (map show arr) ++ ",)"
show (BinOpNode a op b _) = "(" ++ show a ++ op ++ show b ++ ")"
show (IfNode ce te ee _) =
"if " ++ show ce ++ " then " ++ show te ++ maybe "" (\e -> " else " ++ show e) ee
show (DeclNode lhs rhs _) = "let " ++ show lhs ++ " <- " ++ show rhs
show (BoolNode b _) = b
show (UnaryExpr u n _) = "(" ++ u ++ " " ++ show n ++ ")"
show (DataNode n _) = n
show (FromStruct n) = "FromStruct: " ++ show n
show (TypeRefNode e _) = "&" ++ show e
show (StructDefNode id ls st ov _) = isStrict st ++ show id ++ "{" ++ intercalate "; " (map show ls) ++ "}" ++ decCase
where
decCase =
case ov of
Just a -> " -> " ++ show a
Nothing -> ""
isStrict a = if a then "!" else ""
show (SumTypeNode ds _) = intercalate " | " (map show ds)
show (StructInstanceNode id ls _ _) = show id ++ "{" ++ intercalate "; " (map show ls) ++ "}"
show (DeStructure ds _) = "{" ++ intercalate ", " (map show ds) ++ "}"
show (MethodNode id args _) = "open " ++ show id ++ "(" ++ intercalate ", " (map show args) ++ ")"
show (ExternalNode id ds pos) = "external " ++ show id ++ "{" ++ intercalate ", " (map show ds) ++ "}"
show (NewMethodNode id ce te _) =
"close " ++ show id ++ " ? " ++ show ce ++ " -> " ++ show te
show (FuncDefNode f args e _ _) = fname ++ " <- " ++ "(" ++ intercalate ", " (map show args) ++ ")" ++ " -> " ++ show e where
fname = maybe "anonymous" show f
extractString (StringNode str _) = str
extractString (NumNode num _) = num
extractString (IdentifierNode ident _) = ident
extractString (DataNode ident _) = ident
extractList (ListNode ls _) = ls
extractList (ProgramNode ls _) = ls
extractDecl (IdentifierNode id _) = id
extractStructInstance (StructInstanceNode id ls _ _) = (id, ls)