Skip to content

Commit 4303d76

Browse files
oxe-ioxe-i
andauthored
organize generator helper functions in a separate file (#52)
Co-authored-by: oxe-i <[email protected]>
1 parent 7b718fc commit 4303d76

File tree

4 files changed

+57
-42
lines changed

4 files changed

+57
-42
lines changed

generators/GenerateTestFile.lean

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -216,20 +216,17 @@ def addImport (pascalExercise : String) : IO Unit := do
216216

217217
def generateStub (exercise : String) : IO Unit := do
218218
let pascalExercise := pascalCase exercise
219-
let content := s!"import Lean
219+
let content :=
220+
s!"import Lean
220221
import Std
222+
import Helper
221223
222224
open Lean
223225
open Std
226+
open Helper
224227
225228
namespace {pascalExercise}Generator
226229
227-
instance \{α β} [BEq α] [BEq β] : BEq (Except α β) where
228-
beq
229-
| .ok a, .ok b => a == b
230-
| .error e1, .error e2 => e1 == e2
231-
| _, _ => false
232-
233230
def genIntro (exercise : String) : String := s!\"import LeanTest
234231
import \{exercise}
235232
@@ -238,40 +235,6 @@ open LeanTest
238235
def \{exercise.decapitalize}Tests : TestSuite :=
239236
(TestSuite.empty \\\"\{exercise}\\\")\"
240237
241-
def getOk \{α β} (except : Except α β) [Inhabited β] : β := except.toOption |> (·.get!)
242-
243-
def errorToOption (expected : Json) : Option String :=
244-
match expected.getObjVal? \"error\" with
245-
| .error _ => some s!\"\{expected}\"
246-
| .ok _ => none
247-
248-
def toExcept (expected : Json) : Except String String :=
249-
match expected.getObjVal? \"error\" with
250-
| .error _ => .ok s!\"\{expected}\"
251-
| .ok msg => .error msg.compress
252-
253-
def exceptToString \{α β} [ToString α] [ToString β] (except : Except α β) : String :=
254-
match except with
255-
| .ok value => s!\"(.ok \{value})\"
256-
| .error msg => s!\"(.error \{msg})\"
257-
258-
def insertAllInputs (input : Json) : String :=
259-
let map := getOk input.getObj?
260-
map.values.map (fun val => s!\"\{val}\") |> (String.intercalate \" \" .)
261-
262-
def intLiteral (n : Int) : String :=
263-
if n < 0 then s!\"(\{n})\"
264-
else s!\"\{n}\"
265-
266-
def toFloat (value : Json) : Float :=
267-
value.getNum? |> (getOk .) |> (·.toFloat)
268-
269-
def toLiteral (string : String) : String :=
270-
string.dropWhile (·=='\"') |> (·.dropRightWhile (·=='\"'))
271-
272-
def getFunName (property : Json) : String :=
273-
toLiteral property.compress
274-
275238
def genTestCase (exercise : String) (case : TreeMap.Raw String Json) : String :=
276239
let input := case.get! \"input\"
277240
let expected := case.get! \"expected\"

generators/Generator/Generator.lean

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ namespace Generator
2222
abbrev introGenerator := String -> String
2323
abbrev testCaseGenerator := String -> Std.TreeMap.Raw String Lean.Json -> String
2424
abbrev endBodyGenerator := String -> String
25-
abbrev extraCasesList := List String
2625

2726
def dispatch : Std.HashMap String (introGenerator × testCaseGenerator × endBodyGenerator) :=
2827
Std.HashMap.ofList [

generators/Generator/Helper.lean

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Std
2+
import Lean
3+
4+
open Std
5+
open Lean
6+
7+
namespace Helper
8+
9+
instance {α β} [BEq α] [BEq β] : BEq (Except α β) where
10+
beq
11+
| .ok a, .ok b => a == b
12+
| .error e1, .error e2 => e1 == e2
13+
| _, _ => false
14+
15+
def getOk {α β} (except : Except α β) [Inhabited β] : β := except.toOption |> (·.get!)
16+
17+
def errorToOption (expected : Json) : Option String :=
18+
match expected.getObjVal? "error" with
19+
| .error _ => some s!"{expected}"
20+
| .ok _ => none
21+
22+
def toExcept (expected : Json) : Except String String :=
23+
match expected.getObjVal? "error" with
24+
| .error _ => .ok s!"{expected}"
25+
| .ok msg => .error msg.compress
26+
27+
def exceptToString {α β} [ToString α] [ToString β] (except : Except α β) : String :=
28+
match except with
29+
| .ok value => s!"(.ok {value})"
30+
| .error msg => s!"(.error {msg})"
31+
32+
def insertAllInputs (input : Json) : String :=
33+
let map := getOk input.getObj?
34+
map.values.map (fun val => s!"{val}") |> (String.intercalate " " .)
35+
36+
def intLiteral (n : Int) : String :=
37+
if n < 0 then s!"({n})"
38+
else s!"{n}"
39+
40+
def toFloat (value : Json) : Float :=
41+
value.getNum? |> (getOk .) |> (·.toFloat)
42+
43+
def toLiteral (string : String) : String :=
44+
string.dropWhile (·=='"') |> (·.dropRightWhile (·=='"'))
45+
46+
def getFunName (property : Json) : String :=
47+
toLiteral property.compress
48+
49+
end Helper

generators/lakefile.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ defaultTargets = ["generator"]
66
name = "Generator"
77
srcDir = "Generator"
88

9+
[[lean_lib]]
10+
name = "Helper"
11+
srcDir = "Generator"
12+
913
[[lean_exe]]
1014
name = "generator"
1115
root = "GenerateTestFile"

0 commit comments

Comments
 (0)