-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.lean
73 lines (63 loc) · 2.19 KB
/
Main.lean
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
/-
Copyright (c) 2024 VCA Lab, EPFL. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Yann Herklotz
-/
import DataflowRewriter.ExprHigh
import DataflowRewriter.DotParser
import DataflowRewriter.Rewriter
import DataflowRewriter.Rewrites.MergeRewrite
import DataflowRewriter.DynamaticPrinter
import DataflowRewriter.Rewrites.ForkRewrite
open Batteries (AssocList)
open DataflowRewriter
structure CmdArgs where
outputFile : Option System.FilePath
inputFile : Option System.FilePath
help : Bool
deriving Inhabited
def CmdArgs.empty : CmdArgs := ⟨none, none, false⟩
def parseArgs (args : List String) : Except String CmdArgs := go CmdArgs.empty args
where
go (c : CmdArgs) : List String → Except String CmdArgs
| .cons "-h" _rst | .cons "--help" _rst => .ok {c with help := true}
| .cons "-o" (.cons fp rst) | .cons "--output" (.cons fp rst) =>
go {c with outputFile := some fp} rst
| .cons fp rst => do
if "-".isPrefixOf fp then throw s!"argument '{fp}' not recognised"
if c.inputFile.isSome then throw s!"more than one input file passed"
go {c with inputFile := some fp} rst
| [] => do
if c.inputFile.isNone then throw s!"no input file passed"
return c
def helpText : String :=
"dataflow-rewriter -- v0.1.0
FORMAT
dataflow-rewriter [OPTIONS...] FILE
OPTIONS
-h, --help Print this help text
-o, --output FILE Set output file
"
def main (args : List String) : IO Unit := do
let parsed ←
try IO.ofExcept <| parseArgs args
catch
| .userError s => do
IO.eprintln ("error: " ++ s)
IO.print helpText
IO.Process.exit 1
| e => throw e
if parsed.help then
IO.print helpText
return
let fileContents ← IO.FS.readFile parsed.inputFile.get!
let (exprHigh, assoc) ← IO.ofExcept fileContents.toExprHigh
let rewrittenExprHigh ← IO.ofExcept <|
rewrite_loop "rw" exprHigh [MergeRewrite.rewrite, ForkRewrite.rewrite] 100
let some l := dynamaticString rewrittenExprHigh assoc.inverse
| IO.eprintln s!"Failed to print ExprHigh: {rewrittenExprHigh}"
match parsed.outputFile with
| some ofile =>
IO.FS.writeFile ofile l
| none =>
IO.println l