diff --git a/src/nimgen/c2nim.nim b/src/nimgen/c2nim.nim index 3d15243..c9ccff7 100644 --- a/src/nimgen/c2nim.nim +++ b/src/nimgen/c2nim.nim @@ -28,6 +28,9 @@ proc c2nim*(fl, outfile: string, c2nimConfig: c2nimConfigObj) = cfile = "temp-$#.c" % [outfile.extractFilename()] writeFileFlush(cfile, runCtags(file)) + if c2nimConfig.removeBodies: + removeBodies(cfile) + if c2nimConfig.defines and (c2nimConfig.preprocess or c2nimConfig.ctags): prepend(cfile, getDefines(file, c2nimConfig.inline)) @@ -103,6 +106,8 @@ proc c2nim*(fl, outfile: string, c2nimConfig: c2nimConfigObj) = except: discard else: + if c2nimConfig.removeBodies: + reAddBodies(cfile) reAddStatic(cfile) # Nim doesn't like {.cdecl.} for type proc() diff --git a/src/nimgen/fileops.nim b/src/nimgen/fileops.nim index e416cf1..7f16139 100644 --- a/src/nimgen/fileops.nim +++ b/src/nimgen/fileops.nim @@ -91,6 +91,36 @@ proc removeStatic*(filename: string) = result.add(body.replace(re"(?m)^(.*\n?)", "//$1")) ) +proc removeBodies*(filename: string) = + ## Replace function bodies with a semicolon and commented + ## out body + withFile(filename): + content = content.replace( + re"(?m)(.*?\))(\s*\{(\s*?.*?$)*?[\n\r]\})", + proc (m: RegexMatch, s: string): string = + let funcDecl = s[m.group(0)[0]] + let body = s[m.group(1)[0]].strip() + result = "" + + result.add("$#;" % [funcDecl]) + result.add(body.replace(re"(?m)^(.*\n?)", "//$1")) + ) + +proc reAddBodies*(filename: string) = + ## Uncomment out the body and remove the semicolon. Undoes + ## removeBodies + withFile(filename): + content = content.replace( + re"(?m)(.*?\))(\s*\{(\s*?.*?$)*?[\n\r]\})", + proc (m: RegexMatch, s: string): string = + let funcDecl = s[m.group(0)[0]] + let body = s[m.group(1)[0]].strip() + result = "" + + result.add("$# " % [funcDecl]) + result.add(body.replace(re"(?m)^\/\/(.*\n?)", "$1")) + ) + proc reAddStatic*(filename: string) = ## Uncomment out the body and remove the semicolon. Undoes ## removeStatic diff --git a/src/nimgen/globals.nim b/src/nimgen/globals.nim index 9892b99..da90635 100644 --- a/src/nimgen/globals.nim +++ b/src/nimgen/globals.nim @@ -32,7 +32,7 @@ var type c2nimConfigObj* = object flags*, ppflags*: string - recurse*, inline*, preprocess*, ctags*, defines*: bool + recurse*, inline*, preprocess*, removeBodies*, ctags*, defines*: bool dynlib*, compile*, pragma*: seq[string] const gDoc* = """ diff --git a/src/nimgen/runcfg.nim b/src/nimgen/runcfg.nim index a924734..c582c2c 100644 --- a/src/nimgen/runcfg.nim +++ b/src/nimgen/runcfg.nim @@ -130,6 +130,8 @@ proc runFile*(file: string, cfgin: OrderedTableRef = newOrderedTable[string, str c2nimConfig.inline = true elif action == "preprocess": c2nimConfig.preprocess = true + elif action == "removeBodies": + c2nimConfig.removeBodies = true elif action == "ctags": c2nimConfig.ctags = true elif action == "defines": @@ -406,4 +408,4 @@ proc runCli*() = uniq += 1 for file in files: - runFile(file, cfg) \ No newline at end of file + runFile(file, cfg)