aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgenotrance <dev@genotrance.com>2018-11-28 15:49:56 -0600
committerGitHub <noreply@github.com>2018-11-28 15:49:56 -0600
commit77fafa0adaa83c3eb73ce28e4a2a93631c721deb (patch)
tree7968c7d7e142fa9510411074ad704187688710b0
parent691e7d80e5c5c7c099859d240633fe6bbe91f9f5 (diff)
parent4a6973c14de892970cddd952677fc7e578806479 (diff)
downloadnimterop-77fafa0adaa83c3eb73ce28e4a2a93631c721deb.tar.gz
nimterop-77fafa0adaa83c3eb73ce28e4a2a93631c721deb.zip
Merge pull request #21 from timotheecour/pr_misc4
use cligen; fix bugs; some cleanups
-rw-r--r--nimterop.nimble2
-rw-r--r--nimterop/cimport.nim12
-rw-r--r--nimterop/getters.nim2
-rw-r--r--nimterop/globals.nim12
-rw-r--r--nimterop/lisp.nim7
-rw-r--r--toast.nim85
6 files changed, 64 insertions, 56 deletions
diff --git a/nimterop.nimble b/nimterop.nimble
index aef56fc..3bfd982 100644
--- a/nimterop.nimble
+++ b/nimterop.nimble
@@ -10,7 +10,7 @@ installDirs = @["nimterop"]
# 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)=
echo cmd
diff --git a/nimterop/cimport.nim b/nimterop/cimport.nim
index fc0d057..758237b 100644
--- a/nimterop/cimport.nim
+++ b/nimterop/cimport.nim
@@ -26,15 +26,19 @@ proc findPath(path: string, fail = true): string =
proc getToast(fullpath: string): string =
var
- cmd = "toast -n -p "
+ cmd = "toast --pnim --preprocess "
for i in gStateCT.defines:
- cmd &= &"-D\"{i}\" "
+ cmd.add &"--defines+={i.quoteShell} "
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.}=
result = findPath(path, fail = false)
diff --git a/nimterop/getters.nim b/nimterop/getters.nim
index f638edc..daf01ef 100644
--- a/nimterop/getters.nim
+++ b/nimterop/getters.nim
@@ -1,4 +1,4 @@
-import macros, ospaths, strformat, strutils
+import macros, os, strformat, strutils
import regex
diff --git a/nimterop/globals.nim b/nimterop/globals.nim
index 98e4249..1e3a83c 100644
--- a/nimterop/globals.nim
+++ b/nimterop/globals.nim
@@ -12,5 +12,13 @@ var
gStateCT* {.compiletime.}: State
gStateRT*: State
-template nBl*(s: untyped): untyped =
- (s.len != 0) \ No newline at end of file
+template nBl*(s: typed): untyped =
+ (s.len != 0)
+
+
+type CompileMode* = enum
+ c,
+ cpp,
+
+# TODO: can cligen accept enum instead of string?
+const modeDefault* = $cpp # TODO: USE this everywhere relevant
diff --git a/nimterop/lisp.nim b/nimterop/lisp.nim
index 44d609f..8fa73e0 100644
--- a/nimterop/lisp.nim
+++ b/nimterop/lisp.nim
@@ -1,4 +1,5 @@
import strutils
+import strformat
import globals
@@ -11,7 +12,11 @@ proc tokenize(fullpath: string) =
gTokens = @[]
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:
of ' ', '\n', '\r', '(', ')':
if collect.nBl:
diff --git a/toast.nim b/toast.nim
index 4877c50..b418308 100644
--- a/toast.nim
+++ b/toast.nim
@@ -4,17 +4,6 @@ import treesitter/[runtime, c, cpp]
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) =
var
node = root
@@ -74,8 +63,8 @@ proc process(path: string) =
defer:
parser.tsParserDelete()
- if gStateRT.mode.len != 0:
- gStateRT.mode = "cpp"
+ if gStateRT.mode.len == 0:
+ gStateRT.mode = modeDefault
elif ext in [".h", ".c"]:
gStateRT.mode = "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:
printNim(path, root)
-proc parseCli() =
- var params = commandLineParams()
-
- gStateRT.mode = "cpp"
- gStateRT.past = false
- gStateRT.pnim = false
- gStateRT.pretty = true
- gStateRT.preprocess = false
-
- for param in params:
- let flag = if param.len() <= 2: param else: param[0..<2]
-
- if flag in ["-h", "-?"]:
- echo HELP
- quit()
- elif flag == "-a":
- gStateRT.past = true
- elif flag == "-c":
- gStateRT.mode = "c"
- 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()
+proc main(
+ 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 = State(
+ mode: mode,
+ past: past,
+ pnim: pnim,
+ pretty: pretty,
+ preprocess: preprocess,
+ # Note: was: strip(chars={'"'} but that seemed buggy (the shell should remove these already)
+ defines: defines,
+ includeDirs: includeDirs,
+ )
+ process(source)
+
+when isMainModule:
+ import cligen
+ dispatch(main, help = {
+ "past": "print AST output",
+ "mode": "language; see CompileMode", # TODO: auto-generate valid choices
+ "pnim": "run preprocessor on header",
+ "defines": "definitions to pass to preprocessor",
+ "includeDirs": "include directory to pass to preprocessor",
+ "preprocess": "print Nim output",
+ "source" : "C/C++ source/header",
+ })