Skip to content

Commit e67d347

Browse files
for loop compiler and get_lines for all fragment compilers
1 parent c98849c commit e67d347

File tree

14 files changed

+210
-36
lines changed

14 files changed

+210
-36
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"engine": "python3.8",
3+
"__description": "checks if is prime",
4+
"__constructor":{
5+
"number_a":{"type":"str", "default":"num"},
6+
"function_name":{"type":"str", "default":"is_prime"}
7+
},
8+
"type": "function",
9+
"name": "__function_name",
10+
"args":{
11+
"__number_a":{"type":"int", "required":true}
12+
},
13+
"kwargs":{
14+
"debug":{"type":"bool", "default":false}
15+
},
16+
"outputs":{
17+
"is_prime":{"type":"bool", "required":true}
18+
},
19+
"code": [
20+
{
21+
"type": "conditional",
22+
"condition": "__number_a == 0 or __number_a == 1",
23+
"code": [
24+
{
25+
"type": "return",
26+
"args": ["False"]
27+
}
28+
]
29+
},
30+
{
31+
"type": "for",
32+
"iterators": ["x"],
33+
"sequence": "range(2, num)",
34+
"code": [
35+
{
36+
"type": "conditional",
37+
"condition": "__number_a % x == 0",
38+
"code": [
39+
{
40+
"type": "return",
41+
"args": ["False"]
42+
}
43+
]
44+
}
45+
],
46+
"else": [
47+
{
48+
"type": "return",
49+
"args": ["True"]
50+
}
51+
]
52+
}
53+
54+
]
55+
}

code.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def is_prime(num:int, *, debug:bool=False)->bool:
2+
if num == 0 or num == 1:
3+
return False
4+
for x in range(2, num):
5+
if num % x == 0:
6+
return False
7+
else:
8+
return True

compiler/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ def main():
4848
# python .\compiler.py c "django.json.v1" ../blueprints/examples/facebook/main.app.json build
4949
# python .\compiler.py c "python.json.v1" ../blueprints/examples/calculator/v2/services/processing/operations/sum.code.json build/build
5050

51+
# python .\compiler\compiler.py c "python.json.v1" ./blueprints/global/utils/code/is_prime.json code.py

compiler/python_compiler/engines/py3_8/Compiler.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
from .get_fragment_class import get_fragment_class
33

44

5+
def lines_compile(blueprint:dict, *, level = 0)->str:
6+
build = get_fragment_class(blueprint, lines_compile, level=level)
7+
return build.get_lines()
8+
59
def compile(blueprint:dict, *, level = 0)->str:
6-
build = get_fragment_class(blueprint, compile, level=level)
10+
build = get_fragment_class(blueprint, lines_compile, level=level)
711
return build.compile()

