-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmytest.py
More file actions
243 lines (213 loc) · 10.7 KB
/
mytest.py
File metadata and controls
243 lines (213 loc) · 10.7 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import sys
from antlr4 import *
from gen.CLexer import CLexer
from gen.CParser import CParser
from gen.CVisitor import CVisitor
import itertools
result = open('result.java', 'w') # opening output file to write
contextList = []
dataList = ["d", "c", "f", "s"] # popular data types in c like %d, %c, %f, %s
ignoreList = ["unsigned"]
class MyCVisitor(CVisitor):
def __init__(self):
pass
def visitTranslationUnit(self, ctx): # store root of the traversal tree...
contextList.append(ctx)
pass
class CoverCases:
def printWithoutNewLine(self, var): # print the word without newline
print(var, file=result, end=" ")
def printWithNewLine(self, var): # print the word with newline
print(var, file=result, end="\n")
def spaceGeneration(self, tab): # for proper indentations...
for x in range(tab):
print("\t", file=result, end="")
def removeLastChar(self): # remove space before [++] [,] [;] [--]
size = result.tell()
with open("result.java", "r") as f:
file_str = str(f.read())
f.close()
last_chr = file_str[size-1]
if last_chr == " ": # if last character is whitespace the remove
result.truncate(size - 1)
def checkIgnore(self, var):
if var in ignoreList: # write nothing if found this...
return 1
return 0
class Header: # to handle header files
def include_library(self, var):
# to be implemented later...
return
def main(argv):
inputFile = FileStream(argv[1])
lexer = CLexer(inputFile)
stream = CommonTokenStream(lexer)
parser = CParser(stream)
tree = parser.compilationUnit()
v = MyCVisitor()
v.visit(tree)
print("package demo;\n\npublic class DemoTranslation {\n", file=result, end="") # default format of java program
tab = 0 # for handling indentation...
beginFlag = 1 # to check for line beginning...
argFlag = 0 # for command line input...
ignore = 0 # java translation for command line input...
funFlag = 0 # for return type of functions...
printFlag = 0 # to handle print statements...
forFlag = 0 # to handle for loop...
c = 0 # index of variable list...
declarationFlag = 0 # to handle newline in declaration of arrays etc...
stringLiteral = "" # message part in print statements...
variableList = [] # variables part in print statements...
solve = CoverCases() # instance of class to access class functions...
header = Header() # instance of class to handle header files...
mainFlag = 0
skipFlag = 0
defFlag = 0
tempString = ""
while len(contextList) > 0: # runs till we found node in the traversal tree...
t = 0
firstChild = contextList[0]
contextList.pop(0)
# print(str(firstChild.getText()) + "-- " + str(type(firstChild)))
if str(type(firstChild)) == "<class 'gen.CParser.CParser.FunctionDefinitionContext'>": # handle functions
funFlag = 1
if str(type(firstChild)) == "<class 'gen.CParser.CParser.DeclarationContext'>": # to handle array list
declarationFlag += 1
if firstChild.getText() in ["for"]: # to handle for loop
forFlag += 2
if str(type(firstChild)) == "<class 'gen.CParser.CParser.ExternalDeclarationContext'>": # for function declaration
if defFlag != 0:
solve.spaceGeneration(tab + 1)
print(tempString, file=result, end="\n")
tempString = ""
defFlag += 1
declarationFlag = 0
skipFlag = 0
if str(type(firstChild)) == "<class 'gen.CParser.CParser.FunctionDefinitionContext'>": # check if function
defFlag = 0
skipFlag = 0
if str(type(firstChild)) == "<class 'gen.CParser.CParser.IncludeContext'>": # check if header file
header.include_library(firstChild)
continue
if firstChild.getChildCount() > 0:
if str(type(firstChild)) == "<class 'gen.CParser.CParser.ParameterTypeListContext'>" and defFlag != 0:
tempString = ""
skipFlag += 1
defFlag = 0
continue
if skipFlag != 0: # skip function declaration
tempString = ""
continue
for x in range(0, firstChild.getChildCount()): # add child of node for next traversal
contextList.insert(t, firstChild.children[x])
t = t + 1
elif firstChild.getChildCount() == 0: # enters when leaf is found...
word = firstChild.getText()
if skipFlag != 0: # skip function declaration
tempString = ""
continue
if defFlag != 0:
tempString += word + " "
continue
if beginFlag == 1: # to add indentation
solve.spaceGeneration(tab)
if (word != "}") and beginFlag == 1:
print("\t", file=result, end="")
if solve.checkIgnore(word):
continue
elif word in [";"]: # new line begins after this...
solve.removeLastChar()
if forFlag == 0:
solve.printWithNewLine(word)
beginFlag = 1
else:
forFlag -= 1
solve.printWithoutNewLine(word)
declarationFlag = 0
elif word in ["{"]: # new line begins after this...
if declarationFlag == 0:
solve.printWithNewLine(word)
tab += 1
beginFlag = 1
else:
solve.printWithoutNewLine(word)
elif word in ["printf"]: # translate print statement...
print("System.out.print", file=result, end="")
printFlag = 1
beginFlag = 0
elif word in ["int", "void"] and funFlag == 1: # handle return type of functions...
print("public static " + word, file=result, end=" ")
beginFlag = 0
funFlag = 0
elif word in ["main"]:
solve.printWithoutNewLine(word)
mainFlag += 1
argFlag = 1
elif word in ["}"]: # new line begins after this...
if declarationFlag == 0:
solve.printWithNewLine(word)
tab -= 1
beginFlag = 1
else:
print(word, file=result, end=" ")
elif word in ["("] and argFlag == 1:
solve.printWithoutNewLine(word)
ignore = 1
elif word in ["("]:
solve.printWithoutNewLine(word)
c = -1
if printFlag == 1:
printFlag = 2
elif word in [")"] and argFlag == 1:
print("String[] args )", file=result, end=" ")
ignore = 0
argFlag = 0
elif word in ["++", "--"]:
solve.removeLastChar()
solve.printWithoutNewLine(word)
elif word in [","] and printFlag == 0:
solve.removeLastChar()
solve.printWithoutNewLine(word)
else: # translation of print arguments and messages(differently)...
if printFlag == 2: # store message of print statement...
stringLiteral = word
printFlag = 3
elif printFlag == 3: # variable part of print statement...
temp1 = 0
if word in [")"]:
for var in stringLiteral: # look for data type in message part...
if temp1 == 1:
if var in dataList: # if data type is found
print("\");\n", file=result, end="")
solve.spaceGeneration(tab+1) # for proper indentations...
print("System.out.print(", file=result, end=" ")
if variableList[0] in ["argc"]: # command line input translation...
print("args.length+1 ) ;\n", file=result, end="")
else:
print(variableList[0] + " );\n", file=result, end="")
solve.spaceGeneration(tab+1) # for proper indentations...
print("System.out.print( \"", file=result, end="")
variableList.pop(0)
else:
print("%"+var, file=result, end="") # if % is not used as data type(like %d)...
temp1 = 0
elif var == "%":
temp1 = 1
else:
print(var, file=result, end="")
variableList = [""] # reset variable list...
c = -1
printFlag = 0
print(")", file=result, end="")
if word not in [",", ")"]:
variableList.insert(c, variableList[c]+word) # handle expression in print...
else:
variableList.append("")
c += 1
else: # print rest of the program
if ignore == 0:
solve.printWithoutNewLine(word)
beginFlag = 0
print("}", file=result, end="")
if __name__ == '__main__':
main(sys.argv)