Merge pull request #21 from timotheecour/pr_misc4
use cligen; fix bugs; some cleanups
This commit is contained in:
commit
77fafa0ada
6 changed files with 62 additions and 54 deletions
|
|
@ -10,7 +10,7 @@ installDirs = @["nimterop"]
|
||||||
|
|
||||||
# Dependencies
|
# Dependencies
|
||||||
|
|
||||||
requires "nim >= 0.19.0", "treesitter >= 0.1.0", "treesitter_c >= 0.1.0", "treesitter_cpp >= 0.1.0", "regex >= 0.10.0"
|
requires "nim >= 0.19.0", "treesitter >= 0.1.0", "treesitter_c >= 0.1.0", "treesitter_cpp >= 0.1.0", "regex >= 0.10.0", "cligen >= 0.9.17"
|
||||||
|
|
||||||
proc execCmd(cmd:string)=
|
proc execCmd(cmd:string)=
|
||||||
echo cmd
|
echo cmd
|
||||||
|
|
|
||||||
|
|
@ -26,15 +26,19 @@ proc findPath(path: string, fail = true): string =
|
||||||
|
|
||||||
proc getToast(fullpath: string): string =
|
proc getToast(fullpath: string): string =
|
||||||
var
|
var
|
||||||
cmd = "toast -n -p "
|
cmd = "toast --pnim --preprocess "
|
||||||
|
|
||||||
for i in gStateCT.defines:
|
for i in gStateCT.defines:
|
||||||
cmd &= &"-D\"{i}\" "
|
cmd.add &"--defines+={i.quoteShell} "
|
||||||
|
|
||||||
for i in gStateCT.includeDirs:
|
for i in gStateCT.includeDirs:
|
||||||
cmd &= &"-I\"{i}\" "
|
cmd.add &"--includeDirs+={i.quoteShell} "
|
||||||
|
|
||||||
result = staticExec(cmd & fullpath)
|
cmd.add &"--source:{fullpath.quoteShell}"
|
||||||
|
echo cmd
|
||||||
|
var (output, exitCode) = gorgeEx(cmd)
|
||||||
|
doAssert exitCode == 0, $exitCode
|
||||||
|
result = output
|
||||||
|
|
||||||
proc cSearchPath*(path: string): string {.compileTime.}=
|
proc cSearchPath*(path: string): string {.compileTime.}=
|
||||||
result = findPath(path, fail = false)
|
result = findPath(path, fail = false)
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import macros, ospaths, strformat, strutils
|
import macros, os, strformat, strutils
|
||||||
|
|
||||||
import regex
|
import regex
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,5 +12,13 @@ var
|
||||||
gStateCT* {.compiletime.}: State
|
gStateCT* {.compiletime.}: State
|
||||||
gStateRT*: State
|
gStateRT*: State
|
||||||
|
|
||||||
template nBl*(s: untyped): untyped =
|
template nBl*(s: typed): untyped =
|
||||||
(s.len != 0)
|
(s.len != 0)
|
||||||
|
|
||||||
|
|
||||||
|
type CompileMode* = enum
|
||||||
|
c,
|
||||||
|
cpp,
|
||||||
|
|
||||||
|
# TODO: can cligen accept enum instead of string?
|
||||||
|
const modeDefault* = $cpp # TODO: USE this everywhere relevant
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import strutils
|
import strutils
|
||||||
|
import strformat
|
||||||
|
|
||||||
import globals
|
import globals
|
||||||
|
|
||||||
|
|
@ -11,7 +12,11 @@ proc tokenize(fullpath: string) =
|
||||||
|
|
||||||
gTokens = @[]
|
gTokens = @[]
|
||||||
idx = 0
|
idx = 0
|
||||||
for i in staticExec("toast -m " & fullpath):
|
# TODO: consider calling API directly
|
||||||
|
const cmd = &"toast --past --pretty:false --source:{fullpath.quoteShell}"
|
||||||
|
var (output, exitCode) = gorgeEx cmd
|
||||||
|
doAssert exitCode == 0, $exitCode
|
||||||
|
for i in output:
|
||||||
case i:
|
case i:
|
||||||
of ' ', '\n', '\r', '(', ')':
|
of ' ', '\n', '\r', '(', ')':
|
||||||
if collect.nBl:
|
if collect.nBl:
|
||||||
|
|
|
||||||
81
toast.nim
81
toast.nim
|
|
@ -4,17 +4,6 @@ import treesitter/[runtime, c, cpp]
|
||||||
|
|
||||||
import nimterop/[ast, globals, getters]
|
import nimterop/[ast, globals, getters]
|
||||||
|
|
||||||
const HELP = """
|
|
||||||
> toast header.h
|
|
||||||
-a print AST output
|
|
||||||
-m print minimized AST output - non-pretty (implies -a)
|
|
||||||
-n print Nim output
|
|
||||||
|
|
||||||
-c C mode - CPP is default
|
|
||||||
-p run preprocessor on header
|
|
||||||
-D definitions to pass to preprocessor
|
|
||||||
-I include directory to pass to preprocessor"""
|
|
||||||
|
|
||||||
proc printLisp(root: TSNode) =
|
proc printLisp(root: TSNode) =
|
||||||
var
|
var
|
||||||
node = root
|
node = root
|
||||||
|
|
@ -74,8 +63,8 @@ proc process(path: string) =
|
||||||
defer:
|
defer:
|
||||||
parser.tsParserDelete()
|
parser.tsParserDelete()
|
||||||
|
|
||||||
if gStateRT.mode.len != 0:
|
if gStateRT.mode.len == 0:
|
||||||
gStateRT.mode = "cpp"
|
gStateRT.mode = modeDefault
|
||||||
elif ext in [".h", ".c"]:
|
elif ext in [".h", ".c"]:
|
||||||
gStateRT.mode = "c"
|
gStateRT.mode = "c"
|
||||||
elif ext in [".hxx", ".hpp", ".hh", ".H", ".h++", ".cpp", ".cxx", ".cc", ".C", ".c++"]:
|
elif ext in [".hxx", ".hpp", ".hh", ".H", ".h++", ".cpp", ".cxx", ".cc", ".C", ".c++"]:
|
||||||
|
|
@ -110,37 +99,39 @@ proc process(path: string) =
|
||||||
elif gStateRT.pnim:
|
elif gStateRT.pnim:
|
||||||
printNim(path, root)
|
printNim(path, root)
|
||||||
|
|
||||||
proc parseCli() =
|
proc main(
|
||||||
var params = commandLineParams()
|
mode = modeDefault,
|
||||||
|
past = false,
|
||||||
|
pnim = false,
|
||||||
|
pretty = true,
|
||||||
|
preprocess = false,
|
||||||
|
defines: seq[string] = @[],
|
||||||
|
includeDirs: seq[string] = @[],
|
||||||
|
# defines.add(param[2..^1].strip(chars={'"'}))
|
||||||
|
source: string,
|
||||||
|
) =
|
||||||
|
# TODO: should we add back `-m` param? meaning was: print minimized AST output - non-pretty (implies -a)
|
||||||
|
|
||||||
gStateRT.mode = "cpp"
|
gStateRT = State(
|
||||||
gStateRT.past = false
|
mode: mode,
|
||||||
gStateRT.pnim = false
|
past: past,
|
||||||
gStateRT.pretty = true
|
pnim: pnim,
|
||||||
gStateRT.preprocess = false
|
pretty: pretty,
|
||||||
|
preprocess: preprocess,
|
||||||
|
# Note: was: strip(chars={'"'} but that seemed buggy (the shell should remove these already)
|
||||||
|
defines: defines,
|
||||||
|
includeDirs: includeDirs,
|
||||||
|
)
|
||||||
|
process(source)
|
||||||
|
|
||||||
for param in params:
|
when isMainModule:
|
||||||
let flag = if param.len() <= 2: param else: param[0..<2]
|
import cligen
|
||||||
|
dispatch(main, help = {
|
||||||
if flag in ["-h", "-?"]:
|
"past": "print AST output",
|
||||||
echo HELP
|
"mode": "language; see CompileMode", # TODO: auto-generate valid choices
|
||||||
quit()
|
"pnim": "run preprocessor on header",
|
||||||
elif flag == "-a":
|
"defines": "definitions to pass to preprocessor",
|
||||||
gStateRT.past = true
|
"includeDirs": "include directory to pass to preprocessor",
|
||||||
elif flag == "-c":
|
"preprocess": "print Nim output",
|
||||||
gStateRT.mode = "c"
|
"source" : "C/C++ source/header",
|
||||||
elif flag == "-m":
|
})
|
||||||
gStateRT.past = true
|
|
||||||
gStateRT.pretty = false
|
|
||||||
elif flag == "-n":
|
|
||||||
gStateRT.pnim = true
|
|
||||||
elif flag == "-p":
|
|
||||||
gStateRT.preprocess = true
|
|
||||||
elif flag == "-D":
|
|
||||||
gStateRT.defines.add(param[2..^1].strip(chars={'"'}))
|
|
||||||
elif flag == "-I":
|
|
||||||
gStateRT.includeDirs.add(param[2..^1].strip(chars={'"'}))
|
|
||||||
else:
|
|
||||||
process(param)
|
|
||||||
|
|
||||||
parseCli()
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue