-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCompilePipe.fs
More file actions
121 lines (95 loc) · 3.32 KB
/
CompilePipe.fs
File metadata and controls
121 lines (95 loc) · 3.32 KB
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
module YukimiScript.Parser.CompilePipe
open System.IO
open YukimiScript.Parser.Parser
open YukimiScript.Parser.Utils
let loadDom path =
try Ok <| File.ReadAllLines path with e -> Error e
|> Result.bind
(parseLines
>> Result.mapError (fun err -> ParseLinesException (path, err)))
|> Result.bind (Dom.analyze <| Path.GetFileName path)
let findLib libDirs (libName: string) =
let libFileName =
if libName.ToLower().EndsWith ".ykm"
then libName
else "lib" + libName + ".ykm"
libDirs
|> List.tryPick (fun x ->
let libPath = Path.Combine (x, "./" + libFileName)
if File.Exists libPath
then Some libPath else None)
let findLibs libDirs libs =
let succs, fails =
List.foldBack
(fun libName (succs, fails) ->
match findLib libDirs libName with
| Some path -> (path :: succs, fails)
| None -> (succs, libName :: fails))
libs
([], [])
if fails <> [] then Error <| CanNotFindLib fails else Ok succs
let checkRepeat (dom: Dom) =
let findRepeat (items: (string * Elements.DebugInfo) seq) =
Seq.groupBy fst items
|> Seq.choose
(fun (key, matches) ->
if Seq.length matches <= 1 then
None
else
Some(key, matches |> Seq.map snd))
dom.Externs
|> Seq.map (fun (Elements.ExternCommand (cmd, _), _, dbg) -> cmd, dbg)
|> findRepeat
|> Seq.tryHead
|> function
| None -> Ok dom
| Some x ->
Dom.ExternRepeatException x
|> Error
|> Result.bind (fun dom ->
dom.Scenes
|> Seq.map (fun (s, _, dbg) -> s.Name, dbg)
|> findRepeat
|> Seq.tryHead
|> function
| None -> Ok dom
| Some x ->
Dom.SceneRepeatException x
|> Error)
|> Result.bind (fun dom ->
dom.Macros
|> Seq.map (fun (s, _, _, dbg) -> s.Name, dbg)
|> findRepeat
|> Seq.tryHead
|> function
| None -> Ok dom
| Some x ->
Dom.MacroRepeatException x
|> Error)
let checkLib path dom =
if List.isEmpty dom.Scenes then Ok dom else
Error <| Dom.CannotDefineSceneInLibException path
let loadLib path =
loadDom path |> Result.bind (checkLib path)
let loadLibs paths =
List.map loadLib paths
|> Result.transposeList
|> Result.map (List.fold Dom.merge Dom.empty)
|> Result.bind checkRepeat
let getYkmFiles (path: string) =
if File.Exists path
then [|path|]
else
Directory.EnumerateFiles(path, "*.ykm", SearchOption.AllDirectories)
|> Array.ofSeq
let generateIntermediate dom =
checkRepeat dom
|> Result.bind (Dom.expandTextCommands >> Dom.expandUserMacros)
|> Result.bind (fun externAndSysMacro ->
Dom.expandSystemMacros externAndSysMacro
|> Dom.linkToExternCommands)
|> Result.map (fun dom -> dom, Intermediate.ofDom dom)
let compile lib srcPath =
loadDom srcPath
|> Result.map (Dom.merge lib)
|> Result.bind generateIntermediate