@@ -161,10 +161,11 @@ def __init__(self):
161161 """Initialize the interpreter"""
162162 pass
163163
164- def interpret (self , code : str , variables_dict : dict = {}) -> str :
164+ def interpret (self , code : str , variables_dict : dict = {}, function_dict : dict = {} ) -> str :
165165 """Interprets the code"""
166166 code = code .split (" " )
167167 variables = variables_dict
168+ functions = function_dict
168169 for j in range (0 , len (code )):
169170 if code [j ].startswith ("0" ):
170171 variables = {}
@@ -247,4 +248,36 @@ def interpret(self, code: str, variables_dict: dict = {}) -> str:
247248
248249 if code [j ].startswith ("~" ):
249250 recode = code [j ].replace ("~" , "" , 1 )
250- input (recode )
251+ input (recode )
252+
253+ if code [j ].startswith ("7" ):
254+ recode = code [j ].replace ("7" , "" , 1 ).split ("$" , 2 )
255+ function_name = recode [0 ]
256+ function_parameters = recode [1 ].split ("," )
257+ function_code = recode [2 ].split ("|" )
258+ functions [function_name ] = [function_parameters , function_code ]
259+
260+ if any (str (key ) in code [j ] for key in functions .keys ()) and not code [j ].startswith ("7" ):
261+ recode = code [j ].split ("$" , 1 )
262+ func_name = functions [recode [0 ]]
263+ func_params = recode [1 ].split ("," )
264+ for x in range (0 , len (func_params )):
265+ if func_params [x ] in variables .keys ():
266+ variables [func_name [0 ][x ]] = variables [func_params [x ]]
267+ elif func_params [x ].isdigit ():
268+ variables [func_name [0 ][x ]] = int (func_params [x ])
269+ elif func_params [x ].startswith ("^" ):
270+ cal = func_params [x ].replace ("^" , "" , 1 )
271+ variables [func_name [0 ][x ]] = Calculate (cal , variables )
272+ elif func_params [x ].startswith ("~" ):
273+ inp = func_params [x ].replace ("~" , "" , 1 )
274+ variables [func_name [0 ][x ]] = input (inp )
275+ if variables [func_name [0 ][x ]].isdigit ():
276+ variables [func_name [0 ][x ]] = int (variables [func_name [0 ][x ]])
277+ else :
278+ variables [func_name [0 ][x ]] = func_params [x ]
279+ func_code = ""
280+ for j in range (0 , len (func_name [1 ])):
281+ func_code += func_name [1 ][j ] + " "
282+ self .interpret (Interpreter , func_code , variables , functions )
283+
0 commit comments