木蘭編程語言基本功能摸索 (一)

早先 @yang leonier 摸索出部分測試代碼. 下面打算對木蘭的各個基本功能進行逐步測試, 儘量用中文命名標識符(內置函數和關鍵字暫不打算漢化).

順序參考標識符中文化後的Python 3 官方入門文檔 😃

順便與 Python 運行結果作對比, 並加以記錄.

運行環境見前文

懶得格式化了, 直接上 md 格式.

數字

> 2 + 2
4
> 50 - 5*6
20
> (50 - 5*6) / 4
5
> 8/5
1

與 Python 不同的是, 它的單槓除法是整除, 而不留小數點. 繼續:

> 17/3
5
> 17 // 3
17
> 17 // 好
17
> 17 % 3
2
> 5 * 3 + 2
17

看來它的雙斜槓是註釋. 還不知道小數除法怎麼做, 待研究.

> 5 ** 2
SyntaxError: File "<STDIN>", line 1:1, unexpected token "INTEGER_LITERAL"
5 ** 2
^

hmm, 乘方不是**, 待研究. 下面是變量賦值:

> 寬 = 20
> 高 = 5 * 9
> 寬 * 高
900

變量不存在時, 與 Python 報錯信息一樣, 但是沒有所在行數顯示:

> 數
NameError: name '數' is not defined

原來是這樣, 只要有小數, 即判定爲小數運算:

> 4 * 3.75 - 1
14.0
> 4/3.0
1.3333333333333333

幾個發現:

  • 不支持 python 中的_(上一個值)
  • 與 python 一樣, 顯示小數返回值時, 會略去最後的 0
> 稅率 = 12.5 / 100
> 價格 = 100.50
> 價格 * 稅率
12.5625
> 價格 + _
NameError: name '_' is not defined
> 價格
100.5
> round(價格, 2)
100.5
> round(100.444, 1)
100.4
>  round(100.444, 2)
100.44

字符串

很高興看到支持中文字符串, 不用魔改了. 在輸出顯示上, 與 Python 不同的是, 略去了首尾的單雙引號. 視覺上倒是比較清爽.

> '數'
數
> 'spam eggs'
spam eggs
> 'doesn\'t'
doesn't
> "doesn't"
doesn't
> '"Yes," they said.'
"Yes," they said.
> "\"Yes,\" they said."
"Yes," they said.
> '"Isn\'t," they said.'
"Isn't," they said.

看來, 字符串的顯示, 與 println 的效果相同(帶換行), 對應 python 中的 print. 而 print 是不帶換行的輸出. 以後的測試打算用 println 替代python 中的 print 進行.

> '"Isn\'t," they said.'
"Isn't," they said.
> print('"Isn\'t," they said.')
"Isn't," they said.> println('"Isn\'t," they said.')
"Isn't," they said.
> s = 'First line.\nSecond line.'
> s
First line.
Second line.
> print(s)
First line.
Second line.> 

不支持在 python 中可以輸出原始字符串(raw)的 r . 取代的是雙反斜槓, 這個蠻接近其他編程語言的. 挺好.

> println('C:\some\name')
C:\some
ame
> println(r'C:\some\name')
SyntaxError: File "<STDIN>", line 1:10, unexpected token "STRING_LITERAL_II"
println(r'C:\some\name')
         ^
> println('C:\some\\name')
C:\some\
ame

不知如何支持多行字符串, 待研究:

> println("""\
>> 用途: 東西 [選項]
SyntaxError: File "<STDIN>", line 1:1, unknown token is found here
println("""\
^
> println("""""")
SyntaxError: File "<STDIN>", line 1:11, unexpected token "STRING_LITERAL"
println("""""")
          ^
> println("第" +
SyntaxError: File "<STDIN>", line 2:1, unexpected token "$end"

^

支持字乘和加:

> 6 * '長' + '消'
長長長長長長消
> 6 * '長長' + '消'
長長長長長長長長長長長長消

不支持這樣拼接:

> '木' '蘭'
SyntaxError: File "<STDIN>", line 1:5, unexpected token "STRING_LITERAL_II"
'木' '蘭'
    ^

也不支持如下拼接:

> '木' '蘭'
SyntaxError: File "<STDIN>", line 1:5, unexpected token "STRING_LITERAL_II"
'木' '蘭'
    ^
> 文本 = ('把多個字符串放在括號中'
SyntaxError: File "<STDIN>", line 2:1, unexpected token "$end"

^
> 前綴 = '木'
> 前綴 '蘭'
SyntaxError: File "<STDIN>", line 1:4, unexpected token "STRING_LITERAL_II"
前綴 '蘭'
   ^

下面和上面的拼接報錯信息不同:

> ('長' * 6) '消'
SyntaxError: File "<STDIN>", line 1:1, unexpected token "("
('長' * 6) '消'
^
> ('長' * 6)
長長長長長長

支持用 + 拼接:

> 前綴 + '蘭'
木蘭

支持除了負索引之外的字符串截取方式:

> 詞 = '木蘭編程語言'
> 詞[0]
木
> 詞[5]
言
> 詞[-1]
TypeError: object of type 'int' has no len()
> 詞[-6]
TypeError: object of type 'int' has no len()
> 詞[0:2]
木蘭
> 詞[2:5]
編程語
> 詞[:2] + 詞[2:]
木蘭編程語言
> 詞[4:]
語言
> 詞[-2:]
TypeError: object of type 'int' has no len()

超出範圍的索引處理相同:

> 詞[42]
IndexError: string index out of range
> 詞[4:42]
語言
> 詞[42:]

字符串修改處理相同:

> 詞[0] = 'J'
TypeError: 'str' object does not support item assignment
> 詞[2:] = 'py'
TypeError: 'str' object does not support item assignment

同樣要用+拼接:

> '花' + 詞[1:]
花蘭編程語言
> 詞[:2] + '問好'
木蘭問好

取長度相同:

> 短語 = '迅雷不及掩耳之勢'
> len(短語)
8

列表

列表賦值相同, 但與字符串類似, 不支持負索引.

> 平方數 = [1, 4, 9, 16, 25]
> 平方數
[1, 4, 9, 16, 25]
> 平方數[0]
1
> 平方數[-1]
TypeError: object of type 'int' has no len()
> 平方數[-3:]
TypeError: object of type 'int' has no len()

下面這些列表操作結果基本一致:

> 平方數[:]
[1, 4, 9, 16, 25]
> 平方數 + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
> 立方數 = [1, 8, 27, 65, 125]
> 立方數[3] = 64
> 立方數
[1, 8, 27, 64, 125]
> 立方數.append(216)
> 立方數.append(7 * 7 * 7)
> 立方數
[1, 8, 27, 64, 125, 216, 343]
> 字母 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
> 字母
[a, b, c, d, e, f, g]
> 字母[2:5] = ['C', 'D', 'E']
> 字母
[a, b, C, D, E, f, g]
> 字母[2:5] = []
> 字母
[a, b, f, g]
> 字母[:] = []
> 字母
[]
> 字母 = ['a', 'b', 'c', 'd']
> len(字母)
4
> 字母 = ['a', 'b', 'c']
> 數 = [1, 2, 3]
> 甲 = [字母, 數]
> 甲
[[a, b, c], [1, 2, 3]]
> 甲[0]
[a, b, c]
> 甲[0][1]
b

可惜沒試出 while 的語法. 看lexer 裏是有的. 待研究.

[待續]

發佈了198 篇原創文章 · 獲贊 53 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章