Gidsss / UwUIDE

A compiler made to be cute uwu >~<
6 stars 0 forks source link

feat: default `__str__()` and `__repr__()` for user defined classes #287

Closed am-cid closed 3 months ago

am-cid commented 4 months ago

closes #286


output before

image

output after

image

source

sample text file
-----------------------------------------
1 |
2 |     cwass A() [[ a-chan=10~ ]]
3 |     cwass B(b-chan) [[ c-chan=10~ ]]
4 |     cwass C(d-chan) [[
5 |         e-chan=10~
6 |         f-chan=10~
7 |         fwunc g-san() [[
8 |             pwint()~
9 |         ]]
10 |     ]]
11 |     cwass D(h-chan) [[
12 |         fwunc i-san() [[
13 |             pwint()~
14 |         ]]
15 |     ]]
16 |     fwunc mainuwu-san() [[
17 |         a-A = A()~
18 |         pwint(a)~
19 |         b-B = B(1)~
20 |         pwint(b)~
21 |         c-C = C(2)~
22 |         pwint(c)~
23 |         d-D = D(3)~
24 |         pwint(d)~
25 |     ]]
26 |
-----------------------------------------
end of file

transpiled

def main():
    _a: _A = _A()
    print(_a)
    _b: _B = _B(Int(1))
    print(_b)
    _c: _C = _C(Int(2))
    print(_c)
    _d: _D = _D(Int(3))
    print(_d)

class _A:
    def __init__(self):
        self._a: Int = Int(Float(Int(10)))
    def __repr__(self):
        return f"{self.__class__.__name__}({', '.join(f'{key}={value}' for key, value in self.__dict__.items())})".replace("_", "")
    def __str__(self):
        return f"{self.__class__.__name__}({', '.join(f'{key}={value}' for key, value in self.__dict__.items())})".replace("_", "")
class _B:
    def __init__(self    , _b: Int):
        self._b: Int = Int(_b)
        self._c: Int = Int(Float(Int(10)))
    def __repr__(self):
        return f"{self.__class__.__name__}({', '.join(f'{key}={value}' for key, value in self.__dict__.items())})".replace("_", "")
    def __str__(self):
        return f"{self.__class__.__name__}({', '.join(f'{key}={value}' for key, value in self.__dict__.items())})".replace("_", "")
class _C:
    def __init__(self    , _d: Int):
        self._d: Int = Int(_d)
        self._e: Int = Int(Float(Int(10)))
        self._f: Int = Int(Float(Int(10)))
    def __repr__(self):
        return f"{self.__class__.__name__}({', '.join(f'{key}={value}' for key, value in self.__dict__.items())})".replace("_", "")
    def __str__(self):
        return f"{self.__class__.__name__}({', '.join(f'{key}={value}' for key, value in self.__dict__.items())})".replace("_", "")
    def _g(self):
        print()

class _D:
    def __init__(self    , _h: Int):
        self._h: Int = Int(_h)
    def __repr__(self):
        return f"{self.__class__.__name__}({', '.join(f'{key}={value}' for key, value in self.__dict__.items())})".replace("_", "")
    def __str__(self):
        return f"{self.__class__.__name__}({', '.join(f'{key}={value}' for key, value in self.__dict__.items())})".replace("_", "")
    def _i(self):
        print()

if __name__ == '__main__':
    # clear screen before executing
    import platform
    import os
    os.system('cls' if platform.system() == 'Windows' else 'clear')

    # declare globals
    main()

caveat: if the value of the property is a string with underscore, it will remove that underscore too. dunno the solution rn image image