summary refs log tree commit diff stats
path: root/compiler/c2nim/c2nim.nim
blob: 9b12b9e47fcfc0b75b6ad8c126db6e7035bff789 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#
#
#      c2nim - C to Nimrod source converter
#        (c) Copyright 2013 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

import
  strutils, os, times, parseopt, llstream, ast, renderer, options, msgs,
  clex, cparse

const
  Version = NimrodVersion
  Usage = """
c2nim - C to Nimrod source converter
  (c) 2013 Andreas Rumpf
Usage: c2nim [options] inputfile [options]
Options:
  -o, --out:FILE         set output filename
  --cpp                  process C++ input file
  --dynlib:SYMBOL        import from dynlib: SYMBOL will be used for the import
  --header:HEADER_FILE   import from a HEADER_FILE (discouraged!)
  --cdecl                annotate procs with ``{.cdecl.}``
  --stdcall              annotate procs with ``{.stdcall.}``
  --ref                  convert typ* to ref typ (default: ptr typ)
  --prefix:PREFIX        strip prefix for the generated Nimrod identifiers
                         (multiple --prefix options are supported)
  --suffix:SUFFIX        strip suffix for the generated Nimrod identifiers
                         (multiple --suffix options are supported)
  --skipinclude          do not convert ``#include`` to ``import``
  --typeprefixes         generate ``T`` and ``P`` type prefixes
  --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 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)
  result = parseUnit(p)
  closeParser(p)

proc main(infile, outfile: string, options: PParserOptions, spliceHeader: bool) =
  var start = getTime()
  if spliceHeader and infile.splitFile.ext == ".c" and existsFile(infile.changeFileExt(".h")):
    var header_module = parse(infile.changeFileExt(".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
  of cmdArgument: infile = key
  of cmdLongOption, cmdShortOption:
    case key.toLower
    of "help", "h":
      stdout.write(Usage)
      quit(0)
    of "version", "v":
      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)
  of cmdEnd: assert(false)
if infile.len == 0:
  # no filename has been given, so we show the help:
  stdout.write(Usage)
else:
  if outfile.len == 0:
    outfile = changeFileExt(infile, "nim")
  infile = addFileExt(infile, "h")
  main(infile, outfile, parserOptions, spliceHeader)