erg/doc/EN/compiler/transpile.md
2022-12-28 13:40:16 +09:00

1.8 KiB

How is Erg code transpiled to Python code?

To be precise, Erg code is transpiled to Python bytecode. However, since Python bytecode can almost be reconstructed into Python code, the equivalent Python code is used as an example here. By the way, the example presented here is a low optimization level. More advanced optimizations eliminate things that don't need to be instantiated.

Record, Record type

It will be transpiled to a namedtuple. For namedtuple, see here. There is a similar function, dataclass, but dataclass has a slight performance drop due to auto-implementation of __eq__ and __hash__.

employee = Employee.new({.name = "John Smith"; .id = 100})
assert employee.name == "John Smith"
employee = NamedTuple(['name', 'id'])('John Smith', 100)
assert employee.name == 'John Smith'

It will also be converted to a simple tuple if it can be further optimized.

Polymorphic Type

WIPs

Instant Scope

If no namespace conflicts occur, it will simply be mangled and expanded. Names such as x::y are used in bytecode and cannot be associated with Python code, but if you force it to be expressed, it will be as follows.

x =
    y = 1
    y+1
x::y = 1
x = x::y + 1

Visibility

It does nothing for public variables as it is Python's default. Private variables are handled by mangling.

x = 1
y =
    x = 2
    assert module::x == 2 # assert(...) returns None
module::x = 1
y::x = 2
assert module::x == 2
y = None

Patch

func b: Bool =
    Invert = Patch Bool
    Invert.
        invert self = not self
    b.invert()
def func(b):
    def Invert::invert(self): return not self
    return Invert::invert(b)