diff options
Diffstat (limited to 'doc/docs')
| -rwxr-xr-x | doc/docs/doc/README.md | 107 | ||||
| -rwxr-xr-x | doc/docs/zh/doc/README.md | 107 |
2 files changed, 214 insertions, 0 deletions
diff --git a/doc/docs/doc/README.md b/doc/docs/doc/README.md index 7135a26..f0d67a5 100755 --- a/doc/docs/doc/README.md +++ b/doc/docs/doc/README.md | |||
| @@ -444,6 +444,54 @@ print "Valid enum type:", $BodyType Static | |||
| 444 | </pre> | 444 | </pre> |
| 445 | </YueDisplay> | 445 | </YueDisplay> |
| 446 | 446 | ||
| 447 | ### Argument Validation | ||
| 448 | |||
| 449 | You can declare the expected AST node types in the argument list, and check whether the incoming macro arguments meet the expectations at compile time. | ||
| 450 | |||
| 451 | ```moonscript | ||
| 452 | macro printNumAndStr = (num `Num, str `String) -> | | ||
| 453 | print( | ||
| 454 | #{num} | ||
| 455 | #{str} | ||
| 456 | ) | ||
| 457 | |||
| 458 | $printNumAndStr 123, "hello" | ||
| 459 | ``` | ||
| 460 | <YueDisplay> | ||
| 461 | <pre> | ||
| 462 | macro printNumAndStr = (num `Num, str `String) -> | | ||
| 463 | print( | ||
| 464 | #{num} | ||
| 465 | #{str} | ||
| 466 | ) | ||
| 467 | |||
| 468 | $printNumAndStr 123, "hello" | ||
| 469 | </pre> | ||
| 470 | </YueDisplay> | ||
| 471 | |||
| 472 | If you need more flexible argument checking, you can use the built-in `$is_ast` macro function to manually check at the appropriate place. | ||
| 473 | |||
| 474 | ```moonscript | ||
| 475 | macro printNumAndStr = (num, str) -> | ||
| 476 | error "expected Num as first argument" unless $is_ast Num, num | ||
| 477 | error "expected String as second argument" unless $is_ast String, str | ||
| 478 | "print(#{num}, #{str})" | ||
| 479 | |||
| 480 | $printNumAndStr 123, "hello" | ||
| 481 | ``` | ||
| 482 | <YueDisplay> | ||
| 483 | <pre> | ||
| 484 | macro printNumAndStr = (num, str) -> | ||
| 485 | error "expected Num as first argument" unless $is_ast Num, num | ||
| 486 | error "expected String as second argument" unless $is_ast String, str | ||
| 487 | "print(#{num}, #{str})" | ||
| 488 | |||
| 489 | $printNumAndStr 123, "hello" | ||
| 490 | </pre> | ||
| 491 | </YueDisplay> | ||
| 492 | |||
| 493 | For more details about available AST nodes, please refer to the uppercased definitions in [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp). | ||
| 494 | |||
| 447 | ## Operator | 495 | ## Operator |
| 448 | 496 | ||
| 449 | All of Lua's binary and unary operators are available. Additionally **!=** is as an alias for **~=**, and either **\\** or **::** can be used to write a chaining function call like `tb\func!` or `tb::func!`. And Yuescipt offers some other special operators to write more expressive codes. | 497 | All of Lua's binary and unary operators are available. Additionally **!=** is as an alias for **~=**, and either **\\** or **::** can be used to write a chaining function call like `tb\func!` or `tb::func!`. And Yuescipt offers some other special operators to write more expressive codes. |
| @@ -1702,6 +1750,65 @@ binary = 0B10011 | |||
| 1702 | </pre> | 1750 | </pre> |
| 1703 | </YueDisplay> | 1751 | </YueDisplay> |
| 1704 | 1752 | ||
| 1753 | ### YAML Multiline String | ||
| 1754 | |||
| 1755 | The `|` prefix introduces a YAML-style multiline string literal: | ||
| 1756 | |||
| 1757 | ```moonscript | ||
| 1758 | str = | | ||
| 1759 | key: value | ||
| 1760 | list: | ||
| 1761 | - item1 | ||
| 1762 | - #{expr} | ||
| 1763 | ``` | ||
| 1764 | <YueDisplay> | ||
| 1765 | <pre> | ||
| 1766 | str = | | ||
| 1767 | key: value | ||
| 1768 | list: | ||
| 1769 | - item1 | ||
| 1770 | - #{expr} | ||
| 1771 | </pre> | ||
| 1772 | </YueDisplay> | ||
| 1773 | |||
| 1774 | This allows writing structured multiline text conveniently. All line breaks and indentation are preserved relative to the first non-empty line, and expressions inside `#{...}` are interpolated automatically as `tostring(expr)`. | ||
| 1775 | |||
| 1776 | YAML Multiline String automatically detects the common leading whitespace prefix (minimum indentation across all non-empty lines) and removes it from all lines. This makes it easy to indent your code visually without affecting the resulting string content. | ||
| 1777 | |||
| 1778 | ```moonscript | ||
| 1779 | fn = -> | ||
| 1780 | str = | | ||
| 1781 | foo: | ||
| 1782 | bar: baz | ||
| 1783 | return str | ||
| 1784 | ``` | ||
| 1785 | <YueDisplay> | ||
| 1786 | <pre> | ||
| 1787 | fn = -> | ||
| 1788 | str = | | ||
| 1789 | foo: | ||
| 1790 | bar: baz | ||
| 1791 | return str | ||
| 1792 | </pre> | ||
| 1793 | </YueDisplay> | ||
| 1794 | |||
| 1795 | Internal indentation is preserved relative to the removed common prefix, allowing clean nested structures. | ||
| 1796 | |||
| 1797 | All special characters like quotes (`"`) and backslashes (`\`) in the YAMLMultiline block are automatically escaped so that the generated Lua string is syntactically valid and behaves as expected. | ||
| 1798 | |||
| 1799 | ```moonscript | ||
| 1800 | str = | | ||
| 1801 | path: "C:\Program Files\App" | ||
| 1802 | note: 'He said: "#{Hello}!"' | ||
| 1803 | ``` | ||
| 1804 | <YueDisplay> | ||
| 1805 | <pre> | ||
| 1806 | str = | | ||
| 1807 | path: "C:\Program Files\App" | ||
| 1808 | note: 'He said: "#{Hello}!"' | ||
| 1809 | </pre> | ||
| 1810 | </YueDisplay> | ||
| 1811 | |||
| 1705 | ## Function Literals | 1812 | ## Function Literals |
| 1706 | 1813 | ||
| 1707 | All functions are created using a function expression. A simple function is denoted using the arrow: **->**. | 1814 | All functions are created using a function expression. A simple function is denoted using the arrow: **->**. |
diff --git a/doc/docs/zh/doc/README.md b/doc/docs/zh/doc/README.md index 61b746c..15f4768 100755 --- a/doc/docs/zh/doc/README.md +++ b/doc/docs/zh/doc/README.md | |||
| @@ -442,6 +442,54 @@ print "有效的枚举类型:", $BodyType Static | |||
| 442 | </pre> | 442 | </pre> |
| 443 | </YueDisplay> | 443 | </YueDisplay> |
| 444 | 444 | ||
| 445 | ### 宏参数检查 | ||
| 446 | |||
| 447 | 可以直接在参数列表中声明期望的 AST 节点类型,并在编译时检查传入的宏参数是否符合预期。 | ||
| 448 | |||
| 449 | ```moonscript | ||
| 450 | macro printNumAndStr = (num `Num, str `String) -> | | ||
| 451 | print( | ||
| 452 | #{num} | ||
| 453 | #{str} | ||
| 454 | ) | ||
| 455 | |||
| 456 | $printNumAndStr 123, "hello" | ||
| 457 | ``` | ||
| 458 | <YueDisplay> | ||
| 459 | <pre> | ||
| 460 | macro printNumAndStr = (num `Num, str `String) -> | | ||
| 461 | print( | ||
| 462 | #{num} | ||
| 463 | #{str} | ||
| 464 | ) | ||
| 465 | |||
| 466 | $printNumAndStr 123, "hello" | ||
| 467 | </pre> | ||
| 468 | </YueDisplay> | ||
| 469 | |||
| 470 | 如果需要做更加灵活的参数检查操作,可以使用内置的 `$is_ast` 宏函数在合适的位置进行手动检查。 | ||
| 471 | |||
| 472 | ```moonscript | ||
| 473 | macro printNumAndStr = (num, str) -> | ||
| 474 | error "expected Num as first argument" unless $is_ast Num, num | ||
| 475 | error "expected String as second argument" unless $is_ast String, str | ||
| 476 | "print(#{num}, #{str})" | ||
| 477 | |||
| 478 | $printNumAndStr 123, "hello" | ||
| 479 | ``` | ||
| 480 | <YueDisplay> | ||
| 481 | <pre> | ||
| 482 | macro printNumAndStr = (num, str) -> | ||
| 483 | error "expected Num as first argument" unless $is_ast Num, num | ||
| 484 | error "expected String as second argument" unless $is_ast String, str | ||
| 485 | "print(#{num}, #{str})" | ||
| 486 | |||
| 487 | $printNumAndStr 123, "hello" | ||
| 488 | </pre> | ||
| 489 | </YueDisplay> | ||
| 490 | |||
| 491 | 更多关于可用 AST 节点的详细信息,请参考 [yue_parser.cpp](https://github.com/IppClub/YueScript/blob/main/src/yuescript/yue_parser.cpp) 中大写的规则定义。 | ||
| 492 | |||
| 445 | ## 操作符 | 493 | ## 操作符 |
| 446 | 494 | ||
| 447 | Lua的所有二元和一元操作符在月之脚本中都是可用的。此外,**!=** 符号是 **~=** 的别名,而 **\\** 或 **::** 均可用于编写链式函数调用,如写作 `tb\func!` 或 `tb::func!`。此外月之脚本还提供了一些其他特殊的操作符,以编写更具表达力的代码。 | 495 | Lua的所有二元和一元操作符在月之脚本中都是可用的。此外,**!=** 符号是 **~=** 的别名,而 **\\** 或 **::** 均可用于编写链式函数调用,如写作 `tb\func!` 或 `tb::func!`。此外月之脚本还提供了一些其他特殊的操作符,以编写更具表达力的代码。 |
| @@ -1700,6 +1748,65 @@ binary = 0B10011 | |||
| 1700 | </pre> | 1748 | </pre> |
| 1701 | </YueDisplay> | 1749 | </YueDisplay> |
| 1702 | 1750 | ||
| 1751 | ### YAML 风格字符串 | ||
| 1752 | |||
| 1753 | 使用 `|` 前缀标记一个多行 YAML 风格字符串: | ||
| 1754 | |||
| 1755 | ```moonscript | ||
| 1756 | str = | | ||
| 1757 | key: value | ||
| 1758 | list: | ||
| 1759 | - item1 | ||
| 1760 | - #{expr} | ||
| 1761 | ``` | ||
| 1762 | <YueDisplay> | ||
| 1763 | <pre> | ||
| 1764 | str = | | ||
| 1765 | key: value | ||
| 1766 | list: | ||
| 1767 | - item1 | ||
| 1768 | - #{expr} | ||
| 1769 | </pre> | ||
| 1770 | </YueDisplay> | ||
| 1771 | |||
| 1772 | 其效果类似于原生 Lua 的多行拼接,所有文本(含换行)将被保留下来,并支持 `#{...}` 语法,通过 `tostring(expr)` 插入表达式结果。 | ||
| 1773 | |||
| 1774 | YAML 风格的多行字符串会自动检测首行后最小的公共缩进,并从所有行中删除该前缀空白字符。这让你可以在代码中对齐文本,但输出字符串不会带多余缩进。 | ||
| 1775 | |||
| 1776 | ```moonscript | ||
| 1777 | fn = -> | ||
| 1778 | str = | | ||
| 1779 | foo: | ||
| 1780 | bar: baz | ||
| 1781 | return str | ||
| 1782 | ``` | ||
| 1783 | <YueDisplay> | ||
| 1784 | <pre> | ||
| 1785 | fn = -> | ||
| 1786 | str = | | ||
| 1787 | foo: | ||
| 1788 | bar: baz | ||
| 1789 | return str | ||
| 1790 | </pre> | ||
| 1791 | </YueDisplay> | ||
| 1792 | |||
| 1793 | 输出字符串中的 foo: 对齐到行首,不会带有函数缩进空格。保留内部缩进的相对结构,适合书写结构化嵌套样式的内容。 | ||
| 1794 | |||
| 1795 | 支持自动处理字符中的引号、反斜杠等特殊符号,无需手动转义: | ||
| 1796 | |||
| 1797 | ```moonscript | ||
| 1798 | str = | | ||
| 1799 | path: "C:\Program Files\App" | ||
| 1800 | note: 'He said: "#{Hello}!"' | ||
| 1801 | ``` | ||
| 1802 | <YueDisplay> | ||
| 1803 | <pre> | ||
| 1804 | str = | | ||
| 1805 | path: "C:\Program Files\App" | ||
| 1806 | note: 'He said: "#{Hello}!"' | ||
| 1807 | </pre> | ||
| 1808 | </YueDisplay> | ||
| 1809 | |||
| 1703 | ## 函数字面量 | 1810 | ## 函数字面量 |
| 1704 | 1811 | ||
| 1705 | 所有函数都是使用月之脚本的函数表达式创建的。一个简单的函数可以用箭头表示为:**->**。 | 1812 | 所有函数都是使用月之脚本的函数表达式创建的。一个简单的函数可以用箭头表示为:**->**。 |
