From 8a5e2639d349402ad66d838ed45a124b53c35b7e Mon Sep 17 00:00:00 2001 From: Dania Rifki Date: Sat, 7 Feb 2026 18:40:56 +0800 Subject: Format docs with prettier Also adjust custom.css a little --- doc/docs/zh/doc/advanced/do.md | 3 + doc/docs/zh/doc/advanced/line-decorators.md | 3 + doc/docs/zh/doc/advanced/macro.md | 7 + doc/docs/zh/doc/advanced/module.md | 8 + doc/docs/zh/doc/advanced/try.md | 2 + doc/docs/zh/doc/assignment/assignment.md | 5 + .../zh/doc/assignment/destructuring-assignment.md | 12 +- doc/docs/zh/doc/assignment/if-assignment.md | 4 + ...ng-clause-controlling-destructive-assignment.md | 3 + doc/docs/zh/doc/assignment/varargs-assignment.md | 1 + doc/docs/zh/doc/control-flow/conditionals.md | 8 +- doc/docs/zh/doc/control-flow/continue.md | 2 + doc/docs/zh/doc/control-flow/for-loop.md | 6 + doc/docs/zh/doc/control-flow/switch.md | 11 + doc/docs/zh/doc/control-flow/while-loop.md | 3 + doc/docs/zh/doc/data-structures/comprehensions.md | 17 +- doc/docs/zh/doc/data-structures/table-literals.md | 9 + doc/docs/zh/doc/functions/backcalls.md | 4 + doc/docs/zh/doc/functions/function-literals.md | 23 ++ doc/docs/zh/doc/functions/function-stubs.md | 1 + doc/docs/zh/doc/getting-started/introduction.md | 2 + doc/docs/zh/doc/getting-started/usage.md | 88 +++--- doc/docs/zh/doc/language-basics/attributes.md | 3 + doc/docs/zh/doc/language-basics/comment.md | 1 + doc/docs/zh/doc/language-basics/literals.md | 6 +- doc/docs/zh/doc/language-basics/operator.md | 15 +- doc/docs/zh/doc/language-basics/whitespace.md | 2 + .../zh/doc/objects/object-oriented-programming.md | 27 +- doc/docs/zh/doc/objects/with-statement.md | 7 +- doc/docs/zh/doc/reference/license-mit.md | 2 +- doc/docs/zh/doc/reference/the-yuescript-library.md | 303 ++++++++++++--------- doc/docs/zh/try/index.md | 1 + 32 files changed, 404 insertions(+), 185 deletions(-) (limited to 'doc/docs/zh') diff --git a/doc/docs/zh/doc/advanced/do.md b/doc/docs/zh/doc/advanced/do.md index 52dca26..a2d5d19 100644 --- a/doc/docs/zh/doc/advanced/do.md +++ b/doc/docs/zh/doc/advanced/do.md @@ -8,6 +8,7 @@ do print var print var -- 这里是nil ``` + ```yue @@ -31,6 +32,7 @@ counter = do print counter! print counter! ``` + ```yue @@ -53,6 +55,7 @@ tbl = { 1234 } ``` + ```yue diff --git a/doc/docs/zh/doc/advanced/line-decorators.md b/doc/docs/zh/doc/advanced/line-decorators.md index 86a1bd9..853e396 100644 --- a/doc/docs/zh/doc/advanced/line-decorators.md +++ b/doc/docs/zh/doc/advanced/line-decorators.md @@ -5,6 +5,7 @@ ```yuescript print "你好,世界" if name == "Rob" ``` + ```yue @@ -18,6 +19,7 @@ print "你好,世界" if name == "Rob" ```yuescript print "项目: ", item for item in *items ``` + ```yue @@ -33,6 +35,7 @@ game\update! while game\isRunning! reader\parse_line! until reader\eof! ``` + ```yue diff --git a/doc/docs/zh/doc/advanced/macro.md b/doc/docs/zh/doc/advanced/macro.md index 91be42c..64cfa6d 100644 --- a/doc/docs/zh/doc/advanced/macro.md +++ b/doc/docs/zh/doc/advanced/macro.md @@ -32,6 +32,7 @@ macro and = (...) -> "#{ table.concat {...}, ' and ' }" if $and f1!, f2!, f3! print "OK" ``` + ```yue @@ -94,6 +95,7 @@ if cond then end ]==] ``` + ```yue @@ -142,6 +144,7 @@ import "utils" as { } [1, 2, 3] |> $map(_ * 2) |> $filter(_ > 4) |> $each print _ ``` + ```yue @@ -172,6 +175,7 @@ import "utils" as { print $FILE -- 获取当前模块名称的字符串 print $LINE -- 获取当前代码行数:2 ``` + ```yue @@ -202,6 +206,7 @@ macro BodyType = $Enum( print "有效的枚举类型:", $BodyType Static -- print "编译报错的枚举类型:", $BodyType Unknown ``` + ```yue @@ -237,6 +242,7 @@ macro printNumAndStr = (num `Num, str `String) -> | $printNumAndStr 123, "hello" ``` + ```yue @@ -261,6 +267,7 @@ macro printNumAndStr = (num, str) -> $printNumAndStr 123, "hello" ``` + ```yue diff --git a/doc/docs/zh/doc/advanced/module.md b/doc/docs/zh/doc/advanced/module.md index bae6618..6c90f0e 100644 --- a/doc/docs/zh/doc/advanced/module.md +++ b/doc/docs/zh/doc/advanced/module.md @@ -28,6 +28,7 @@ do import "lpeg" as :C, :Ct, :Cmt import "export" as {one, two, Something:{umm:{ch}}} ``` + ```yue @@ -67,6 +68,7 @@ do import table.concat print concat ["a", tostring 1] ``` + ```yue @@ -98,6 +100,7 @@ do print FLAG FLAG = 123 ``` + ```yue @@ -140,6 +143,7 @@ export y = -> export class Something umm: "cool" ``` + ```yue @@ -166,6 +170,7 @@ export class Something export :loadstring, to_lua: tolua = yue export {itemA: {:fieldA = '默认值'}} = tb ``` + ```yue @@ -182,6 +187,7 @@ export.itemA = tb export. = items export["a-b-c"] = 123 ``` + ```yue @@ -208,6 +214,7 @@ else export with tmp j = 2000 ``` + ```yue @@ -234,6 +241,7 @@ export default -> print "你好" 123 ``` + ```yue diff --git a/doc/docs/zh/doc/advanced/try.md b/doc/docs/zh/doc/advanced/try.md index b4de24d..ef90831 100644 --- a/doc/docs/zh/doc/advanced/try.md +++ b/doc/docs/zh/doc/advanced/try.md @@ -29,6 +29,7 @@ catch err print yue.traceback err print result ``` + ```yue @@ -82,6 +83,7 @@ catch e print e e ``` + ```yue diff --git a/doc/docs/zh/doc/assignment/assignment.md b/doc/docs/zh/doc/assignment/assignment.md index 49561fb..7245e18 100644 --- a/doc/docs/zh/doc/assignment/assignment.md +++ b/doc/docs/zh/doc/assignment/assignment.md @@ -7,6 +7,7 @@ hello = "world" a, b, c = 1, 2, 3 hello = 123 -- 访问现有的变量 ``` + ```yue @@ -31,6 +32,7 @@ x %= 10 s ..= "world" -- 如果执行更新的局部变量不存在,将新建一个局部变量 arg or= "默认值" ``` + ```yue @@ -54,6 +56,7 @@ arg or= "默认值" a = b = c = d = e = 0 x = y = z = f! ``` + ```yue @@ -81,6 +84,7 @@ do a = 1 B = 2 ``` + ```yue @@ -120,6 +124,7 @@ do B = 2 local Temp = "一个局部值" ``` + ```yue diff --git a/doc/docs/zh/doc/assignment/destructuring-assignment.md b/doc/docs/zh/doc/assignment/destructuring-assignment.md index 29219d3..469071f 100644 --- a/doc/docs/zh/doc/assignment/destructuring-assignment.md +++ b/doc/docs/zh/doc/assignment/destructuring-assignment.md @@ -12,8 +12,8 @@ thing = [1, 2] [a, b] = thing print a, b ``` - + ```yue thing = [1, 2] @@ -38,6 +38,7 @@ print hello, the_day :day = obj -- 可以不带大括号进行简单的解构 ``` + ```yue @@ -69,6 +70,7 @@ obj2 = { {numbers: [first, second], properties: {color: color}} = obj2 print first, second, color ``` + ```yue @@ -96,6 +98,7 @@ print first, second, color } } = obj2 ``` + ```yue @@ -114,6 +117,7 @@ print first, second, color ```yuescript {:concat, :insert} = table ``` + ```yue @@ -127,6 +131,7 @@ print first, second, color ```yuescript {:mix, :max, random: rand} = math ``` + ```yue @@ -140,6 +145,7 @@ print first, second, color ```yuescript {:name = "nameless", :job = "jobless"} = person ``` + ```yue @@ -153,6 +159,7 @@ print first, second, color ```yuescript [_, two, _, four] = items ``` + ```yue @@ -172,6 +179,7 @@ print first -- 打印: first print bulk -- 打印: {"second", "third", "fourth"} print last -- 打印: last ``` + ```yue @@ -196,6 +204,7 @@ print last -- 打印: last -- 跳过中间的元素,只捕获第一个和最后一个元素 [first, ..._, last] = orders ``` + ```yue @@ -224,6 +233,7 @@ tuples = [ for [left, right] in *tuples print left, right ``` + ```yue diff --git a/doc/docs/zh/doc/assignment/if-assignment.md b/doc/docs/zh/doc/assignment/if-assignment.md index e4077e1..915b1d5 100644 --- a/doc/docs/zh/doc/assignment/if-assignment.md +++ b/doc/docs/zh/doc/assignment/if-assignment.md @@ -6,6 +6,7 @@ if user := database.find_user "moon" print user.name ``` + ```yue @@ -23,6 +24,7 @@ elseif world := os.getenv "world" else print "什么都没有 :(" ``` + ```yue @@ -43,6 +45,7 @@ if success, result := pcall -> "无报错地获取结果" print result -- 变量 result 是有作用域的 print "好的" ``` + ```yue @@ -62,6 +65,7 @@ while byte := stream\read_one! -- 对 byte 做一些操作 print byte ``` + ```yue diff --git a/doc/docs/zh/doc/assignment/the-using-clause-controlling-destructive-assignment.md b/doc/docs/zh/doc/assignment/the-using-clause-controlling-destructive-assignment.md index 722de6f..8a9acf4 100644 --- a/doc/docs/zh/doc/assignment/the-using-clause-controlling-destructive-assignment.md +++ b/doc/docs/zh/doc/assignment/the-using-clause-controlling-destructive-assignment.md @@ -17,6 +17,7 @@ my_func! print i -- 将打印 0 ``` + ```yue @@ -52,6 +53,7 @@ my_func = (using nil) -> my_func! print i -- 打印 100,i 没有受到影响 ``` + ```yue @@ -80,6 +82,7 @@ my_func = (add using k, i) -> my_func(22) print i, k -- 这些已经被更新 ``` + ```yue diff --git a/doc/docs/zh/doc/assignment/varargs-assignment.md b/doc/docs/zh/doc/assignment/varargs-assignment.md index 6cc4278..cc9206e 100644 --- a/doc/docs/zh/doc/assignment/varargs-assignment.md +++ b/doc/docs/zh/doc/assignment/varargs-assignment.md @@ -10,6 +10,7 @@ count = select '#', ... first = select 1, ... print ok, count, first ``` + ```yue diff --git a/doc/docs/zh/doc/control-flow/conditionals.md b/doc/docs/zh/doc/control-flow/conditionals.md index ba77f40..3a4d5d1 100644 --- a/doc/docs/zh/doc/control-flow/conditionals.md +++ b/doc/docs/zh/doc/control-flow/conditionals.md @@ -7,6 +7,7 @@ if have_coins else print "没有硬币" ``` + ```yue @@ -25,6 +26,7 @@ else have_coins = false if have_coins then print "有硬币" else print "没有硬币" ``` + ```yue @@ -40,6 +42,7 @@ if have_coins then print "有硬币" else print "没有硬币" have_coins = false print if have_coins then "有硬币" else "没有硬币" ``` + ```yue @@ -65,6 +68,7 @@ else print message -- 打印: 我很高 ``` + ```yue @@ -90,8 +94,8 @@ print message -- 打印: 我很高 unless os.date("%A") == "Monday" print "今天不是星期一!" ``` - + ```yue unless os.date("%A") == "Monday" @@ -103,6 +107,7 @@ unless os.date("%A") == "Monday" ```yuescript print "你真幸运!" unless math.random! > 0.1 ``` + ```yue @@ -124,6 +129,7 @@ if a in [1, 3, 5, 7] if a in list print "检查`a`是否在列表中" ``` + ```yue diff --git a/doc/docs/zh/doc/control-flow/continue.md b/doc/docs/zh/doc/control-flow/continue.md index 9fab8a3..3b1a093 100644 --- a/doc/docs/zh/doc/control-flow/continue.md +++ b/doc/docs/zh/doc/control-flow/continue.md @@ -9,6 +9,7 @@ while i < 10 continue if i % 2 == 0 print i ``` + ```yue @@ -29,6 +30,7 @@ odds = for x in *my_numbers continue if x % 2 == 1 x ``` + ```yue diff --git a/doc/docs/zh/doc/control-flow/for-loop.md b/doc/docs/zh/doc/control-flow/for-loop.md index 212ff81..52180b7 100644 --- a/doc/docs/zh/doc/control-flow/for-loop.md +++ b/doc/docs/zh/doc/control-flow/for-loop.md @@ -12,6 +12,7 @@ for k = 1, 15, 2 -- 提供了一个遍历的步长 for key, value in pairs object print key, value ``` + ```yue @@ -33,6 +34,7 @@ for key, value in pairs object for item in *items[2, 4] print item ``` + ```yue @@ -49,6 +51,7 @@ for item in *items do print item for j = 1, 10, 3 do print j ``` + ```yue @@ -70,6 +73,7 @@ doubled_evens = for i = 1, 20 else i ``` + ```yue @@ -90,6 +94,7 @@ doubled_evens = for i = 1, 20 first_large = for n in *numbers break n if n > 10 ``` + ```yue @@ -110,6 +115,7 @@ func_b = -> return for i = 1, 10 do i print func_a! -- 打印 nil print func_b! -- 打印 table 对象 ``` + ```yue diff --git a/doc/docs/zh/doc/control-flow/switch.md b/doc/docs/zh/doc/control-flow/switch.md index 700bc2a..69ba5b5 100644 --- a/doc/docs/zh/doc/control-flow/switch.md +++ b/doc/docs/zh/doc/control-flow/switch.md @@ -11,6 +11,7 @@ switch name := "Dan" else print "我不认识你,你的名字是#{name}" ``` + ```yue @@ -39,6 +40,7 @@ next_number = switch b else error "数字数得太大了!" ``` + ```yue @@ -62,6 +64,7 @@ msg = switch math.random(1, 5) when 2 then "你差点很幸运" else "不太幸运" ``` + ```yue @@ -87,6 +90,7 @@ switch math.random(1, 5) when 1 else print "不太幸运" ``` + ```yue @@ -124,6 +128,7 @@ for item in *items when :width, :height print "尺寸 #{width}, #{height}" ``` + ```yue @@ -154,6 +159,7 @@ switch item when {pos: {:x = 50, :y = 200}} print "Vec2 #{x}, #{y}" -- 表格解构仍然会通过 ``` + ```yue @@ -181,6 +187,7 @@ switch tb when [1, 2, b = 3] -- 变量b有默认值 print "1, 2, #{b}" ``` + ```yue @@ -206,6 +213,7 @@ switch tb else print "无效值" ``` + ```yue @@ -231,6 +239,7 @@ switch tb else print "无效值" ``` + ```yue @@ -257,6 +266,7 @@ switch tb ] print "匹配成功", fourth ``` + ```yue @@ -282,6 +292,7 @@ switch segments print "Resource:", resource -- 打印: "logs" print "Action:", action -- 打印: "view" ``` + ```yue diff --git a/doc/docs/zh/doc/control-flow/while-loop.md b/doc/docs/zh/doc/control-flow/while-loop.md index 5995890..3c624fe 100644 --- a/doc/docs/zh/doc/control-flow/while-loop.md +++ b/doc/docs/zh/doc/control-flow/while-loop.md @@ -10,6 +10,7 @@ while i > 0 while running == true do my_function! ``` + ```yue @@ -31,6 +32,7 @@ until i == 0 until running == false do my_function! ``` + ```yue @@ -56,6 +58,7 @@ repeat i -= 1 until i == 0 ``` + ```yue diff --git a/doc/docs/zh/doc/data-structures/comprehensions.md b/doc/docs/zh/doc/data-structures/comprehensions.md index fd25b22..62f0896 100644 --- a/doc/docs/zh/doc/data-structures/comprehensions.md +++ b/doc/docs/zh/doc/data-structures/comprehensions.md @@ -10,6 +10,7 @@ items = [1, 2, 3, 4] doubled = [item * 2 for i, item in ipairs items] ``` + ```yue @@ -24,6 +25,7 @@ doubled = [item * 2 for i, item in ipairs items] ```yuescript slice = [item for i, item in ipairs items when i > 1 and i < 3] ``` + ```yue @@ -37,6 +39,7 @@ slice = [item for i, item in ipairs items when i > 1 and i < 3] ```yuescript doubled = [item * 2 for item in *items] ``` + ```yue @@ -55,6 +58,7 @@ data = flat = [...v for k,v in pairs data] -- flat 现在为 [1, 2, 3, 4, 5, 6] ``` + ```yue @@ -79,6 +83,7 @@ y_coords = [9, 2, 3] points = [ [x, y] for x in *x_coords \ for y in *y_coords] ``` + ```yue @@ -96,6 +101,7 @@ for y in *y_coords] ```yuescript evens = [i for i = 1, 100 when i % 2 == 0] ``` + ```yue @@ -119,6 +125,7 @@ thing = { thing_copy = {k, v for k, v in pairs thing} ``` + ```yue @@ -136,6 +143,7 @@ thing_copy = {k, v for k, v in pairs thing} ```yuescript no_color = {k, v for k, v in pairs thing when k != "color"} ``` + ```yue @@ -150,6 +158,7 @@ no_color = {k, v for k, v in pairs thing when k != "color"} numbers = [1, 2, 3, 4] sqrts = {i, math.sqrt i for i in *numbers} ``` + ```yue @@ -167,6 +176,7 @@ sqrts = {i, math.sqrt i for i in *numbers} tuples = [ ["hello", "world"], ["foo", "bar"]] tbl = {unpack tuple for tuple in *tuples} ``` + ```yue @@ -185,6 +195,7 @@ tbl = {unpack tuple for tuple in *tuples} ```yuescript slice = [item for item in *items[1, 5]] ``` + ```yue @@ -198,6 +209,7 @@ slice = [item for item in *items[1, 5]] ```yuescript slice = [item for item in *items[2,]] ``` + ```yue @@ -211,8 +223,8 @@ slice = [item for item in *items[2,]] ```yuescript slice = [item for item in *items[,,2]] ``` - + ```yue slice = [item for item in *items[,,2]] @@ -226,6 +238,7 @@ slice = [item for item in *items[,,2]] -- 取最后4个元素 slice = [item for item in *items[-4,-1]] ``` + ```yue @@ -240,6 +253,7 @@ slice = [item for item in *items[-4,-1]] ```yuescript reverse_slice = [item for item in *items[-1,1,-1]] ``` + ```yue @@ -259,6 +273,7 @@ sub_list = items[2, 4] -- 取最后4个元素作为新的列表 last_four_items = items[-4, -1] ``` + ```yue diff --git a/doc/docs/zh/doc/data-structures/table-literals.md b/doc/docs/zh/doc/data-structures/table-literals.md index a111950..a8ff9b8 100644 --- a/doc/docs/zh/doc/data-structures/table-literals.md +++ b/doc/docs/zh/doc/data-structures/table-literals.md @@ -5,6 +5,7 @@ ```yuescript some_values = [1, 2, 3, 4] ``` + ```yue @@ -22,6 +23,7 @@ some_values = { ["favorite food"]: "rice" } ``` + ```yue @@ -42,6 +44,7 @@ profile = shoe_size: 13, favorite_foods: ["冰淇淋", "甜甜圈"] ``` + ```yue @@ -63,6 +66,7 @@ values = { occupation: "打击犯罪" } ``` + ```yue @@ -83,6 +87,7 @@ my_function dance: "探戈", partner: "无" y = type: "狗", legs: 4, tails: 1 ``` + ```yue @@ -101,6 +106,7 @@ tbl = { end: "饥饿" } ``` + ```yue @@ -121,6 +127,7 @@ person = { :hair, :height, shoe_size: 40 } print_table :hair, :height ``` + ```yue @@ -141,6 +148,7 @@ t = { "你好 世界": true } ``` + ```yue @@ -158,6 +166,7 @@ t = { some_values = [ 1, 2, 3, 4 ] list_with_one_element = [ 1, ] ``` + ```yue diff --git a/doc/docs/zh/doc/functions/backcalls.md b/doc/docs/zh/doc/functions/backcalls.md index 4c32fac..7baac90 100644 --- a/doc/docs/zh/doc/functions/backcalls.md +++ b/doc/docs/zh/doc/functions/backcalls.md @@ -6,6 +6,7 @@ x <- f print "hello" .. x ``` + ```yue @@ -21,6 +22,7 @@ print "hello" .. x <= f print @value ``` + ```yue @@ -36,6 +38,7 @@ print @value (x) <- map _, [1, 2, 3] x * 2 ``` + ```yue @@ -55,6 +58,7 @@ result, msg = do check info print result, msg ``` + ```yue diff --git a/doc/docs/zh/doc/functions/function-literals.md b/doc/docs/zh/doc/functions/function-literals.md index efe920b..163d155 100644 --- a/doc/docs/zh/doc/functions/function-literals.md +++ b/doc/docs/zh/doc/functions/function-literals.md @@ -6,6 +6,7 @@ my_function = -> my_function() -- 调用空函数 ``` + ```yue @@ -24,6 +25,7 @@ func_b = -> value = 100 print "这个值是:", value ``` + ```yue @@ -42,6 +44,7 @@ func_b = -> func_a! func_b() ``` + ```yue @@ -56,6 +59,7 @@ func_b() ```yuescript sum = (x, y) -> print "数字的和", x + y ``` + ```yue @@ -72,6 +76,7 @@ print sum 10, 20 a b c "a", "b", "c" ``` + ```yue @@ -88,6 +93,7 @@ a b c "a", "b", "c" ```yuescript print "x:", sum(10, 20), "y:", sum(30, 40) ``` + ```yue @@ -104,6 +110,7 @@ print "x:", sum(10, 20), "y:", sum(30, 40) sum = (x, y) -> x + y print "数字的和是", sum 10, 20 ``` + ```yue @@ -118,6 +125,7 @@ print "数字的和是", sum 10, 20 ```yuescript sum = (x, y) -> return x + y ``` + ```yue @@ -132,6 +140,7 @@ sum = (x, y) -> return x + y mystery = (x, y) -> x + y, x - y a, b = mystery 10, 20 ``` + ```yue @@ -148,6 +157,7 @@ a, b = mystery 10, 20 ```yuescript func = (num) => @value + num ``` + ```yue @@ -165,6 +175,7 @@ my_function = (name = "某物", height = 100) -> print "你好,我是", name print "我的高度是", height ``` + ```yue @@ -181,6 +192,7 @@ my_function = (name = "某物", height = 100) -> some_args = (x = 100, y = x + 1000) -> print x + y ``` + ```yue @@ -202,6 +214,7 @@ b = x-10 c = x -y d = x- z ``` + ```yue @@ -223,6 +236,7 @@ d = x- z x = func"hello" + 100 y = func "hello" + 100 ``` + ```yue @@ -247,6 +261,7 @@ cool_func 1, 2, 5, 6, 7, 8 ``` + ```yue @@ -269,6 +284,7 @@ my_func 5, 6, 7, 9, 1, 2, 5, 4 ``` + ```yue @@ -289,6 +305,7 @@ x = [ 8, 9, 10 ] ``` + ```yue @@ -309,6 +326,7 @@ y = [ my_func 1, 2, 3, 5, 6, 7 ] ``` + ```yue @@ -335,6 +353,7 @@ if func 1, 2, 3, print "hello" print "我在if内部" ``` + ```yue @@ -373,6 +392,7 @@ f2 = ({a: a1 = 123, :b = 'abc'}, c = {}) -> arg1 = {a: 0} f2 arg1, arg2 ``` + ```yue @@ -402,6 +422,7 @@ findFirstEven = (list): nil -> if sub % 2 == 0 return sub ``` + ```yue @@ -426,6 +447,7 @@ findFirstEven = (list) -> return sub nil ``` + ```yue @@ -467,6 +489,7 @@ process = (...args) -> process 1, nil, 3, nil, 5 ``` + ```yue diff --git a/doc/docs/zh/doc/functions/function-stubs.md b/doc/docs/zh/doc/functions/function-stubs.md index af08feb..888c3eb 100644 --- a/doc/docs/zh/doc/functions/function-stubs.md +++ b/doc/docs/zh/doc/functions/function-stubs.md @@ -24,6 +24,7 @@ run_callback my_object.write -- 让我们把对象捆绑到一个新函数中 run_callback my_object\write ``` + ```yue diff --git a/doc/docs/zh/doc/getting-started/introduction.md b/doc/docs/zh/doc/getting-started/introduction.md index 827d163..7911363 100644 --- a/doc/docs/zh/doc/getting-started/introduction.md +++ b/doc/docs/zh/doc/getting-started/introduction.md @@ -5,6 +5,7 @@ Yue(月)是中文中“月亮”的名称。 ## 月之脚本概览 + ```yuescript -- 导入语法 import p, to_lua from "yue" @@ -49,6 +50,7 @@ with apple -- 类似js的导出语法 export 🌛 = "月之脚本" ``` + ```yue diff --git a/doc/docs/zh/doc/getting-started/usage.md b/doc/docs/zh/doc/getting-started/usage.md index b9d84d4..c9bad2e 100644 --- a/doc/docs/zh/doc/getting-started/usage.md +++ b/doc/docs/zh/doc/getting-started/usage.md @@ -4,49 +4,51 @@   在 Lua 中使用月之脚本模块: -* **用法 1** - -   在 Lua 中引入 "你的脚本入口文件.yue"。 - - ```Lua - require("yue")("你的脚本入口文件") - ``` - -   当你在同一路径下把 "你的脚本入口文件.yue" 编译成了 "你的脚本入口文件.lua" 时,仍然可以使用这个代码加载 .lua 代码文件。在其余的月之脚本文件中,只需正常使用 **require** 或 **import** 进行脚本引用即可。错误消息中的代码行号也会被正确处理。 - -* **用法 2** - -   手动引入月之脚本模块并重写错误消息来帮助调试。 - ```lua - local yue = require("yue") - yue.insert_loaders() - local success, result = xpcall(function() - return require("yuescript_module_name") - end, function(err) - return yue.traceback(err) - end) - ``` - -* **用法 3** - -   在 Lua 中使用月之脚本编译器功能。 - ```lua - local yue = require("yue") - local codes, err, globals = yue.to_lua([[ - f = -> - print "hello world" - f! - ]],{ - implicit_return_root = true, - reserve_line_number = true, - lint_global = true, - space_over_tab = false, - options = { - target = "5.4", - path = "/script" - } - }) - ``` +- **用法 1** + +   在 Lua 中引入 "你的脚本入口文件.yue"。 + + ```Lua + require("yue")("你的脚本入口文件") + ``` + +   当你在同一路径下把 "你的脚本入口文件.yue" 编译成了 "你的脚本入口文件.lua" 时,仍然可以使用这个代码加载 .lua 代码文件。在其余的月之脚本文件中,只需正常使用 **require** 或 **import** 进行脚本引用即可。错误消息中的代码行号也会被正确处理。 + +- **用法 2** + +   手动引入月之脚本模块并重写错误消息来帮助调试。 + + ```lua + local yue = require("yue") + yue.insert_loaders() + local success, result = xpcall(function() + return require("yuescript_module_name") + end, function(err) + return yue.traceback(err) + end) + ``` + +- **用法 3** + +   在 Lua 中使用月之脚本编译器功能。 + + ```lua + local yue = require("yue") + local codes, err, globals = yue.to_lua([[ + f = -> + print "hello world" + f! + ]],{ + implicit_return_root = true, + reserve_line_number = true, + lint_global = true, + space_over_tab = false, + options = { + target = "5.4", + path = "/script" + } + }) + ``` ## 月之脚本编译工具 diff --git a/doc/docs/zh/doc/language-basics/attributes.md b/doc/docs/zh/doc/language-basics/attributes.md index 3dc3b0b..43bc5f9 100644 --- a/doc/docs/zh/doc/language-basics/attributes.md +++ b/doc/docs/zh/doc/language-basics/attributes.md @@ -6,6 +6,7 @@ const a = 123 close _ = : -> print "超出范围。" ``` + ```yue @@ -21,6 +22,7 @@ close _ = : -> print "超出范围。" const {:a, :b, c, d} = tb -- a = 1 ``` + ```yue @@ -36,6 +38,7 @@ const {:a, :b, c, d} = tb global const Constant = 123 -- Constant = 1 ``` + ```yue diff --git a/doc/docs/zh/doc/language-basics/comment.md b/doc/docs/zh/doc/language-basics/comment.md index f6a7b4e..0e69979 100644 --- a/doc/docs/zh/doc/language-basics/comment.md +++ b/doc/docs/zh/doc/language-basics/comment.md @@ -12,6 +12,7 @@ str = --[[ func --[[端口]] 3000, --[[ip]] "192.168.1.1" ``` + ```yue diff --git a/doc/docs/zh/doc/language-basics/literals.md b/doc/docs/zh/doc/language-basics/literals.md index 1592bae..0837c71 100644 --- a/doc/docs/zh/doc/language-basics/literals.md +++ b/doc/docs/zh/doc/language-basics/literals.md @@ -12,6 +12,7 @@ some_string = "这是一个字符串 -- 字符串插值只在双引号字符串中可用。 print "我有#{math.random! * 100}%的把握。" ``` + ```yue @@ -34,8 +35,8 @@ integer = 1_000_000 hex = 0xEF_BB_BF binary = 0B10011 ``` - + ```yue integer = 1_000_000 @@ -56,6 +57,7 @@ str = | - item1 - #{expr} ``` + ```yue @@ -79,6 +81,7 @@ fn = -> bar: baz return str ``` + ```yue @@ -100,6 +103,7 @@ str = | path: "C:\Program Files\App" note: 'He said: "#{Hello}!"' ``` + ```yue diff --git a/doc/docs/zh/doc/language-basics/operator.md b/doc/docs/zh/doc/language-basics/operator.md index 6c9fc5b..d948667 100644 --- a/doc/docs/zh/doc/language-basics/operator.md +++ b/doc/docs/zh/doc/language-basics/operator.md @@ -6,6 +6,7 @@ tb\func! if tb ~= nil tb::func! if tb != nil ``` + ```yue @@ -27,6 +28,7 @@ a = 5 print 1 <= a <= 10 -- 输出:true ``` + ```yue @@ -64,6 +66,7 @@ print v(1) > v(2) <= v(3) false ]] ``` + ```yue @@ -101,6 +104,7 @@ print v(1) > v(2) <= v(3) tab = [] tab[] = "Value" ``` + ```yue @@ -118,6 +122,7 @@ tbB = [4, 5, 6] tbA[] = ...tbB -- tbA 现在为 [1, 2, 3, 4, 5, 6] ``` + ```yue @@ -149,6 +154,7 @@ a = {1, 2, 3, x: 1} b = {4, 5, y: 1} merge = {...a, ...b} ``` + ```yue @@ -179,6 +185,7 @@ last = data.items[#] second_last = data.items[#-1] data.items[#] = 1 ``` + ```yue @@ -212,6 +219,7 @@ print d.value close _ = : -> print "超出范围" ``` + ```yue @@ -245,8 +253,8 @@ print tb.value tb.<> = __index: {item: "hello"} print tb.item ``` - + ```yue -- 使用包含字段 "value" 的元表创建 @@ -267,6 +275,7 @@ print tb.item {item, :new, :, : getter} = tb print item, new, close, getter ``` + ```yue @@ -294,6 +303,7 @@ with? io.open "test.txt", "w" \write "你好" \close! ``` + ```yue @@ -330,6 +340,7 @@ readFile "example.txt" |> render |> print ``` + ```yue @@ -358,6 +369,7 @@ func a ?? {} a ??= false ``` + ```yue @@ -414,6 +426,7 @@ tb = func: => @value + 2 tb: { } ``` + ```yue diff --git a/doc/docs/zh/doc/language-basics/whitespace.md b/doc/docs/zh/doc/language-basics/whitespace.md index 1886e23..e27165f 100644 --- a/doc/docs/zh/doc/language-basics/whitespace.md +++ b/doc/docs/zh/doc/language-basics/whitespace.md @@ -9,6 +9,7 @@ ```yuescript a = 1; b = 2; print a + b ``` + ```yue @@ -29,6 +30,7 @@ Rx.Observable \map (value) -> value .. '!' \subscribe print ``` + ```yue diff --git a/doc/docs/zh/doc/objects/object-oriented-programming.md b/doc/docs/zh/doc/objects/object-oriented-programming.md index 9eb94d8..04f816a 100644 --- a/doc/docs/zh/doc/objects/object-oriented-programming.md +++ b/doc/docs/zh/doc/objects/object-oriented-programming.md @@ -15,6 +15,7 @@ class Inventory else @items[name] = 1 ``` + ```yue @@ -44,8 +45,8 @@ inv = Inventory! inv\add_item "t-shirt" inv\add_item "pants" ``` - + ```yue inv = Inventory! @@ -76,6 +77,7 @@ b\give_item "shirt" -- 会同时打印出裤子和衬衫 print item for item in *a.clothes ``` + ```yue @@ -103,6 +105,7 @@ class Person new: => @clothes = [] ``` + ```yue @@ -124,6 +127,7 @@ class BackPack extends Inventory if #@items > size then error "背包已满" super name ``` + ```yue @@ -150,6 +154,7 @@ class Shelf -- 将打印: Shelf 被 Cupboard 继承 class Cupboard extends Shelf ``` + ```yue @@ -186,6 +191,7 @@ class MyClass extends ParentClass -- super 作为值等于父类: assert super == ParentClass ``` + ```yue @@ -214,6 +220,7 @@ assert b.__class == BackPack print BackPack.size -- 打印 10 ``` + ```yue @@ -235,13 +242,14 @@ print BackPack.size -- 打印 10   如果在类对象的元表中找不到某个属性,系统会从基表中检索该属性。这就意味着我们可以直接从类本身访问到其方法和属性。 -  需要特别注意的是,对类对象的赋值并不会影响到基表,因此这不是向实例添加新方法的正确方式。相反,需要直接修改基表。关于这点,可以参考下面的 “__base” 字段。 +  需要特别注意的是,对类对象的赋值并不会影响到基表,因此这不是向实例添加新方法的正确方式。相反,需要直接修改基表。关于这点,可以参考下面的 “\_\_base” 字段。 -  此外,类对象包含几个特殊的属性:当类被声明时,类的名称会作为一个字符串存储在类对象的 “__name” 字段中。 +  此外,类对象包含几个特殊的属性:当类被声明时,类的名称会作为一个字符串存储在类对象的 “\_\_name” 字段中。 ```yuescript print BackPack.__name -- 打印 Backpack ``` + ```yue @@ -267,6 +275,7 @@ Things\some_func! -- 类变量在实例中不可见 assert Things().some_func == nil ``` + ```yue @@ -295,6 +304,7 @@ Counter! print Counter.count -- 输出 2 ``` + ```yue @@ -317,6 +327,7 @@ print Counter.count -- 输出 2 ```yuescript @@hello 1,2,3,4 ``` + ```yue @@ -335,6 +346,7 @@ print Counter.count -- 输出 2 class Things @class_var = "hello world" ``` + ```yue @@ -356,6 +368,7 @@ class MoreThings some_method: => log "hello world: " .. secret ``` + ```yue @@ -379,6 +392,7 @@ class MoreThings assert @ == self assert @@ == self.__class ``` + ```yue @@ -393,6 +407,7 @@ assert @@ == self.__class ```yuescript some_instance_method = (...) => @@ ... ``` + ```yue @@ -418,6 +433,7 @@ class Something @@biz = biz @@baz = baz ``` + ```yue @@ -443,6 +459,7 @@ new = (@fieldA, @fieldB) => @ obj = new {}, 123, "abc" print obj ``` + ```yue @@ -462,6 +479,7 @@ x = class Bucket drops: 0 add_drop: => @drops += 1 ``` + ```yue @@ -482,6 +500,7 @@ BigBucket = class extends Bucket assert Bucket.__name == "BigBucket" ``` + ```yue @@ -498,6 +517,7 @@ assert Bucket.__name == "BigBucket" ```yuescript x = class ``` + ```yue @@ -527,6 +547,7 @@ y\func! assert y.__class.__parent ~= X -- X 不是 Y 的父类 ``` + ```yue diff --git a/doc/docs/zh/doc/objects/with-statement.md b/doc/docs/zh/doc/objects/with-statement.md index fbd3633..0925050 100644 --- a/doc/docs/zh/doc/objects/with-statement.md +++ b/doc/docs/zh/doc/objects/with-statement.md @@ -1,6 +1,5 @@ # with 语句 - 在编写 Lua 代码时,我们在创建对象后的常见操作是立即调用这个对象一系列操作函数并设置一系列属性。 这导致在代码中多次重复引用对象的名称,增加了不必要的文本噪音。一个常见的解决方案是在创建对象时,在构造函数传入一个表,该表包含要覆盖设置的键和值的集合。这样做的缺点是该对象的构造函数必须支持这种初始化形式。 @@ -16,6 +15,7 @@ with Person! \save! print .name ``` + ```yue @@ -34,6 +34,7 @@ with 语句也可以用作一个表达式,并返回它的代码块正在处理 file = with File "favorite_foods.txt" \set_encoding "utf8" ``` + ```yue @@ -53,6 +54,7 @@ create_person = (name, relatives) -> me = create_person "Leaf", [dad, mother, sister] ``` + ```yue @@ -75,6 +77,7 @@ with str := "你好" print "原始:", str print "大写:", \upper! ``` + ```yue @@ -96,6 +99,7 @@ with tb ["key-name"] = value [] = "abc" -- 追加到 "tb" ``` + ```yue @@ -116,6 +120,7 @@ with tb with? obj print obj.name ``` + ```yue diff --git a/doc/docs/zh/doc/reference/license-mit.md b/doc/docs/zh/doc/reference/license-mit.md index 24b5b15..7979663 100644 --- a/doc/docs/zh/doc/reference/license-mit.md +++ b/doc/docs/zh/doc/reference/license-mit.md @@ -1,6 +1,6 @@ # MIT 许可证 -版权 (c) 2017-2026 李瑾 \ +版权 (c) 2017-2026 李瑾 \ 特此免费授予任何获得本软件副本和相关文档文件(下称“软件”)的人不受限制地处置该软件的权利,包括不受限制地使用、复制、修改、合并、发布、分发、转授许可和/或出售该软件副本,以及再授权被配发了本软件的人如上的权利,须在下列条件下: 上述版权声明和本许可声明应包含在该软件的所有副本或实质成分中。 diff --git a/doc/docs/zh/doc/reference/the-yuescript-library.md b/doc/docs/zh/doc/reference/the-yuescript-library.md index 54e26f7..9565b01 100644 --- a/doc/docs/zh/doc/reference/the-yuescript-library.md +++ b/doc/docs/zh/doc/reference/the-yuescript-library.md @@ -17,6 +17,7 @@ 月之脚本版本。 **签名:** + ```lua version: string ``` @@ -30,6 +31,7 @@ version: string 当前平台的文件分隔符。 **签名:** + ```lua dirsep: string ``` @@ -43,6 +45,7 @@ dirsep: string 编译模块代码缓存。 **签名:** + ```lua yue_compiled: {string: string} ``` @@ -56,6 +59,7 @@ yue_compiled: {string: string} 月之脚本的编译函数。它将 YueScript 代码编译为 Lua 代码。 **签名:** + ```lua to_lua: function(code: string, config?: Config): --[[codes]] string | nil, @@ -65,17 +69,17 @@ to_lua: function(code: string, config?: Config): **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | -| code | string | YueScript 代码。 | +| 参数名 | 类型 | 描述 | +| ------ | ------ | ------------------- | +| code | string | YueScript 代码。 | | config | Config | [可选] 编译器选项。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | -| string \| nil | 编译后的 Lua 代码,如果编译失败则为 nil。 | -| string \| nil | 错误消息,如果编译成功则为 nil。 | +| 返回类型 | 描述 | +| ----------------------------------- | ------------------------------------------------------------------------------------------ | +| string \| nil | 编译后的 Lua 代码,如果编译失败则为 nil。 | +| string \| nil | 错误消息,如果编译成功则为 nil。 | | {{string, integer, integer}} \| nil | 代码中出现的全局变量(带有名称、行和列),如果编译器选项 `lint_global` 为 false 则为 nil。 | ### file_exist @@ -87,21 +91,22 @@ to_lua: function(code: string, config?: Config): 检查源文件是否存在的函数。可以覆盖该函数以自定义行为。 **签名:** + ```lua file_exist: function(filename: string): boolean ``` **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | +| 参数名 | 类型 | 描述 | +| -------- | ------ | -------- | | filename | string | 文件名。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | -| boolean | 文件是否存在。 | +| 返回类型 | 描述 | +| -------- | -------------- | +| boolean | 文件是否存在。 | ### read_file @@ -112,21 +117,22 @@ file_exist: function(filename: string): boolean 读取源文件的函数。可以覆盖该函数以自定义行为。 **签名:** + ```lua read_file: function(filename: string): string ``` **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | +| 参数名 | 类型 | 描述 | +| -------- | ------ | -------- | | filename | string | 文件名。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | -| string | 文件内容。 | +| 返回类型 | 描述 | +| -------- | ---------- | +| string | 文件内容。 | ### insert_loader @@ -137,21 +143,22 @@ read_file: function(filename: string): string 将 YueScript 加载器插入到 Lua 包加载器(搜索器)中。 **签名:** + ```lua insert_loader: function(pos?: integer): boolean ``` **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | -| pos | integer | [可选] 要插入加载器的位置。默认为 3。 | +| 参数名 | 类型 | 描述 | +| ------ | ------- | ------------------------------------- | +| pos | integer | [可选] 要插入加载器的位置。默认为 3。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | -| boolean | 是否成功插入加载器。如果加载器已经插入,则返回失败。 | +| 返回类型 | 描述 | +| -------- | ---------------------------------------------------- | +| boolean | 是否成功插入加载器。如果加载器已经插入,则返回失败。 | ### remove_loader @@ -162,15 +169,16 @@ insert_loader: function(pos?: integer): boolean 从 Lua 包加载器(搜索器)中移除 YueScript 加载器。 **签名:** + ```lua remove_loader: function(): boolean ``` **返回值:** -| 返回类型 | 描述 | -| --- | --- | -| boolean | 是否成功移除加载器。如果加载器未插入,则返回失败。 | +| 返回类型 | 描述 | +| -------- | -------------------------------------------------- | +| boolean | 是否成功移除加载器。如果加载器未插入,则返回失败。 | ### loadstring @@ -181,6 +189,7 @@ remove_loader: function(): boolean 将 YueScript 代码字符串加载为一个函数。 **签名:** + ```lua loadstring: function(input: string, chunkname: string, env: table, config?: Config): --[[loaded function]] nil | function(...: any): (any...), @@ -189,19 +198,19 @@ loadstring: function(input: string, chunkname: string, env: table, config?: Conf **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | -| input | string | YueScript 代码。 | -| chunkname | string | 代码块的名称。 | -| env | table | 环境表。 | -| config | Config | [可选] 编译器选项。 | +| 参数名 | 类型 | 描述 | +| --------- | ------ | ------------------- | +| input | string | YueScript 代码。 | +| chunkname | string | 代码块的名称。 | +| env | table | 环境表。 | +| config | Config | [可选] 编译器选项。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | +| 返回类型 | 描述 | +| --------------- | ---------------------------------- | | function \| nil | 加载的函数,如果加载失败则为 nil。 | -| string \| nil | 错误消息,如果加载成功则为 nil。 | +| string \| nil | 错误消息,如果加载成功则为 nil。 | ### loadstring @@ -212,6 +221,7 @@ loadstring: function(input: string, chunkname: string, env: table, config?: Conf 将 YueScript 代码字符串加载为一个函数。 **签名:** + ```lua loadstring: function(input: string, chunkname: string, config?: Config): --[[loaded function]] nil | function(...: any): (any...), @@ -220,18 +230,18 @@ loadstring: function(input: string, chunkname: string, config?: Config): **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | -| input | string | YueScript 代码。 | -| chunkname | string | 代码块的名称。 | -| config | Config | [可选] 编译器选项。 | +| 参数名 | 类型 | 描述 | +| --------- | ------ | ------------------- | +| input | string | YueScript 代码。 | +| chunkname | string | 代码块的名称。 | +| config | Config | [可选] 编译器选项。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | +| 返回类型 | 描述 | +| --------------- | ---------------------------------- | | function \| nil | 加载的函数,如果加载失败则为 nil。 | -| string \| nil | 错误消息,如果加载成功则为 nil。 | +| string \| nil | 错误消息,如果加载成功则为 nil。 | ### loadstring @@ -242,6 +252,7 @@ loadstring: function(input: string, chunkname: string, config?: Config): 将 YueScript 代码字符串加载为一个函数。 **签名:** + ```lua loadstring: function(input: string, config?: Config): --[[loaded function]] nil | function(...: any): (any...), @@ -250,17 +261,17 @@ loadstring: function(input: string, config?: Config): **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | -| input | string | YueScript 代码。 | +| 参数名 | 类型 | 描述 | +| ------ | ------ | ------------------- | +| input | string | YueScript 代码。 | | config | Config | [可选] 编译器选项。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | +| 返回类型 | 描述 | +| --------------- | ---------------------------------- | | function \| nil | 加载的函数,如果加载失败则为 nil。 | -| string \| nil | 错误消息,如果加载成功则为 nil。 | +| string \| nil | 错误消息,如果加载成功则为 nil。 | ### loadfile @@ -271,6 +282,7 @@ loadstring: function(input: string, config?: Config): 将 YueScript 代码文件加载为一个函数。 **签名:** + ```lua loadfile: function(filename: string, env: table, config?: Config): nil | function(...: any): (any...), @@ -279,18 +291,18 @@ loadfile: function(filename: string, env: table, config?: Config): **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | -| filename | string | 文件名。 | -| env | table | 环境表。 | -| config | Config | [可选] 编译器选项。 | +| 参数名 | 类型 | 描述 | +| -------- | ------ | ------------------- | +| filename | string | 文件名。 | +| env | table | 环境表。 | +| config | Config | [可选] 编译器选项。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | +| 返回类型 | 描述 | +| --------------- | ---------------------------------- | | function \| nil | 加载的函数,如果加载失败则为 nil。 | -| string \| nil | 错误消息,如果加载成功则为 nil。 | +| string \| nil | 错误消息,如果加载成功则为 nil。 | ### loadfile @@ -301,6 +313,7 @@ loadfile: function(filename: string, env: table, config?: Config): 将 YueScript 代码文件加载为一个函数。 **签名:** + ```lua loadfile: function(filename: string, config?: Config): nil | function(...: any): (any...), @@ -309,17 +322,17 @@ loadfile: function(filename: string, config?: Config): **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | -| filename | string | 文件名。 | -| config | Config | [可选] 编译器选项。 | +| 参数名 | 类型 | 描述 | +| -------- | ------ | ------------------- | +| filename | string | 文件名。 | +| config | Config | [可选] 编译器选项。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | +| 返回类型 | 描述 | +| --------------- | ---------------------------------- | | function \| nil | 加载的函数,如果加载失败则为 nil。 | -| string \| nil | 错误消息,如果加载成功则为 nil。 | +| string \| nil | 错误消息,如果加载成功则为 nil。 | ### dofile @@ -330,23 +343,24 @@ loadfile: function(filename: string, config?: Config): 将 YueScript 代码文件加载为一个函数并执行。 **签名:** + ```lua dofile: function(filename: string, env: table, config?: Config): any... ``` **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | -| filename | string | 文件名。 | -| env | table | 环境表。 | -| config | Config | [可选] 编译器选项。 | +| 参数名 | 类型 | 描述 | +| -------- | ------ | ------------------- | +| filename | string | 文件名。 | +| env | table | 环境表。 | +| config | Config | [可选] 编译器选项。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | -| any... | 加载的函数执行后的返回值。 | +| 返回类型 | 描述 | +| -------- | -------------------------- | +| any... | 加载的函数执行后的返回值。 | ### dofile @@ -357,22 +371,23 @@ dofile: function(filename: string, env: table, config?: Config): any... 将 YueScript 代码文件加载为一个函数并执行。 **签名:** + ```lua dofile: function(filename: string, config?: Config): any... ``` **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | -| filename | string | 文件名。 | -| config | Config | [可选] 编译器选项。 | +| 参数名 | 类型 | 描述 | +| -------- | ------ | ------------------- | +| filename | string | 文件名。 | +| config | Config | [可选] 编译器选项。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | -| any... | 加载的函数执行后的返回值。 | +| 返回类型 | 描述 | +| -------- | -------------------------- | +| any... | 加载的函数执行后的返回值。 | ### find_modulepath @@ -383,21 +398,22 @@ dofile: function(filename: string, config?: Config): any... 将 YueScript 模块名解析为文件路径。 **签名:** + ```lua find_modulepath: function(name: string): string ``` **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | -| name | string | 模块名。 | +| 参数名 | 类型 | 描述 | +| ------ | ------ | -------- | +| name | string | 模块名。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | -| string | 文件路径。 | +| 返回类型 | 描述 | +| -------- | ---------- | +| string | 文件路径。 | ### pcall @@ -410,21 +426,22 @@ find_modulepath: function(name: string): string 当发生错误时,将错误信息中的代码行号重写为 YueScript 代码中的原始行号。 **签名:** + ```lua pcall: function(f: function, ...: any): boolean, any... ``` **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | -| f | function | 要调用的函数。 | -| ... | any | 要传递给函数的参数。 | +| 参数名 | 类型 | 描述 | +| ------ | -------- | -------------------- | +| f | function | 要调用的函数。 | +| ... | any | 要传递给函数的参数。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | +| 返回类型 | 描述 | +| ------------ | ---------------------------- | | boolean, ... | 状态码和函数结果或错误信息。 | ### require @@ -437,21 +454,22 @@ pcall: function(f: function, ...: any): boolean, any... 如果模块是 YueScript 模块且加载失败,则将错误信息中的代码行号重写为 YueScript 代码中的原始行号。 **签名:** + ```lua require: function(name: string): any... ``` **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | +| 参数名 | 类型 | 描述 | +| ------- | ------ | ---------------- | | modname | string | 要加载的模块名。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | -| any | 如果模块已经加载,则返回 package.loaded[modname] 中存储的值。否则,尝试查找加载器并返回 package.loaded[modname] 的最终值和加载器数据作为第二个结果。 | +| 返回类型 | 描述 | +| -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | +| any | 如果模块已经加载,则返回 package.loaded[modname] 中存储的值。否则,尝试查找加载器并返回 package.loaded[modname] 的最终值和加载器数据作为第二个结果。 | ### p @@ -462,15 +480,16 @@ require: function(name: string): any... 检查传递的值的内部结构,并打印值出它的字符串表示。 **签名:** + ```lua p: function(...: any) ``` **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | -| ... | any | 要检查的值。 | +| 参数名 | 类型 | 描述 | +| ------ | ---- | ------------ | +| ... | any | 要检查的值。 | ### options @@ -481,6 +500,7 @@ p: function(...: any) 当前编译器选项。 **签名:** + ```lua options: Config.Options ``` @@ -494,21 +514,22 @@ options: Config.Options 重写堆栈跟踪中的行号为 YueScript 代码中的原始行号的 traceback 函数。 **签名:** + ```lua traceback: function(message: string): string ``` **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | +| 参数名 | 类型 | 描述 | +| ------- | ------ | -------------- | | message | string | 堆栈跟踪消息。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | -| string | 重写后的堆栈跟踪消息。 | +| 返回类型 | 描述 | +| -------- | ---------------------- | +| string | 重写后的堆栈跟踪消息。 | ### is_ast @@ -519,22 +540,23 @@ traceback: function(message: string): string 检查代码是否匹配指定的 AST。 **签名:** + ```lua is_ast: function(astName: string, code: string): boolean ``` **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | +| 参数名 | 类型 | 描述 | +| ------- | ------ | ---------- | | astName | string | AST 名称。 | -| code | string | 代码。 | +| code | string | 代码。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | -| boolean | 代码是否匹配 AST。 | +| 返回类型 | 描述 | +| -------- | ------------------ | +| boolean | 代码是否匹配 AST。 | ### AST @@ -545,6 +567,7 @@ is_ast: function(astName: string, code: string): boolean AST 类型定义,带有名称、行、列和子节点。 **签名:** + ```lua type AST = {string, integer, integer, any} ``` @@ -558,6 +581,7 @@ type AST = {string, integer, integer, any} 将代码转换为 AST。 **签名:** + ```lua to_ast: function(code: string, flattenLevel?: number, astName?: string, reserveComment?: boolean): --[[AST]] AST | nil, @@ -566,18 +590,18 @@ to_ast: function(code: string, flattenLevel?: number, astName?: string, reserveC **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | -| code | string | 代码。 | -| flattenLevel | integer | [可选] 扁平化级别。级别越高,会消除更多的 AST 结构的嵌套。默认为 0。最大为 2。 | -| astName | string | [可选] AST 名称。默认为 "File"。 | -| reserveComment | boolean | [可选] 是否保留原始注释。默认为 false。 | +| 参数名 | 类型 | 描述 | +| -------------- | ------- | ------------------------------------------------------------------------------ | +| code | string | 代码。 | +| flattenLevel | integer | [可选] 扁平化级别。级别越高,会消除更多的 AST 结构的嵌套。默认为 0。最大为 2。 | +| astName | string | [可选] AST 名称。默认为 "File"。 | +| reserveComment | boolean | [可选] 是否保留原始注释。默认为 false。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | -| AST \| nil | AST,如果转换失败则为 nil。 | +| 返回类型 | 描述 | +| ------------- | -------------------------------- | +| AST \| nil | AST,如果转换失败则为 nil。 | | string \| nil | 错误消息,如果转换成功则为 nil。 | ### format @@ -589,25 +613,26 @@ to_ast: function(code: string, flattenLevel?: number, astName?: string, reserveC 格式化 YueScript 代码。 **签名:** + ```lua format: function(code: string, tabSize?: number, reserveComment?: boolean): string ``` **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | -| code | string | 代码。 | -| tabSize | integer | [可选] 制表符大小。默认为 4。 | +| 参数名 | 类型 | 描述 | +| -------------- | ------- | -------------------------------------- | +| code | string | 代码。 | +| tabSize | integer | [可选] 制表符大小。默认为 4。 | | reserveComment | boolean | [可选] 是否保留原始注释。默认为 true。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | -| string | 格式化后的代码。 | +| 返回类型 | 描述 | +| -------- | ---------------- | +| string | 格式化后的代码。 | -### __call +### \_\_call **类型:** 元方法。 @@ -617,21 +642,22 @@ format: function(code: string, tabSize?: number, reserveComment?: boolean): stri 如果发生加载失败,则将错误信息中的代码行号重写为 YueScript 代码中的原始行号。 **签名:** + ```lua metamethod __call: function(self: yue, module: string): any... ``` **参数:** -| 参数名 | 类型 | 描述 | -| --- | --- | --- | +| 参数名 | 类型 | 描述 | +| ------ | ------ | -------- | | module | string | 模块名。 | **返回值:** -| 返回类型 | 描述 | -| --- | --- | -| any | 模块值。 | +| 返回类型 | 描述 | +| -------- | -------- | +| any | 模块值。 | ## Config @@ -648,6 +674,7 @@ metamethod __call: function(self: yue, module: string): any... 编译器是否应该收集代码中出现的全局变量。 **签名:** + ```lua lint_global: boolean ``` @@ -661,6 +688,7 @@ lint_global: boolean 编译器是否应该对根层级的代码块进行隐式的表达式返回。 **签名:** + ```lua implicit_return_root: boolean ``` @@ -674,6 +702,7 @@ implicit_return_root: boolean 编译器是否应该在编译后的代码中保留原始行号。 **签名:** + ```lua reserve_line_number: boolean ``` @@ -687,6 +716,7 @@ reserve_line_number: boolean 编译器是否应该在编译后的代码中保留原始注释。 **签名:** + ```lua reserve_comment: boolean ``` @@ -700,6 +730,7 @@ reserve_comment: boolean 编译器是否应该在编译后的代码中使用空格字符而不是制表符字符。 **签名:** + ```lua space_over_tab: boolean ``` @@ -713,6 +744,7 @@ space_over_tab: boolean 编译器是否应该将要编译的代码视为当前正在编译的模块。仅供编译器内部使用。 **签名:** + ```lua same_module: boolean ``` @@ -726,6 +758,7 @@ same_module: boolean 编译器错误消息是否应该包含行号偏移量。仅供编译器内部使用。 **签名:** + ```lua line_offset: integer ``` @@ -739,6 +772,7 @@ line_offset: integer 目标 Lua 版本枚举。 **签名:** + ```lua enum LuaTarget "5.1" @@ -758,6 +792,7 @@ end 要传递给编译函数的额外选项。 **签名:** + ```lua options: Options ``` @@ -777,6 +812,7 @@ options: Options 编译目标 Lua 版本。 **签名:** + ```lua target: LuaTarget ``` @@ -790,6 +826,7 @@ target: LuaTarget 额外模块搜索路径。 **签名:** + ```lua path: string ``` @@ -803,6 +840,7 @@ path: string 是否在回溯错误消息中输出代码块的局部变量。默认为 false。 **签名:** + ```lua dump_locals: boolean ``` @@ -816,6 +854,7 @@ dump_locals: boolean 是否简化输出的错误消息。默认为 true。 **签名:** + ```lua simplified: boolean ``` diff --git a/doc/docs/zh/try/index.md b/doc/docs/zh/try/index.md index c5678b7..7e198b0 100755 --- a/doc/docs/zh/try/index.md +++ b/doc/docs/zh/try/index.md @@ -5,6 +5,7 @@ next: false --- # 月之脚本的在线编译器 + --- 在这里试试 WASM 版的 YueScript 吧。 -- cgit v1.2.3-55-g6feb