Skip to content

Commit c1c8c37

Browse files
Variable Compiler
1 parent 4c409b9 commit c1c8c37

File tree

12 files changed

+123
-24
lines changed

12 files changed

+123
-24
lines changed

blueprints/examples/calculator/v2/services/processing/operations/sum.code.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"__number_a":{"type":"int", "required":true},
1414
"__number_b":{"type":"int", "required":true}
1515
},
16+
"kwargs":{
17+
"debug":{"type":"bool", "default":false}
18+
},
1619
"outputs":{
1720
"result":{"type":"int", "required":true}
1821
},
@@ -21,7 +24,17 @@
2124
"type": "variable",
2225
"variable_type": "str",
2326
"name": "result",
24-
"equals": "__number_a+number_b"
27+
"expression": "__number_a+number_b"
28+
},
29+
{
30+
"type": "conditional",
31+
"condition": "debug",
32+
"code": [
33+
{
34+
"type": "function_call",
35+
"args": ["result"]
36+
}
37+
]
2538
},
2639
{
2740
"type": "return",

compiler/python_compiler/engines/py3_8/Compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44

55
def compile(blueprint:dict)->str:
6-
build = get_fragment_class(blueprint)
6+
build = get_fragment_class(blueprint, compile)
77
return build.compile()

compiler/python_compiler/engines/py3_8/Conditional.py

Whitespace-only changes.

compiler/python_compiler/engines/py3_8/Fragment.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class Fragment:
22
blueprint:dict
3-
def __init__(self, blueprint) -> None:
3+
general_compile=None#:function
4+
def __init__(self, blueprint, *, compile) -> None:
5+
self.general_compile = compile
46
self.blueprint = blueprint
57
def compile(self)->str:
68
fragment_build = self.blueprint.get("type","")

compiler/python_compiler/engines/py3_8/Function.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class Function(Fragment):
3434
args:dict
3535
kwargs:dict
3636
outputs:dict
37-
def __init__(self, blueprint) -> None:
38-
super().__init__(blueprint)
37+
def __init__(self, blueprint, *args, **kwargs) -> None:
38+
super().__init__( blueprint, *args, **kwargs)
3939
self.name = get_function_name(blueprint)
4040
self.args = get_function_args(blueprint)
4141
self.kwargs = get_function_kwargs(blueprint)
@@ -44,24 +44,26 @@ def __init__(self, blueprint) -> None:
4444
def inputs_compile(self) -> str:
4545
inputs_build = ""
4646
if self.args:
47-
function_args = FunctionArgs(self.args)
47+
function_args = FunctionArgs(self.args, compile=self.general_compile)
4848
inputs_build += function_args.compile()
4949

5050
if self.kwargs:
51-
function_kwargs = FunctionArgs(self.kwargs)
51+
function_kwargs = FunctionArgs(self.kwargs, compile=self.general_compile)
52+
if self.args:
53+
inputs_build += ","
5254
inputs_build += f" *, {function_kwargs.compile()}"
5355
return inputs_build
5456
def outputs_compile(self) -> str:
5557
outputs_build = ""
5658
if len(self.outputs) == 1:
5759
if not (arg_type_name := self.outputs[list(self.outputs)[0]].get(ATTRIBUTE_TYPE)): # TODO: Temporal Outoput test
5860
arg_type_name = ANY
59-
outputs_build += f" -> {get_python_type_str(arg_type_name)} "
61+
outputs_build += f"->{get_python_type_str(arg_type_name)}"
6062
return outputs_build
6163
def code_lines_compile(self) -> list:
6264
code_build_lines = []
6365
for line in self.code:
64-
code_build_lines.append("\t"+line["type"])
66+
code_build_lines.append(TAB+self.general_compile(line))
6567
return code_build_lines
6668
def compile(self)->str:
6769
fragment_lines = []

compiler/python_compiler/engines/py3_8/FunctionArgs.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55

66

77
class FunctionArgs(Fragment):
8-
def __init__(self, blueprint) -> None:
9-
super().__init__(blueprint)
8+
def __init__(self, blueprint, *args, **kwargs) -> None:
9+
super().__init__(blueprint, *args, **kwargs)
1010
def compile(self)->str:
1111
fragment_build = ""
1212
cleaned_args = []
1313
for arg in self.blueprint:
1414
if not (arg_type_name := self.blueprint[arg].get(ATTRIBUTE_TYPE)):
1515
arg_type_name = ANY
16-
cleaned_arg = ""
16+
if (arg_default_value := self.blueprint[arg].get(ATTRIBUTE_DEFAULT, "")) != "":
17+
arg_default_value = "="+str(arg_default_value)
1718
if arg_type := get_python_type_str(arg_type_name):
18-
cleaned_arg = f"{arg}:{arg_type}"
19-
else:
20-
cleaned_arg = arg
19+
arg_type = ":"+arg_type
20+
cleaned_arg = f"{arg}{arg_type}{arg_default_value}"
2121
cleaned_args.append(cleaned_arg)
2222
fragment_build = ", ".join(cleaned_args)
2323
return fragment_build

compiler/python_compiler/engines/py3_8/FunctionCall.py

Whitespace-only changes.

compiler/python_compiler/engines/py3_8/Return.py

Whitespace-only changes.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
7+
DEFAULT_ASSIGN_OPERATOR = "="
8+
ASSIGN_OPERATORS = {
9+
"=":"=",
10+
"+=":"+=",
11+
"-=":"-=",
12+
"*=":"*=",
13+
"/=":"/=",
14+
"//=":"//=",
15+
"%=":"%=",
16+
"**=":"**=",
17+
"&=":"&=",
18+
"|=":"|=",
19+
"^=":"^=",
20+
">>=":">>=",
21+
"<<=":"<<=",
22+
}
23+
24+
def get_variable_name(fragment) -> str:
25+
if not (variable_name := fragment.get(ATTRIBUTE_VARIABLE_NAME)):
26+
CustomLogging.critical(f"Fragment type variable '{ATTRIBUTE_VARIABLE_NAME}' attribute does not exists")
27+
return variable_name
28+
29+
def get_variable_type(fragment) -> str:
30+
if not (variable_type := fragment.get(ATTRIBUTE_VARIABLE_TYPE)):
31+
variable_type = ""
32+
else:
33+
variable_type = ":"+variable_type
34+
return variable_type
35+
36+
def get_variable_assign_operator(fragment) -> str:
37+
if not (variable_assign_operator := fragment.get(ATTRIBUTE_VARIABLE_ASSIGN_OPERATOR)):
38+
variable_assign_operator = DEFAULT_ASSIGN_OPERATOR
39+
return ASSIGN_OPERATORS.get(variable_assign_operator)
40+
41+
def get_variable_expression(fragment) -> str:
42+
if not (variable_expression := fragment.get(ATTRIBUTE_VARIABLE_EXPRESSION)):
43+
CustomLogging.critical(f"Fragment type variable '{ATTRIBUTE_VARIABLE_EXPRESSION}' attribute does not exists")
44+
return variable_expression
45+
46+
47+
class Variable(Fragment):
48+
name:str
49+
variable_type:str
50+
assign_operator:str
51+
expression:str
52+
def __init__(self, blueprint, *args, **kwargs) -> None:
53+
super().__init__(blueprint, *args, **kwargs)
54+
self.name = get_variable_name(blueprint)
55+
self.variable_type = get_variable_type(blueprint)
56+
self.assign_operator = get_variable_assign_operator(blueprint)
57+
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}"
61+
return fragment_build

