stack-lang-engine 0.0.2__tar.gz → 0.0.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,100 @@
1
+ Metadata-Version: 2.4
2
+ Name: stack_lang_engine
3
+ Version: 0.0.3
4
+ Summary: An engine for creating stack-oriented DSLs or mini-programming languages.
5
+ License-Expression: GPL-3.0-or-later
6
+ Project-URL: Repository, https://github.com/MrHacer201145/SLE/
7
+ Requires-Python: >=3.8
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Dynamic: license-file
11
+
12
+ # What is "Stack Lang Engine"
13
+ SLE (Stack Lang Engine) is a simple tool for creating
14
+ stack-oriented DSL or even small programming languages.
15
+
16
+ # Usage
17
+ ## Firstly
18
+ Of course we need to import a module, and create object with type
19
+ "StackEngine"
20
+ ```py
21
+ import stack_lang_engine as sle
22
+
23
+ eng = sle.StackEngine()
24
+ ```
25
+
26
+ ## Definition of words:
27
+ This is how to define/delete words in StackEngine
28
+ ```py
29
+ # Define new word
30
+ eng.add_word("print", lambda: print(eng.pop()))
31
+
32
+ # Delete word
33
+ eng.del_word("print")
34
+ ```
35
+
36
+ ## How to execute text (given as string)
37
+ ```py
38
+ # If you use the default regex pattern,
39
+ # it automatically recognizes whether it is a string or a number.
40
+ eng.exec('10 20 30 "40"')
41
+ ```
42
+
43
+ ## How to create stack and APIs for it
44
+ ```py
45
+ # Create new stack, we can use "create_api" option
46
+ # to create eng.{name}_push, eng.{name}_pop() methods
47
+ # for object
48
+
49
+ eng.new_stack("stacky", create_api=True)
50
+ # now we have eng.stacky_push and eng.stacky_pop
51
+
52
+ eng.stacky_push(10)
53
+ print(eng.stacky)
54
+ eng.stacky_pop()
55
+ ```
56
+
57
+ ## Usefull APIs
58
+ ```py
59
+ # Instead of writing "eng.stack.pop" or more we can use builtin
60
+ # methods like eng.(push, pop, clear)
61
+
62
+ eng.push(10) # For example
63
+ ```
64
+
65
+ ## Recognizers
66
+ ```py
67
+ # We can create a new regex pattern,
68
+ # and both the parser and the interpreter must handle it.
69
+
70
+ eng.add_recognizer(
71
+ name="Test", # Name of recognizer
72
+ pattern=r'Test', # Regex pattern
73
+ on_regex=lambda val: val, # What parser should get (or do) when it parses it
74
+ on_interpret=lambda val: print("Hello, Test!?"), # What interpreter should do when it gets it
75
+ )
76
+
77
+ # and if then we execute it will output
78
+ # "Hello, Test!?" when it interprets word
79
+ # with "Test" kind
80
+
81
+ eng.exec("Test 10 20")
82
+ print(eng.stack) # It will work correctly, so in stack only 10 and 20
83
+ ```
84
+
85
+ ## How to write more short code in this engine
86
+ ```py
87
+ # For example we can set words dict instead of creating each
88
+ # word over and over
89
+
90
+ eng = sle.StackEngine(words={
91
+ "print": lambda: print(eng.pop())
92
+ })
93
+
94
+ # You can also read the interpreter's code itself on
95
+ # GitHub to better understand how it works.
96
+ ```
97
+ ### Thanks for reading the documentation!
98
+
99
+ # License
100
+ SLE is licensed under GPLv3 License
@@ -0,0 +1,89 @@
1
+ # What is "Stack Lang Engine"
2
+ SLE (Stack Lang Engine) is a simple tool for creating
3
+ stack-oriented DSL or even small programming languages.
4
+
5
+ # Usage
6
+ ## Firstly
7
+ Of course we need to import a module, and create object with type
8
+ "StackEngine"
9
+ ```py
10
+ import stack_lang_engine as sle
11
+
12
+ eng = sle.StackEngine()
13
+ ```
14
+
15
+ ## Definition of words:
16
+ This is how to define/delete words in StackEngine
17
+ ```py
18
+ # Define new word
19
+ eng.add_word("print", lambda: print(eng.pop()))
20
+
21
+ # Delete word
22
+ eng.del_word("print")
23
+ ```
24
+
25
+ ## How to execute text (given as string)
26
+ ```py
27
+ # If you use the default regex pattern,
28
+ # it automatically recognizes whether it is a string or a number.
29
+ eng.exec('10 20 30 "40"')
30
+ ```
31
+
32
+ ## How to create stack and APIs for it
33
+ ```py
34
+ # Create new stack, we can use "create_api" option
35
+ # to create eng.{name}_push, eng.{name}_pop() methods
36
+ # for object
37
+
38
+ eng.new_stack("stacky", create_api=True)
39
+ # now we have eng.stacky_push and eng.stacky_pop
40
+
41
+ eng.stacky_push(10)
42
+ print(eng.stacky)
43
+ eng.stacky_pop()
44
+ ```
45
+
46
+ ## Usefull APIs
47
+ ```py
48
+ # Instead of writing "eng.stack.pop" or more we can use builtin
49
+ # methods like eng.(push, pop, clear)
50
+
51
+ eng.push(10) # For example
52
+ ```
53
+
54
+ ## Recognizers
55
+ ```py
56
+ # We can create a new regex pattern,
57
+ # and both the parser and the interpreter must handle it.
58
+
59
+ eng.add_recognizer(
60
+ name="Test", # Name of recognizer
61
+ pattern=r'Test', # Regex pattern
62
+ on_regex=lambda val: val, # What parser should get (or do) when it parses it
63
+ on_interpret=lambda val: print("Hello, Test!?"), # What interpreter should do when it gets it
64
+ )
65
+
66
+ # and if then we execute it will output
67
+ # "Hello, Test!?" when it interprets word
68
+ # with "Test" kind
69
+
70
+ eng.exec("Test 10 20")
71
+ print(eng.stack) # It will work correctly, so in stack only 10 and 20
72
+ ```
73
+
74
+ ## How to write more short code in this engine
75
+ ```py
76
+ # For example we can set words dict instead of creating each
77
+ # word over and over
78
+
79
+ eng = sle.StackEngine(words={
80
+ "print": lambda: print(eng.pop())
81
+ })
82
+
83
+ # You can also read the interpreter's code itself on
84
+ # GitHub to better understand how it works.
85
+ ```
86
+ ### Thanks for reading the documentation!
87
+
88
+ # License
89
+ SLE is licensed under GPLv3 License
@@ -0,0 +1,14 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "stack_lang_engine"
7
+ version = "0.0.3"
8
+ description = "An engine for creating stack-oriented DSLs or mini-programming languages."
9
+ readme = "README.md"
10
+ requires-python = ">=3.8"
11
+ license = "GPL-3.0-or-later"
12
+
13
+ [project.urls]
14
+ Repository = "https://github.com/MrHacer201145/SLE/"
@@ -0,0 +1,83 @@
1
+ import re
2
+
3
+
4
+ class UndefinedWordError(BaseException):
5
+ "Exception for words that do not exist"
6
+
7
+
8
+ class StackEngine:
9
+ def __init__(self,
10
+ words={},
11
+ stack=[],
12
+ recognizers={},
13
+ regex_recognizers={},
14
+ undefined_error_msg=lambda word: f"Word '{word}' is not defined",
15
+ token_spec = [
16
+ ("STR", r'"[^"]*"'),
17
+ ("INT", r'\d+(\.\d+)?'),
18
+ ("IDENT", r'[a-zA-Z_][a-zA-Z0-9_]*'),
19
+ ("SKIP", r'[ \n\t]')
20
+ ]
21
+ ):
22
+ self.words = words
23
+ self.stack = stack
24
+ self.error_msg = undefined_error_msg
25
+ self.recognizers = recognizers
26
+ self.regex_recognizers = regex_recognizers
27
+ self.token_spec = token_spec
28
+
29
+ def add_word(self, name: str, func) -> None:
30
+ self.words[name] = func
31
+ def del_word(self, name: str) -> None:
32
+ del self.words[name]
33
+
34
+ def add_recognizer(self, name: str, pattern: str, on_regex: callable, on_interpret: callable) -> None:
35
+ self.token_spec.insert(0, (name, pattern))
36
+ self.recognizers[name] = on_regex
37
+ self.regex_recognizers[name] = on_interpret
38
+
39
+ def new_stack(self, name, content=[], create_api=False):
40
+ self.__dict__[name] = content
41
+
42
+ if create_api:
43
+ self.__dict__[f'{name}_push'] = lambda val: self.__dict__[name].append(val)
44
+ self.__dict__[f'{name}_pop'] = lambda: self.__dict__[name].pop()
45
+
46
+ def push(self, value: any) -> None:
47
+ self.stack.append(value)
48
+ def pop(self) -> any:
49
+ return self.stack.pop()
50
+ def clear(self) -> None:
51
+ self.stack.clear()
52
+
53
+ def _parse(self, text: str) -> list[tuple[str, any]]:
54
+ tokens = []
55
+ tok_regex = "|".join(f"(?P<{pair[0]}>{pair[1]})" for pair in self.token_spec)
56
+
57
+ for mo in re.finditer(tok_regex, text):
58
+ kind, val = mo.lastgroup, mo.group()
59
+
60
+ if kind == "SKIP": continue
61
+ elif kind in self.regex_recognizers:
62
+ val = self.regex_recognizers[kind](val)
63
+ elif kind == "STR": val = val[1:-1]
64
+ elif kind == "INT": val = int(val)
65
+ tokens.append((kind, val))
66
+
67
+ return tokens
68
+
69
+ def exec(self, text: str) -> None:
70
+ "Interprets given string"
71
+ words = self._parse(text)
72
+
73
+ for word in words:
74
+ kind, val = word
75
+ if kind == "IDENT":
76
+ if val in self.words:
77
+ self.words[val]()
78
+ else:
79
+ raise UndefinedWordError(self.error_msg(val))
80
+ elif kind in self.recognizers:
81
+ self.recognizers[kind](val)
82
+ else:
83
+ self.stack.append(val)
@@ -0,0 +1,100 @@
1
+ Metadata-Version: 2.4
2
+ Name: stack_lang_engine
3
+ Version: 0.0.3
4
+ Summary: An engine for creating stack-oriented DSLs or mini-programming languages.
5
+ License-Expression: GPL-3.0-or-later
6
+ Project-URL: Repository, https://github.com/MrHacer201145/SLE/
7
+ Requires-Python: >=3.8
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Dynamic: license-file
11
+
12
+ # What is "Stack Lang Engine"
13
+ SLE (Stack Lang Engine) is a simple tool for creating
14
+ stack-oriented DSL or even small programming languages.
15
+
16
+ # Usage
17
+ ## Firstly
18
+ Of course we need to import a module, and create object with type
19
+ "StackEngine"
20
+ ```py
21
+ import stack_lang_engine as sle
22
+
23
+ eng = sle.StackEngine()
24
+ ```
25
+
26
+ ## Definition of words:
27
+ This is how to define/delete words in StackEngine
28
+ ```py
29
+ # Define new word
30
+ eng.add_word("print", lambda: print(eng.pop()))
31
+
32
+ # Delete word
33
+ eng.del_word("print")
34
+ ```
35
+
36
+ ## How to execute text (given as string)
37
+ ```py
38
+ # If you use the default regex pattern,
39
+ # it automatically recognizes whether it is a string or a number.
40
+ eng.exec('10 20 30 "40"')
41
+ ```
42
+
43
+ ## How to create stack and APIs for it
44
+ ```py
45
+ # Create new stack, we can use "create_api" option
46
+ # to create eng.{name}_push, eng.{name}_pop() methods
47
+ # for object
48
+
49
+ eng.new_stack("stacky", create_api=True)
50
+ # now we have eng.stacky_push and eng.stacky_pop
51
+
52
+ eng.stacky_push(10)
53
+ print(eng.stacky)
54
+ eng.stacky_pop()
55
+ ```
56
+
57
+ ## Usefull APIs
58
+ ```py
59
+ # Instead of writing "eng.stack.pop" or more we can use builtin
60
+ # methods like eng.(push, pop, clear)
61
+
62
+ eng.push(10) # For example
63
+ ```
64
+
65
+ ## Recognizers
66
+ ```py
67
+ # We can create a new regex pattern,
68
+ # and both the parser and the interpreter must handle it.
69
+
70
+ eng.add_recognizer(
71
+ name="Test", # Name of recognizer
72
+ pattern=r'Test', # Regex pattern
73
+ on_regex=lambda val: val, # What parser should get (or do) when it parses it
74
+ on_interpret=lambda val: print("Hello, Test!?"), # What interpreter should do when it gets it
75
+ )
76
+
77
+ # and if then we execute it will output
78
+ # "Hello, Test!?" when it interprets word
79
+ # with "Test" kind
80
+
81
+ eng.exec("Test 10 20")
82
+ print(eng.stack) # It will work correctly, so in stack only 10 and 20
83
+ ```
84
+
85
+ ## How to write more short code in this engine
86
+ ```py
87
+ # For example we can set words dict instead of creating each
88
+ # word over and over
89
+
90
+ eng = sle.StackEngine(words={
91
+ "print": lambda: print(eng.pop())
92
+ })
93
+
94
+ # You can also read the interpreter's code itself on
95
+ # GitHub to better understand how it works.
96
+ ```
97
+ ### Thanks for reading the documentation!
98
+
99
+ # License
100
+ SLE is licensed under GPLv3 License
@@ -1,25 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: stack_lang_engine
3
- Version: 0.0.2
4
- Summary: Simple engine for building stack-oriented languages
5
- Requires-Python: >=3.8
6
- Description-Content-Type: text/markdown
7
- License-File: LICENSE
8
- Dynamic: license-file
9
-
10
- # About
11
- SLE (Stack Lang Engine) is a simple tool for creating
12
- stack-oriented DSL or even small programming languages
13
-
14
- # Example of usage
15
- ```py
16
- import stack_lang_engine as sle
17
-
18
- eng = sle.StackEngine()
19
-
20
- eng.add_word("print", lambda: print(eng.stack.pop()))
21
-
22
- eng.exec('"test" print') # Will print "test"
23
- ```
24
- # License
25
- SLE is licensed under GPLv3 License
@@ -1,16 +0,0 @@
1
- # About
2
- SLE (Stack Lang Engine) is a simple tool for creating
3
- stack-oriented DSL or even small programming languages
4
-
5
- # Example of usage
6
- ```py
7
- import stack_lang_engine as sle
8
-
9
- eng = sle.StackEngine()
10
-
11
- eng.add_word("print", lambda: print(eng.stack.pop()))
12
-
13
- eng.exec('"test" print') # Will print "test"
14
- ```
15
- # License
16
- SLE is licensed under GPLv3 License
@@ -1,10 +0,0 @@
1
- [build-system]
2
- requires = ["setuptools>=61.0"]
3
- build-backend = "setuptools.build_meta"
4
-
5
- [project]
6
- name = "stack_lang_engine"
7
- version = "0.0.2"
8
- description = "Simple engine for building stack-oriented languages"
9
- readme = "README.md"
10
- requires-python = ">=3.8"
@@ -1,46 +0,0 @@
1
- import re
2
-
3
- class StackEngine:
4
- def __init__(self, words={}, stack=[]):
5
- self.words = words
6
- self.stack = stack
7
-
8
- def add_word(self, name, func):
9
- self.words[name] = func
10
- def del_word(self, name):
11
- del self.words[name]
12
-
13
- def new_stack(self, name, content=[]):
14
- self.__dict__[name] = content
15
-
16
- @staticmethod
17
- def _parse(text):
18
- tokens = []
19
- token_spec = [
20
- ("STR", r'"[^"]*"'),
21
- ("NUM", r'\d+(\.\d+)?'),
22
- ("IDENT", r'[a-zA-Z_][a-zA-Z0-9_]*'),
23
- ("SKIP", r'[ \n\t]')
24
- ]
25
- tok_regex = "|".join(f"(?P<{pair[0]}>{pair[1]})" for pair in token_spec)
26
-
27
- for mo in re.finditer(tok_regex, text):
28
- kind, val = mo.lastgroup, mo.group()
29
-
30
- if kind == "SKIP": continue
31
- elif kind == "STR": val = val[1:-1]
32
- elif kind == "NUM": val = int(val)
33
- tokens.append((kind, val))
34
-
35
- return tokens
36
-
37
- def exec(self, text):
38
- words = self._parse(text)
39
-
40
- for word in words:
41
- kind, val = word
42
- if kind == "IDENT":
43
- if val in self.words:
44
- self.words[val]()
45
- else:
46
- self.stack.append(val)
@@ -1,25 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: stack_lang_engine
3
- Version: 0.0.2
4
- Summary: Simple engine for building stack-oriented languages
5
- Requires-Python: >=3.8
6
- Description-Content-Type: text/markdown
7
- License-File: LICENSE
8
- Dynamic: license-file
9
-
10
- # About
11
- SLE (Stack Lang Engine) is a simple tool for creating
12
- stack-oriented DSL or even small programming languages
13
-
14
- # Example of usage
15
- ```py
16
- import stack_lang_engine as sle
17
-
18
- eng = sle.StackEngine()
19
-
20
- eng.add_word("print", lambda: print(eng.stack.pop()))
21
-
22
- eng.exec('"test" print') # Will print "test"
23
- ```
24
- # License
25
- SLE is licensed under GPLv3 License