From c22bf883d3ddeaf3e62a2196e2c7f689a2158f2d Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Sun, 1 Oct 2006 05:29:08 +0000 Subject: [PATCH] Enhanced debugging, remove block delimiters last. --- ctopy | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/ctopy b/ctopy index 19258fd..b8e70aa 100755 --- a/ctopy +++ b/ctopy @@ -2,16 +2,11 @@ """ ctopy -- a quick and dirty C-to-Python translator -Relies on the fact that C and Python have fundamentally similar syntax -and control structures, and C is normally indented in a way that indicates -its block structure. - -Stages: - -First, define a set of regions in the input corresponding to code (that is, -not within comments or string literals). - -Then, apply the specified set of regular-expression mappings. +Relies on the fact that C and Python have fundamentally similar +expression syntax and control structures, and C is normally indented +in a way that indicates its block structure. The only parts of +expressions that don't nap over without change are ?: and comma +expressions. Limitations: * Requires source to be syntactically valid, @@ -87,9 +82,9 @@ region_transformations = ( # externs can just disappear -- this may discard a following comment (r"extern[^;]+;.*\n", r""), # macros - (r"#define\s+(%(id)s)\s+(.*)\n", r"\1\t= \2\n"), - (r"#define\s+(%(id)s)\(([^)]*)\)\s+(.*\))\n", r"def \1(\2):\treturn \3\n"), - (r"#define\s+(%(id)s)\(([^)]*)\)\s+([^(].*)\n", r"def \1(\2):\t\3\n"), + (r"#define\s+(%(id)s)\s+(.*)[ \t]*\n", r"\1\t= \2\n"), + (r"#define\s+(%(id)s)\(([^)]*)\)\s+(.*\))[ \t]*\n", r"def \1(\2):\treturn \3\n"), + (r"#define\s+(%(id)s)\(([^)]*)\)\s+([^(].*)[ \t]*\n", r"def \1(\2):\t\3\n"), # function headers (r"(?:^|\n)(?:static\s+)?(?:%(type)s) +(.*\)) *\n", r"\ndef \1:\n"), (r"\(void\):\n", r"():\n"), @@ -118,10 +113,6 @@ region_transformations = ( (r"\bfalse\b", r"False"), # C99 (r"\bTRUE\b", r"True"), # pre-C99 (r"\bFALSE\b", r"False"), # pre-C99 - # block delimiters - (r";\n", r"\n"), - (r"\n[ \t]*}", r""), - (r"(?\n", r"import time\n"), (r"#include \\n", r"import re\n"), (r"#include \\n", r"import curses\n"), + # block delimiters -- do these as late as possible, they're useful + (r";\n", r"\n"), + (r"\n[ \t]*}\n", r"\n"), + (r"(?= 2: + print "After pre-transformations: %s" % `code` if debug: print "Region analysis begins" boundaries = [0] @@ -241,6 +238,7 @@ def ctopy(input, debug=0): if debug: import pprint pp = pprint.PrettyPrinter(indent=4) + print "Regions:" pp.pprint(regions) try: for i in range(len(regions)): @@ -294,8 +292,10 @@ if __name__ == "__main__": debug = 0; for (switch, val) in options: if (switch == '-d'): - debug = 1 + debug += 1 code = sys.stdin.read() + if debug >= 2: + print "Input is: %s" % `code` text = ctopy(code, debug) sys.stdout.write(text) -- 2.43.0