erg/doc/zh_CN/compiler/transpile.md

91 lines
2.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Erg 代码如何转译成 Python 代码?
[![badge](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fgezf7g7pd5.execute-api.ap-northeast-1.amazonaws.com%2Fdefault%2Fsource_up_to_date%3Fowner%3Derg-lang%26repos%3Derg%26ref%3Dmain%26path%3Ddoc/EN/compiler/transpile.md%26commit_hash%3D13f2d31aee9012f60b7a40d4b764921f1419cdfe)](https://gezf7g7pd5.execute-api.ap-northeast-1.amazonaws.com/default/source_up_to_date?owner=erg-lang&repos=erg&ref=main&path=doc/EN/compiler/transpile.md&commit_hash=13f2d31aee9012f60b7a40d4b764921f1419cdfe)
准确地说Erg 代码是被转译为 Python 字节码。鉴于 Python 字节码几乎可以被重构为 Python 文本代码,因此这里以等效的 Python 代码为例。
顺便说一下,这里展示的示例是低优化级别。更高级的优化会消除不需要实例化的东西。
## 记录,记录类型
它将被转换为一个命名元组namedtuple
对于 namedtuple请参阅 [此处](https://docs.python.org/zh-cn/3/library/collections.html#collections.namedtuple)。
有一个类似的功能数据类dataclass但由于__eq__和__hash__的自动实现数据类在性能上略有下降。
```python
Employee = Class {.name = Str; .id = Int}
employee = Employee.new({.name = "John Smith"; .id = 100})
assert employee.name == "John Smith"
```
```python
from typing import NamedTuple
class Employee(NamedTuple):
__records__ = ['name', 'id']
name: str
id: int
employee = Employee('John Smith', 100)
assert employee.name == 'John Smith'
```
如果可以进一步优化,它还将被转换为简单的元组。
## 多态类型
> 在制品
## 即时范围
如果没有发生命名空间冲突,它只会被破坏和扩展
`x::y` 等名称在字节码中使用,不能与 Python 代码关联,但如果强制表示,则会如下所示
```python
x =
y = 1
y+1
```
```python
x::y = 1
x = x::y + 1
```
万一发生冲突,定义和使用只能在内部引用的函数
```python
x =
y = 1
y+1
```
```python
def _():
x=1
y = x
return y + 1
x = _()
```
## 可见性
它对公共变量没有任何作用,因为它是 Python 的默认值
私有变量由 mangling 处理
```python
x=1
y =
x = 2
assert module::x == 2
```
```python
module::x = 1
y::x = 2
assert module::x == 2
y = None
```