11from functools import reduce
22from operator import mod
33from random import randint
4+ from os import path , name
5+
6+ import site
7+
8+ Default_Libs_Path = site .getusersitepackages () + "\\ NumberScript\\ Libs" if name == "nt" else site .getusersitepackages () + "/NumberScript/Libs"
49
510def Calculate (removed_code : str , variables : dict ) -> int :
611 """Calculator"""
@@ -162,7 +167,7 @@ def __init__(self):
162167 """Initialize the interpreter"""
163168 pass
164169
165- def interpret (self , code : str , variables_dict : dict = {}, function_dict : dict = {}) -> str :
170+ def interpret (self , code : str , variables_dict : dict = {}, function_dict : dict = {}, library : bool = False ) -> str :
166171 """Interprets the code"""
167172 code = code .split (" " )
168173 variables = variables_dict
@@ -172,8 +177,20 @@ def interpret(self, code: str, variables_dict: dict = {}, function_dict: dict =
172177 variables = {}
173178
174179 if code [j ].startswith ("1" ):
175- variables = {}
176- return
180+ break
181+
182+ if code [j ].startswith ("#" ):
183+ recode = code [j ].replace ("#" , "" , 1 )
184+ file_location = f"{ recode } .ns"
185+ module_code = open (f"{ file_location } " , "r" ).read ()
186+ module_code = module_code .replace ("\n " , " " )
187+ module_args = self .interpret (Interpreter , module_code , variables , functions , True )
188+ module_varNames = list (module_args [0 ].keys ())
189+ module_funcNames = list (module_args [1 ].keys ())
190+ for x in range (0 , len (module_args [0 ])):
191+ variables [module_varNames [x ]] = module_args [0 ][module_varNames [x ]]
192+ for x in range (0 , len (module_args [1 ])):
193+ functions [module_funcNames [x ]] = module_args [1 ][module_funcNames [x ]]
177194
178195 if code [j ].startswith ("2" ):
179196 if code [j ][1 :] in variables .keys ():
@@ -280,10 +297,10 @@ def interpret(self, code: str, variables_dict: dict = {}, function_dict: dict =
280297 repeat_times = int (variables [recode [1 ]]) if recode [1 ] in variables .keys () else int (recode [1 ])
281298 repeat_code = recode [2 ].replace (";" , " " )
282299 variables [repeat_name ] = 0
283- variabless = variables
300+ loop_variables = variables
284301 for x in range (0 , repeat_times ):
285- self .interpret (Interpreter , repeat_code , variabless )
286- variabless [repeat_name ] += 1
302+ self .interpret (Interpreter , repeat_code , loop_variables )
303+ loop_variables [repeat_name ] += 1
287304
288305 if code [j ].startswith ("~" ):
289306 recode = code [j ].replace ("~" , "" , 1 )
@@ -292,7 +309,7 @@ def interpret(self, code: str, variables_dict: dict = {}, function_dict: dict =
292309 if code [j ].startswith ("7" ):
293310 recode = code [j ].replace ("7" , "" , 1 ).split ("$" , 2 )
294311 function_name = recode [0 ]
295- function_parameters = recode [1 ].split ("," )
312+ function_parameters = recode [1 ].split ("," ) if "," in recode [ 1 ] else [ recode [ 1 ]]
296313 function_code = recode [2 ].split ("|" )
297314 functions [function_name ] = [function_parameters , function_code ]
298315
@@ -329,4 +346,7 @@ def interpret(self, code: str, variables_dict: dict = {}, function_dict: dict =
329346 for j in range (0 , len (func_name [1 ])):
330347 func_code += func_name [1 ][j ] + " "
331348 self .interpret (Interpreter , func_code , variables , functions )
349+
350+ if library == True :
351+ return [variables , functions ]
332352
0 commit comments