summary refs log tree commit diff stats
path: root/compiler/c2nim
diff options
context:
space:
mode:
authorVincent Burns <discoloda@gmail.com>2014-01-12 12:48:06 -0500
committerVincent Burns <discoloda@gmail.com>2014-01-12 12:48:06 -0500
commit0a0fec4a5c6dcd3c6ac007877ef297914657b7f2 (patch)
tree99fe5ef559504a3351aa2b1458bc9983adc6e3e5 /compiler/c2nim
parent1280c56f386111b542c8f0effdd3e5cbc5e84622 (diff)
downloadNim-0a0fec4a5c6dcd3c6ac007877ef297914657b7f2.tar.gz
Added spliceHeader option to c2nim
parse a header file first, then the source. completing a c 'module'
Diffstat (limited to 'compiler/c2nim')
-rw-r--r--compiler/c2nim/c2nim.nim22
1 files changed, 17 insertions, 5 deletions
diff --git a/compiler/c2nim/c2nim.nim b/compiler/c2nim/c2nim.nim
index 1c701a386..c4fc8ad67 100644
--- a/compiler/c2nim/c2nim.nim
+++ b/compiler/c2nim/c2nim.nim
@@ -34,25 +34,36 @@ Options:
   --skipcomments         do not copy comments
   --ignoreRValueRefs     translate C++'s ``T&&`` to ``T`` instead ``of var T``
   --keepBodies           keep C++'s method bodies
+  --spliceHeader         parse and emit header before source file
   -v, --version          write c2nim's version
   -h, --help             show this help
 """
 
-proc main(infile, outfile: string, options: PParserOptions) =
-  var start = getTime()
+proc parse(infile: string, options: PParserOptions): PNode =
   var stream = llStreamOpen(infile, fmRead)
   if stream == nil: rawMessage(errCannotOpenFile, infile)
   var p: TParser
   openParser(p, infile, stream, options)
-  var module = parseUnit(p)
+  result = parseUnit(p)
   closeParser(p)
-  renderModule(module, outfile)
+
+proc main(infile, outfile: string, options: PParserOptions, spliceHeader: bool) =
+  var start = getTime()
+  if spliceHeader and infile[infile.len-2 .. infile.len] == ".c" and existsFile(infile[0 .. infile.len-2] & "h"):
+    var header_module = parse(infile[0 .. infile.len-2] & "h", options)
+    var source_module = parse(infile, options)
+    for n in source_module:
+      addson(header_module, n)
+    renderModule(header_module, outfile)
+  else:
+    renderModule(parse(infile, options), outfile)
   rawMessage(hintSuccessX, [$gLinesCompiled, $(getTime() - start), 
                             formatSize(getTotalMem())])
 
 var
   infile = ""
   outfile = ""
+  spliceHeader = false
   parserOptions = newParserOptions()
 for kind, key, val in getopt():
   case kind
@@ -66,6 +77,7 @@ for kind, key, val in getopt():
       stdout.write(Version & "\n")
       quit(0)
     of "o", "out": outfile = val
+    of "spliceheader": spliceHeader = true
     else:
       if not parserOptions.setOption(key, val):
         stdout.writeln("[Error] unknown option: " & key)
@@ -77,4 +89,4 @@ else:
   if outfile.len == 0:
     outfile = changeFileExt(infile, "nim")
   infile = addFileExt(infile, "h")
-  main(infile, outfile, parserOptions)
+  main(infile, outfile, parserOptions, spliceHeader)