IncludeCPP 4.0.2__py3-none-any.whl → 4.1.0__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.
- includecpp/__init__.py +1 -1
- includecpp/__init__.pyi +3 -1
- includecpp/cli/commands.py +3 -3
- includecpp/core/ai_integration.py +46 -13
- includecpp/core/cpp_api_extensions.pyi +622 -0
- includecpp/core/cssl/CSSL_DOCUMENTATION.md +186 -5
- includecpp/core/cssl/cssl_builtins.py +87 -2
- includecpp/core/cssl/cssl_languages.py +836 -0
- includecpp/core/cssl/cssl_parser.py +138 -38
- includecpp/core/cssl/cssl_runtime.py +364 -24
- includecpp/core/cssl/cssl_syntax.py +88 -4
- includecpp/core/cssl/cssl_types.py +172 -1
- includecpp/core/cssl_bridge.py +194 -8
- includecpp/core/cssl_bridge.pyi +148 -10
- includecpp/vscode/cssl/package.json +43 -1
- includecpp/vscode/cssl/syntaxes/cssl.tmLanguage.json +140 -17
- {includecpp-4.0.2.dist-info → includecpp-4.1.0.dist-info}/METADATA +101 -1
- {includecpp-4.0.2.dist-info → includecpp-4.1.0.dist-info}/RECORD +22 -20
- {includecpp-4.0.2.dist-info → includecpp-4.1.0.dist-info}/WHEEL +0 -0
- {includecpp-4.0.2.dist-info → includecpp-4.1.0.dist-info}/entry_points.txt +0 -0
- {includecpp-4.0.2.dist-info → includecpp-4.1.0.dist-info}/licenses/LICENSE +0 -0
- {includecpp-4.0.2.dist-info → includecpp-4.1.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,836 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CSSL Multi-Language Support Module
|
|
3
|
+
|
|
4
|
+
Provides language definitions, syntax transformers, and cross-language instance access
|
|
5
|
+
for Python, Java, C#, C++, and JavaScript.
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
@py = libinclude("python")
|
|
9
|
+
cpp = libinclude("c++")
|
|
10
|
+
|
|
11
|
+
define my_func() : supports @py {
|
|
12
|
+
# Python syntax here
|
|
13
|
+
for i in range(10):
|
|
14
|
+
print(i)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class MyClass : extends cpp$BaseClass {
|
|
18
|
+
// C++ style
|
|
19
|
+
}
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from typing import Dict, List, Any, Optional, Callable
|
|
23
|
+
from dataclasses import dataclass, field
|
|
24
|
+
from enum import Enum
|
|
25
|
+
import re
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class SupportedLanguage(Enum):
|
|
29
|
+
"""Enumeration of supported programming languages"""
|
|
30
|
+
PYTHON = "python"
|
|
31
|
+
JAVA = "java"
|
|
32
|
+
CSHARP = "c#"
|
|
33
|
+
CPP = "c++"
|
|
34
|
+
JAVASCRIPT = "javascript"
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass
|
|
38
|
+
class LanguageSyntax:
|
|
39
|
+
"""Defines syntax rules for a programming language"""
|
|
40
|
+
name: str
|
|
41
|
+
statement_terminator: str # ";" or "\n"
|
|
42
|
+
uses_braces: bool # True for {}, False for indentation (Python)
|
|
43
|
+
boolean_true: str # "True", "true"
|
|
44
|
+
boolean_false: str # "False", "false"
|
|
45
|
+
null_keyword: str # "None", "null", "nullptr"
|
|
46
|
+
variable_keywords: List[str] # ["let", "const", "var"] for JS
|
|
47
|
+
function_keywords: List[str] # ["def"] for Python, ["function"] for JS
|
|
48
|
+
class_keywords: List[str] # ["class"]
|
|
49
|
+
constructor_name: str # "__init__" for Python, "constructor" for JS
|
|
50
|
+
print_function: str # "print", "console.log", "System.out.println"
|
|
51
|
+
comment_single: str # "#" or "//"
|
|
52
|
+
comment_multi_start: str # "/*" or '"""'
|
|
53
|
+
comment_multi_end: str # "*/" or '"""'
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass
|
|
57
|
+
class LanguageSupport:
|
|
58
|
+
"""
|
|
59
|
+
Language support object returned by libinclude().
|
|
60
|
+
|
|
61
|
+
Provides syntax transformation and cross-language instance sharing.
|
|
62
|
+
"""
|
|
63
|
+
language: SupportedLanguage
|
|
64
|
+
syntax: LanguageSyntax
|
|
65
|
+
name: str
|
|
66
|
+
_instances: Dict[str, Any] = field(default_factory=dict)
|
|
67
|
+
_transformer: Optional['LanguageTransformer'] = field(default=None, repr=False)
|
|
68
|
+
|
|
69
|
+
def share(self, name: str, instance: Any) -> None:
|
|
70
|
+
"""
|
|
71
|
+
Share an instance for cross-language access.
|
|
72
|
+
|
|
73
|
+
Usage in CSSL:
|
|
74
|
+
cpp.share("Engine", myEngine)
|
|
75
|
+
|
|
76
|
+
Then accessible via:
|
|
77
|
+
cpp$Engine
|
|
78
|
+
"""
|
|
79
|
+
self._instances[name] = instance
|
|
80
|
+
|
|
81
|
+
def get_instance(self, name: str) -> Any:
|
|
82
|
+
"""
|
|
83
|
+
Get a shared instance by name.
|
|
84
|
+
|
|
85
|
+
Usage in CSSL:
|
|
86
|
+
engine = cpp.get("Engine")
|
|
87
|
+
"""
|
|
88
|
+
return self._instances.get(name)
|
|
89
|
+
|
|
90
|
+
def has_instance(self, name: str) -> bool:
|
|
91
|
+
"""Check if an instance is shared"""
|
|
92
|
+
return name in self._instances
|
|
93
|
+
|
|
94
|
+
def list_instances(self) -> List[str]:
|
|
95
|
+
"""List all shared instance names"""
|
|
96
|
+
return list(self._instances.keys())
|
|
97
|
+
|
|
98
|
+
def remove_instance(self, name: str) -> bool:
|
|
99
|
+
"""Remove a shared instance"""
|
|
100
|
+
if name in self._instances:
|
|
101
|
+
del self._instances[name]
|
|
102
|
+
return True
|
|
103
|
+
return False
|
|
104
|
+
|
|
105
|
+
def get_transformer(self) -> 'LanguageTransformer':
|
|
106
|
+
"""Get the syntax transformer for this language"""
|
|
107
|
+
if self._transformer is None:
|
|
108
|
+
self._transformer = create_transformer(self)
|
|
109
|
+
return self._transformer
|
|
110
|
+
|
|
111
|
+
def __getattr__(self, name: str) -> Any:
|
|
112
|
+
"""Allow method-like access for convenience"""
|
|
113
|
+
if name == 'get':
|
|
114
|
+
return self.get_instance
|
|
115
|
+
raise AttributeError(f"'{type(self).__name__}' has no attribute '{name}'")
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
class LanguageTransformer:
|
|
119
|
+
"""
|
|
120
|
+
Base class for transforming language-specific syntax to CSSL.
|
|
121
|
+
|
|
122
|
+
Each language has a specific transformer that handles its unique syntax.
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
def __init__(self, lang_support: LanguageSupport):
|
|
126
|
+
self.lang = lang_support
|
|
127
|
+
self.syntax = lang_support.syntax
|
|
128
|
+
|
|
129
|
+
def transform_source(self, source: str) -> str:
|
|
130
|
+
"""Transform source code from target language to CSSL"""
|
|
131
|
+
raise NotImplementedError("Subclasses must implement transform_source")
|
|
132
|
+
|
|
133
|
+
def _common_replacements(self, stmt: str) -> str:
|
|
134
|
+
"""Apply common replacements across all languages"""
|
|
135
|
+
return stmt
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class PythonTransformer(LanguageTransformer):
|
|
139
|
+
"""
|
|
140
|
+
Transforms Python syntax to CSSL.
|
|
141
|
+
|
|
142
|
+
Handles:
|
|
143
|
+
- Indentation-based blocks -> brace-based blocks
|
|
144
|
+
- def -> define
|
|
145
|
+
- print() -> printl()
|
|
146
|
+
- self. -> this->
|
|
147
|
+
- None -> null
|
|
148
|
+
- Python-style for loops -> CSSL for loops
|
|
149
|
+
"""
|
|
150
|
+
|
|
151
|
+
def transform_source(self, source: str) -> str:
|
|
152
|
+
lines = source.split('\n')
|
|
153
|
+
result = []
|
|
154
|
+
indent_stack = [0] # Stack of indentation levels
|
|
155
|
+
|
|
156
|
+
for i, line in enumerate(lines):
|
|
157
|
+
stripped = line.lstrip()
|
|
158
|
+
|
|
159
|
+
# Handle empty lines and comments
|
|
160
|
+
if not stripped:
|
|
161
|
+
continue
|
|
162
|
+
if stripped.startswith('#'):
|
|
163
|
+
# Convert Python comment to CSSL comment
|
|
164
|
+
result.append('// ' + stripped[1:].lstrip())
|
|
165
|
+
continue
|
|
166
|
+
|
|
167
|
+
current_indent = len(line) - len(stripped)
|
|
168
|
+
|
|
169
|
+
# Handle dedent - close blocks
|
|
170
|
+
while len(indent_stack) > 1 and current_indent < indent_stack[-1]:
|
|
171
|
+
indent_stack.pop()
|
|
172
|
+
result.append(' ' * indent_stack[-1] + '}')
|
|
173
|
+
|
|
174
|
+
# Transform the statement
|
|
175
|
+
transformed = self._transform_statement(stripped)
|
|
176
|
+
|
|
177
|
+
# Check if line opens a new block (ends with :)
|
|
178
|
+
if stripped.rstrip().endswith(':'):
|
|
179
|
+
# Remove trailing colon, add opening brace
|
|
180
|
+
transformed = transformed.rstrip(':').rstrip() + ' {'
|
|
181
|
+
# Get next line's indentation
|
|
182
|
+
next_indent = self._get_next_indent(lines, i)
|
|
183
|
+
if next_indent > current_indent:
|
|
184
|
+
indent_stack.append(next_indent)
|
|
185
|
+
elif not transformed.endswith(('{', '}', ';')):
|
|
186
|
+
# Add semicolon if not a block statement
|
|
187
|
+
transformed += ';'
|
|
188
|
+
|
|
189
|
+
result.append(' ' * current_indent + transformed)
|
|
190
|
+
|
|
191
|
+
# Close remaining open blocks
|
|
192
|
+
while len(indent_stack) > 1:
|
|
193
|
+
indent_stack.pop()
|
|
194
|
+
result.append(' ' * indent_stack[-1] + '}')
|
|
195
|
+
|
|
196
|
+
return '\n'.join(result)
|
|
197
|
+
|
|
198
|
+
def _transform_statement(self, stmt: str) -> str:
|
|
199
|
+
"""Transform a single Python statement to CSSL"""
|
|
200
|
+
|
|
201
|
+
# def func(args): -> define func(args)
|
|
202
|
+
if stmt.startswith('def '):
|
|
203
|
+
match = re.match(r'def\s+(\w+)\s*\((.*?)\)\s*:', stmt)
|
|
204
|
+
if match:
|
|
205
|
+
func_name = match.group(1)
|
|
206
|
+
params = match.group(2)
|
|
207
|
+
return f"define {func_name}({params})"
|
|
208
|
+
|
|
209
|
+
# class ClassName(Parent): -> class ClassName : extends Parent
|
|
210
|
+
if stmt.startswith('class '):
|
|
211
|
+
match = re.match(r'class\s+(\w+)(?:\s*\((.*?)\))?\s*:', stmt)
|
|
212
|
+
if match:
|
|
213
|
+
class_name = match.group(1)
|
|
214
|
+
parent = match.group(2)
|
|
215
|
+
if parent and parent.strip():
|
|
216
|
+
return f"class {class_name} : extends {parent}"
|
|
217
|
+
return f"class {class_name}"
|
|
218
|
+
|
|
219
|
+
# if condition: -> if (condition)
|
|
220
|
+
if stmt.startswith('if '):
|
|
221
|
+
match = re.match(r'if\s+(.+?):', stmt)
|
|
222
|
+
if match:
|
|
223
|
+
condition = match.group(1)
|
|
224
|
+
return f"if ({condition})"
|
|
225
|
+
|
|
226
|
+
# elif condition: -> elif (condition)
|
|
227
|
+
if stmt.startswith('elif '):
|
|
228
|
+
match = re.match(r'elif\s+(.+?):', stmt)
|
|
229
|
+
if match:
|
|
230
|
+
condition = match.group(1)
|
|
231
|
+
return f"elif ({condition})"
|
|
232
|
+
|
|
233
|
+
# else: -> else
|
|
234
|
+
if stmt.strip() == 'else:':
|
|
235
|
+
return 'else'
|
|
236
|
+
|
|
237
|
+
# while condition: -> while (condition)
|
|
238
|
+
if stmt.startswith('while '):
|
|
239
|
+
match = re.match(r'while\s+(.+?):', stmt)
|
|
240
|
+
if match:
|
|
241
|
+
condition = match.group(1)
|
|
242
|
+
return f"while ({condition})"
|
|
243
|
+
|
|
244
|
+
# for i in range(n): -> for (i in range(0, n))
|
|
245
|
+
# for i in iterable: -> for (i in iterable)
|
|
246
|
+
if stmt.startswith('for '):
|
|
247
|
+
match = re.match(r'for\s+(\w+)\s+in\s+(.+?):', stmt)
|
|
248
|
+
if match:
|
|
249
|
+
var = match.group(1)
|
|
250
|
+
iterable = match.group(2)
|
|
251
|
+
# Handle range with single argument
|
|
252
|
+
range_match = re.match(r'range\s*\(\s*(\d+)\s*\)', iterable)
|
|
253
|
+
if range_match:
|
|
254
|
+
return f"for ({var} in range(0, {range_match.group(1)}))"
|
|
255
|
+
return f"for ({var} in {iterable})"
|
|
256
|
+
|
|
257
|
+
# try: -> try
|
|
258
|
+
if stmt.strip() == 'try:':
|
|
259
|
+
return 'try'
|
|
260
|
+
|
|
261
|
+
# except Exception as e: -> catch (e)
|
|
262
|
+
if stmt.startswith('except'):
|
|
263
|
+
match = re.match(r'except\s*(?:\w+\s+)?(?:as\s+(\w+))?\s*:', stmt)
|
|
264
|
+
if match:
|
|
265
|
+
var = match.group(1) or 'e'
|
|
266
|
+
return f"catch ({var})"
|
|
267
|
+
|
|
268
|
+
# finally: -> finally
|
|
269
|
+
if stmt.strip() == 'finally:':
|
|
270
|
+
return 'finally'
|
|
271
|
+
|
|
272
|
+
# return value -> return value
|
|
273
|
+
# (no change needed, just ensure semicolon is added)
|
|
274
|
+
|
|
275
|
+
# Common replacements
|
|
276
|
+
stmt = self._apply_replacements(stmt)
|
|
277
|
+
|
|
278
|
+
return stmt
|
|
279
|
+
|
|
280
|
+
def _apply_replacements(self, stmt: str) -> str:
|
|
281
|
+
"""Apply common Python to CSSL replacements"""
|
|
282
|
+
# print() -> printl()
|
|
283
|
+
stmt = re.sub(r'\bprint\s*\(', 'printl(', stmt)
|
|
284
|
+
|
|
285
|
+
# self. -> this->
|
|
286
|
+
stmt = stmt.replace('self.', 'this->')
|
|
287
|
+
|
|
288
|
+
# None -> null
|
|
289
|
+
stmt = re.sub(r'\bNone\b', 'null', stmt)
|
|
290
|
+
|
|
291
|
+
# True/False stay the same (CSSL supports both cases)
|
|
292
|
+
|
|
293
|
+
# __init__ -> constructor handling would be done at class level
|
|
294
|
+
|
|
295
|
+
return stmt
|
|
296
|
+
|
|
297
|
+
def _get_next_indent(self, lines: List[str], current_idx: int) -> int:
|
|
298
|
+
"""Get indentation of next non-empty, non-comment line"""
|
|
299
|
+
for i in range(current_idx + 1, len(lines)):
|
|
300
|
+
line = lines[i]
|
|
301
|
+
stripped = line.lstrip()
|
|
302
|
+
if stripped and not stripped.startswith('#'):
|
|
303
|
+
return len(line) - len(stripped)
|
|
304
|
+
return 0
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
class JavaScriptTransformer(LanguageTransformer):
|
|
308
|
+
"""
|
|
309
|
+
Transforms JavaScript syntax to CSSL.
|
|
310
|
+
|
|
311
|
+
Handles:
|
|
312
|
+
- let/const/var -> dynamic
|
|
313
|
+
- function name() -> define name()
|
|
314
|
+
- console.log() -> printl()
|
|
315
|
+
- null/undefined -> null
|
|
316
|
+
- Arrow functions (basic support)
|
|
317
|
+
"""
|
|
318
|
+
|
|
319
|
+
def transform_source(self, source: str) -> str:
|
|
320
|
+
lines = source.split('\n')
|
|
321
|
+
result = []
|
|
322
|
+
|
|
323
|
+
for line in lines:
|
|
324
|
+
stripped = line.strip()
|
|
325
|
+
|
|
326
|
+
# Skip empty lines
|
|
327
|
+
if not stripped:
|
|
328
|
+
continue
|
|
329
|
+
|
|
330
|
+
# Convert comments
|
|
331
|
+
if stripped.startswith('//'):
|
|
332
|
+
result.append(stripped)
|
|
333
|
+
continue
|
|
334
|
+
|
|
335
|
+
# Transform the line
|
|
336
|
+
transformed = self._transform_line(stripped)
|
|
337
|
+
result.append(transformed)
|
|
338
|
+
|
|
339
|
+
return '\n'.join(result)
|
|
340
|
+
|
|
341
|
+
def _transform_line(self, line: str) -> str:
|
|
342
|
+
"""Transform a single JavaScript line to CSSL"""
|
|
343
|
+
|
|
344
|
+
# function name(args) { -> define name(args) {
|
|
345
|
+
match = re.match(r'function\s+(\w+)\s*\((.*?)\)\s*\{?', line)
|
|
346
|
+
if match:
|
|
347
|
+
func_name = match.group(1)
|
|
348
|
+
params = match.group(2)
|
|
349
|
+
suffix = ' {' if line.rstrip().endswith('{') else ''
|
|
350
|
+
return f"define {func_name}({params}){suffix}"
|
|
351
|
+
|
|
352
|
+
# const/let/var name = value; -> dynamic name = value;
|
|
353
|
+
match = re.match(r'(const|let|var)\s+(\w+)\s*=\s*(.+)', line)
|
|
354
|
+
if match:
|
|
355
|
+
var_name = match.group(2)
|
|
356
|
+
value = match.group(3)
|
|
357
|
+
return f"dynamic {var_name} = {value}"
|
|
358
|
+
|
|
359
|
+
# const/let/var name; -> dynamic name;
|
|
360
|
+
match = re.match(r'(const|let|var)\s+(\w+)\s*;', line)
|
|
361
|
+
if match:
|
|
362
|
+
var_name = match.group(2)
|
|
363
|
+
return f"dynamic {var_name};"
|
|
364
|
+
|
|
365
|
+
# class Name { or class Name extends Parent {
|
|
366
|
+
match = re.match(r'class\s+(\w+)(?:\s+extends\s+(\w+))?\s*\{?', line)
|
|
367
|
+
if match:
|
|
368
|
+
class_name = match.group(1)
|
|
369
|
+
parent = match.group(2)
|
|
370
|
+
suffix = ' {' if line.rstrip().endswith('{') else ''
|
|
371
|
+
if parent:
|
|
372
|
+
return f"class {class_name} : extends {parent}{suffix}"
|
|
373
|
+
return f"class {class_name}{suffix}"
|
|
374
|
+
|
|
375
|
+
# constructor(args) { -> constr ClassName(args) {
|
|
376
|
+
if line.strip().startswith('constructor'):
|
|
377
|
+
match = re.match(r'constructor\s*\((.*?)\)\s*\{?', line)
|
|
378
|
+
if match:
|
|
379
|
+
params = match.group(1)
|
|
380
|
+
suffix = ' {' if line.rstrip().endswith('{') else ''
|
|
381
|
+
return f"constr __init__({params}){suffix}"
|
|
382
|
+
|
|
383
|
+
# Common replacements
|
|
384
|
+
line = self._apply_replacements(line)
|
|
385
|
+
|
|
386
|
+
return line
|
|
387
|
+
|
|
388
|
+
def _apply_replacements(self, line: str) -> str:
|
|
389
|
+
"""Apply common JavaScript to CSSL replacements"""
|
|
390
|
+
# console.log() -> printl()
|
|
391
|
+
line = re.sub(r'console\.log\s*\(', 'printl(', line)
|
|
392
|
+
|
|
393
|
+
# console.error() -> error()
|
|
394
|
+
line = re.sub(r'console\.error\s*\(', 'error(', line)
|
|
395
|
+
|
|
396
|
+
# console.warn() -> warn()
|
|
397
|
+
line = re.sub(r'console\.warn\s*\(', 'warn(', line)
|
|
398
|
+
|
|
399
|
+
# true/false -> True/False
|
|
400
|
+
line = re.sub(r'\btrue\b', 'True', line)
|
|
401
|
+
line = re.sub(r'\bfalse\b', 'False', line)
|
|
402
|
+
|
|
403
|
+
# undefined -> null
|
|
404
|
+
line = re.sub(r'\bundefined\b', 'null', line)
|
|
405
|
+
|
|
406
|
+
# this. stays as this. (CSSL uses this-> but also supports this.)
|
|
407
|
+
|
|
408
|
+
return line
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
class JavaTransformer(LanguageTransformer):
|
|
412
|
+
"""
|
|
413
|
+
Transforms Java syntax to CSSL.
|
|
414
|
+
|
|
415
|
+
Handles:
|
|
416
|
+
- System.out.println() -> printl()
|
|
417
|
+
- true/false -> True/False
|
|
418
|
+
- String -> string (optional lowercase)
|
|
419
|
+
"""
|
|
420
|
+
|
|
421
|
+
def transform_source(self, source: str) -> str:
|
|
422
|
+
lines = source.split('\n')
|
|
423
|
+
result = []
|
|
424
|
+
|
|
425
|
+
for line in lines:
|
|
426
|
+
stripped = line.strip()
|
|
427
|
+
|
|
428
|
+
if not stripped:
|
|
429
|
+
continue
|
|
430
|
+
|
|
431
|
+
if stripped.startswith('//'):
|
|
432
|
+
result.append(stripped)
|
|
433
|
+
continue
|
|
434
|
+
|
|
435
|
+
transformed = self._transform_line(stripped)
|
|
436
|
+
result.append(transformed)
|
|
437
|
+
|
|
438
|
+
return '\n'.join(result)
|
|
439
|
+
|
|
440
|
+
def _transform_line(self, line: str) -> str:
|
|
441
|
+
"""Transform a single Java line to CSSL"""
|
|
442
|
+
|
|
443
|
+
# public/private/protected static void main(String[] args)
|
|
444
|
+
# -> define main(args)
|
|
445
|
+
match = re.match(r'(?:public|private|protected)?\s*(?:static)?\s*(?:void|int|String|boolean|float|double)\s+(\w+)\s*\((.*?)\)\s*\{?', line)
|
|
446
|
+
if match:
|
|
447
|
+
func_name = match.group(1)
|
|
448
|
+
params = match.group(2)
|
|
449
|
+
# Simplify Java params: String[] args -> args
|
|
450
|
+
params = re.sub(r'\w+(?:\[\])?\s+(\w+)', r'\1', params)
|
|
451
|
+
suffix = ' {' if line.rstrip().endswith('{') else ''
|
|
452
|
+
return f"define {func_name}({params}){suffix}"
|
|
453
|
+
|
|
454
|
+
# class Name extends Parent implements Interface {
|
|
455
|
+
match = re.match(r'(?:public\s+)?class\s+(\w+)(?:\s+extends\s+(\w+))?(?:\s+implements\s+\w+(?:,\s*\w+)*)?\s*\{?', line)
|
|
456
|
+
if match:
|
|
457
|
+
class_name = match.group(1)
|
|
458
|
+
parent = match.group(2)
|
|
459
|
+
suffix = ' {' if line.rstrip().endswith('{') else ''
|
|
460
|
+
if parent:
|
|
461
|
+
return f"class {class_name} : extends {parent}{suffix}"
|
|
462
|
+
return f"class {class_name}{suffix}"
|
|
463
|
+
|
|
464
|
+
# Common replacements
|
|
465
|
+
line = self._apply_replacements(line)
|
|
466
|
+
|
|
467
|
+
return line
|
|
468
|
+
|
|
469
|
+
def _apply_replacements(self, line: str) -> str:
|
|
470
|
+
"""Apply common Java to CSSL replacements"""
|
|
471
|
+
# System.out.println() -> printl()
|
|
472
|
+
line = re.sub(r'System\.out\.println\s*\(', 'printl(', line)
|
|
473
|
+
line = re.sub(r'System\.out\.print\s*\(', 'print(', line)
|
|
474
|
+
|
|
475
|
+
# true/false -> True/False
|
|
476
|
+
line = re.sub(r'\btrue\b', 'True', line)
|
|
477
|
+
line = re.sub(r'\bfalse\b', 'False', line)
|
|
478
|
+
|
|
479
|
+
# String -> string (CSSL convention)
|
|
480
|
+
line = re.sub(r'\bString\b', 'string', line)
|
|
481
|
+
|
|
482
|
+
return line
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
class CSharpTransformer(LanguageTransformer):
|
|
486
|
+
"""
|
|
487
|
+
Transforms C# syntax to CSSL.
|
|
488
|
+
|
|
489
|
+
Handles:
|
|
490
|
+
- Console.WriteLine() -> printl()
|
|
491
|
+
- true/false -> True/False
|
|
492
|
+
- var -> dynamic
|
|
493
|
+
"""
|
|
494
|
+
|
|
495
|
+
def transform_source(self, source: str) -> str:
|
|
496
|
+
lines = source.split('\n')
|
|
497
|
+
result = []
|
|
498
|
+
|
|
499
|
+
for line in lines:
|
|
500
|
+
stripped = line.strip()
|
|
501
|
+
|
|
502
|
+
if not stripped:
|
|
503
|
+
continue
|
|
504
|
+
|
|
505
|
+
if stripped.startswith('//'):
|
|
506
|
+
result.append(stripped)
|
|
507
|
+
continue
|
|
508
|
+
|
|
509
|
+
transformed = self._transform_line(stripped)
|
|
510
|
+
result.append(transformed)
|
|
511
|
+
|
|
512
|
+
return '\n'.join(result)
|
|
513
|
+
|
|
514
|
+
def _transform_line(self, line: str) -> str:
|
|
515
|
+
"""Transform a single C# line to CSSL"""
|
|
516
|
+
|
|
517
|
+
# public/private void MethodName(params) {
|
|
518
|
+
match = re.match(r'(?:public|private|protected|internal)?\s*(?:static)?\s*(?:async)?\s*(?:void|int|string|bool|float|double|var|dynamic|\w+)\s+(\w+)\s*\((.*?)\)\s*\{?', line)
|
|
519
|
+
if match and not line.strip().startswith('class'):
|
|
520
|
+
func_name = match.group(1)
|
|
521
|
+
params = match.group(2)
|
|
522
|
+
# Simplify C# params: string name -> name
|
|
523
|
+
params = re.sub(r'\w+\s+(\w+)', r'\1', params)
|
|
524
|
+
suffix = ' {' if line.rstrip().endswith('{') else ''
|
|
525
|
+
return f"define {func_name}({params}){suffix}"
|
|
526
|
+
|
|
527
|
+
# class Name : Parent {
|
|
528
|
+
match = re.match(r'(?:public\s+)?(?:partial\s+)?class\s+(\w+)(?:\s*:\s*(\w+))?\s*\{?', line)
|
|
529
|
+
if match:
|
|
530
|
+
class_name = match.group(1)
|
|
531
|
+
parent = match.group(2)
|
|
532
|
+
suffix = ' {' if line.rstrip().endswith('{') else ''
|
|
533
|
+
if parent:
|
|
534
|
+
return f"class {class_name} : extends {parent}{suffix}"
|
|
535
|
+
return f"class {class_name}{suffix}"
|
|
536
|
+
|
|
537
|
+
# var name = value; -> dynamic name = value;
|
|
538
|
+
match = re.match(r'var\s+(\w+)\s*=\s*(.+)', line)
|
|
539
|
+
if match:
|
|
540
|
+
var_name = match.group(1)
|
|
541
|
+
value = match.group(2)
|
|
542
|
+
return f"dynamic {var_name} = {value}"
|
|
543
|
+
|
|
544
|
+
# Common replacements
|
|
545
|
+
line = self._apply_replacements(line)
|
|
546
|
+
|
|
547
|
+
return line
|
|
548
|
+
|
|
549
|
+
def _apply_replacements(self, line: str) -> str:
|
|
550
|
+
"""Apply common C# to CSSL replacements"""
|
|
551
|
+
# Console.WriteLine() -> printl()
|
|
552
|
+
line = re.sub(r'Console\.WriteLine\s*\(', 'printl(', line)
|
|
553
|
+
line = re.sub(r'Console\.Write\s*\(', 'print(', line)
|
|
554
|
+
|
|
555
|
+
# true/false -> True/False
|
|
556
|
+
line = re.sub(r'\btrue\b', 'True', line)
|
|
557
|
+
line = re.sub(r'\bfalse\b', 'False', line)
|
|
558
|
+
|
|
559
|
+
return line
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
class CppTransformer(LanguageTransformer):
|
|
563
|
+
"""
|
|
564
|
+
Transforms C++ syntax to CSSL.
|
|
565
|
+
|
|
566
|
+
Handles:
|
|
567
|
+
- std::cout << x << std::endl; -> printl(x);
|
|
568
|
+
- nullptr -> null
|
|
569
|
+
- auto -> dynamic
|
|
570
|
+
- true/false -> True/False
|
|
571
|
+
"""
|
|
572
|
+
|
|
573
|
+
def transform_source(self, source: str) -> str:
|
|
574
|
+
lines = source.split('\n')
|
|
575
|
+
result = []
|
|
576
|
+
|
|
577
|
+
for line in lines:
|
|
578
|
+
stripped = line.strip()
|
|
579
|
+
|
|
580
|
+
if not stripped:
|
|
581
|
+
continue
|
|
582
|
+
|
|
583
|
+
if stripped.startswith('//'):
|
|
584
|
+
result.append(stripped)
|
|
585
|
+
continue
|
|
586
|
+
|
|
587
|
+
transformed = self._transform_line(stripped)
|
|
588
|
+
result.append(transformed)
|
|
589
|
+
|
|
590
|
+
return '\n'.join(result)
|
|
591
|
+
|
|
592
|
+
def _transform_line(self, line: str) -> str:
|
|
593
|
+
"""Transform a single C++ line to CSSL"""
|
|
594
|
+
|
|
595
|
+
# void/int/etc functionName(params) {
|
|
596
|
+
match = re.match(r'(?:virtual\s+)?(?:static\s+)?(?:inline\s+)?(?:void|int|string|bool|float|double|auto|\w+)\s+(\w+)\s*\((.*?)\)\s*(?:const)?\s*(?:override)?\s*\{?', line)
|
|
597
|
+
if match and not any(kw in line for kw in ['class ', 'struct ', 'namespace ']):
|
|
598
|
+
func_name = match.group(1)
|
|
599
|
+
params = match.group(2)
|
|
600
|
+
# Simplify C++ params: const std::string& name -> name
|
|
601
|
+
params = re.sub(r'(?:const\s+)?(?:std::)?(?:\w+)(?:&|\*)?\s+(\w+)', r'\1', params)
|
|
602
|
+
suffix = ' {' if line.rstrip().endswith('{') else ''
|
|
603
|
+
return f"define {func_name}({params}){suffix}"
|
|
604
|
+
|
|
605
|
+
# class Name : public Parent {
|
|
606
|
+
match = re.match(r'class\s+(\w+)(?:\s*:\s*(?:public|protected|private)\s+(\w+))?\s*\{?', line)
|
|
607
|
+
if match:
|
|
608
|
+
class_name = match.group(1)
|
|
609
|
+
parent = match.group(2)
|
|
610
|
+
suffix = ' {' if line.rstrip().endswith('{') else ''
|
|
611
|
+
if parent:
|
|
612
|
+
return f"class {class_name} : extends {parent}{suffix}"
|
|
613
|
+
return f"class {class_name}{suffix}"
|
|
614
|
+
|
|
615
|
+
# auto name = value; -> dynamic name = value;
|
|
616
|
+
match = re.match(r'auto\s+(\w+)\s*=\s*(.+)', line)
|
|
617
|
+
if match:
|
|
618
|
+
var_name = match.group(1)
|
|
619
|
+
value = match.group(2)
|
|
620
|
+
return f"dynamic {var_name} = {value}"
|
|
621
|
+
|
|
622
|
+
# Common replacements
|
|
623
|
+
line = self._apply_replacements(line)
|
|
624
|
+
|
|
625
|
+
return line
|
|
626
|
+
|
|
627
|
+
def _apply_replacements(self, line: str) -> str:
|
|
628
|
+
"""Apply common C++ to CSSL replacements"""
|
|
629
|
+
# std::cout << x << std::endl; -> printl(x);
|
|
630
|
+
match = re.match(r'std::cout\s*<<\s*(.*?)\s*<<\s*std::endl\s*;', line)
|
|
631
|
+
if match:
|
|
632
|
+
content = match.group(1)
|
|
633
|
+
return f'printl({content});'
|
|
634
|
+
|
|
635
|
+
match = re.match(r'std::cout\s*<<\s*(.*?)\s*;', line)
|
|
636
|
+
if match:
|
|
637
|
+
content = match.group(1)
|
|
638
|
+
return f'print({content});'
|
|
639
|
+
|
|
640
|
+
# true/false -> True/False
|
|
641
|
+
line = re.sub(r'\btrue\b', 'True', line)
|
|
642
|
+
line = re.sub(r'\bfalse\b', 'False', line)
|
|
643
|
+
|
|
644
|
+
# nullptr -> null
|
|
645
|
+
line = re.sub(r'\bnullptr\b', 'null', line)
|
|
646
|
+
|
|
647
|
+
# auto -> dynamic (for standalone declarations)
|
|
648
|
+
line = re.sub(r'\bauto\b', 'dynamic', line)
|
|
649
|
+
|
|
650
|
+
return line
|
|
651
|
+
|
|
652
|
+
|
|
653
|
+
# Language Registry
|
|
654
|
+
LANGUAGE_DEFINITIONS: Dict[str, LanguageSupport] = {}
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
def register_language(lang_id: str, lang_support: LanguageSupport) -> None:
|
|
658
|
+
"""Register a language definition"""
|
|
659
|
+
LANGUAGE_DEFINITIONS[lang_id.lower()] = lang_support
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
def get_language(lang_id: str) -> Optional[LanguageSupport]:
|
|
663
|
+
"""Get a language definition by ID"""
|
|
664
|
+
return LANGUAGE_DEFINITIONS.get(lang_id.lower())
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+
def list_languages() -> List[str]:
|
|
668
|
+
"""List all registered language IDs"""
|
|
669
|
+
return list(LANGUAGE_DEFINITIONS.keys())
|
|
670
|
+
|
|
671
|
+
|
|
672
|
+
def create_transformer(lang_support: LanguageSupport) -> LanguageTransformer:
|
|
673
|
+
"""Create the appropriate transformer for a language"""
|
|
674
|
+
if lang_support.language == SupportedLanguage.PYTHON:
|
|
675
|
+
return PythonTransformer(lang_support)
|
|
676
|
+
elif lang_support.language == SupportedLanguage.JAVASCRIPT:
|
|
677
|
+
return JavaScriptTransformer(lang_support)
|
|
678
|
+
elif lang_support.language == SupportedLanguage.JAVA:
|
|
679
|
+
return JavaTransformer(lang_support)
|
|
680
|
+
elif lang_support.language == SupportedLanguage.CSHARP:
|
|
681
|
+
return CSharpTransformer(lang_support)
|
|
682
|
+
elif lang_support.language == SupportedLanguage.CPP:
|
|
683
|
+
return CppTransformer(lang_support)
|
|
684
|
+
else:
|
|
685
|
+
return LanguageTransformer(lang_support)
|
|
686
|
+
|
|
687
|
+
|
|
688
|
+
def _init_languages() -> None:
|
|
689
|
+
"""Initialize all built-in language definitions"""
|
|
690
|
+
|
|
691
|
+
# Python
|
|
692
|
+
python_syntax = LanguageSyntax(
|
|
693
|
+
name="Python",
|
|
694
|
+
statement_terminator="\n",
|
|
695
|
+
uses_braces=False,
|
|
696
|
+
boolean_true="True",
|
|
697
|
+
boolean_false="False",
|
|
698
|
+
null_keyword="None",
|
|
699
|
+
variable_keywords=[],
|
|
700
|
+
function_keywords=["def"],
|
|
701
|
+
class_keywords=["class"],
|
|
702
|
+
constructor_name="__init__",
|
|
703
|
+
print_function="print",
|
|
704
|
+
comment_single="#",
|
|
705
|
+
comment_multi_start='"""',
|
|
706
|
+
comment_multi_end='"""'
|
|
707
|
+
)
|
|
708
|
+
python_support = LanguageSupport(
|
|
709
|
+
language=SupportedLanguage.PYTHON,
|
|
710
|
+
syntax=python_syntax,
|
|
711
|
+
name="Python"
|
|
712
|
+
)
|
|
713
|
+
register_language("python", python_support)
|
|
714
|
+
register_language("py", python_support)
|
|
715
|
+
|
|
716
|
+
# Java
|
|
717
|
+
java_syntax = LanguageSyntax(
|
|
718
|
+
name="Java",
|
|
719
|
+
statement_terminator=";",
|
|
720
|
+
uses_braces=True,
|
|
721
|
+
boolean_true="true",
|
|
722
|
+
boolean_false="false",
|
|
723
|
+
null_keyword="null",
|
|
724
|
+
variable_keywords=["int", "String", "boolean", "float", "double", "var"],
|
|
725
|
+
function_keywords=["public", "private", "protected", "static", "void"],
|
|
726
|
+
class_keywords=["class", "interface", "enum"],
|
|
727
|
+
constructor_name="<classname>",
|
|
728
|
+
print_function="System.out.println",
|
|
729
|
+
comment_single="//",
|
|
730
|
+
comment_multi_start="/*",
|
|
731
|
+
comment_multi_end="*/"
|
|
732
|
+
)
|
|
733
|
+
java_support = LanguageSupport(
|
|
734
|
+
language=SupportedLanguage.JAVA,
|
|
735
|
+
syntax=java_syntax,
|
|
736
|
+
name="Java"
|
|
737
|
+
)
|
|
738
|
+
register_language("java", java_support)
|
|
739
|
+
|
|
740
|
+
# C#
|
|
741
|
+
csharp_syntax = LanguageSyntax(
|
|
742
|
+
name="C#",
|
|
743
|
+
statement_terminator=";",
|
|
744
|
+
uses_braces=True,
|
|
745
|
+
boolean_true="true",
|
|
746
|
+
boolean_false="false",
|
|
747
|
+
null_keyword="null",
|
|
748
|
+
variable_keywords=["int", "string", "bool", "float", "double", "var", "dynamic"],
|
|
749
|
+
function_keywords=["public", "private", "protected", "static", "void", "async"],
|
|
750
|
+
class_keywords=["class", "interface", "struct", "enum"],
|
|
751
|
+
constructor_name="<classname>",
|
|
752
|
+
print_function="Console.WriteLine",
|
|
753
|
+
comment_single="//",
|
|
754
|
+
comment_multi_start="/*",
|
|
755
|
+
comment_multi_end="*/"
|
|
756
|
+
)
|
|
757
|
+
csharp_support = LanguageSupport(
|
|
758
|
+
language=SupportedLanguage.CSHARP,
|
|
759
|
+
syntax=csharp_syntax,
|
|
760
|
+
name="C#"
|
|
761
|
+
)
|
|
762
|
+
register_language("c#", csharp_support)
|
|
763
|
+
register_language("csharp", csharp_support)
|
|
764
|
+
|
|
765
|
+
# C++
|
|
766
|
+
cpp_syntax = LanguageSyntax(
|
|
767
|
+
name="C++",
|
|
768
|
+
statement_terminator=";",
|
|
769
|
+
uses_braces=True,
|
|
770
|
+
boolean_true="true",
|
|
771
|
+
boolean_false="false",
|
|
772
|
+
null_keyword="nullptr",
|
|
773
|
+
variable_keywords=["int", "string", "bool", "float", "double", "auto", "const"],
|
|
774
|
+
function_keywords=["void", "int", "string", "bool", "float", "double", "auto"],
|
|
775
|
+
class_keywords=["class", "struct"],
|
|
776
|
+
constructor_name="<classname>",
|
|
777
|
+
print_function="std::cout",
|
|
778
|
+
comment_single="//",
|
|
779
|
+
comment_multi_start="/*",
|
|
780
|
+
comment_multi_end="*/"
|
|
781
|
+
)
|
|
782
|
+
cpp_support = LanguageSupport(
|
|
783
|
+
language=SupportedLanguage.CPP,
|
|
784
|
+
syntax=cpp_syntax,
|
|
785
|
+
name="C++"
|
|
786
|
+
)
|
|
787
|
+
register_language("c++", cpp_support)
|
|
788
|
+
register_language("cpp", cpp_support)
|
|
789
|
+
|
|
790
|
+
# JavaScript
|
|
791
|
+
js_syntax = LanguageSyntax(
|
|
792
|
+
name="JavaScript",
|
|
793
|
+
statement_terminator=";",
|
|
794
|
+
uses_braces=True,
|
|
795
|
+
boolean_true="true",
|
|
796
|
+
boolean_false="false",
|
|
797
|
+
null_keyword="null",
|
|
798
|
+
variable_keywords=["let", "const", "var"],
|
|
799
|
+
function_keywords=["function", "async"],
|
|
800
|
+
class_keywords=["class"],
|
|
801
|
+
constructor_name="constructor",
|
|
802
|
+
print_function="console.log",
|
|
803
|
+
comment_single="//",
|
|
804
|
+
comment_multi_start="/*",
|
|
805
|
+
comment_multi_end="*/"
|
|
806
|
+
)
|
|
807
|
+
js_support = LanguageSupport(
|
|
808
|
+
language=SupportedLanguage.JAVASCRIPT,
|
|
809
|
+
syntax=js_syntax,
|
|
810
|
+
name="JavaScript"
|
|
811
|
+
)
|
|
812
|
+
register_language("javascript", js_support)
|
|
813
|
+
register_language("js", js_support)
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
# Initialize languages on module load
|
|
817
|
+
_init_languages()
|
|
818
|
+
|
|
819
|
+
|
|
820
|
+
# Export public API
|
|
821
|
+
__all__ = [
|
|
822
|
+
'SupportedLanguage',
|
|
823
|
+
'LanguageSyntax',
|
|
824
|
+
'LanguageSupport',
|
|
825
|
+
'LanguageTransformer',
|
|
826
|
+
'PythonTransformer',
|
|
827
|
+
'JavaScriptTransformer',
|
|
828
|
+
'JavaTransformer',
|
|
829
|
+
'CSharpTransformer',
|
|
830
|
+
'CppTransformer',
|
|
831
|
+
'register_language',
|
|
832
|
+
'get_language',
|
|
833
|
+
'list_languages',
|
|
834
|
+
'create_transformer',
|
|
835
|
+
'LANGUAGE_DEFINITIONS',
|
|
836
|
+
]
|