IncludeCPP 3.8.0__py3-none-any.whl → 3.8.9__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/core/cssl/CSSL_DOCUMENTATION.md +411 -1
- includecpp/core/cssl/cssl_builtins.py +133 -0
- includecpp/core/cssl/cssl_parser.py +480 -8
- includecpp/core/cssl/cssl_runtime.py +616 -16
- includecpp/core/cssl/cssl_types.py +58 -9
- includecpp/vscode/cssl/package.json +1 -1
- includecpp/vscode/cssl/syntaxes/cssl.tmLanguage.json +250 -3
- {includecpp-3.8.0.dist-info → includecpp-3.8.9.dist-info}/METADATA +1 -1
- {includecpp-3.8.0.dist-info → includecpp-3.8.9.dist-info}/RECORD +14 -14
- {includecpp-3.8.0.dist-info → includecpp-3.8.9.dist-info}/WHEEL +0 -0
- {includecpp-3.8.0.dist-info → includecpp-3.8.9.dist-info}/entry_points.txt +0 -0
- {includecpp-3.8.0.dist-info → includecpp-3.8.9.dist-info}/licenses/LICENSE +0 -0
- {includecpp-3.8.0.dist-info → includecpp-3.8.9.dist-info}/top_level.txt +0 -0
includecpp/__init__.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# CSSL - C-Style Scripting Language
|
|
2
2
|
|
|
3
|
-
> Version 3.
|
|
3
|
+
> Version 3.8.9 | A modern scripting language with C++-style syntax and unique features like CodeInfusion, BruteInjection, and Python Interop.
|
|
4
4
|
|
|
5
5
|
---
|
|
6
6
|
|
|
@@ -1696,6 +1696,412 @@ string result = b.add("Hello ").add("World!").build();
|
|
|
1696
1696
|
printl(result); // prints: "Hello World!"
|
|
1697
1697
|
```
|
|
1698
1698
|
|
|
1699
|
+
### Class Inheritance (extends)
|
|
1700
|
+
|
|
1701
|
+
CSSL supports class inheritance using the `: extends` syntax:
|
|
1702
|
+
|
|
1703
|
+
```cssl
|
|
1704
|
+
// Base class
|
|
1705
|
+
class Animal {
|
|
1706
|
+
string name;
|
|
1707
|
+
|
|
1708
|
+
void Animal(string n) {
|
|
1709
|
+
this->name = n;
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
void speak() {
|
|
1713
|
+
printl("Some sound");
|
|
1714
|
+
}
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
// Derived class - inherits all members and methods from Animal
|
|
1718
|
+
class Dog : extends Animal {
|
|
1719
|
+
void Dog(string n) {
|
|
1720
|
+
this->name = n;
|
|
1721
|
+
}
|
|
1722
|
+
|
|
1723
|
+
// Override parent method
|
|
1724
|
+
void speak() {
|
|
1725
|
+
printl("Woof! I'm " + this->name);
|
|
1726
|
+
}
|
|
1727
|
+
|
|
1728
|
+
// New method only in Dog
|
|
1729
|
+
void fetch() {
|
|
1730
|
+
printl(this->name + " fetches the ball!");
|
|
1731
|
+
}
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1734
|
+
Dog d = new Dog("Buddy");
|
|
1735
|
+
d.speak(); // "Woof! I'm Buddy"
|
|
1736
|
+
d.fetch(); // "Buddy fetches the ball!"
|
|
1737
|
+
```
|
|
1738
|
+
|
|
1739
|
+
### Extending Python Objects
|
|
1740
|
+
|
|
1741
|
+
You can extend Python classes/objects directly:
|
|
1742
|
+
|
|
1743
|
+
```cssl
|
|
1744
|
+
// Extend a Python object using $
|
|
1745
|
+
class ExtendedGame : extends $GameEngine {
|
|
1746
|
+
void newFeature() {
|
|
1747
|
+
printl("New CSSL feature!");
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
// Override a Python method
|
|
1751
|
+
void showUsername() {
|
|
1752
|
+
printl("Custom username display: " + this->username);
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
```
|
|
1756
|
+
|
|
1757
|
+
In Python:
|
|
1758
|
+
```python
|
|
1759
|
+
class GameEngine:
|
|
1760
|
+
def __init__(self):
|
|
1761
|
+
self.username = "Player1"
|
|
1762
|
+
def showUsername(self):
|
|
1763
|
+
print(f"Username: {self.username}")
|
|
1764
|
+
|
|
1765
|
+
cssl = CSSL.CsslLang()
|
|
1766
|
+
cssl.share('GameEngine', GameEngine())
|
|
1767
|
+
cssl.run('''
|
|
1768
|
+
class MyGame : extends $GameEngine {
|
|
1769
|
+
void customMethod() {
|
|
1770
|
+
printl("Extended from Python!");
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
''')
|
|
1774
|
+
```
|
|
1775
|
+
|
|
1776
|
+
### Class Overwrites
|
|
1777
|
+
|
|
1778
|
+
Use `: overwrites` to replace methods in an existing class/object:
|
|
1779
|
+
|
|
1780
|
+
```cssl
|
|
1781
|
+
// Overwrite methods in a Python object
|
|
1782
|
+
class MyOverrides : extends $GameEngine : overwrites $GameEngine {
|
|
1783
|
+
// This method will REPLACE the original showUsername() in $GameEngine
|
|
1784
|
+
void showUsername() {
|
|
1785
|
+
printl("OVERWRITTEN: " + this->username);
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
```
|
|
1789
|
+
|
|
1790
|
+
When you use `: overwrites TargetClass`, all methods defined in your class will replace the corresponding methods in the target. This works with:
|
|
1791
|
+
- Other CSSL classes
|
|
1792
|
+
- Python objects via `$ObjectName`
|
|
1793
|
+
- Shared objects
|
|
1794
|
+
|
|
1795
|
+
### python::csslize() - Import Python Objects
|
|
1796
|
+
|
|
1797
|
+
Convert Python objects to CSSL-compatible wrappers:
|
|
1798
|
+
|
|
1799
|
+
```cssl
|
|
1800
|
+
// Import a Python object
|
|
1801
|
+
py_obj <== python::csslize($MyPythonInstance);
|
|
1802
|
+
|
|
1803
|
+
// Now use it like a CSSL object
|
|
1804
|
+
py_obj.someMethod();
|
|
1805
|
+
printl(py_obj.someProperty);
|
|
1806
|
+
|
|
1807
|
+
// Or use it as a base class
|
|
1808
|
+
class Extended : extends py_obj {
|
|
1809
|
+
void newMethod() { ... }
|
|
1810
|
+
}
|
|
1811
|
+
```
|
|
1812
|
+
|
|
1813
|
+
In Python:
|
|
1814
|
+
```python
|
|
1815
|
+
class MyPythonClass:
|
|
1816
|
+
def __init__(self, name):
|
|
1817
|
+
self.name = name
|
|
1818
|
+
def greet(self):
|
|
1819
|
+
return f"Hello, {self.name}!"
|
|
1820
|
+
|
|
1821
|
+
cssl = CSSL.CsslLang()
|
|
1822
|
+
cssl.share('MyPython', MyPythonClass("World"))
|
|
1823
|
+
result = cssl.run('''
|
|
1824
|
+
py_obj <== python::csslize($MyPython);
|
|
1825
|
+
printl(py_obj.greet()); // "Hello, World!"
|
|
1826
|
+
''')
|
|
1827
|
+
```
|
|
1828
|
+
|
|
1829
|
+
### Function Extends & Overwrites
|
|
1830
|
+
|
|
1831
|
+
Functions can also extend or overwrite other functions:
|
|
1832
|
+
|
|
1833
|
+
```cssl
|
|
1834
|
+
// Base function
|
|
1835
|
+
define baseFunction() {
|
|
1836
|
+
string greeting = "Hello";
|
|
1837
|
+
int count = 42;
|
|
1838
|
+
printl(greeting);
|
|
1839
|
+
}
|
|
1840
|
+
|
|
1841
|
+
// Extend: inherit local variables from baseFunction
|
|
1842
|
+
define extendedFunc : extends baseFunction() {
|
|
1843
|
+
// 'greeting' and 'count' are available here!
|
|
1844
|
+
printl(greeting + " World! Count: " + count);
|
|
1845
|
+
}
|
|
1846
|
+
|
|
1847
|
+
// Overwrite: replace the original function
|
|
1848
|
+
define myReplacement : overwrites baseFunction() {
|
|
1849
|
+
printl("This replaces baseFunction entirely!");
|
|
1850
|
+
}
|
|
1851
|
+
|
|
1852
|
+
// After defining myReplacement, calling baseFunction()
|
|
1853
|
+
// will now execute myReplacement instead
|
|
1854
|
+
baseFunction(); // prints: "This replaces baseFunction entirely!"
|
|
1855
|
+
```
|
|
1856
|
+
|
|
1857
|
+
Overwriting Python functions:
|
|
1858
|
+
```cssl
|
|
1859
|
+
// Replace a shared Python function
|
|
1860
|
+
define myVersion : overwrites $pythonFunc() {
|
|
1861
|
+
printl("CSSL implementation replaces Python!");
|
|
1862
|
+
}
|
|
1863
|
+
```
|
|
1864
|
+
|
|
1865
|
+
### Non-Null Declarations
|
|
1866
|
+
|
|
1867
|
+
Use `*` prefix to declare non-nullable items:
|
|
1868
|
+
|
|
1869
|
+
```cssl
|
|
1870
|
+
// Non-null variable - can never be None
|
|
1871
|
+
vector<dynamic> *myList; // Always initialized, never null
|
|
1872
|
+
|
|
1873
|
+
// Non-null function - must never return None
|
|
1874
|
+
define *alwaysReturns() {
|
|
1875
|
+
return "Always a value"; // Error if returns null
|
|
1876
|
+
}
|
|
1877
|
+
|
|
1878
|
+
// Non-null class - all methods return non-null
|
|
1879
|
+
class *MyClass {
|
|
1880
|
+
string getValue() {
|
|
1881
|
+
return "Value"; // All methods must return non-null
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
// Non-null open parameter - filters out None values
|
|
1886
|
+
define process(open *Params) {
|
|
1887
|
+
// Params will never contain None values
|
|
1888
|
+
foreach item in Params {
|
|
1889
|
+
printl(item); // Guaranteed non-null
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
```
|
|
1893
|
+
|
|
1894
|
+
### Class Constructors with `constr` Keyword
|
|
1895
|
+
|
|
1896
|
+
The `constr` keyword defines explicit constructors inside classes. Multiple constructors can be defined and will be executed in order.
|
|
1897
|
+
|
|
1898
|
+
```cssl
|
|
1899
|
+
// Class with constructor parameters in declaration
|
|
1900
|
+
class Vehicle (string brand, int year) {
|
|
1901
|
+
string make;
|
|
1902
|
+
int modelYear;
|
|
1903
|
+
|
|
1904
|
+
// Constructor using constr keyword
|
|
1905
|
+
constr Initialize() {
|
|
1906
|
+
// brand and year are available from class parameters
|
|
1907
|
+
this->make = brand;
|
|
1908
|
+
this->modelYear = year;
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1911
|
+
// Additional constructor - executed after Initialize
|
|
1912
|
+
constr SetupDefaults() {
|
|
1913
|
+
printl("Vehicle created: " + this->make);
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1916
|
+
|
|
1917
|
+
Vehicle v = new Vehicle("Toyota", 2024);
|
|
1918
|
+
```
|
|
1919
|
+
|
|
1920
|
+
### Constructor Inheritance with `super()`
|
|
1921
|
+
|
|
1922
|
+
Use `super()` to call parent class constructors and methods:
|
|
1923
|
+
|
|
1924
|
+
```cssl
|
|
1925
|
+
class Animal {
|
|
1926
|
+
string name;
|
|
1927
|
+
|
|
1928
|
+
void Animal(string n) {
|
|
1929
|
+
this->name = n;
|
|
1930
|
+
}
|
|
1931
|
+
|
|
1932
|
+
void speak() {
|
|
1933
|
+
printl("Generic sound");
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1936
|
+
|
|
1937
|
+
class Dog : extends Animal {
|
|
1938
|
+
string breed;
|
|
1939
|
+
|
|
1940
|
+
constr DogInit(string n, string b) {
|
|
1941
|
+
super(n); // Call parent constructor with name
|
|
1942
|
+
this->breed = b;
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
void speak() {
|
|
1946
|
+
super::speak(); // Call parent's speak method
|
|
1947
|
+
printl("Woof! I'm " + this->name);
|
|
1948
|
+
}
|
|
1949
|
+
}
|
|
1950
|
+
|
|
1951
|
+
Dog d = new Dog("Buddy", "Labrador");
|
|
1952
|
+
d.speak();
|
|
1953
|
+
// Output:
|
|
1954
|
+
// Generic sound
|
|
1955
|
+
// Woof! I'm Buddy
|
|
1956
|
+
```
|
|
1957
|
+
|
|
1958
|
+
### Constructor Extends with Arguments
|
|
1959
|
+
|
|
1960
|
+
Pass arguments to parent constructors in class declaration:
|
|
1961
|
+
|
|
1962
|
+
```cssl
|
|
1963
|
+
class Parent {
|
|
1964
|
+
int value;
|
|
1965
|
+
|
|
1966
|
+
void Parent(int v) {
|
|
1967
|
+
this->value = v;
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
|
|
1971
|
+
// Pass arguments to parent constructor using extends (arg1, arg2)
|
|
1972
|
+
class Child : extends Parent ("defaultValue") {
|
|
1973
|
+
constr ChildInit() {
|
|
1974
|
+
// Parent constructor already called with "defaultValue"
|
|
1975
|
+
printl("Child value: " + this->value);
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
```
|
|
1979
|
+
|
|
1980
|
+
### Method-Level Extends & Overwrites
|
|
1981
|
+
|
|
1982
|
+
Functions and methods can extend or overwrite specific methods in parent classes using `::` syntax:
|
|
1983
|
+
|
|
1984
|
+
```cssl
|
|
1985
|
+
class Base {
|
|
1986
|
+
constr Init() {
|
|
1987
|
+
printl("Base init");
|
|
1988
|
+
}
|
|
1989
|
+
|
|
1990
|
+
void process() {
|
|
1991
|
+
printl("Base processing");
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
|
|
1995
|
+
class Derived : extends Base {
|
|
1996
|
+
// Extend specific parent constructor
|
|
1997
|
+
constr DerivedInit() :: extends Base::Init {
|
|
1998
|
+
// Base::Init local vars are available here
|
|
1999
|
+
printl("Derived init");
|
|
2000
|
+
}
|
|
2001
|
+
|
|
2002
|
+
// Or define a method that extends parent method
|
|
2003
|
+
define customProcess :: extends Base::process() {
|
|
2004
|
+
// Has access to Base::process local variables
|
|
2005
|
+
printl("Extended processing");
|
|
2006
|
+
}
|
|
2007
|
+
}
|
|
2008
|
+
```
|
|
2009
|
+
|
|
2010
|
+
### Append Mode with `++` Operator
|
|
2011
|
+
|
|
2012
|
+
The `++` append operator allows you to extend constructors and functions by keeping the original code and adding new code. This is useful for extending behavior without completely replacing it.
|
|
2013
|
+
|
|
2014
|
+
#### Basic Function Append
|
|
2015
|
+
|
|
2016
|
+
```cssl
|
|
2017
|
+
// Original function
|
|
2018
|
+
define BaseFunc() {
|
|
2019
|
+
printl("Base functionality");
|
|
2020
|
+
}
|
|
2021
|
+
|
|
2022
|
+
// Append to BaseFunc - keeps original + adds new
|
|
2023
|
+
define ExtendedFunc() &BaseFunc ++ {
|
|
2024
|
+
printl("Extended functionality");
|
|
2025
|
+
}
|
|
2026
|
+
|
|
2027
|
+
ExtendedFunc();
|
|
2028
|
+
// Output:
|
|
2029
|
+
// Base functionality
|
|
2030
|
+
// Extended functionality
|
|
2031
|
+
```
|
|
2032
|
+
|
|
2033
|
+
#### Constructor Append with Class Reference
|
|
2034
|
+
|
|
2035
|
+
```cssl
|
|
2036
|
+
class MyClass {
|
|
2037
|
+
constr MyClassConstructor() {
|
|
2038
|
+
printl("MyClass constructor");
|
|
2039
|
+
this->value = 10;
|
|
2040
|
+
}
|
|
2041
|
+
}
|
|
2042
|
+
|
|
2043
|
+
class BetterClass :: extends MyClass {
|
|
2044
|
+
// Append to specific parent constructor
|
|
2045
|
+
constr BetterConstructor() &MyClass::MyClassConstructor ++ {
|
|
2046
|
+
printl("BetterClass - added code");
|
|
2047
|
+
this->extra = 20;
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
|
|
2051
|
+
$instance = new BetterClass();
|
|
2052
|
+
// Output:
|
|
2053
|
+
// MyClass constructor
|
|
2054
|
+
// BetterClass - added code
|
|
2055
|
+
printl($instance.value); // 10
|
|
2056
|
+
printl($instance.extra); // 20
|
|
2057
|
+
```
|
|
2058
|
+
|
|
2059
|
+
#### Method Append
|
|
2060
|
+
|
|
2061
|
+
```cssl
|
|
2062
|
+
class Parent {
|
|
2063
|
+
define greet() {
|
|
2064
|
+
printl("Hello from Parent");
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
|
|
2068
|
+
class Child :: extends Parent {
|
|
2069
|
+
// Append to parent method
|
|
2070
|
+
define betterGreet() &Parent::greet ++ {
|
|
2071
|
+
printl("And also from Child!");
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
$child = new Child();
|
|
2076
|
+
$child.betterGreet();
|
|
2077
|
+
// Output:
|
|
2078
|
+
// Hello from Parent
|
|
2079
|
+
// And also from Child!
|
|
2080
|
+
```
|
|
2081
|
+
|
|
2082
|
+
#### Dynamic Instance Reference
|
|
2083
|
+
|
|
2084
|
+
You can also reference methods from instances using `&$instanceVar::member`:
|
|
2085
|
+
|
|
2086
|
+
```cssl
|
|
2087
|
+
global $myInstance = new SomeClass();
|
|
2088
|
+
|
|
2089
|
+
class Extended {
|
|
2090
|
+
define extendedMethod() &$myInstance::originalMethod ++ {
|
|
2091
|
+
printl("Added to instance method");
|
|
2092
|
+
}
|
|
2093
|
+
}
|
|
2094
|
+
```
|
|
2095
|
+
|
|
2096
|
+
#### Append Mode Syntax Summary
|
|
2097
|
+
|
|
2098
|
+
| Syntax | Description |
|
|
2099
|
+
|--------|-------------|
|
|
2100
|
+
| `&FunctionName ++` | Append to a function |
|
|
2101
|
+
| `&ClassName::member ++` | Append to a class member (constructor/method) |
|
|
2102
|
+
| `&$instanceVar::member ++` | Append to an instance member |
|
|
2103
|
+
| `&ClassName::constructors ++` | Append to all constructors of a class |
|
|
2104
|
+
|
|
1699
2105
|
---
|
|
1700
2106
|
|
|
1701
2107
|
## Map Container
|
|
@@ -2064,6 +2470,7 @@ print(result) # ["Missing colon after def"]
|
|
|
2064
2470
|
| `-<==` | BruteInjection | Move data |
|
|
2065
2471
|
| `==>` | BruteInjection | Replace data |
|
|
2066
2472
|
| `==>-` | BruteInjection | Remove matching items |
|
|
2473
|
+
| `++` | AppendMode | Append to parent (with `&ref`) |
|
|
2067
2474
|
|
|
2068
2475
|
### Special Syntax
|
|
2069
2476
|
|
|
@@ -2075,6 +2482,9 @@ print(result) # ["Missing colon after def"]
|
|
|
2075
2482
|
| `$name` | Access shared Python object |
|
|
2076
2483
|
| `s@name` | Self-reference to struct |
|
|
2077
2484
|
| `r@name` | Global variable declaration |
|
|
2485
|
+
| `&ClassName::member` | Reference class member for append mode |
|
|
2486
|
+
| `&$instance::member` | Reference instance member for append mode |
|
|
2487
|
+
| `&FunctionName ++` | Append to function (keeps old + adds new) |
|
|
2078
2488
|
|
|
2079
2489
|
---
|
|
2080
2490
|
|
|
@@ -209,6 +209,8 @@ class CSSLBuiltins:
|
|
|
209
209
|
self._functions['python::pythonize'] = self.builtin_python_pythonize
|
|
210
210
|
self._functions['python::wrap'] = self.builtin_python_pythonize # Alias
|
|
211
211
|
self._functions['python::export'] = self.builtin_python_pythonize # Alias
|
|
212
|
+
self._functions['python::csslize'] = self.builtin_python_csslize
|
|
213
|
+
self._functions['python::import'] = self.builtin_python_csslize # Alias
|
|
212
214
|
|
|
213
215
|
# Regex functions
|
|
214
216
|
self._functions['match'] = self.builtin_match
|
|
@@ -2524,6 +2526,137 @@ class CSSLBuiltins:
|
|
|
2524
2526
|
|
|
2525
2527
|
return PythonizedCSSLInstance(cssl_instance, self.runtime)
|
|
2526
2528
|
|
|
2529
|
+
def builtin_python_csslize(self, python_obj: Any) -> Any:
|
|
2530
|
+
"""Convert a Python object (class instance or function) to a CSSL-compatible wrapper.
|
|
2531
|
+
|
|
2532
|
+
Syntax:
|
|
2533
|
+
cssl_obj <== python::csslize($python_instance);
|
|
2534
|
+
cssl_func <== python::csslize($python_function);
|
|
2535
|
+
|
|
2536
|
+
Example:
|
|
2537
|
+
# In Python:
|
|
2538
|
+
class MyPythonClass:
|
|
2539
|
+
def __init__(self, name):
|
|
2540
|
+
self.name = name
|
|
2541
|
+
def greet(self):
|
|
2542
|
+
return f"Hello, {self.name}!"
|
|
2543
|
+
|
|
2544
|
+
cssl = CSSL.CsslLang()
|
|
2545
|
+
cssl.set_variable('MyPython', MyPythonClass("World"))
|
|
2546
|
+
result = cssl.run('''
|
|
2547
|
+
py_obj <== python::csslize($MyPython);
|
|
2548
|
+
printl(py_obj.greet()); // "Hello, World!"
|
|
2549
|
+
''')
|
|
2550
|
+
|
|
2551
|
+
For class inheritance:
|
|
2552
|
+
py_class <== python::csslize($MyPythonClass);
|
|
2553
|
+
class MyExtended : extends py_class {
|
|
2554
|
+
void newMethod() { ... }
|
|
2555
|
+
}
|
|
2556
|
+
|
|
2557
|
+
Args:
|
|
2558
|
+
python_obj: A Python object (class instance, function, or class)
|
|
2559
|
+
|
|
2560
|
+
Returns:
|
|
2561
|
+
CSSLizedPythonObject - A CSSL-compatible wrapper
|
|
2562
|
+
"""
|
|
2563
|
+
if python_obj is None:
|
|
2564
|
+
return None
|
|
2565
|
+
|
|
2566
|
+
# Already csslized
|
|
2567
|
+
if isinstance(python_obj, CSSLizedPythonObject):
|
|
2568
|
+
return python_obj
|
|
2569
|
+
|
|
2570
|
+
# Wrap the Python object
|
|
2571
|
+
return CSSLizedPythonObject(python_obj, self.runtime)
|
|
2572
|
+
|
|
2573
|
+
|
|
2574
|
+
class CSSLizedPythonObject:
|
|
2575
|
+
"""CSSL wrapper for Python objects (classes, instances, functions).
|
|
2576
|
+
|
|
2577
|
+
Allows Python objects to be used within CSSL code, including as base classes.
|
|
2578
|
+
"""
|
|
2579
|
+
|
|
2580
|
+
def __init__(self, python_obj: Any, runtime: Any = None):
|
|
2581
|
+
self._python_obj = python_obj
|
|
2582
|
+
self._runtime = runtime
|
|
2583
|
+
self._is_class = isinstance(python_obj, type)
|
|
2584
|
+
self._is_callable = callable(python_obj) and not self._is_class
|
|
2585
|
+
|
|
2586
|
+
def __repr__(self):
|
|
2587
|
+
if self._is_class:
|
|
2588
|
+
return f"<CSSLizedPythonClass: {self._python_obj.__name__}>"
|
|
2589
|
+
elif self._is_callable:
|
|
2590
|
+
return f"<CSSLizedPythonFunction: {getattr(self._python_obj, '__name__', 'anonymous')}>"
|
|
2591
|
+
else:
|
|
2592
|
+
return f"<CSSLizedPythonInstance: {type(self._python_obj).__name__}>"
|
|
2593
|
+
|
|
2594
|
+
def __call__(self, *args, **kwargs):
|
|
2595
|
+
"""Call the wrapped object (for functions or class instantiation)."""
|
|
2596
|
+
return self._python_obj(*args, **kwargs)
|
|
2597
|
+
|
|
2598
|
+
def __getattr__(self, name: str) -> Any:
|
|
2599
|
+
"""Get attribute from wrapped Python object."""
|
|
2600
|
+
if name.startswith('_'):
|
|
2601
|
+
raise AttributeError(f"Cannot access private attribute '{name}'")
|
|
2602
|
+
|
|
2603
|
+
obj = object.__getattribute__(self, '_python_obj')
|
|
2604
|
+
|
|
2605
|
+
if hasattr(obj, name):
|
|
2606
|
+
attr = getattr(obj, name)
|
|
2607
|
+
# If it's a method, wrap it for CSSL calling
|
|
2608
|
+
if callable(attr):
|
|
2609
|
+
return CSSLizedPythonMethod(attr, self._runtime)
|
|
2610
|
+
return attr
|
|
2611
|
+
|
|
2612
|
+
raise AttributeError(f"Python object has no attribute '{name}'")
|
|
2613
|
+
|
|
2614
|
+
def __setattr__(self, name: str, value: Any):
|
|
2615
|
+
"""Set attribute on wrapped Python object."""
|
|
2616
|
+
if name.startswith('_'):
|
|
2617
|
+
object.__setattr__(self, name, value)
|
|
2618
|
+
else:
|
|
2619
|
+
setattr(self._python_obj, name, value)
|
|
2620
|
+
|
|
2621
|
+
def has_member(self, name: str) -> bool:
|
|
2622
|
+
"""Check if the Python object has a member."""
|
|
2623
|
+
return hasattr(self._python_obj, name)
|
|
2624
|
+
|
|
2625
|
+
def get_member(self, name: str) -> Any:
|
|
2626
|
+
"""Get a member from the Python object."""
|
|
2627
|
+
return getattr(self._python_obj, name, None)
|
|
2628
|
+
|
|
2629
|
+
def set_member(self, name: str, value: Any):
|
|
2630
|
+
"""Set a member on the Python object."""
|
|
2631
|
+
setattr(self._python_obj, name, value)
|
|
2632
|
+
|
|
2633
|
+
def get_method(self, name: str):
|
|
2634
|
+
"""Get a method from the Python object."""
|
|
2635
|
+
if hasattr(self._python_obj, name):
|
|
2636
|
+
method = getattr(self._python_obj, name)
|
|
2637
|
+
if callable(method):
|
|
2638
|
+
return CSSLizedPythonMethod(method, self._runtime)
|
|
2639
|
+
return None
|
|
2640
|
+
|
|
2641
|
+
def get_python_obj(self):
|
|
2642
|
+
"""Get the underlying Python object (for inheritance)."""
|
|
2643
|
+
return self._python_obj
|
|
2644
|
+
|
|
2645
|
+
|
|
2646
|
+
class CSSLizedPythonMethod:
|
|
2647
|
+
"""Wrapper for Python methods to be called from CSSL."""
|
|
2648
|
+
|
|
2649
|
+
def __init__(self, method: Any, runtime: Any = None):
|
|
2650
|
+
self._method = method
|
|
2651
|
+
self._runtime = runtime
|
|
2652
|
+
|
|
2653
|
+
def __call__(self, *args, **kwargs):
|
|
2654
|
+
"""Call the Python method."""
|
|
2655
|
+
return self._method(*args, **kwargs)
|
|
2656
|
+
|
|
2657
|
+
def __repr__(self):
|
|
2658
|
+
return f"<CSSLizedPythonMethod: {getattr(self._method, '__name__', 'anonymous')}>"
|
|
2659
|
+
|
|
2527
2660
|
|
|
2528
2661
|
class PythonizedCSSLInstance:
|
|
2529
2662
|
"""Python wrapper for CSSL class instances.
|