compiler/python_compiler/engines/py3_8/Conditional.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ def __init__(self, blueprint, *args, **kwargs) -> None:
1919
super().__init__( blueprint, *args, **kwargs)
2020
self.condition = get_conditional_condition(blueprint)
2121
self.code = get_conditional_code(blueprint)
22-
def code_lines_compile(self) -> list:
23-
code_build_lines = []
24-
for line in self.code:
25-
code_build_lines.append(TAB*(self.level+1)+self.general_compile(line))
26-
return code_build_lines
27-
def compile(self)->str:
22+
23+
def get_lines(self) -> list:
2824
fragment_lines = []
2925
fragment_lines.append(f"if {self.condition}:")
30-
fragment_lines.extend(self.code_lines_compile())
31-
fragment_build = "\n".join(fragment_lines)
32-
26+
fragment_lines.extend(self.code_lines_compile(self.code))
27+
fragment_lines = self.tabulate(fragment_lines)
28+
return fragment_lines
29+
30+
def compile(self) -> str:
31+
fragment_build = "\n".join(self.get_lines())
3332
return fragment_build
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from .Fragment import Fragment
2+
from utils.flags import *
3+
from utils.CustomLogging import CustomLogging
4+
from python_compiler.engines.utils.types import get_python_type_str, ANY
5+
6+
def get_for_code(fragment) -> str:
7+
if not (code := fragment.get(ATTRIBUTE_FOR_CODE)):
8+
CustomLogging.critical(f"Fragment type for '{ATTRIBUTE_FOR_CODE}' attribute does not exist")
9+
return code
10+
11+
def get_for_else_code(fragment) -> str:
12+
if not (code := fragment.get(ATTRIBUTE_FOR_ELSE_CODE)):
13+
CustomLogging.critical(f"Fragment type for '{ATTRIBUTE_FOR_ELSE_CODE}' attribute does not exist")
14+
return code
15+
16+
def get_for_iterators(fragment) -> list:
17+
if not (iterators := fragment.get(ATTRIBUTE_FOR_ITERATORS)):
18+
CustomLogging.critical(f"Fragment type for '{ATTRIBUTE_FOR_ITERATORS}' attribute does not exist")
19+
return iterators
20+
21+
def get_for_sequence(fragment) -> list:
22+
if not (sequence := fragment.get(ATTRIBUTE_FOR_SEQUENCE)):
23+
CustomLogging.critical(f"Fragment type for '{ATTRIBUTE_FOR_SEQUENCE}' attribute does not exist")
24+
return sequence
25+
26+
27+
class For(Fragment):
28+
iterators:list
29+
sequence:str
30+
code:list
31+
else_code:list
32+
def __init__(self, blueprint, *args, **kwargs) -> None:
33+
super().__init__( blueprint, *args, **kwargs)
34+
self.iterators = get_for_iterators(blueprint)
35+
self.sequence = get_for_sequence(blueprint)
36+
self.code = get_for_code(blueprint)
37+
self.else_code = get_for_else_code(blueprint)
38+
39+
def iterators_compile(self) -> str:
40+
iterators_build = ", ".join(self.iterators)
41+
return iterators_build
42+
43+
def get_lines(self) -> list:
44+
fragment_lines = []
45+
fragment_lines.append(f"for {self.iterators_compile()} in {self.sequence}:")
46+
fragment_lines.extend(self.code_lines_compile(self.code))
47+
fragment_lines.append("else:")
48+
fragment_lines.extend(self.code_lines_compile(self.else_code))
49+
fragment_lines = self.tabulate(fragment_lines)
50+
return fragment_lines
51+
52+
def compile(self) -> str:
53+
fragment_build = "\n".join(self.get_lines())
54+
return fragment_build
Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
1+
from utils.flags import *
2+
13
class Fragment:
2-
blueprint:dict
3-
general_compile=None#:function
4-
level=0
4+
blueprint = {}
5+
general_compile = None#:function
6+
level = 0
7+
inports = []
58
def __init__(self, blueprint, *, compile, level=0) -> None:
69
self.general_compile = compile
710
self.blueprint = blueprint
811
self.level = level
9-
def compile(self)->str:
12+
13+
def get_lines(self) -> list:
14+
fragment_lines = []
15+
fragment_lines = self.tabulate(fragment_lines)
16+
return fragment_lines
17+
18+
def code_lines_compile(self, fragments) -> list:
19+
code_build_lines = []
20+
for line in fragments:
21+
line_build = self.general_compile(line, level=self.level+1) # level=self.level+1
22+
code_build_lines.extend(line_build)
23+
return code_build_lines
24+
25+
def tabulate(self, lines) -> list:
26+
tabulated_lines = []
27+
for line in lines:
28+
tabs = TAB * ( 0 if self.level == 0 else 1)
29+
tabulated_lines.append(tabs+line)
30+
return tabulated_lines
31+
32+
def compile(self) -> str:
1033
fragment_build = self.blueprint.get("type","")
11-
return fragment_build
34+
return fragment_build
35+

compiler/python_compiler/engines/py3_8/Function.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,14 @@ def outputs_compile(self) -> str:
6060
arg_type_name = ANY
6161
outputs_build += f"->{get_python_type_str(arg_type_name)}"
6262
return outputs_build
63-
def code_lines_compile(self) -> list:
64-
code_build_lines = []
65-
for line in self.code:
66-
code_build_lines.append(TAB*(self.level+1)+self.general_compile(line, level=self.level+1))
67-
return code_build_lines
68-
def compile(self)->str:
63+
64+
def get_lines(self) -> list:
6965
fragment_lines = []
7066
fragment_lines.append(f"def {self.name}({self.inputs_compile()}){self.outputs_compile()}:")
71-
fragment_lines.extend(self.code_lines_compile())
72-
fragment_build = "\n".join(fragment_lines)
73-
67+
fragment_lines.extend(self.code_lines_compile(self.code))
68+
fragment_lines = self.tabulate(fragment_lines)
69+
return fragment_lines
70+
71+
def compile(self) -> str:
72+
fragment_build = "\n".join(self.get_lines())
7473
return fragment_build

