Skip to content

Commit 41a44c5

Browse files
ynfleee7
andcommitted
dedent and fix underslug
Co-authored-by: ee7 <[email protected]>
1 parent 53e4a05 commit 41a44c5

File tree

2 files changed

+101
-14
lines changed

2 files changed

+101
-14
lines changed

src/representer.nim

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@ import representer/[mapping, types]
44
import docopt
55

66
const doc = """
7-
Exercism nim representation normalizer.
7+
Exercism nim representation normalizer.
88
9-
Usage:
10-
representer --slug=<slug> --input-dir=<in-dir> [--output-dir=<out-dir>] [--print]
9+
Usage:
10+
representer --slug=<slug> --input-dir=<in-dir> [--output-dir=<out-dir>] [--print]
1111
12-
Options:
13-
-h --help Show this help message.
14-
-v, --version Display version.
15-
-p, --print Print the results.
16-
-s <slug>, --slug=<slug> The exercise slug.
17-
-i <in-dir>, --input-dir=<in-dir> The directory of the submission and exercise files.
18-
-o <out-dir>, --output-dir=<out-dir> The directory to output to.
19-
If omitted, output will be written to stdout.
20-
"""
12+
Options:
13+
-h --help Show this help message.
14+
-v, --version Display version.
15+
-p, --print Print the results.
16+
-s <slug>, --slug=<slug> The exercise slug.
17+
-i <in-dir>, --input-dir=<in-dir> The directory of the submission and exercise files.
18+
-o <out-dir>, --output-dir=<out-dir> The directory to output to.
19+
If omitted, output will be written to stdout.
20+
""".dedent
2121

2222
proc getFileContents(fileName: string): string = readFile fileName
2323

24-
func underSlug(s: string): string = s.replace('-', '_')
24+
func kebabToSnakeCase(s: string): string = s.replace('-', '_')
2525

2626
proc main() =
2727
let args = docopt(doc)
2828
let intr = loadScript(NimScriptPath("src/representer/loader.nims"))
2929
let (tree, map) = intr.invoke(
3030
getTestableRepresentation,
31-
getFileContents($args["--input-dir"] / underslug($args["--slug"]) & ".nim"), true,
31+
getFileContents($args["--input-dir"] / kebabToSnakeCase($args["--slug"]) & ".nim"), true,
3232
returnType = SerializedRepresentation
3333
)
3434
if args["--output-dir"]:
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import micros
2+
# CaseStmt ConstDef ElIfBranch ElseBranch EnumDef EnumField ForLoop IdentDef IfStmt LetDef NimName ObjectDef OfBranch PragmaVal RoutineNode RoutineSym StmtList TypeDefs = (EnumDef or ObjectDef) VarDef VarDefs = (VarDef or LetDef or ConstDef) WhenStmt WhileLoop
3+
4+
type
5+
Comment* = distinct NimNode
6+
Call* = distinct NimNode
7+
Infix* = distinct NimNode
8+
Command* = distinct NimNode
9+
Prefix* = distinct NimNode
10+
Calls* = Call | Infix | Command | Prefix
11+
ImportStmt* = distinct NimNode
12+
ImportFrom* = distinct NimNode
13+
ImportExcept* = distinct NimNode
14+
Imports* = ImportStmt | ImportFrom | ImportExcept
15+
ExportStmt* = distinct NimNode
16+
DotExpr* = distinct NimNode
17+
18+
func isa*(n: NimNode, _: typedesc[Comment]): bool =
19+
n.checkIt {nnkCommentStmt}
20+
21+
func comment*(n: NimNode): Comment = n.checkConv Comment
22+
23+
func isa*(n: NimNode, _: typedesc[Call]): bool =
24+
n.checkIt {nnkCall}
25+
26+
func call*(n: NimNode): Call = n.checkConv Call
27+
28+
func isa*(n: NimNode, _: typedesc[Infix]): bool =
29+
n.checkIt {nnkInfix}
30+
31+
func infix*(n: NimNode): Infix = n.checkConv Infix
32+
33+
func isa*(n: NimNode, _: typedesc[Command]): bool =
34+
n.checkIt {nnkCommand}
35+
36+
func command*(n: NimNode): Command = n.checkConv Command
37+
38+
func isa*(n: NimNode, _: typedesc[Prefix]): bool =
39+
n.checkIt {nnkPrefix}
40+
41+
func prefix*(n: NimNode): Prefix = n.checkConv Prefix
42+
43+
func isa*(n: NimNode, T: typedesc[Calls]): bool =
44+
n.checkIt:
45+
when T is Call:
46+
{nnkCall}
47+
elif T is Infix:
48+
{nnkInfix}
49+
elif T is Command:
50+
{nnkCommand}
51+
elif T is Prefix:
52+
{nnkPrefix}
53+
54+
func isa*(n: NimNode, _: typedesc[ImportStmt]): bool =
55+
n.checkIt {nnkImportStmt}
56+
57+
func importStmt*(n: NimNode): ImportStmt = n.checkConv ImportStmt
58+
59+
func isa*(n: NimNode, _: typedesc[ImportFrom]): bool =
60+
n.checkIt {nnkFromStmt}
61+
62+
func importFrom*(n: NimNode): ImportFrom = n.checkConv ImportFrom
63+
64+
func isa*(n: NimNode, _: typedesc[ImportExcept]): bool =
65+
n.checkIt {nnkImportExceptStmt}
66+
67+
func importExcept*(n: NimNode): ImportExcept = n.checkConv ImportExcept
68+
69+
func isa*(n: NimNode, _: typedesc[Imports]): bool =
70+
n.checkIt:
71+
when T is ImportStmt:
72+
{nnkImportStmt}
73+
elif T is From:
74+
{nnkFromStmt}
75+
elif T is ImportExcept:
76+
{nnkImportExceptStmt}
77+
78+
func isa*(n: NimNode, _: typedesc[ExportStmt]): bool = n.checkIt {nnkExportStmt}
79+
80+
func exportStmt*(n: NimNode): ExportStmt = n.checkConv ExportStmt
81+
82+
func isa*(n: NimNode, _: typedesc[DotExpr]): bool = n.checkIt {nnkDotExpr}
83+
84+
func dotExpr*(n: NimNode): DotExpr = n.checkConv DotExpr
85+
86+
func quoted*(n: NimName): NimName =
87+
NimName nnkAccQuoted.newTree(n.NimNode)

0 commit comments

Comments
 (0)