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 ++++++++++++++++++++++++++++++++++++++++++++ spec/outputs/macro.lua | 13 +++++++- spec/outputs/string.lua | 41 ++++++++++++++++++++++++- 7 files changed, 192 insertions(+), 40 deletions(-) (limited to 'spec') 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 + diff --git a/spec/outputs/macro.lua b/spec/outputs/macro.lua index 9f5507c..89c6e63 100644 --- a/spec/outputs/macro.lua +++ b/spec/outputs/macro.lua @@ -26,6 +26,10 @@ print({ 123, 'xyz' }) +print({ + 456, + 'abc' +}) do assert(item == nil) end @@ -213,6 +217,13 @@ function tb:func() end end print(x) +local yue = require("yue") +do +local function f2(a) + return a + 1 +end +x = x + f2(3) +end local sel sel = function(a, b, c) if a then @@ -317,7 +328,7 @@ print((setmetatable({ return 998 end })) -print("current line: " .. tostring(323)) +print("current line: " .. tostring(349)) do do -- TODO diff --git a/spec/outputs/string.lua b/spec/outputs/string.lua index febea62..b536e6d 100644 --- a/spec/outputs/string.lua +++ b/spec/outputs/string.lua @@ -1,3 +1,4 @@ +local _module_0 = { } local hi = "hello" local hello = "what the heckyes" print(hi) @@ -41,4 +42,42 @@ local _ = "hello"; ("hello"):format().hello(1, 2, 3); ("hello"):format(1, 2, 3) something("hello"):world() -return something(("hello"):world()) +something(("hello"):world()) +do + local str = "key: value" + str = "config:\n\tenabled: true\n\tlevel: 5" + str = "header: start\nfooter: end" + str = "name: " .. tostring(username) + str = "count: " .. tostring(total) .. " items" + str = "user: " .. tostring(name) .. "\nid: " .. tostring(id) + str = "path: \"C:\\\\Program Files\\\\App\"\ndesc: 'single \"quote\" test'" + str = "key: value \nnext: 123 " + str = "list:\n - \"one\"\n - \"two\"" + str = "-- comment\ncontent text\n-- comment" + str = tostring(1 + 2) .. '\n' .. tostring(2 + 3) .. '\n' .. tostring("a" .. "b") + local obj = { + settings = "mode: " .. tostring(mode) .. "\nflags:\n\t- " .. tostring(flag1) .. "\n\t- default" + } + local fn + fn = function() + return "Hello\nname: " .. tostring(userName) + end + str = "result:\n\tstatus: " .. tostring((function() + if ok then + return "pass" + else + return "fail" + end + end)()) .. "\n\tcode: " .. tostring(code) + local summary = "date: " .. tostring(os.date()) .. "\nvalues:\n\t-\n\t\ta: " .. tostring(aVal) .. "\n\t\tb: " .. tostring(bVal or defaultB) + local msg = send("Hello, " .. tostring(user) .. "!\nToday is " .. tostring(os.date("%A")) .. ".") + local desc + do + local prefix = "Result" + desc = tostring(prefix) .. ":\nvalue: " .. tostring(compute()) + end + print(("1\n2\n3")) +end +local yaml = "version: " .. tostring(ver) .. "\nok: true" +_module_0["yaml"] = yaml +return _module_0 -- cgit v1.2.3-55-g6feb