-
-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathfunction_statement_to_str.py
More file actions
58 lines (53 loc) · 2.53 KB
/
function_statement_to_str.py
File metadata and controls
58 lines (53 loc) · 2.53 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
from importlib import import_module
from lark import Tree
def function_statement_to_str(statement: Tree) -> str:
expression_to_str_module = import_module("gdtoolkit.formatter.expression_to_str")
expression_to_str = expression_to_str_module.expression_to_str
standalone_expression_to_str = expression_to_str_module.standalone_expression_to_str
return {
"pass_stmt": lambda _: "pass",
"func_var_stmt": lambda s: function_statement_to_str(s.children[0]),
"const_stmt": lambda s: function_statement_to_str(s.children[0]),
"expr_stmt": lambda s: expression_to_str(s.children[0]), # TODO: standalone?
"return_stmt": lambda s: "return" if len(s.children) == 0 else f"return {standalone_expression_to_str(s.children[0])}",
"break_stmt": _not_implemented,
"breakpoint_stmt": lambda _: "breakpoint",
"continue_stmt": _not_implemented,
"if_stmt": _not_implemented,
"while_stmt": _not_implemented,
"for_stmt": _not_implemented,
"for_stmt_typed": _not_implemented,
"match_stmt": _not_implemented,
"annotation": _not_implemented,
# statement fragments:
"func_var_empty": lambda s: f"var {s.children[0].value}",
"func_var_assigned": lambda s: "var {} = {}".format(
s.children[0].value, standalone_expression_to_str(s.children[1])
),
"func_var_inf": lambda s: "var {} := {}".format(
s.children[0].value, standalone_expression_to_str(s.children[1])
),
"func_var_typed": lambda s: "var {}: {}".format(
s.children[0].value, standalone_expression_to_str(s.children[1])
),
"func_var_typed_assgnd": lambda s: "var {}: {} = {}".format(
s.children[0].value,
s.children[1].value,
standalone_expression_to_str(s.children[2]),
),
"const_assigned": lambda s: "const {} = {}".format(
s.children[0].value, standalone_expression_to_str(s.children[1])
),
"const_typed_assigned": lambda s: "const {}: {} = {}".format(
s.children[0].value,
s.children[1].value,
standalone_expression_to_str(s.children[2]),
),
"const_inf": lambda s: "const {} := {}".format(
s.children[0].value, standalone_expression_to_str(s.children[1])
),
"match_branch": _not_implemented,
"guarded_match_branch": _not_implemented,
}[statement.data](statement)
def _not_implemented(statement: Tree) -> str:
raise NotImplementedError