From 9d3d8ef2be15dfbf279de71241ff747a568e2c49 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 18 Jul 2025 11:51:39 +0800 Subject: Added specs, tests and docs. --- spec/inputs/macro.yue | 54 +++++++++++++++++++++++--------- spec/inputs/macro_export.yue | 31 ++++++++++--------- spec/inputs/macro_teal.yue | 13 +++++--- spec/inputs/macro_todo.yue | 7 ++--- spec/inputs/string.yue | 73 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 140 insertions(+), 38 deletions(-) (limited to 'spec/inputs') diff --git a/spec/inputs/macro.yue b/spec/inputs/macro.yue index 5d5f1a9..191f09f 100644 --- a/spec/inputs/macro.yue +++ b/spec/inputs/macro.yue @@ -60,6 +60,11 @@ macro NumAndStr = (num, str) -> print $NumAndStr 123, 'xyz' +macro NumAndStr2 = (num`Num, str`SingleString) -> | + [#{num}, #{str}] + +print $NumAndStr2 456, 'abc' + $asserts item == nil $myconfig false @@ -100,13 +105,14 @@ macro filter = (items, action)-> $showMacro "filter", "[_ for _ in *#{items} when #{action}]" macro reduce = (items, def, action)-> - $showMacro "reduce", "if ##{items} == 0 - #{def} -else - _1 = #{def} - for _2 in *#{items} - _1 = #{action} - _1" + $showMacro "reduce", | + if ##{items} == 0 + #{def} + else + _1 = #{def} + for _2 in *#{items} + _1 = #{action} + _1 macro foreach = (items, action)-> $showMacro "foreach", "for _ in *#{items} @@ -154,13 +160,15 @@ macro curry = (...)-> f = $curry x,y,z,do print x,y,z -macro get_inner = (var)-> "do - a = 1 - a + 1" +macro get_inner = (var)-> | + do + a = 1 + a + 1 -macro get_inner_hygienic = (var)-> "(-> - local a = 1 - a + 1)!" +macro get_inner_hygienic = (var)-> | + (-> + local a = 1 + a + 1)! do a = 8 @@ -196,6 +204,18 @@ end print x +import "yue" +macro lua = (code`YAMLMultiline) -> { + code: yue.loadstring(code)! + type: "lua" +} + +$lua | + local function f2(a) + return a + 1 + end + x = x + f2(3) + macro def = (fname, ...)-> args = {...} last = table.remove args @@ -317,7 +337,13 @@ $chainC( Destroy! ) -macro tb = -> "{'abc', a:123, :=> 998}" +macro tb = -> | + { + 'abc' + a: 123 + : => 998 + } + print $tb[1], $tb.a, ($tb)!, $tb! print "current line: #{ $LINE }" diff --git a/spec/inputs/macro_export.yue b/spec/inputs/macro_export.yue index 75fd813..22905b5 100644 --- a/spec/inputs/macro_export.yue +++ b/spec/inputs/macro_export.yue @@ -8,13 +8,12 @@ export macro config = (debugging = true)-> "" export macro showMacro = (name, res)-> - if debugMacro then " -do - txt = #{res} - print '[macro ' .. #{name} .. ']' - print txt - txt -" + if debugMacro then | + do + txt = #{res} + print '[macro #{name}]' + print txt + txt else res @@ -35,14 +34,16 @@ export macro copy = (src, dst, ...)-> src != "_src_" and src != "_dst_" and dst != "_src_" and dst != "_dst_" "copy targets can not be _src_ or _dst_" ) - " -do - local _src_, _dst_ - with _dst_ := #{dst} - with _src_ := #{src} -#{table.concat for field in *{...} do " - _dst_.#{field} = _src_.#{field} -"}" + copyFields = table.concat( + ["_dst_.#{field} = _src_.#{field}" for field in *{...}] + "\n\t\t\t" + ) + | + do + local _src_, _dst_ + with _dst_ := #{dst} + with _src_ := #{src} + #{copyFields} export macro enum = (...) -> items = {...} diff --git a/spec/inputs/macro_teal.yue b/spec/inputs/macro_teal.yue index 0cfd862..e51bcd7 100644 --- a/spec/inputs/macro_teal.yue +++ b/spec/inputs/macro_teal.yue @@ -4,11 +4,16 @@ $ -> options.target_extension = "tl" package.path ..= ";./spec/lib/?.lua" -macro to_lua = (code)-> - "require('yue').to_lua(#{code}, reserve_line_number:false, same_module:true)" +macro to_lua = (code)-> | + require('yue').to_lua #{code}, + reserve_line_number: false + same_module: true -macro trim = (name)-> - "if result := #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}" +macro trim = (name)-> | + if result := #{name}\match '[\'"](.*)[\'"]' + result + else + #{name} export macro local = (decl, value = nil)-> import "yue" as {options:{:tl_enabled}} diff --git a/spec/inputs/macro_todo.yue b/spec/inputs/macro_todo.yue index 752c9cb..c9c8f77 100644 --- a/spec/inputs/macro_todo.yue +++ b/spec/inputs/macro_todo.yue @@ -5,9 +5,6 @@ export macro todoInner = (module, line, msg)-> type: "lua" } -export macro todo = (msg)-> - if msg - "$todoInner $FILE, $LINE, #{msg}" - else - "$todoInner $FILE, $LINE" +export macro todo = (msg)-> | + $todoInner $FILE, $LINE#{msg and ", #{msg}" or ""} diff --git a/spec/inputs/string.yue b/spec/inputs/string.yue index f91383e..1f0fba8 100644 --- a/spec/inputs/string.yue +++ b/spec/inputs/string.yue @@ -74,3 +74,76 @@ _ = "hello" something"hello"\world! something "hello"\world! +do + str = | + key: value + str = | + config: + enabled: true + level: 5 + str = | + header: start + + footer: end + str = | + name: #{username} + str = | + count: #{total} items + str = | + user: #{name} + id: #{id} + str = | + path: "C:\\Program Files\\App" + desc: 'single "quote" test' + str = | + key: value + next: 123 + str = | + list: + - "one" + - "two" + str = | + -- comment + content text + -- comment + str = | + #{1 + 2} + #{2 + 3} + #{"a" .. "b"} + obj = + settings: | + mode: #{mode} + flags: + - #{flag1} + - default + fn = -> | + Hello + name: #{userName} + str = | + result: + status: #{if ok then "pass" else "fail"} + code: #{code} + summary = | + date: #{os.date()} + values: + - + a: #{aVal} + b: #{bVal or defaultB} + msg = send | + Hello, #{user}! + Today is #{os.date("%A")}. + desc = do + prefix = "Result" + | + #{prefix}: + value: #{compute!} + (| + 1 + 2 + 3 + ) |> print + +export yaml = | + version: #{ver} + ok: true + -- cgit v1.2.3-55-g6feb