mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 04:24:43 +00:00
191 lines
5.6 KiB
Markdown
191 lines
5.6 KiB
Markdown
# 基本
|
||
|
||
[](https://gezf7g7pd5.execute-api.ap-northeast-1.amazonaws.com/default/source_up_to_date?owner=erg-lang&repos=erg&ref=main&path=doc/EN/syntax/00_basic.md&commit_hash=8f874b3251e0f85832a1c0fd80fffd408844e8a1)
|
||
|
||
> __Warning__: 本文檔不完整。它未經校對(樣式、正確鏈接、誤譯等)。此外,Erg 的語法可能在版本 0.* 期間發生破壞性更改,并且文檔可能沒有相應更新。請事先了解這一點
|
||
> 如果您在本文檔中發現任何錯誤,請報告至 [此處的表單](https://forms.gle/HtLYRfYzWCAaeTGb6) 或 [GitHub repo](https://github.com/erg-lang/erg/issues/new?assignees=&labels=bug&template=bug_report.yaml)。我們將不勝感激您的建議
|
||
|
||
本文檔描述 Erg 的基本語法
|
||
如果您已經有使用 Python 等語言的經驗,請參閱 [快速瀏覽](quick_tour.md) 了解概覽
|
||
還有一個單獨的 [標準 API](../API/index.md) 和 [Erg 貢獻者的內部文檔](../dev_guide/README.md)。如果您需要語法或 Erg 本身的詳細說明, 請參閱那些文檔
|
||
|
||
## 你好,世界!
|
||
|
||
首先,讓我們做"Hello World"
|
||
|
||
```python
|
||
print!("Hello, World!")
|
||
```
|
||
|
||
這與 Python 和同一家族中的其他語言幾乎相同。最顯著的Trait是`!`,后面會解釋它的含義
|
||
在 Erg 中,括號 `()` 可以省略,除非在解釋上有一些混淆
|
||
括號的省略與 Ruby 類似,但不能省略可以以多種方式解釋的括號
|
||
|
||
```python
|
||
print! "Hello, World!" # OK
|
||
print! "Hello,", "World!" # OK
|
||
print!() # OK
|
||
print! # OK, 但這并不意味著調用,只是將 `print!` 作為可調用對象
|
||
|
||
print! f x # OK, 解釋為 `print!(f(x))`
|
||
print!(f(x, y)) # OK
|
||
print! f(x, y) # OK
|
||
print! f(x, g y) # OK
|
||
print! f x, y # NG, 可以理解為 `print!(f(x), y)` 或 `print!(f(x, y))` print!
|
||
print!(f x, y) # NG, 可以表示"print!(f(x),y)"或"print!(f(x,y))"
|
||
print! f(x, g y, z) # NG, 可以表示"print!(x,g(y),z)"或"print!(x,g(y,z))"
|
||
```
|
||
|
||
## 腳本
|
||
|
||
Erg 代碼稱為腳本。腳本可以以文件格式 (.er) 保存和執行
|
||
|
||
## REPL/文件執行
|
||
|
||
要啟動 REPL,只需鍵入:
|
||
|
||
```sh
|
||
> erg
|
||
```
|
||
|
||
`>` mark is a prompt, just type `erg`.
|
||
Then the REPL should start.
|
||
|
||
```sh
|
||
> erg
|
||
Starting the REPL server...
|
||
Connecting to the REPL server...
|
||
Erg interpreter 0.2.4 (tags/?:, 2022/08/17 0:55:12.95) on x86_64/windows
|
||
>>>
|
||
```
|
||
|
||
Or you can compile from a file.
|
||
|
||
```sh
|
||
> 'print! "hello, world!"' >> hello.er
|
||
|
||
> erg hello.er
|
||
hello, world!
|
||
```
|
||
|
||
## 注釋
|
||
|
||
`#` 之后的代碼作為注釋被忽略。使用它來解釋代碼的意圖或暫時禁用代碼
|
||
|
||
```python
|
||
# Comment
|
||
# `#` and after are ignored until a new line is inserted
|
||
#[
|
||
Multi-line comment
|
||
Treated as a comment all the way up to the corresponding `]#`
|
||
]#
|
||
```
|
||
|
||
## 文檔注釋
|
||
`'''...'''`是一個文檔注釋。注意,與Python不同,它是在任何類或函數之外定義的。
|
||
|
||
```python
|
||
'''
|
||
PI is a constant that is the ratio of the circumference of a circle to its diameter.
|
||
'''
|
||
PI = 3.141592653589793
|
||
'''
|
||
This function returns twice the given number.
|
||
'''
|
||
twice x = x * 2
|
||
|
||
print! twice.__doc__
|
||
# This function returns twice the given number.
|
||
|
||
'''
|
||
Documentation comments for the entire class
|
||
'''
|
||
C = Class {x = Int}
|
||
'''
|
||
Method documentation comments
|
||
'''
|
||
.method self = ...
|
||
```
|
||
|
||
您可以通過在`'''`之后立即寫入語言代碼來指定文檔的語言。然后,[Erg語言服務器](https://github.com/erg-lang/erg/tree/main/crates/els)將以Markdown格式顯示每種語言版本的文檔(默認語言為英語)。
|
||
參見[這里](https://github.com/erg-lang/erg/blob/main/doc/zh_TW/dev_guide/i18n_messages.md)獲取多語言相關文檔
|
||
|
||
```python
|
||
'''
|
||
Answer to the Ultimate Question of Life, the Universe, and Everything.
|
||
cf. https://www.google.co.jp/search?q=answer+to+life+the+universe+and+everything
|
||
'''
|
||
'''japanese
|
||
生命、宇宙、そして全てについての究極の謎への答え
|
||
參照: https://www.google.co.jp/search?q=answer+to+life+the+universe+and+everything
|
||
'''
|
||
ANSWER = 42
|
||
```
|
||
|
||
## 表達式,分隔符
|
||
|
||
腳本是一系列表達式。表達式是可以計算或評估的東西,在 Erg 中幾乎所有東西都是表達式
|
||
每個表達式由分隔符分隔 - 新行或分號 `;`-
|
||
Erg 腳本基本上是從左到右、從上到下進行評估的
|
||
|
||
```python
|
||
n = 1 # 賦值表達式
|
||
f(1, 2) # 函數調用表達式
|
||
1 + 1 # 運算符調用表達式
|
||
f(1, 2); 1 + 1
|
||
```
|
||
|
||
如下所示,有一種稱為 Instant block 的語法,它將塊中評估的最后一個表達式作為變量的值
|
||
這與沒有參數的函數不同,它不添加 `()`。請注意,即時塊僅在運行中評估一次
|
||
|
||
```python
|
||
i =
|
||
x = 1
|
||
x + 1
|
||
assert i == 2
|
||
```
|
||
|
||
這不能用分號 (`;`) 完成
|
||
|
||
```python,compile_fail
|
||
i = (x = 1; x + 1) # 語法錯誤: 不能在括號中使用 `;`
|
||
```
|
||
|
||
## 縮進
|
||
|
||
Erg 和 Python 一樣,使用縮進來表示塊。有三個運算符(特殊形式)觸發塊的開始: `=`、`->` 和 `=>`(此外,`:` 和 `|` ,雖然不是運算符,但也會產生縮進)。每個的含義將在后面描述
|
||
|
||
```python
|
||
f x, y =
|
||
x + y
|
||
|
||
for! 0..9, i =>
|
||
print!
|
||
|
||
for! 0..9, i =>
|
||
print! i; print! i
|
||
|
||
ans = match x:
|
||
0 -> "zero"
|
||
_: 0..9 -> "1 dight"
|
||
_: 10..99 -> "2 dights"
|
||
_ -> "unknown"
|
||
```
|
||
|
||
如果一行太長,可以使用 `\` 將其斷開
|
||
|
||
```python
|
||
# 這不是表示 `x + y + z` 而是表示 `x; +y; +z`
|
||
X
|
||
+ y
|
||
+ z
|
||
|
||
# 這意味著`x + y + z`
|
||
x \
|
||
+ y \
|
||
+ z
|
||
```
|
||
|
||
<p align='center'>
|
||
上一頁 | <a href='./01_literal.md'>下一頁</a>
|
||
</p>
|