summaryrefslogtreecommitdiffstats
path: root/python/lvalue_cast_post_process.py
blob: 661290049181738eb4400c8a1dab702a3ebc303d (plain)
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
#!/bin/env python

import re
import sys

exp_pattern = re.compile('(.*)=(.*);')
lval_pyobject_pattern = re.compile('\s*\(\((PyObject[ ]?\*)\)([A-Za-z0-9_ ]+)\)')
lval_structcast_pattern = re.compile('\s*\((struct [A-Za-z0-9_]+ \*)\)([A-Za-z0-9_]+)\-\>([A-Za-z0-9_]+)')

def parse_expression(exp):
	exp_match = exp_pattern.match(exp)
	if exp_match:
		lvalue = exp_match.group(1)
		rvalue = exp_match.group(2)

		lval_match = lval_pyobject_pattern.match(lvalue)

		if lval_match:
			cast = lval_match.group(1)
			lvar = lval_match.group(2)

			return "%s = (%s)(%s);" % (lvar, cast, rvalue)
		else:
			lval_match = lval_structcast_pattern.match(lvalue)
			if lval_match:
				cast = lval_match.group(1)
				casted_var = lval_match.group(2)
				member_var = lval_match.group(3)

				result = "%s->%s = ((%s)%s);" % (
					    casted_var, 
					    member_var, 
					    cast,
					    rvalue)

				return result

	return None

def main():
	if len(sys.argv) != 2:
		print "USAGE: " + sys.argv[0] + " <file name>" 
		return(-1)	

	file = sys.argv[1]
	f = open(file)
	gcc4fix_filename = file + ".gcc4fix"
	outputf = open(gcc4fix_filename, 'w')

	lines = f.readlines()
	f.close()
	for line in lines:
		c = line.count(";")
		if c == 0:
			outputf.write(line)
			continue

		exprs = line.split(';')
		line = ""
		last = exprs.pop()
		for expr in exprs:
			expr = expr + ";"

			result = parse_expression(expr)
			if result:
				line = line + result
			else:
				line = line + expr

		if (last.strip()!=''):
			line = line + last
		else:
			line = line + "\n"
			
		outputf.write(line)
	
	outputf.close()

if __name__ == "__main__":
	sys.exit(main())