-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavax_runtime.py
More file actions
116 lines (103 loc) · 4.53 KB
/
javax_runtime.py
File metadata and controls
116 lines (103 loc) · 4.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
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
import errors
import jax_print
def run_javax(ast):
output_code = True
global_variables = []
def run_node(node):
match node["type"]:
case "VAR_DECL":
for index in range(len(global_variables)):
if node["name"] == list(global_variables[index].keys())[0]:
global_variables.pop(index)
break
match node["value"]["type"]:
case "STRING":
global_variables.append({node["name"]: str(node["value"]["value"])})
case "INT":
global_variables.append({node["name"]: int(node["value"]["value"])})
case "FLOAT":
global_variables.append({node["name"]: float(node["value"]["value"])})
case "BOOL":
if node["value"]["value"] == "True":
global_variables.append({node["name"]: True})
elif node["value"]["value"] == "False":
global_variables.append({node["name"]: False})
case "FUNCTION_CALL":
variable = run_node(node["value"])
global_variables.append({node["name"]: variable})
case "BIN_EXPR":
variable = run_node(node["value"])
global_variables.append({node["name"]: variable})
case "IDENTIFIER":
for variable in global_variables:
if node["value"]["value"] == list(variable.keys())[0]:
value = run_node(node["value"])
global_variables.append({node["name"]: value})
break
case "FUNCTION_CALL":
if node["name"] == "print":
if output_code:
print(jax_print.jax_print(node, global_variables))
elif node["name"] == "read":
arguments = node["arguments"]
string = arguments[0]["value"]
variable = input(string)
if variable == "True":
variable = True
elif variable == "False":
variable = False
elif variable.isnumeric():
variable = int(variable)
return variable
case "BIN_EXPR":
if type(node["left"]) == list:
left = run_node(node["left"][0])
else:
left = run_node(node["left"])
if type(node["right"]) == list:
right = run_node(node["right"][0])
else:
right = run_node(node["right"])
match node["operator"]:
case "+":
return left + right
case "-":
return left - right
case "*":
return left * right
case "/":
return left / right
case "%":
return left % right
case "**":
return left ** right
case "INT":
return int(node["value"])
case "FLOAT":
return float(node["value"])
case "STRING":
return str(node["value"])
case "BOOL":
if node["value"] == "True":
return True
elif node["value"] == "False":
return False
case "IDENTIFIER":
for variable in global_variables:
if node["value"] == list(variable.keys())[0]:
value = list(variable.values())[0]
if isinstance(value, int):
return int(value)
elif isinstance(value, float):
return float(value)
elif isinstance(value, bool):
return bool(value)
else:
return value
case "IF_STATEMENT":
condition = run_node(node["condition"])
if condition:
for child in node["child"]:
run_node(child)
for node_data in ast:
run_node(node_data["NODE"])