-
-
Couldn't load subscription status.
- Fork 6
Feature: upgrade macro evaluation strategy to use nimscripter
#37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
449066a
Feature: use nimscripter
ynfle ac1bc76
CI: update to use version 1.6.6 and refractor of the representer
ynfle ddc0d6a
Move each dependency to it's own line
ynfle 883e49f
Pin action and action runner version
ynfle 708eeb7
Remove unnecessary conditional check in CI
ynfle e3ba0ac
Fix formatting and casing
ynfle 6c95e17
CI: use latest released version of `ubuntu` not beta
ynfle efc253a
Docs: clarify help message wording
ynfle 5f51ebc
Refactor: remove `getFileContents` from being exported
ynfle 4005edd
Refactor: fix string interpolation
ynfle b94f336
Refactor: clarify variable names
ynfle 8c6f984
Refactor: avoid intermediate allocations
ynfle 27c9bad
Style: remove unnecessary empty lines
ynfle ff93cad
Style: use `strutils.dedent` to improve readibility of multiline strings
ynfle 828f799
Refactor: remove unnecessary `experimental` flag
ynfle d9f83e8
Refactor: order statements
ynfle a4d2362
Refactor: identify local nim modules with `"."` on imports
ynfle f8042be
move to 1.6.6 in nimble file and add prefixes for import
ynfle c004739
Upgrade to latest versions of Github Actions
ynfle 03b4067
Keep consistency of testing format
ynfle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,42 @@ | ||
| import macros, os, sequtils, strutils | ||
| import representer/[mapping, normalizations] | ||
| import std/[json, os, strformat, strutils] | ||
| import nimscripter | ||
| import representer/[mapping, types] | ||
| import docopt | ||
ynfle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| proc switchKeysValues*(map: IdentMap): OrderedTable[string, NormalizedIdent] = | ||
| toSeq(map.pairs).mapIt((it[1], it[0])).toOrderedTable | ||
| const doc = """ | ||
| Exercism nim representation normalizer. | ||
|
|
||
| proc createRepresentation*(fileName: string): tuple[tree: NimNode, map: IdentMap] = | ||
| var map: IdentMap | ||
| let code = parseStmt fileName.staticRead | ||
| result = (tree: code.normalizeStmtList(map), map: map) | ||
| Usage: | ||
| representer --slug=<slug> --input-dir=<in-dir> [--output-dir=<out-dir>] [--print] | ||
|
|
||
| Options: | ||
| -h --help Show this help message. | ||
| -v, --version Display version. | ||
| -p, --print Print the results. | ||
| -s <slug>, --slug=<slug> The exercise slug. | ||
| -i <in-dir>, --input-dir=<in-dir> The directory of the submission and exercise files. | ||
| -o <out-dir>, --output-dir=<out-dir> The directory to output to. | ||
| If omitted, output will be written to stdout. | ||
| """.dedent | ||
|
|
||
| const inDir {.strdefine.} = "" | ||
| const outDir {.strdefine.} = "" | ||
| const slug {.strdefine.} = "" | ||
| const underSlug = slug.replace('-', '_') | ||
| proc getFileContents(fileName: string): string = readFile fileName | ||
|
|
||
| func kebabToSnakeCase(s: string): string = s.replace('-', '_') | ||
|
|
||
| proc main() = | ||
| let args = docopt(doc) | ||
ynfle marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let intr = loadScript(NimScriptPath("src/representer/loader.nims")) | ||
| let (tree, map) = intr.invoke( | ||
| getTestableRepresentation, | ||
| getFileContents($args["--input-dir"] / kebabToSnakeCase($args["--slug"]) & ".nim"), true, | ||
| returnType = SerializedRepresentation | ||
| ) | ||
| if args["--output-dir"]: | ||
| let outDir = $args["--output-dir"] | ||
| writeFile outDir / "mapping.json", $map.parseJson | ||
| writeFile outDir / "representation.txt", tree | ||
| if not args["--output-dir"] or args["--print"]: | ||
| echo &"{tree = }\n{map.parseJson.pretty = }" | ||
|
|
||
| when isMainModule: | ||
| import json | ||
| static: | ||
| let (tree, map) = createRepresentation(inDir / underSlug & ".nim") | ||
| let finalMapping = map.switchKeysValues | ||
| echo (%*{"map": finalMapping, "tree": tree.repr}).pretty | ||
| when defined(outDir): | ||
| writeFile(outDir / "representation.txt", tree.repr) | ||
| writeFile(outDir / "mapping.json", $(%finalMapping)) | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import std/[json, macros] | ||
| import "."/[mapping, normalizations, types] | ||
|
|
||
| proc createRepresentation(contents: string): tuple[tree: NimNode, map: IdentMap] = | ||
| var map: IdentMap | ||
| let code = parseStmt(contents) | ||
| result = (tree: code.normalizeStmtList(map), map: map) | ||
|
|
||
| proc getTestableRepresentation*(contents: string, switch = false): SerializedRepresentation = | ||
| let (tree, map) = createRepresentation(contents) | ||
| result = (repr tree, $(if switch: %map.switchKeysValues else: %map)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import std/[sugar, tables] | ||
| import mapping | ||
|
|
||
| type | ||
| Representation* = tuple | ||
| tree: string | ||
| map: IdentMap | ||
| SerializedRepresentation* = tuple | ||
| tree: string | ||
| map: string | ||
|
|
||
| proc switchKeysValues*(map: IdentMap): OrderedTable[string, NormalizedIdent] = | ||
| result = collect(initOrderedTable): | ||
| for key, val in map.pairs: | ||
| {val: key} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.