IncludeCPP 3.3.20__py3-none-any.whl → 3.4.8__py3-none-any.whl

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,409 @@
1
+ """
2
+ CSSL Bridge - Python API for CSSL Language
3
+ Provides CsslLang class for executing CSSL code from Python.
4
+ """
5
+
6
+ import threading
7
+ from pathlib import Path
8
+ from typing import Any, List, Optional, Callable, Dict
9
+
10
+
11
+ class CSSLModule:
12
+ """
13
+ A callable CSSL module that executes code with arguments.
14
+
15
+ Created via CSSL.module() - the code is executed each time the module is called,
16
+ with arguments accessible via parameter.get(index).
17
+ """
18
+
19
+ def __init__(self, cssl_instance: 'CsslLang', code: str):
20
+ self._cssl = cssl_instance
21
+ self._code = code
22
+
23
+ def __call__(self, *args) -> Any:
24
+ """Execute the module code with the given arguments."""
25
+ return self._cssl.exec(self._code, *args)
26
+
27
+ def __repr__(self) -> str:
28
+ return f"<CSSLModule code_len={len(self._code)}>"
29
+
30
+
31
+ class CSSLFunctionModule:
32
+ """
33
+ A CSSL module with accessible functions as methods.
34
+
35
+ Created via CSSL.makemodule() - functions defined in the CSSL code
36
+ become callable attributes on this module.
37
+ """
38
+
39
+ def __init__(self, cssl_instance: 'CsslLang', code: str):
40
+ self._cssl = cssl_instance
41
+ self._code = code
42
+ self._runtime = None
43
+ self._functions: Dict[str, Any] = {}
44
+ self._initialized = False
45
+
46
+ def _ensure_initialized(self):
47
+ """Initialize the module by parsing and registering functions."""
48
+ if self._initialized:
49
+ return
50
+
51
+ from .cssl import CSSLRuntime, parse_cssl_program, ASTNode
52
+
53
+ # Create a dedicated runtime for this module
54
+ self._runtime = CSSLRuntime()
55
+
56
+ # Parse the code
57
+ ast = parse_cssl_program(self._code)
58
+
59
+ # Execute to register all function definitions
60
+ for child in ast.children:
61
+ if child.type == 'function':
62
+ func_info = child.value
63
+ func_name = func_info.get('name')
64
+ self._functions[func_name] = child
65
+ self._runtime.scope.set(func_name, child)
66
+ else:
67
+ # Execute other statements (like struct definitions)
68
+ try:
69
+ self._runtime._execute_node(child)
70
+ except Exception:
71
+ pass
72
+
73
+ self._initialized = True
74
+
75
+ def __getattr__(self, name: str) -> Callable:
76
+ """Get a function from the module."""
77
+ # Avoid recursion for internal attributes
78
+ if name.startswith('_'):
79
+ raise AttributeError(f"'{type(self).__name__}' has no attribute '{name}'")
80
+
81
+ self._ensure_initialized()
82
+
83
+ if name in self._functions:
84
+ func_node = self._functions[name]
85
+
86
+ def wrapper(*args):
87
+ from .cssl import Parameter
88
+ # Set up parameter object for this call
89
+ self._runtime.global_scope.set('parameter', Parameter(list(args)))
90
+ self._runtime.global_scope.set('args', list(args))
91
+ self._runtime.global_scope.set('argc', len(args))
92
+ return self._runtime._call_function(func_node, list(args))
93
+
94
+ return wrapper
95
+
96
+ raise AttributeError(f"CSSL module has no function '{name}'")
97
+
98
+ def __dir__(self) -> List[str]:
99
+ """List available functions."""
100
+ self._ensure_initialized()
101
+ return list(self._functions.keys())
102
+
103
+ def __repr__(self) -> str:
104
+ self._ensure_initialized()
105
+ funcs = ', '.join(self._functions.keys())
106
+ return f"<CSSLFunctionModule functions=[{funcs}]>"
107
+
108
+
109
+ class CsslLang:
110
+ """
111
+ CSSL Language interface for Python.
112
+
113
+ Usage:
114
+ from includecpp import CSSL
115
+ cssl = CSSL.CsslLang()
116
+ result = cssl.exec("script.cssl", arg1, arg2)
117
+ cssl.T_exec("async_script.cssl", arg1) # Threaded
118
+ """
119
+
120
+ def __init__(self, output_callback: Optional[Callable[[str, str], None]] = None):
121
+ """
122
+ Initialize CSSL runtime.
123
+
124
+ Args:
125
+ output_callback: Optional callback for output (text, level)
126
+ """
127
+ self._output_callback = output_callback
128
+ self._runtime = None
129
+ self._threads: List[threading.Thread] = []
130
+
131
+ def _get_runtime(self):
132
+ """Lazy load CSSL runtime."""
133
+ if self._runtime is None:
134
+ from .cssl import CSSLRuntime
135
+ self._runtime = CSSLRuntime(output_callback=self._output_callback)
136
+ return self._runtime
137
+
138
+ def exec(self, path_or_code: str, *args) -> Any:
139
+ """
140
+ Execute CSSL code or file.
141
+
142
+ Args:
143
+ path_or_code: Path to .cssl file or CSSL code string
144
+ *args: Arguments to pass to the script
145
+
146
+ Returns:
147
+ Execution result
148
+ """
149
+ runtime = self._get_runtime()
150
+
151
+ # Check if it's a file path
152
+ path = Path(path_or_code)
153
+ if path.exists() and path.suffix in ('.cssl', '.cssl-mod'):
154
+ source = path.read_text(encoding='utf-8')
155
+ else:
156
+ source = path_or_code
157
+
158
+ # Set arguments in runtime scope
159
+ from .cssl import Parameter
160
+ runtime.global_scope.set('args', list(args))
161
+ runtime.global_scope.set('argc', len(args))
162
+ runtime.global_scope.set('parameter', Parameter(list(args)))
163
+
164
+ # Execute as standalone program
165
+ try:
166
+ result = runtime.execute_program(source)
167
+ return result
168
+ except Exception as e:
169
+ raise RuntimeError(f"CSSL Error: {e}") from e
170
+
171
+ def T_exec(self, path_or_code: str, *args, callback: Optional[Callable[[Any], None]] = None) -> threading.Thread:
172
+ """
173
+ Execute CSSL code asynchronously in a thread.
174
+
175
+ Args:
176
+ path_or_code: Path to .cssl file or CSSL code string
177
+ *args: Arguments to pass to the script
178
+ callback: Optional callback when execution completes
179
+
180
+ Returns:
181
+ Thread object
182
+ """
183
+ def _run():
184
+ try:
185
+ result = self.exec(path_or_code, *args)
186
+ if callback:
187
+ callback(result)
188
+ except Exception as e:
189
+ if callback:
190
+ callback(e)
191
+
192
+ thread = threading.Thread(target=_run, daemon=True)
193
+ thread.start()
194
+ self._threads.append(thread)
195
+ return thread
196
+
197
+ def wait_all(self, timeout: Optional[float] = None):
198
+ """Wait for all async executions to complete."""
199
+ for thread in self._threads:
200
+ thread.join(timeout=timeout)
201
+ self._threads.clear()
202
+
203
+ def get_output(self) -> List[str]:
204
+ """Get output buffer from last execution."""
205
+ runtime = self._get_runtime()
206
+ return list(runtime.output_buffer)
207
+
208
+ def clear_output(self):
209
+ """Clear output buffer."""
210
+ runtime = self._get_runtime()
211
+ runtime.output_buffer.clear()
212
+
213
+ def set_global(self, name: str, value: Any):
214
+ """Set a global variable in CSSL runtime."""
215
+ runtime = self._get_runtime()
216
+ runtime.global_scope.set(name, value)
217
+
218
+ def get_global(self, name: str) -> Any:
219
+ """Get a global variable from CSSL runtime."""
220
+ runtime = self._get_runtime()
221
+ return runtime.global_scope.get(name)
222
+
223
+ def module(self, code: str) -> 'CSSLModule':
224
+ """
225
+ Create a callable CSSL module from code.
226
+
227
+ The module can be called with arguments that are passed to the CSSL code.
228
+
229
+ Usage:
230
+ module = CSSL.module('''
231
+ printl(parameter.get(0));
232
+ ''')
233
+ module("Hello") # Prints "Hello"
234
+
235
+ Args:
236
+ code: CSSL code string
237
+
238
+ Returns:
239
+ CSSLModule - a callable module
240
+ """
241
+ return CSSLModule(self, code)
242
+
243
+ def makemodule(self, code: str) -> 'CSSLFunctionModule':
244
+ """
245
+ Create a CSSL module with accessible functions.
246
+
247
+ Functions defined in the code become methods on the returned module.
248
+
249
+ Usage:
250
+ module = CSSL.makemodule('''
251
+ string greet(string name) {
252
+ return "Hello, " + name + "!";
253
+ }
254
+
255
+ int add(int a, int b) {
256
+ return a + b;
257
+ }
258
+ ''')
259
+ module.greet("World") # Returns "Hello, World!"
260
+ module.add(2, 3) # Returns 5
261
+
262
+ Args:
263
+ code: CSSL code string with function definitions
264
+
265
+ Returns:
266
+ CSSLFunctionModule - module with callable function attributes
267
+ """
268
+ return CSSLFunctionModule(self, code)
269
+
270
+
271
+ # Singleton for convenience
272
+ _default_instance: Optional[CsslLang] = None
273
+
274
+ def get_cssl() -> CsslLang:
275
+ """Get default CSSL instance."""
276
+ global _default_instance
277
+ if _default_instance is None:
278
+ _default_instance = CsslLang()
279
+ return _default_instance
280
+
281
+
282
+ # Module-level convenience functions
283
+ def exec(path_or_code: str, *args) -> Any:
284
+ """
285
+ Execute CSSL code or file.
286
+
287
+ Usage:
288
+ from includecpp import CSSL
289
+ CSSL.exec("script.cssl", arg1, arg2)
290
+ CSSL.exec("printl('Hello World');")
291
+
292
+ Args:
293
+ path_or_code: Path to .cssl file or CSSL code string
294
+ *args: Arguments to pass to the script
295
+
296
+ Returns:
297
+ Execution result
298
+ """
299
+ return get_cssl().exec(path_or_code, *args)
300
+
301
+
302
+ def T_exec(path_or_code: str, *args, callback: Optional[Callable[[Any], None]] = None) -> threading.Thread:
303
+ """
304
+ Execute CSSL code asynchronously in a thread.
305
+
306
+ Usage:
307
+ from includecpp import CSSL
308
+ CSSL.T_exec("async_script.cssl", arg1, callback=on_done)
309
+
310
+ Args:
311
+ path_or_code: Path to .cssl file or CSSL code string
312
+ *args: Arguments to pass to the script
313
+ callback: Optional callback when execution completes
314
+
315
+ Returns:
316
+ Thread object
317
+ """
318
+ return get_cssl().T_exec(path_or_code, *args, callback=callback)
319
+
320
+
321
+ def set_global(name: str, value: Any) -> None:
322
+ """Set a global variable in CSSL runtime."""
323
+ get_cssl().set_global(name, value)
324
+
325
+
326
+ def get_global(name: str) -> Any:
327
+ """Get a global variable from CSSL runtime."""
328
+ return get_cssl().get_global(name)
329
+
330
+
331
+ def get_output() -> List[str]:
332
+ """Get output buffer from last execution."""
333
+ return get_cssl().get_output()
334
+
335
+
336
+ def clear_output() -> None:
337
+ """Clear output buffer."""
338
+ get_cssl().clear_output()
339
+
340
+
341
+ # Aliases to avoid conflict with Python builtin exec
342
+ _exec = exec
343
+ _T_exec = T_exec
344
+
345
+
346
+ def module(code: str) -> CSSLModule:
347
+ """
348
+ Create a callable CSSL module from code.
349
+
350
+ Usage:
351
+ from includecpp import CSSL
352
+ greet = CSSL.module('''
353
+ printl("Hello, " + parameter.get(0) + "!");
354
+ ''')
355
+ greet("World") # Prints "Hello, World!"
356
+
357
+ Args:
358
+ code: CSSL code string
359
+
360
+ Returns:
361
+ CSSLModule - a callable module
362
+ """
363
+ return get_cssl().module(code)
364
+
365
+
366
+ def makemodule(code: str) -> CSSLFunctionModule:
367
+ """
368
+ Create a CSSL module with accessible functions.
369
+
370
+ Usage:
371
+ from includecpp import CSSL
372
+ math_mod = CSSL.makemodule('''
373
+ int add(int a, int b) {
374
+ return a + b;
375
+ }
376
+
377
+ int multiply(int a, int b) {
378
+ return a * b;
379
+ }
380
+ ''')
381
+ math_mod.add(2, 3) # Returns 5
382
+ math_mod.multiply(4, 5) # Returns 20
383
+
384
+ Args:
385
+ code: CSSL code string with function definitions
386
+
387
+ Returns:
388
+ CSSLFunctionModule - module with callable function attributes
389
+ """
390
+ return get_cssl().makemodule(code)
391
+
392
+
393
+ # Export all
394
+ __all__ = [
395
+ 'CsslLang',
396
+ 'CSSLModule',
397
+ 'CSSLFunctionModule',
398
+ 'get_cssl',
399
+ 'exec',
400
+ '_exec', # Alias for exec (avoids Python builtin conflict)
401
+ 'T_exec',
402
+ '_T_exec', # Alias for T_exec
403
+ 'set_global',
404
+ 'get_global',
405
+ 'get_output',
406
+ 'clear_output',
407
+ 'module',
408
+ 'makemodule',
409
+ ]
@@ -0,0 +1,311 @@
1
+ """
2
+ CSSL Bridge - Type Stubs for Python API
3
+ """
4
+
5
+ import threading
6
+ from typing import Any, List, Optional, Callable, Dict
7
+
8
+
9
+ class CSSLModule:
10
+ """
11
+ A callable CSSL module that executes code with arguments.
12
+
13
+ Created via CSSL.module() - the code is executed each time the module is called,
14
+ with arguments accessible via parameter.get(index).
15
+ """
16
+
17
+ def __init__(self, cssl_instance: 'CsslLang', code: str) -> None: ...
18
+ def __call__(self, *args: Any) -> Any:
19
+ """Execute the module code with the given arguments."""
20
+ ...
21
+ def __repr__(self) -> str: ...
22
+
23
+
24
+ class CSSLFunctionModule:
25
+ """
26
+ A CSSL module with accessible functions as methods.
27
+
28
+ Created via CSSL.makemodule() - functions defined in the CSSL code
29
+ become callable attributes on this module.
30
+ """
31
+
32
+ def __init__(self, cssl_instance: 'CsslLang', code: str) -> None: ...
33
+ def __getattr__(self, name: str) -> Callable[..., Any]:
34
+ """Get a function from the module."""
35
+ ...
36
+ def __dir__(self) -> List[str]:
37
+ """List available functions."""
38
+ ...
39
+ def __repr__(self) -> str: ...
40
+
41
+
42
+ class CsslLang:
43
+ """
44
+ CSSL Language interface for Python.
45
+
46
+ Usage:
47
+ from includecpp import CSSL
48
+ cssl = CSSL.CsslLang()
49
+ result = cssl.exec("script.cssl", arg1, arg2)
50
+ cssl.T_exec("async_script.cssl", arg1) # Threaded
51
+ """
52
+
53
+ def __init__(self, output_callback: Optional[Callable[[str, str], None]] = ...) -> None:
54
+ """
55
+ Initialize CSSL runtime.
56
+
57
+ Args:
58
+ output_callback: Optional callback for output (text, level)
59
+ """
60
+ ...
61
+
62
+ def exec(self, path_or_code: str, *args: Any) -> Any:
63
+ """
64
+ Execute CSSL code or file.
65
+
66
+ Args:
67
+ path_or_code: Path to .cssl file or CSSL code string
68
+ *args: Arguments to pass to the script
69
+
70
+ Returns:
71
+ Execution result
72
+ """
73
+ ...
74
+
75
+ def T_exec(
76
+ self,
77
+ path_or_code: str,
78
+ *args: Any,
79
+ callback: Optional[Callable[[Any], None]] = ...
80
+ ) -> threading.Thread:
81
+ """
82
+ Execute CSSL code asynchronously in a thread.
83
+
84
+ Args:
85
+ path_or_code: Path to .cssl file or CSSL code string
86
+ *args: Arguments to pass to the script
87
+ callback: Optional callback when execution completes
88
+
89
+ Returns:
90
+ Thread object
91
+ """
92
+ ...
93
+
94
+ def wait_all(self, timeout: Optional[float] = ...) -> None:
95
+ """Wait for all async executions to complete."""
96
+ ...
97
+
98
+ def get_output(self) -> List[str]:
99
+ """Get output buffer from last execution."""
100
+ ...
101
+
102
+ def clear_output(self) -> None:
103
+ """Clear output buffer."""
104
+ ...
105
+
106
+ def set_global(self, name: str, value: Any) -> None:
107
+ """Set a global variable in CSSL runtime."""
108
+ ...
109
+
110
+ def get_global(self, name: str) -> Any:
111
+ """Get a global variable from CSSL runtime."""
112
+ ...
113
+
114
+ def module(self, code: str) -> CSSLModule:
115
+ """
116
+ Create a callable CSSL module from code.
117
+
118
+ Usage:
119
+ module = cssl.module('''
120
+ printl(parameter.get(0));
121
+ ''')
122
+ module("Hello") # Prints "Hello"
123
+
124
+ Args:
125
+ code: CSSL code string
126
+
127
+ Returns:
128
+ CSSLModule - a callable module
129
+ """
130
+ ...
131
+
132
+ def makemodule(self, code: str) -> CSSLFunctionModule:
133
+ """
134
+ Create a CSSL module with accessible functions.
135
+
136
+ Usage:
137
+ module = cssl.makemodule('''
138
+ string greet(string name) {
139
+ return "Hello, " + name + "!";
140
+ }
141
+ ''')
142
+ module.greet("World") # Returns "Hello, World!"
143
+
144
+ Args:
145
+ code: CSSL code string with function definitions
146
+
147
+ Returns:
148
+ CSSLFunctionModule - module with callable function attributes
149
+ """
150
+ ...
151
+
152
+
153
+ def get_cssl() -> CsslLang:
154
+ """Get default CSSL instance."""
155
+ ...
156
+
157
+
158
+ def exec(path_or_code: str, *args: Any) -> Any:
159
+ """
160
+ Execute CSSL code or file.
161
+
162
+ Usage:
163
+ from includecpp import CSSL
164
+ CSSL.exec("script.cssl", arg1, arg2)
165
+ CSSL.exec("printl('Hello World');")
166
+
167
+ Args:
168
+ path_or_code: Path to .cssl file or CSSL code string
169
+ *args: Arguments to pass to the script
170
+
171
+ Returns:
172
+ Execution result
173
+ """
174
+ ...
175
+
176
+
177
+ def T_exec(
178
+ path_or_code: str,
179
+ *args: Any,
180
+ callback: Optional[Callable[[Any], None]] = ...
181
+ ) -> threading.Thread:
182
+ """
183
+ Execute CSSL code asynchronously in a thread.
184
+
185
+ Usage:
186
+ from includecpp import CSSL
187
+ CSSL.T_exec("async_script.cssl", arg1, callback=on_done)
188
+
189
+ Args:
190
+ path_or_code: Path to .cssl file or CSSL code string
191
+ *args: Arguments to pass to the script
192
+ callback: Optional callback when execution completes
193
+
194
+ Returns:
195
+ Thread object
196
+ """
197
+ ...
198
+
199
+
200
+ def set_global(name: str, value: Any) -> None:
201
+ """Set a global variable in CSSL runtime."""
202
+ ...
203
+
204
+
205
+ def get_global(name: str) -> Any:
206
+ """Get a global variable from CSSL runtime."""
207
+ ...
208
+
209
+
210
+ def get_output() -> List[str]:
211
+ """Get output buffer from last execution."""
212
+ ...
213
+
214
+
215
+ def clear_output() -> None:
216
+ """Clear output buffer."""
217
+ ...
218
+
219
+
220
+ # Aliases to avoid conflict with Python builtin exec
221
+ def _exec(code: str, *args: Any) -> Any:
222
+ """
223
+ Execute CSSL code directly (alias for exec).
224
+
225
+ Supports triple-quoted docstrings for inline CSSL code:
226
+
227
+ Usage:
228
+ from includecpp import CSSL
229
+ CSSL._exec('''
230
+ global base = include("abc.cssl-mod");
231
+
232
+ void TestFunc() {
233
+ printl("hey");
234
+ @base.RechneWas(4, 5);
235
+ }
236
+
237
+ TestFunc();
238
+ ''')
239
+
240
+ Args:
241
+ code: CSSL code string (supports triple-quoted docstrings)
242
+ *args: Arguments to pass to the script
243
+
244
+ Returns:
245
+ Execution result
246
+ """
247
+ ...
248
+
249
+
250
+ def _T_exec(
251
+ code: str,
252
+ *args: Any,
253
+ callback: Optional[Callable[[Any], None]] = ...
254
+ ) -> threading.Thread:
255
+ """
256
+ Execute CSSL code asynchronously in a thread (alias for T_exec).
257
+
258
+ Args:
259
+ code: CSSL code string
260
+ *args: Arguments to pass to the script
261
+ callback: Optional callback when execution completes
262
+
263
+ Returns:
264
+ Thread object
265
+ """
266
+ ...
267
+
268
+
269
+ def module(code: str) -> CSSLModule:
270
+ """
271
+ Create a callable CSSL module from code.
272
+
273
+ Usage:
274
+ from includecpp import CSSL
275
+ greet = CSSL.module('''
276
+ printl("Hello, " + parameter.get(0) + "!");
277
+ ''')
278
+ greet("World") # Prints "Hello, World!"
279
+
280
+ Args:
281
+ code: CSSL code string
282
+
283
+ Returns:
284
+ CSSLModule - a callable module
285
+ """
286
+ ...
287
+
288
+
289
+ def makemodule(code: str) -> CSSLFunctionModule:
290
+ """
291
+ Create a CSSL module with accessible functions.
292
+
293
+ Usage:
294
+ from includecpp import CSSL
295
+ math_mod = CSSL.makemodule('''
296
+ int add(int a, int b) {
297
+ return a + b;
298
+ }
299
+ ''')
300
+ math_mod.add(2, 3) # Returns 5
301
+
302
+ Args:
303
+ code: CSSL code string with function definitions
304
+
305
+ Returns:
306
+ CSSLFunctionModule - module with callable function attributes
307
+ """
308
+ ...
309
+
310
+
311
+ __all__: List[str]