This repository has been archived by the owner on Nov 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NishiTranspiler.py
477 lines (319 loc) · 13.8 KB
/
NishiTranspiler.py
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""""""
import antlr4
from NishiLexer import NishiLexer
from NishiListener import NishiListener
from NishiParser import NishiParser
import io
from ast import literal_eval
import os
import sys
import argparse
class NishiTranspiler(NishiListener):
def __init__(self, output: io.FileIO, pretty: bool=True):
self.output = output
self.pretty_print = pretty
self.context = None
self.context_type = ""
self.access = None
self.static = False
self.package = False
self.variable_contexts = {"classes": {},
"functions": {}}
self.indent = ""
self.variables = {}
self.python_to_csharp = {str: "string",
int: "int",
float: "float",
list: "List<>",
bool: "bool",
None: "null"}
self.carp_to_csharp = {"String": "string",
"Integer": "int",
"Float": "float",
"List": "List<>",
"Boolean": "bool",
"Void": "void",
"None": "null"}
# self.insert_text("using System;{}using System.Collections.Generic".format("\n" if self.pretty_print else ""), 1, True)
# Package
def enterPackage(self, ctx:NishiParser.PackageContext):
self.insert_text(f"namespace {ctx.ID()[0]} %s" % "{", 1)
self.do_indent()
self.package = True
def exitProgram(self, ctx:NishiParser.ProgramContext):
if self.package:
self.context = None
self.do_dedent()
self.insert_text("}", 1)
# Imports
def enterImport_(self, ctx:NishiParser.Import_Context):
packages = []
for item in ctx.ID():
packages.append(item.getText())
self.insert_text(f"using {'.'.join(packages)}", 1, True)
# Normal Classes
def enterNormalClass(self, ctx:NishiParser.NormalClassContext):
self.context = str(ctx.ID())
self.context_type = "classes"
self.variable_contexts["classes"][str(ctx.ID())] = []
self.insert_text(f"class {str(ctx.ID())} %s" % "{", 1)
self.do_indent()
def exitNormalClass(self, ctx:NishiParser.NormalClassContext):
self.context = None
self.do_dedent()
self.insert_text("}", 1)
# Normal Functions
def enterNormalFunction(self, ctx:NishiParser.NormalFunctionContext):
if self.context is not None:
void = " void" if self.context != str(ctx.ID()) else ""
self.context = str(ctx.ID())
self.context_type = "functions"
self.variable_contexts["functions"][str(ctx.ID())] = []
parameters = self.get_parameters(ctx.parameter())
self.insert_text(f"public{void} {str(ctx.ID())}({', '.join(parameters)}) %s" % "{", 1)
self.do_indent()
def exitNormalFunction(self, ctx:NishiParser.NormalFunctionContext):
# self.context = None
self.do_dedent()
self.insert_text("}", 1)
# Override Functions
def enterOverrideFunction(self, ctx:NishiParser.OverrideFunctionContext):
if self.context is not None:
self.context = str(ctx.ID())
self.context_type = "functions"
self.variable_contexts["functions"][str(ctx.ID())] = []
parameters = self.get_parameters(ctx.parameter())
self.insert_text(f"public override {str(ctx.ID())}({', '.join(parameters)}) %s" % "{", 1)
self.do_indent()
def exitOverrideFunction(self, ctx:NishiParser.OverrideFunctionContext):
# self.context = None
self.do_dedent()
self.insert_text("}", 1)
# Function Setters
def enterFunctionSetter(self, ctx:NishiParser.FunctionSetterContext):
if self.context is not None:
self.context = str(ctx.ID())
self.context_type = "functions"
self.variable_contexts["functions"][str(ctx.ID())] = []
parameters = self.get_parameters(ctx.parameter())
self.insert_text(f"public {self.carp_to_csharp[str(ctx.type_().getText())]} {str(ctx.ID())}({', '.join(parameters)}) %s" % "{", 1)
self.do_indent()
def exitFunctionSetter(self, ctx:NishiParser.FunctionSetterContext):
# self.context = None
self.do_dedent()
self.insert_text("}", 1)
# Override Function Setters
def enterOverrideFunctionSetter(self, ctx:NishiParser.OverrideFunctionSetterContext):
if self.context is not None:
self.context = str(ctx.ID())
self.context_type = "functions"
self.variable_contexts["functions"][str(ctx.ID())] = []
parameters = self.get_parameters(ctx.parameter())
self.insert_text(f"public override {str(ctx.ID())}({', '.join(parameters)}) %s" % "{", 1)
self.do_indent()
def exitOverrideFunctionSetter(self, ctx:NishiParser.OverrideFunctionSetterContext):
# self.context = None
self.do_dedent()
self.insert_text("}", 1)
# Function Call
def enterCall_function(self, ctx:NishiParser.Call_functionContext):
func = ctx.ID().getText()
parameters = ctx.call_parameter()
params = []
par = 0
for item in parameters:
params.append(item.getText())
if item.getText() != parameters[-1].getText():
params.append(",")
par += 1
self.insert_text(f"{func}({' '.join(params)})", 1, True)
# Create Class
def enterClass_access(self, ctx:NishiParser.Class_accessContext):
parameters = ctx.call_parameter()
for clas in ctx.ID():
clas = clas.getText()
params = []
par = 0
for item in parameters:
params.append(item.getText())
if item.getText() != parameters[-1].getText():
params.append(",")
par += 1
if clas in self.variable_contexts["classes"]:
self.insert_text(f"new {clas}({' '.join(params)}){'.' if ctx.AT()[0] else ''}", 0, False)
elif clas in self.variable_contexts["functions"][self.context]:
self.insert_text(f"{clas}{'.' if ctx.AT()[0] else ''}", 0, False)
else:
self.insert_text(f"{clas}{'.' if ctx.AT()[0] and clas != ctx.ID()[-1].getText() else ''}", 1, True)
# Private Block
def enterPrivate_block(self, ctx:NishiParser.Private_blockContext):
self.access = "private"
def exitPrivate_block(self, ctx:NishiParser.Private_blockContext):
self.access = None
# Public Block
def enterPublic_block(self, ctx:NishiParser.Public_blockContext):
self.access = "public"
def exitPublic_block(self, ctx:NishiParser.Public_blockContext):
self.access = None
# Static Block
def enterStatic_block(self, ctx:NishiParser.Static_blockContext):
self.static = True
def exitStatic_block(self, ctx:NishiParser.Static_blockContext):
self.static = False
# Import Block
def enterImport_item(self, ctx:NishiParser.Import_itemContext):
imports = []
for item in ctx.ID():
imports.append(item.getText())
self.insert_text(f"using {'.'.join(imports)}", 1, True)
# Assignment
def enterAssignment(self, ctx:NishiParser.AssignmentContext):
type_ = ctx.type_().getText() if ctx.type_() is not None else None
value = ctx.value().getText() if ctx.value() is not None else None
class_var = True if str(ctx.ID()).startswith("this@") else False
is_float = False
multi_string = False
capital_type = False
string = []
if self.access is not None:
string.append(self.access)
if self.static:
string.append("static")
if value:
if value.startswith("`") and value.endswith("`"):
multi_string = True
value = value.replace("`", "\"")
value = "\\n".join(value.splitlines())
if str(ctx.ID()) not in self.variable_contexts[self.context_type][self.context] and type_ is None:
if value and self.access is not None:
string.append(self.convert_types(type(literal_eval(value))))
elif value and self.access is None and not class_var and type_ is None:
string.append("var")
try:
if self.convert_types(type(literal_eval(value))) == "float":
is_float = True
except ValueError:
pass
if self.convert_types(type_) == "float":
is_float = True
if type_:
string.append(self.convert_types(type_))
if not self.istype(type_):
if type_[0].isupper():
capital_type = True
string.append(f"{ctx.ID()}")
if value:
if value.split("(")[0] in self.variable_contexts["classes"]:
self.insert_text(f"{' '.join(string)} = ")
return
elif value.split("(")[0] in self.carp_to_csharp.keys():
capital_type = True
elif value[0].isupper() and value.split("<")[0] in self.carp_to_csharp.keys():
capital_type = True
if value:
if value.startswith("'") and value.endswith("'"):
value = value.replace("'", "\"").replace("\\", "\\\\")
if multi_string:
value = value.replace("\"\"\"", "@\"", 1).replace("\"\"\"", "\"", 1)
if "@" in value:
self.insert_text(f"{' '.join(string)} = {value.split('@')[0]}.")
return
string.append(f"= {'new ' if capital_type else ''}{value}{'f' if is_float else ''}")
self.variable_contexts[self.context_type][self.context].append(str(ctx.ID()))
self.insert_text(f"{' '.join(string).replace('this@', '').replace('@', '.')}", 1, True)
# self.insert_text(f"{' '.join(string).replace('this@', '').replace('@', '.')}", 1, True)
# Print
def enterPrint_(self, ctx:NishiParser.Print_Context):
values = [i.getText() for i in ctx.value()]
if ctx.PRINTLN():
values.append('"\\n"')
prints = []
for item in values:
prints.append(f"System.Console.Write({item})")
self.insert_text("; ".join(prints), 1, True)
def enterReturn_(self, ctx:NishiParser.Return_Context):
value = ctx.value()
expression = ctx.expression()
self.insert_text(f"return {value.getText() if value is not None else expression.getText()}", 1, True)
# If
def enterIf_(self, ctx:NishiParser.If_Context):
self.insert_text(f"if ({ctx.getText().split('{')[0].replace('if', '')}) %s" % "{", 1)
self.do_indent()
def exitIf_(self, ctx:NishiParser.If_Context):
self.do_dedent()
self.insert_text("}", 1)
# Else if
def enterElif_(self, ctx:NishiParser.Elif_Context):
self.insert_text(f"else if ({ctx.getText().split('{')[0].replace('elf', '')}) %s" % "{", 1)
self.do_indent()
def exitElif_(self, ctx:NishiParser.Elif_Context):
self.do_dedent()
self.insert_text("}", 1)
# Else
def enterElse_(self, ctx:NishiParser.Else_Context):
self.insert_text("else {", 1)
self.do_indent()
def exitElse_(self, ctx:NishiParser.Else_Context):
self.do_dedent()
self.insert_text("}", 1)
# Other Methods
def do_indent(self):
self.indent += " " * 4
def do_dedent(self):
self.indent = self.indent[:-4]
def insert_text(self, text: str, newlines: int=0, line_end: bool=False):
if self.pretty_print:
self.output.write(f"{self.indent}{text}{';' if line_end else ''}{os.linesep * newlines}")
else:
self.output.write(f"{text}{';' if line_end else ''}")
# print(self.context, self.variable_contexts)
def get_parameters(self, ctxparameter):
parameters = []
for item in ctxparameter:
name = item.ID().getText()
param = []
if item.type_():
type_ = item.type_().getText()
param.append(self.carp_to_csharp[type_])
param.append(name)
if item.value():
value = item.value().getText()
param.append(f"= {value}")
parameters.append(" ".join(param))
return parameters
def convert_types(self, type_):
try:
return self.carp_to_csharp[type_]
except KeyError:
try:
return self.python_to_csharp[type_]
except KeyError:
return "var" # type_
def istype(self, type_):
if type_ in self.python_to_csharp.keys():
return True
elif type_ in self.carp_to_csharp.keys():
return True
else:
return False
if __name__ == "__main__":
parse_args = argparse.ArgumentParser()
parse_args.add_argument("-f", "--file", type=str, help="the Nishi file")
parse_args.add_argument("-p", "--pretty", action="store_true", help="pretty print the C# file")
args = parse_args.parse_args()
file = args.file
pretty = args.pretty
# file = "examples/simple_class.nishi"
# pretty = True
if file is None:
sys.exit()
lexerer = NishiLexer(antlr4.FileStream(file))
stream = antlr4.CommonTokenStream(lexerer)
parser = NishiParser(stream)
tree = parser.program()
with open(f"{str(file).split('.')[0]}.cs", "w") as out:
interpreter = NishiTranspiler(out, pretty)
walker = antlr4.ParseTreeWalker()
walker.walk(interpreter, tree)