archivesOfToyLang/tshunhue/docs/source/型別.rst

71 lines
1.2 KiB
ReStructuredText
Raw Permalink 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.

=============
型別
=============
簡單型別
=============
數字
-------------
* `int`:整數,初期可用 64 位元帶符號整數處理。
* `dou`:雙精度浮點數
字串
-------------
* `str`:字串
暫不支援 `char`
布林
------------
* `bool`:布林值。#t真值#f假值。
複雜型別
============
自訂型別
------------
可以自訂 product type 或 sum type。為求簡化不支援型別當參數。
.. function :: (Type TYPE-NAME (Type-constronctor x1 x2 ...))
.. function :: (Type TYPE-NAME (U (Type-const1 x1 x2 ...)(Type-const2 x1 x2 ...)...))
.. function :: (Type TYPE-NAME (Type-constronctor))
.. function :: (Type TYPE-NAME (U (Type-const1)(Type-const2)...))
U 是 union 的意思,指 Sum Type。
例如:
::
(Type IntPair (IntPair Int Int))
如果可以模式匹配,就變這樣:
::
(\ ((IntPair x))
(match (x)
((IntPair 7 9) 9)
((IntPair 8 a) a)
((IntPair x y) (+ x y))))
Sum Type 的用法:
(Type OrigColor (U (Red)(Green)(Blue))) ; 三原色
如果可以模式匹配,就變這樣:
::
(orig-color-to-str ((OrigColor x))
(match (x)
((Red) "Red")
((Green) "Green")
((Blue) "Blue")))