Skip to content

Commit 30011f7

Browse files
committed
1.12.0 - Added Import statements and an example Library
1 parent 7e50cea commit 30011f7

File tree

7 files changed

+58
-12
lines changed

7 files changed

+58
-12
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,5 @@ cython_debug/
161161

162162
#VS-Code
163163
.vscode/
164-
site/
164+
site/
165+
unix-venv/

Libs/extends.ns

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
%The-Extends-Library-For-NumberScript
2+
%By-Sasen-Perera-2022
3+
%
4+
%This-library-is-used-to-make-the-normal-single-character-commands-into-more-readable-functions.
5+
0
6+
7print$string$2string
7+
7print_char$string,[email protected]
8+
7input$string$~string
9+
7bool$val1,val2,sign$?sign==|4val1=val2|?sign=!|4val1!val2|?sign=>|4val1>val2|?sign=<|4val1<val2|2Error:Invalid-sign
10+
1

NumberScript/__main__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
parser = argparse.ArgumentParser()
1212
parser.add_argument("-f", "--file", help="file to interpret")
13+
parser.add_argument("-m", "--module", help="module to interpret", action="store_true")
1314
parser.add_argument("-v", "--version", action="store_true", help="show version")
1415
args = parser.parse_args()
1516

@@ -29,6 +30,7 @@
2930
% <- comment
3031
^number[+\-\/\*\#]number <- Math-Operation-Start
3132
?condition|True|False <- If-Else
33+
#path/to/file(don't put .ns at the end) <- Import
3234
"""
3335

3436
space = ""
@@ -42,7 +44,11 @@
4244
with open(args.file, "r") as file:
4345
code = file.read()
4446
code = code.replace("\n", " ")
45-
Interpreter.interpret(Interpreter, code)
47+
if args.module:
48+
print(Interpreter.interpret(Interpreter, code, library=True))
49+
else:
50+
Interpreter.interpret(Interpreter, code)
51+
4652
elif args.version:
4753
print(f"Version: {ver}")
4854
else:

NumberScript/interpreter.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from functools import reduce
22
from operator import mod
33
from 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

510
def 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

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,4 @@ You can find more examples in the `examples` folder.
8282
8383
## To-Do
8484

85-
- [ ] Import and library system
8685
- [ ] Possibly OOP

examples/example.ns

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
3a:Hello
3+
?a=Hello|2bye|2hello
4+
1

setup.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55

66
setuptools.setup(
77
name="NumberScript",
8-
version="1.11.7",
8+
version="1.12.0",
99
description="possibly the world's most simplest and restricted language.",
1010
author="Sasen Perera",
1111
long_description=longest_description,
1212
long_description_content_type="text/markdown",
1313
packages=["NumberScript"],
1414
github_url="https://github.com/Sas2k/NumberScript",
15-
license="MIT"
15+
license="MIT",
16+
classifiers=[
17+
"Programming Language :: Python :: 3",
18+
"License :: OSI Approved :: MIT License",
19+
"Operating System :: OS Independent",
20+
],
21+
python_requires='>=3.6'
1622
)

0 commit comments

Comments
 (0)