compiler/python_compiler/engines/py3_8/FunctionArgs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class FunctionArgs(Fragment):
88
def __init__(self, blueprint, *args, **kwargs) -> None:
99
super().__init__(blueprint, *args, **kwargs)
10-
def compile(self)->str:
10+
def compile(self) -> str:
1111
fragment_build = ""
1212
cleaned_args = []
1313
for arg in self.blueprint:

compiler/python_compiler/engines/py3_8/FunctionCall.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ def inputs_compile(self) -> str:
3737

3838
inputs_build = ", ".join(inputs)
3939
return inputs_build
40-
def compile(self)->str:
41-
fragment_build = f"{self.name}({self.inputs_compile()})"
42-
40+
41+
def get_lines(self) -> list:
42+
fragment_lines = []
43+
fragment_lines.append(f"{self.name}({self.inputs_compile()})")
44+
fragment_lines = self.tabulate(fragment_lines)
45+
return fragment_lines
46+
47+
def compile(self) -> str:
48+
fragment_build = "\n".join(self.get_lines())
4349
return fragment_build

compiler/python_compiler/engines/py3_8/Return.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,18 @@ class Return(Fragment):
1313
def __init__(self, blueprint, *args, **kwargs) -> None:
1414
super().__init__( blueprint, *args, **kwargs)
1515
self.return_attributes = get_return_attributes(blueprint)
16-
def compile(self)->str:
17-
fragment_build = ""
16+
17+
def return_compile(self) -> list:
1818
return_attributes_build = ", ".join(self.return_attributes)
19-
fragment_build = f"return {return_attributes_build}"
19+
return_fragment_build = f"return {return_attributes_build}"
20+
return [return_fragment_build]
21+
22+
def get_lines(self) -> list:
23+
fragment_lines = []
24+
fragment_lines.extend(self.return_compile())
25+
fragment_lines = self.tabulate(fragment_lines)
26+
return fragment_lines
27+
28+
def compile(self) -> str:
29+
fragment_build = "\n".join(self.get_lines())
2030
return fragment_build

compiler/python_compiler/engines/py3_8/Variable.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ def __init__(self, blueprint, *args, **kwargs) -> None:
5555
self.variable_type = get_variable_type(blueprint)
5656
self.assign_operator = get_variable_assign_operator(blueprint)
5757
self.expression = get_variable_expression(blueprint)
58-
def compile(self)->str:
59-
fragment_build = ""
60-
fragment_build = f"{self.name}{self.variable_type} {self.assign_operator} {self.expression}"
58+
59+
def get_lines(self) -> list:
60+
fragment_lines = []
61+
fragment_lines.append(f"{self.name}{self.variable_type} {self.assign_operator} {self.expression}")
62+
fragment_lines = self.tabulate(fragment_lines)
63+
return fragment_lines
64+
65+
def compile(self) -> str:
66+
67+
fragment_build = "\n".join(self.get_lines())
6168
return fragment_build

compiler/python_compiler/engines/py3_8/get_fragment_class.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .Conditional import Conditional
88
from .FunctionCall import FunctionCall
99
from .Return import Return
10+
from .For import For
1011

1112

1213
FRAGMENT_TYPES={
@@ -15,7 +16,8 @@
1516
"class":Fragment,
1617
"variable":Variable,
1718
"conditional":Conditional,
18-
"return":Return
19+
"return":Return,
20+
"for":For
1921
}
2022

2123
def get_fragment_class(blueprint, compile, *, level=0):

compiler/utils/flags.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def to_flag(text:str):
1414
FLAG_FROM = to_flag("from")
1515

1616

17-
TAB = "\t"
17+
TAB = ["\t", " "*4,"*"][1]
1818

1919
ATTRIBUTE_FRAGMENT_TYPE = "type"
2020
ATTRIBUTE_TYPE = "type"
@@ -35,5 +35,10 @@ def to_flag(text:str):
3535
ATTRIBUTE_VARIABLE_EXPRESSION = "expression"
3636
ATTRIBUTE_VARIABLE_ASSIGN_OPERATOR = "assign_operator"
3737

38+
ATTRIBUTE_FOR_ITERATORS = "iterators"
39+
ATTRIBUTE_FOR_SEQUENCE = "sequence"
40+
ATTRIBUTE_FOR_CODE = "code"
41+
ATTRIBUTE_FOR_ELSE_CODE = "else"
42+
3843
ATTRIBUTE_CONDITIONAL_CONDITION = "condition"
3944
ATTRIBUTE_CONDITIONAL_CODE = "code"

0 commit comments

Comments
 (0)