-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCompiler.elm
209 lines (167 loc) · 5.31 KB
/
Compiler.elm
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
module Compiler exposing (version, tree)
{-| Module responsible for compiling Elm code to Elixir
@docs version, tree
-}
import Ast
import Ast.Statement exposing (Statement)
import List exposing (..)
import Helpers exposing (..)
import ExContext exposing (Context, Aliases)
import ExAlias
import ExStatement
import Dict
import Regex exposing (..)
{-| Returns current version
-}
version : String
version =
"0.4.25"
glueStart : String
glueStart =
(ind 0)
++ "use Elchemy"
++ "\n"
glueEnd : String
glueEnd =
"\n"
++ String.trim
"""
end
"""
getName : String -> ( String, String )
getName file =
case String.split "\n" file of
n :: rest ->
( n, String.join "\n" rest )
[] ->
( "", "" )
{-| Transforms a code in Elm to code in Elixir
-}
tree : String -> String
tree m =
case String.split ">>>>" m of
[ single ] ->
single
|> parse "NoName.elm"
|> getContext
|> (\( c, a ) ->
case c of
Nothing ->
Debug.crash "Failed getting context"
Just c ->
getCode c a
)
multiple ->
let
files =
multiple
|> map getName
|> map (\( name, code ) -> ( name, parse name code ))
wContexts =
files
|> map (\( name, ast ) -> ( name, getContext ast ))
|> filterMap
(\a ->
case a of
( _, ( Nothing, _ ) ) ->
Nothing
( name, ( Just c, ast ) ) ->
Just ( name, c, ast )
)
commonAliases =
wContexts
|> map (\( name, ctx, ast ) -> ctx.aliases)
|> getCommonAliases
wTrueContexts =
wContexts
|> map (\( name, c, ast ) -> ( name, { c | aliases = commonAliases }, ast ))
in
wTrueContexts
|> map
(\( name, c, ast ) ->
">>>>" ++ name ++ "\n" ++ getCode c ast
)
|> String.join "\n"
getCommonAliases : List Aliases -> Aliases
getCommonAliases a =
foldl
(\aliases acc ->
Dict.merge
Dict.insert
typeAliasDuplicate
Dict.insert
acc
aliases
Dict.empty
)
(Dict.empty)
a
typeAliasDuplicate : comparable -> a -> a -> Dict.Dict comparable a -> Dict.Dict comparable a
typeAliasDuplicate k v v2 =
if v /= v2 then
Debug.crash ("You can't have two different type aliases for " ++ toString k)
else
Dict.insert k v
getContext : List Statement -> ( Maybe Context, List Statement )
getContext statements =
case statements of
[] ->
( Nothing, [] )
mod :: statements ->
let
base =
ExStatement.moduleStatement mod
in
( Just (ExAlias.getAliases base statements), statements )
aggregateStatements : Statement -> ( Context, String ) -> ( Context, String )
aggregateStatements s ( c, code ) =
let
( newC, newCode ) =
ExStatement.elixirS c s
in
( newC, code ++ newCode )
getCode : Context -> List Statement -> String
getCode context statements =
("# Compiled using Elchemy v" ++ version)
++ "\n"
++ ("defmodule " ++ context.mod ++ " do")
++ glueStart
++ ((List.foldl (aggregateStatements) ( context, "" ) statements)
|> Tuple.second
)
++ glueEnd
parse : String -> String -> List Statement
parse fileName m =
case Ast.parse (prepare m) of
Ok ( _, _, statements ) ->
statements
Err ( (), { input, position }, [ msg ] ) ->
Debug.crash
("]ERR> Compilation error in:\n "
++ fileName
++ "\nat:\n "
++ (input
|> String.lines
|> List.take 30
|> String.join "\n"
)
++ "\n"
)
err ->
Debug.crash (toString err)
prepare : String -> String
prepare codebase =
codebase |> removeComments
removeComments : String -> String
removeComments =
-- Need to remove the second one
Regex.replace All (regex "\\s--.*\n") (always "")
>> Regex.replace All (regex "\n +\\w+ : .*") (always "")
crunchSplitLines : String -> String
crunchSplitLines =
Regex.replace All (regex "(?:({-(?:\\n|.)*?-})|([\\w\\])}\"][\\t ]*)\\n[\\t ]+((?!.*\\s->\\s)(?!.*=)(?!.*\\bin\\b)[\\w[({\"]))") <|
\m ->
m.submatches
|> map (Maybe.map (flip (++) " "))
|> filterMap identity
|> String.join " "