compiler/python_compiler/engines/py3_8/get_fragment_class.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
from .Fragment import Fragment
2-
from .Function import Function
31
from utils.flags import *
42
from utils.CustomLogging import CustomLogging
3+
4+
from .Fragment import Fragment
5+
from .Function import Function
6+
from .Variable import Variable
7+
from .Conditional import Conditional
8+
from .FunctionCall import FunctionCall
9+
from .Return import Return
10+
11+
512
FRAGMENT_TYPES={
613
"function": Function,
14+
"function_call":FunctionCall,
715
"class":Fragment,
8-
"variable":Fragment,
9-
"conditional":Fragment,
10-
"return":Fragment
16+
"variable":Variable,
17+
"conditional":Conditional,
18+
"return":Return
1119
}
1220

13-
def get_fragment_class(blueprint):
21+
def get_fragment_class(blueprint, compile):
1422
if fragment_type := blueprint.get(ATTRIBUTE_FRAGMENT_TYPE):
1523
if fragment_class := FRAGMENT_TYPES[fragment_type]:
16-
return fragment_class(blueprint)
24+
return fragment_class(blueprint, compile=compile)
1725
else:
1826
CustomLogging.error(f"Fragment type {fragment_type} does not exists")
1927
else:

compiler/utils/CustomLogging.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ def log(msg:str):
33
print("LOG:",msg)
44
def error(msg:str):
55
raise NameError(msg)
6+
def critical(msg:str):
7+
raise NameError(msg)
68
def warning(msg:str):
79
print("WARNING:",msg)

compiler/utils/flags.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
1-
MODELS_FIELD = "models"
2-
SEVICES_FIELD = "services"
31
SPECIAL_FIELD_FLAG = "__"
42
def is_flag(text:str):
53
return text.startswith(SPECIAL_FIELD_FLAG)
64
def to_flag(text:str):
75
return SPECIAL_FIELD_FLAG+text
86

7+
MODELS_FIELD = "models"
8+
SEVICES_FIELD = "services"
9+
910
FLAG_EXTENDS = to_flag("extends")
1011
FLAG_EXCLUDES = to_flag("excludes")
1112
FLAG_INCLUDES = to_flag("includes")
1213

1314
FLAG_FROM = to_flag("from")
1415

1516

17+
TAB = "\t"
1618

1719
ATTRIBUTE_FRAGMENT_TYPE = "type"
1820
ATTRIBUTE_TYPE = "type"
21+
ATTRIBUTE_DEFAULT = "default"
1922

2023
ATTRIBUTE_FUNCTION_NAME = "name"
2124
ATTRIBUTE_FUNCTION_ARGS = "args"
2225
ATTRIBUTE_FUNCTION_KWARGS = "kwargs"
2326
ATTRIBUTE_FUNCTION_OUTPUTS = "outputs"
2427
ATTRIBUTE_FUNCTION_CODE = "code"
28+
29+
ATTRIBUTE_FUNCTION_RETURN_ARGS = "args"
30+
31+
32+
ATTRIBUTE_VARIABLE_NAME = "name"
33+
ATTRIBUTE_VARIABLE_TYPE = "variable_type"
34+
ATTRIBUTE_VARIABLE_EXPRESSION = "expression"
35+
ATTRIBUTE_VARIABLE_ASSIGN_OPERATOR = "assign_operator"

0 commit comments

Comments
 (0)