This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objgen.py
75 lines (57 loc) · 3.09 KB
/
objgen.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
# Import all the objects
from Objects.varObject import VariableObject
from Objects.conditionObject import ConditionObject
from Objects.builtinObject import BuiltInFunctionObject
from Objects.commentObject import CommentObject
from Objects.loopObject import LoopObject
class ObjectGenerator():
def __init__(self, source_ast):
# This will contain all the AST's in forms of dictionaries to help with creating AST object
self.source_ast = source_ast['main_scope']
# This will hold the executable string of transplied Y# code to python
self.exec_string = ""
def object_definer(self, isGettingBody):
""" Object Definer
This method will find all the different ast objects within the ast dictionary
and call all the objects and pass in the ast dictionary to get a python
string of code back which is the Y# code transpiled into python
returns:
python_code (str) : This will written a string of python code
"""
# Iterate through all ast dictionaries
for ast in self.source_ast:
# Create dictionary var object and append exec string global exec string
if self.check_ast('VariableDecleration', ast):
gen_var = VariableObject(ast)
self.exec_string += gen_var.transpile() + '\n'
# Create dictionary condition object and append exec string global exec string
if self.check_ast('ConditionalStatement', ast):
gen_condition = ConditionObject(ast, 1)
self.exec_string += gen_condition.transpile() + '\n'
# Create dictionary builtin object and append exec string global exec string
if self.check_ast('PrebuiltFunction', ast):
gen_builtin = BuiltInFunctionObject(ast)
self.exec_string += gen_builtin.transpile() + "\n"
# Create dictionary comment object and append exec string to global exec string when return from transpile methof
if self.check_ast('Comment', ast):
gen_comment = CommentObject(ast)
self.exec_string += gen_comment.transpile() + "\n"
# Create dictionary repetition object and append exec string global exec string
if self.check_ast('ForLoop', ast):
gen_loop = LoopObject(ast, 1)
self.exec_string += gen_loop.transpile() + "\n"
return self.exec_string
def check_ast(self, astName, ast):
""" Call and Set Exec
This method will check if the AST dictionary item being looped through has the
same key name as the `astName` argument
args:
astName (str) : This will hold the ast name we are matching
ast (dict) : The dict which the astName match will be done against
returns:
True (bool) : If the astName matches the one in `ast` arg
False (bool) : If the astName doesn't matches the one in `ast` arg
"""
try:
if ast[astName]: return True
except: return False