diff options
Diffstat (limited to 'spec/inputs')
| -rw-r--r-- | spec/inputs/destructure.yue | 50 | ||||
| -rw-r--r-- | spec/inputs/funcs.yue | 33 | ||||
| -rw-r--r-- | spec/inputs/import.yue | 12 | ||||
| -rw-r--r-- | spec/inputs/lists.yue | 190 | ||||
| -rw-r--r-- | spec/inputs/loops.yue | 15 | ||||
| -rw-r--r-- | spec/inputs/macro.yue | 54 | ||||
| -rw-r--r-- | spec/inputs/macro_export.yue | 31 | ||||
| -rw-r--r-- | spec/inputs/macro_teal.yue | 13 | ||||
| -rw-r--r-- | spec/inputs/macro_todo.yue | 7 | ||||
| -rw-r--r-- | spec/inputs/props.yue | 61 | ||||
| -rw-r--r-- | spec/inputs/string.yue | 73 | ||||
| -rw-r--r-- | spec/inputs/switch.yue | 71 | ||||
| -rw-r--r-- | spec/inputs/syntax.yue | 12 | ||||
| -rw-r--r-- | spec/inputs/tables.yue | 18 | ||||
| -rw-r--r-- | spec/inputs/test/format_spec.yue | 2 | ||||
| -rw-r--r-- | spec/inputs/try_catch.yue | 122 | ||||
| -rw-r--r-- | spec/inputs/unicode/destructure.yue | 2 | ||||
| -rw-r--r-- | spec/inputs/unicode/funcs.yue | 2 | ||||
| -rw-r--r-- | spec/inputs/unicode/import.yue | 2 | ||||
| -rw-r--r-- | spec/inputs/unicode/macro_export.yue | 4 | ||||
| -rw-r--r-- | spec/inputs/unicode/syntax.yue | 4 | ||||
| -rw-r--r-- | spec/inputs/unicode/vararg.yue | 4 | ||||
| -rw-r--r-- | spec/inputs/unicode/with.yue | 24 | ||||
| -rw-r--r-- | spec/inputs/vararg.yue | 48 | ||||
| -rw-r--r-- | spec/inputs/with.yue | 33 |
25 files changed, 805 insertions, 82 deletions
diff --git a/spec/inputs/destructure.yue b/spec/inputs/destructure.yue index 674dfe4..179056c 100644 --- a/spec/inputs/destructure.yue +++ b/spec/inputs/destructure.yue | |||
| @@ -94,7 +94,7 @@ do | |||
| 94 | -- | 94 | -- |
| 95 | 95 | ||
| 96 | do | 96 | do |
| 97 | with {a,b} = thing | 97 | with {a,b} := thing |
| 98 | print a, b | 98 | print a, b |
| 99 | 99 | ||
| 100 | 100 | ||
| @@ -240,5 +240,53 @@ do | |||
| 240 | switch tb | 240 | switch tb |
| 241 | when {c: {<"abc">: meta_field = "def"}, <[[any string]]>: {d: abc = 123}, <'str'>: {e: def = {}}} | 241 | when {c: {<"abc">: meta_field = "def"}, <[[any string]]>: {d: abc = 123}, <'str'>: {e: def = {}}} |
| 242 | print meta_field, abc, def | 242 | print meta_field, abc, def |
| 243 | |||
| 244 | do | ||
| 245 | clients = ["VIP_Alice", "User_Bob", "User_Clara", "VIP_Eva"] | ||
| 246 | [vipStart, ...regulars, vipEnd] = clients | ||
| 247 | print vipStart -- "VIP_Alice" | ||
| 248 | print regulars -- {"User_Bob", "User_Clara"} | ||
| 249 | print vipEnd -- "VIP_Eva" | ||
| 250 | |||
| 251 | do | ||
| 252 | setupMeeting = (participants) -> | ||
| 253 | [chair, ..._, secretary] = participants | ||
| 254 | print chair, secretary | ||
| 255 | |||
| 256 | setupMeeting ["Alice", "Bob", "Charlie", "David"] | ||
| 257 | -- Output: Alice David | ||
| 258 | |||
| 259 | do | ||
| 260 | getTransactions = -> | ||
| 261 | { | ||
| 262 | {id: "T1", amount: 100} | ||
| 263 | {id: "T2", amount: 200} | ||
| 264 | {id: "T3", amount: 300} | ||
| 265 | } | ||
| 266 | |||
| 267 | :id, :amount = getTransactions![#] | ||
| 268 | assert id == "T3" | ||
| 269 | assert amount == 300 | ||
| 270 | |||
| 271 | do | ||
| 272 | [ | ||
| 273 | _ | ||
| 274 | ...middle | ||
| 275 | _ | ||
| 276 | ] = tb | ||
| 277 | |||
| 278 | do | ||
| 279 | {a, :abc, b, :def, ...sub, d, e} = tb | ||
| 280 | |||
| 281 | do | ||
| 282 | for {:a, :b} in *items | ||
| 283 | print a, b | ||
| 284 | |||
| 285 | for :a, :b in *items | ||
| 286 | print a, b | ||
| 287 | |||
| 288 | for :body in pairs data | ||
| 289 | print body if body | ||
| 290 | |||
| 243 | nil | 291 | nil |
| 244 | 292 | ||
diff --git a/spec/inputs/funcs.yue b/spec/inputs/funcs.yue index e647edc..d19c2d1 100644 --- a/spec/inputs/funcs.yue +++ b/spec/inputs/funcs.yue | |||
| @@ -98,7 +98,7 @@ f( | |||
| 98 | 98 | ||
| 99 | x = (a, | 99 | x = (a, |
| 100 | b) -> | 100 | b) -> |
| 101 | print "what" | 101 | print "what" |
| 102 | 102 | ||
| 103 | 103 | ||
| 104 | y = (a="hi", | 104 | y = (a="hi", |
| @@ -193,4 +193,35 @@ do | |||
| 193 | func = (): -> check 123 | 193 | func = (): -> check 123 |
| 194 | print func! -- get nil | 194 | print func! -- get nil |
| 195 | 195 | ||
| 196 | do | ||
| 197 | f = ({:a, :b, :c}) -> print a, b, c | ||
| 198 | f = (:a, :b, :c) -> print a, b, c | ||
| 199 | g = (x, :y) -> print x, y | ||
| 200 | i = ({a: ax = 0, b: by = 0}) -> print ax, by | ||
| 201 | j = (name, {id: uid = "n/a", :role = "guest"}) -> print name, uid, role | ||
| 202 | m = ({user: {:name, :age}, meta: {:ver = 1}}) -> print name, age, ver | ||
| 203 | m1 = ({user: {:name, :age}, :meta = {}}) -> print name, age, meta and meta.ver or "nil" | ||
| 204 | new = ({:name = "anon", :age = 0}) => | ||
| 205 | @name = name | ||
| 206 | @age = age | ||
| 207 | set = ({:name = @name, :age = @age}) => | ||
| 208 | @name = name | ||
| 209 | @age = age | ||
| 210 | logKV = ({:k, :v}, ...) -> | ||
| 211 | print "kv:", k, v | ||
| 212 | print "rest count:", select "#", ... | ||
| 213 | macro gen = (fname) -> | | ||
| 214 | #{fname} = ({:a, :b = 0}) -> print a, b | ||
| 215 | $gen foo | ||
| 216 | t1 = (:a, x) -> print a, x | ||
| 217 | t2 = (:a) -> print a | ||
| 218 | w = ( | ||
| 219 | id | ||
| 220 | {:x = 0, :y = 0} | ||
| 221 | :flag | ||
| 222 | ) -> | ||
| 223 | print id, x, y, flag | ||
| 224 | g1 = ({:a, a: ax}) -> print a, ax | ||
| 225 | g4 = ({:a, :b, ...rest}) -> print a, b | ||
| 226 | |||
| 196 | nil | 227 | nil |
diff --git a/spec/inputs/import.yue b/spec/inputs/import.yue index b8ffc24..ee1fdfc 100644 --- a/spec/inputs/import.yue +++ b/spec/inputs/import.yue | |||
| @@ -23,7 +23,7 @@ do | |||
| 23 | b, c from z | 23 | b, c from z |
| 24 | 24 | ||
| 25 | do | 25 | do |
| 26 | import a | 26 | import a, |
| 27 | b | 27 | b |
| 28 | c from z | 28 | c from z |
| 29 | 29 | ||
| @@ -139,3 +139,13 @@ do | |||
| 139 | import "m" as {c: d} | 139 | import "m" as {c: d} |
| 140 | import "m" as {g, {<close>: i}} | 140 | import "m" as {g, {<close>: i}} |
| 141 | 141 | ||
| 142 | do | ||
| 143 | import require | ||
| 144 | import string as stringlib | ||
| 145 | import string.format | ||
| 146 | import io.read as io_read | ||
| 147 | |||
| 148 | type = -> | ||
| 149 | import type as tp | ||
| 150 | import 月 as yue | ||
| 151 | |||
diff --git a/spec/inputs/lists.yue b/spec/inputs/lists.yue index 921cae0..dd951a5 100644 --- a/spec/inputs/lists.yue +++ b/spec/inputs/lists.yue | |||
| @@ -87,4 +87,194 @@ do | |||
| 87 | [a, b] = hello | 87 | [a, b] = hello |
| 88 | [name = "nameless", job = "jobless"] = person | 88 | [name = "nameless", job = "jobless"] = person |
| 89 | 89 | ||
| 90 | do | ||
| 91 | transactions = ["T001", "T002", "T003", "T004", "T005"] | ||
| 92 | middleTransactions = transactions[2, -2] | ||
| 93 | print middleTransactions -- => {"T002", "T003", "T004"} | ||
| 94 | |||
| 95 | do | ||
| 96 | logs = | ||
| 97 | - start: 0, end: 100 | ||
| 98 | - start: 100, end: 200 | ||
| 99 | - start: 200, end: 123 | ||
| 100 | print logs[#].end -- => 123 | ||
| 101 | |||
| 102 | do | ||
| 103 | pendingOrders = ["O001", "O002", "O003", "O004"] | ||
| 104 | print pendingOrders[# - 1] -- => "O003" | ||
| 105 | |||
| 106 | do | ||
| 107 | getOrders = -> | ||
| 108 | { | ||
| 109 | { id: "O1001", status: "pending" } | ||
| 110 | { id: "O1002", status: "processing" } | ||
| 111 | { id: "O1003", status: "done" } | ||
| 112 | } | ||
| 113 | |||
| 114 | lastStatus = getOrders()[#].status | ||
| 115 | assert lastStatus == "done" | ||
| 116 | |||
| 117 | do | ||
| 118 | cloneList1 = (list) -> list[,] | ||
| 119 | cloneList2 = (list) -> [...list,] | ||
| 120 | cloneTable = (tb) -> {...tb} | ||
| 121 | |||
| 122 | do | ||
| 123 | print( | ||
| 124 | globalTB[#] | ||
| 125 | a.b.c[# - 2] | ||
| 126 | x?\y?!.z?[# - 3] | ||
| 127 | ) | ||
| 128 | |||
| 129 | do | ||
| 130 | f = -> | ||
| 131 | print( | ||
| 132 | globalTB[#]\end 123 | ||
| 133 | a.b.c[5,-5][# - 2] | ||
| 134 | x?\y?!.z?[# - 3]?[, -3] | ||
| 135 | ) | ||
| 136 | |||
| 137 | do | ||
| 138 | tb = [1, 2, 3] | ||
| 139 | tb[#] = 40 | ||
| 140 | tb[# - 1] = 20 | ||
| 141 | |||
| 142 | do | ||
| 143 | a = b = c = "x" | ||
| 144 | lst = [] | ||
| 145 | lst[#] = a | ||
| 146 | lst[# - 1] = b | ||
| 147 | |||
| 148 | do | ||
| 149 | x, y, z = 1, 2, 3 | ||
| 150 | arr = [] | ||
| 151 | arr[#], head = x, y | ||
| 152 | arr[#] = z | ||
| 153 | |||
| 154 | do | ||
| 155 | triple = ["keep", "skip", "tail"] | ||
| 156 | [head, _, tailv] = triple | ||
| 157 | buf = [] | ||
| 158 | buf[#] = head | ||
| 159 | buf[#] = tailv | ||
| 160 | |||
| 161 | do | ||
| 162 | src = ["a", "", "c", nil, "d"] | ||
| 163 | collected = [] | ||
| 164 | for item in *src | ||
| 165 | if item and #item > 0 | ||
| 166 | collected[#] = item | ||
| 167 | |||
| 168 | do | ||
| 169 | nums = [1, 2, 3, 4, 5] | ||
| 170 | last_two = [v for v in *nums when v > 3] | ||
| 171 | nums[#] = last_two[1] | ||
| 172 | nums[#] = last_two[2] | ||
| 173 | |||
| 174 | do | ||
| 175 | store = [] | ||
| 176 | store[#] = { meta: { id: 1, ok: true }, payload: [10, 20] } | ||
| 177 | store[#] = { meta: { id: 1, ok: false }, payload: [10, 20, 30] } | ||
| 178 | |||
| 179 | do | ||
| 180 | f = -> | ||
| 181 | q = [] | ||
| 182 | tb.tmp = [n for n = 1, 4] | ||
| 183 | if #tb.tmp >= 3 | ||
| 184 | q[#] = {head: tb.tmp[1], tail: tb.tmp[#]} | ||
| 185 | |||
| 186 | do | ||
| 187 | make_pair = (a, b) -> [a, b] | ||
| 188 | pairs = [] | ||
| 189 | p1 = make_pair 7, 8 | ||
| 190 | pairs[#] = p1 | ||
| 191 | k, v = "key", 42 | ||
| 192 | pairs[#] = {k: k, v: v} | ||
| 193 | |||
| 194 | do | ||
| 195 | cfg = {mode: "safe", tags: []} | ||
| 196 | if cfg.mode == "safe" | ||
| 197 | cfg.mode = "fast" | ||
| 198 | cfg.tags[#] = "newbie" | ||
| 199 | |||
| 200 | do | ||
| 201 | mat = [ [1,2], [3,4], [5,6]] | ||
| 202 | last_row = mat[#] | ||
| 203 | rows = [] | ||
| 204 | rows[#] = last_row[1] | ||
| 205 | |||
| 206 | do | ||
| 207 | kv = [] | ||
| 208 | kv[#] = {k: "a", v: 1} | ||
| 209 | kv[#] = {k: "b", v: 2} | ||
| 210 | pair_last = kv[#] | ||
| 211 | dict = {} | ||
| 212 | dict[pair_last.k] = pair_last.v | ||
| 213 | dict[pair_last.k] = 3 | ||
| 214 | |||
| 215 | do | ||
| 216 | base = [ i for i = 1, 4 ] | ||
| 217 | pack = [] | ||
| 218 | pack[#] = [ base[1], base[#] ] | ||
| 219 | pack[#] = { first: base[1], last: base[#] } | ||
| 220 | |||
| 221 | do | ||
| 222 | opts = {limit: 10} | ||
| 223 | {:limit, :offset = 0} = opts | ||
| 224 | pages = [] | ||
| 225 | pages[#] = {limit: limit, offset: offset} | ||
| 226 | |||
| 227 | do | ||
| 228 | chain = { a: { b: { c: 0 } }, list: [ {x:0}, {x:0} ] } | ||
| 229 | chain.a.b.c = 1 | ||
| 230 | chain.list[1].x = 10 | ||
| 231 | chain.list[#].x = 20 | ||
| 232 | chain.list[# - 1] = { x: 30 } | ||
| 233 | |||
| 234 | do | ||
| 235 | node = {left: {v:0}, right: {v:0}} | ||
| 236 | bag = [] | ||
| 237 | { :left, :right } = node | ||
| 238 | bag[#], left.v, right.v = "k", 1, 2 | ||
| 239 | |||
| 240 | do | ||
| 241 | a1, a2, a3 = 100, 200, 300 | ||
| 242 | mix = [] | ||
| 243 | mix[#], mix[#], meta = a1, a2, {tag: "ok"} | ||
| 244 | |||
| 245 | do | ||
| 246 | cfg2 = {limit: 5, opts: {flag: false}} | ||
| 247 | {limit: lim, opts: opt2} = cfg2 | ||
| 248 | bucket = {xs: []} | ||
| 249 | bucket.xs[#], bucket.flag, opt2.flags[] = lim, true, 123 | ||
| 250 | |||
| 251 | do | ||
| 252 | ret2 = ()-> 7, 8 | ||
| 253 | box = [] | ||
| 254 | box[#], x1 = ret2! | ||
| 255 | |||
| 256 | do | ||
| 257 | q = [1, 2] | ||
| 258 | lastq = q[#] | ||
| 259 | q[# - 1] = lastq * 10 | ||
| 260 | |||
| 261 | do | ||
| 262 | mat2 = [[9,8], [7,6]] | ||
| 263 | t = { hold: nil } | ||
| 264 | t.hold = mat2[#][1] | ||
| 265 | |||
| 266 | do | ||
| 267 | f = -> globalTB[#][#] = 1 | ||
| 268 | f1 = -> globalTB[#][# - 1] | ||
| 269 | |||
| 270 | do | ||
| 271 | tbA[] = ...tbB | ||
| 272 | a, tb[], b[], c = 1, ...x, 3, 4 | ||
| 273 | |||
| 274 | data = | ||
| 275 | a: {1,2,3} | ||
| 276 | b: {4,5,6} | ||
| 277 | |||
| 278 | flat = [...v for k,v in pairs data] | ||
| 279 | |||
| 90 | nil | 280 | nil |
diff --git a/spec/inputs/loops.yue b/spec/inputs/loops.yue index 9a91b42..5df10ca 100644 --- a/spec/inputs/loops.yue +++ b/spec/inputs/loops.yue | |||
| @@ -251,4 +251,17 @@ do | |||
| 251 | if value > 5 | 251 | if value > 5 |
| 252 | item | 252 | item |
| 253 | 253 | ||
| 254 | 254 | do | |
| 255 | repeat print 1 until true | ||
| 256 | |||
| 257 | x = repeat | ||
| 258 | a = func! | ||
| 259 | break a.x | ||
| 260 | until a.v | ||
| 261 | |||
| 262 | items = repeat | ||
| 263 | item = getItem! | ||
| 264 | break unless item | ||
| 265 | item if item.value > 0 | ||
| 266 | until false | ||
| 267 | |||
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) -> | |||
| 60 | 60 | ||
| 61 | print $NumAndStr 123, 'xyz' | 61 | print $NumAndStr 123, 'xyz' |
| 62 | 62 | ||
| 63 | macro NumAndStr2 = (num`Num, str`SingleString) -> | | ||
| 64 | [#{num}, #{str}] | ||
| 65 | |||
| 66 | print $NumAndStr2 456, 'abc' | ||
| 67 | |||
| 63 | $asserts item == nil | 68 | $asserts item == nil |
| 64 | 69 | ||
| 65 | $myconfig false | 70 | $myconfig false |
| @@ -100,13 +105,14 @@ macro filter = (items, action)-> | |||
| 100 | $showMacro "filter", "[_ for _ in *#{items} when #{action}]" | 105 | $showMacro "filter", "[_ for _ in *#{items} when #{action}]" |
| 101 | 106 | ||
| 102 | macro reduce = (items, def, action)-> | 107 | macro reduce = (items, def, action)-> |
| 103 | $showMacro "reduce", "if ##{items} == 0 | 108 | $showMacro "reduce", | |
| 104 | #{def} | 109 | if ##{items} == 0 |
| 105 | else | 110 | #{def} |
| 106 | _1 = #{def} | 111 | else |
| 107 | for _2 in *#{items} | 112 | _1 = #{def} |
| 108 | _1 = #{action} | 113 | for _2 in *#{items} |
| 109 | _1" | 114 | _1 = #{action} |
| 115 | _1 | ||
| 110 | 116 | ||
| 111 | macro foreach = (items, action)-> | 117 | macro foreach = (items, action)-> |
| 112 | $showMacro "foreach", "for _ in *#{items} | 118 | $showMacro "foreach", "for _ in *#{items} |
| @@ -154,13 +160,15 @@ macro curry = (...)-> | |||
| 154 | f = $curry x,y,z,do | 160 | f = $curry x,y,z,do |
| 155 | print x,y,z | 161 | print x,y,z |
| 156 | 162 | ||
| 157 | macro get_inner = (var)-> "do | 163 | macro get_inner = (var)-> | |
| 158 | a = 1 | 164 | do |
| 159 | a + 1" | 165 | a = 1 |
| 166 | a + 1 | ||
| 160 | 167 | ||
| 161 | macro get_inner_hygienic = (var)-> "(-> | 168 | macro get_inner_hygienic = (var)-> | |
| 162 | local a = 1 | 169 | (-> |
| 163 | a + 1)!" | 170 | local a = 1 |
| 171 | a + 1)! | ||
| 164 | 172 | ||
| 165 | do | 173 | do |
| 166 | a = 8 | 174 | a = 8 |
| @@ -196,6 +204,18 @@ end | |||
| 196 | 204 | ||
| 197 | print x | 205 | print x |
| 198 | 206 | ||
| 207 | import "yue" | ||
| 208 | macro lua = (code`YAMLMultiline) -> { | ||
| 209 | code: yue.loadstring(code)! | ||
| 210 | type: "lua" | ||
| 211 | } | ||
| 212 | |||
| 213 | $lua | | ||
| 214 | local function f2(a) | ||
| 215 | return a + 1 | ||
| 216 | end | ||
| 217 | x = x + f2(3) | ||
| 218 | |||
| 199 | macro def = (fname, ...)-> | 219 | macro def = (fname, ...)-> |
| 200 | args = {...} | 220 | args = {...} |
| 201 | last = table.remove args | 221 | last = table.remove args |
| @@ -317,7 +337,13 @@ $chainC( | |||
| 317 | Destroy! | 337 | Destroy! |
| 318 | ) | 338 | ) |
| 319 | 339 | ||
| 320 | macro tb = -> "{'abc', a:123, <call>:=> 998}" | 340 | macro tb = -> | |
| 341 | { | ||
| 342 | 'abc' | ||
| 343 | a: 123 | ||
| 344 | <call>: => 998 | ||
| 345 | } | ||
| 346 | |||
| 321 | print $tb[1], $tb.a, ($tb)!, $tb! | 347 | print $tb[1], $tb.a, ($tb)!, $tb! |
| 322 | 348 | ||
| 323 | print "current line: #{ $LINE }" | 349 | print "current line: #{ $LINE }" |
diff --git a/spec/inputs/macro_export.yue b/spec/inputs/macro_export.yue index cc7d459..22905b5 100644 --- a/spec/inputs/macro_export.yue +++ b/spec/inputs/macro_export.yue | |||
| @@ -8,13 +8,12 @@ export macro config = (debugging = true)-> | |||
| 8 | "" | 8 | "" |
| 9 | 9 | ||
| 10 | export macro showMacro = (name, res)-> | 10 | export macro showMacro = (name, res)-> |
| 11 | if debugMacro then " | 11 | if debugMacro then | |
| 12 | do | 12 | do |
| 13 | txt = #{res} | 13 | txt = #{res} |
| 14 | print '[macro ' .. #{name} .. ']' | 14 | print '[macro #{name}]' |
| 15 | print txt | 15 | print txt |
| 16 | txt | 16 | txt |
| 17 | " | ||
| 18 | else | 17 | else |
| 19 | res | 18 | res |
| 20 | 19 | ||
| @@ -35,14 +34,16 @@ export macro copy = (src, dst, ...)-> | |||
| 35 | src != "_src_" and src != "_dst_" and dst != "_src_" and dst != "_dst_" | 34 | src != "_src_" and src != "_dst_" and dst != "_src_" and dst != "_dst_" |
| 36 | "copy targets can not be _src_ or _dst_" | 35 | "copy targets can not be _src_ or _dst_" |
| 37 | ) | 36 | ) |
| 38 | " | 37 | copyFields = table.concat( |
| 39 | do | 38 | ["_dst_.#{field} = _src_.#{field}" for field in *{...}] |
| 40 | local _src_, _dst_ | 39 | "\n\t\t\t" |
| 41 | with _dst_ = #{dst} | 40 | ) |
| 42 | with _src_ = #{src} | 41 | | |
| 43 | #{table.concat for field in *{...} do " | 42 | do |
| 44 | _dst_.#{field} = _src_.#{field} | 43 | local _src_, _dst_ |
| 45 | "}" | 44 | with _dst_ := #{dst} |
| 45 | with _src_ := #{src} | ||
| 46 | #{copyFields} | ||
| 46 | 47 | ||
| 47 | export macro enum = (...) -> | 48 | export macro enum = (...) -> |
| 48 | items = {...} | 49 | 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 @@ $ -> | |||
| 4 | options.target_extension = "tl" | 4 | options.target_extension = "tl" |
| 5 | package.path ..= ";./spec/lib/?.lua" | 5 | package.path ..= ";./spec/lib/?.lua" |
| 6 | 6 | ||
| 7 | macro to_lua = (code)-> | 7 | macro to_lua = (code)-> | |
| 8 | "require('yue').to_lua(#{code}, reserve_line_number:false, same_module:true)" | 8 | require('yue').to_lua #{code}, |
| 9 | reserve_line_number: false | ||
| 10 | same_module: true | ||
| 9 | 11 | ||
| 10 | macro trim = (name)-> | 12 | macro trim = (name)-> | |
| 11 | "if result := #{name}\\match '[\\'\"](.*)[\\'\"]' then result else #{name}" | 13 | if result := #{name}\match '[\'"](.*)[\'"]' |
| 14 | result | ||
| 15 | else | ||
| 16 | #{name} | ||
| 12 | 17 | ||
| 13 | export macro local = (decl, value = nil)-> | 18 | export macro local = (decl, value = nil)-> |
| 14 | import "yue" as {options:{:tl_enabled}} | 19 | 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)-> | |||
| 5 | type: "lua" | 5 | type: "lua" |
| 6 | } | 6 | } |
| 7 | 7 | ||
| 8 | export macro todo = (msg)-> | 8 | export macro todo = (msg)-> | |
| 9 | if msg | 9 | $todoInner $FILE, $LINE#{msg and ", #{msg}" or ""} |
| 10 | "$todoInner $FILE, $LINE, #{msg}" | ||
| 11 | else | ||
| 12 | "$todoInner $FILE, $LINE" | ||
| 13 | 10 | ||
diff --git a/spec/inputs/props.yue b/spec/inputs/props.yue new file mode 100644 index 0000000..bbb7aae --- /dev/null +++ b/spec/inputs/props.yue | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | class Props | ||
| 2 | __index: (name): nil => | ||
| 3 | cls = @.<> | ||
| 4 | if item := cls.__getter?[name] -- access properties | ||
| 5 | return item @ | ||
| 6 | elseif item := rawget cls, name -- access member functions | ||
| 7 | return item | ||
| 8 | else | ||
| 9 | c = cls | ||
| 10 | while c := c.<> -- recursive to access base classes | ||
| 11 | if item := c.__getter?[name] | ||
| 12 | cls.__getter ??= {} | ||
| 13 | cls.__getter[name] = item -- cache base properties to class | ||
| 14 | return item @ | ||
| 15 | elseif item := rawget c, name | ||
| 16 | rawset cls, name, item -- cache base member to class | ||
| 17 | return item | ||
| 18 | |||
| 19 | __newindex: (name, value) => | ||
| 20 | cls = @.<> | ||
| 21 | if item := cls.__setter?[name] -- access properties | ||
| 22 | item @, value | ||
| 23 | else | ||
| 24 | c = cls | ||
| 25 | while c := c.<> -- recursive to access base classes | ||
| 26 | if item := c.__setter?[name] | ||
| 27 | cls.__setter ??= {} | ||
| 28 | cls.__setter[name] = item -- cache base property to class | ||
| 29 | item @, value | ||
| 30 | return | ||
| 31 | rawset @, name, value -- assign field to self | ||
| 32 | |||
| 33 | assignReadOnly = -> error "assigning a readonly property" | ||
| 34 | |||
| 35 | prop: (name, props) => | ||
| 36 | { | ||
| 37 | :get | ||
| 38 | :set = assignReadOnly | ||
| 39 | } = props | ||
| 40 | if getter := rawget @__base, "__getter" | ||
| 41 | getter[name] = get | ||
| 42 | else | ||
| 43 | rawset @__base, "__getter", [name]: get | ||
| 44 | if setter := rawget @__base, "__setter" | ||
| 45 | setter[name] = set | ||
| 46 | else | ||
| 47 | rawset @__base, "__setter", [name]: set | ||
| 48 | |||
| 49 | class A extends Props | ||
| 50 | @prop 'x' | ||
| 51 | get: => @_x + 1000 | ||
| 52 | set: (v) => @_x = v | ||
| 53 | new: => | ||
| 54 | @_x = 0 | ||
| 55 | |||
| 56 | class B extends A | ||
| 57 | @prop 'abc', get: => "hello" | ||
| 58 | |||
| 59 | b = B! | ||
| 60 | b.x = 999 | ||
| 61 | print b.x, b.abc | ||
diff --git a/spec/inputs/string.yue b/spec/inputs/string.yue index f91383e..611205d 100644 --- a/spec/inputs/string.yue +++ b/spec/inputs/string.yue | |||
| @@ -74,3 +74,76 @@ _ = "hello" | |||
| 74 | something"hello"\world! | 74 | something"hello"\world! |
| 75 | something "hello"\world! | 75 | something "hello"\world! |
| 76 | 76 | ||
| 77 | do | ||
| 78 | str = | | ||
| 79 | key: value | ||
| 80 | str = | | ||
| 81 | config: | ||
| 82 | enabled: true | ||
| 83 | level: 5 | ||
| 84 | str = | | ||
| 85 | header: start | ||
| 86 | |||
| 87 | footer: end | ||
| 88 | str = | | ||
| 89 | name: #{username} | ||
| 90 | str = | | ||
| 91 | count: #{total} items | ||
| 92 | str = | | ||
| 93 | user: #{name} | ||
| 94 | id: #{id} | ||
| 95 | str = | | ||
| 96 | path: "C:\\Program Files\\App" | ||
| 97 | desc: 'single "quote" test' | ||
| 98 | str = | | ||
| 99 | key: value | ||
| 100 | next: 123 | ||
| 101 | str = | | ||
| 102 | list: | ||
| 103 | - "one" | ||
| 104 | - "two" | ||
| 105 | str = | | ||
| 106 | -- comment | ||
| 107 | content text | ||
| 108 | -- comment | ||
| 109 | str = | | ||
| 110 | #{1 + 2} | ||
| 111 | #{2 + 3} | ||
| 112 | #{"a" .. "b"} | ||
| 113 | obj = | ||
| 114 | settings: | | ||
| 115 | mode: #{mode} | ||
| 116 | flags: | ||
| 117 | - #{flag1} | ||
| 118 | - default | ||
| 119 | fn = -> | | ||
| 120 | Hello | ||
| 121 | name: #{userName} | ||
| 122 | str = | | ||
| 123 | result: | ||
| 124 | status: #{if ok then "pass" else "fail"} | ||
| 125 | code: #{code} | ||
| 126 | summary = | | ||
| 127 | date: #{os.date()} | ||
| 128 | values: | ||
| 129 | - | ||
| 130 | a: #{aVal} | ||
| 131 | b: #{bVal or defaultB} | ||
| 132 | msg = send | | ||
| 133 | Hello, #{user}! | ||
| 134 | Today is #{os.date("%A")}. | ||
| 135 | desc = do | ||
| 136 | prefix = "Result" | ||
| 137 | | | ||
| 138 | #{prefix}: | ||
| 139 | value: #{compute!} | ||
| 140 | (| | ||
| 141 | 1 | ||
| 142 | 2 | ||
| 143 | 3 | ||
| 144 | |||
| 145 | |||
| 146 | export yaml = | | ||
| 147 | version: #{ver} | ||
| 148 | ok: true | ||
| 149 | |||
diff --git a/spec/inputs/switch.yue b/spec/inputs/switch.yue index 7ff3118..2b0669c 100644 --- a/spec/inputs/switch.yue +++ b/spec/inputs/switch.yue | |||
| @@ -220,4 +220,73 @@ do | |||
| 220 | ] | 220 | ] |
| 221 | print "matched", sixth | 221 | print "matched", sixth |
| 222 | 222 | ||
| 223 | nil \ No newline at end of file | 223 | do |
| 224 | switch v := "hello" | ||
| 225 | when "hello" | ||
| 226 | print "matched hello" | ||
| 227 | else | ||
| 228 | print "not matched" | ||
| 229 | -- output: matched hello | ||
| 230 | |||
| 231 | do | ||
| 232 | f = -> "ok" | ||
| 233 | switch val := f! | ||
| 234 | when "ok" | ||
| 235 | print "it's ok" | ||
| 236 | -- output: it's ok | ||
| 237 | |||
| 238 | |||
| 239 | do | ||
| 240 | g = -> 42 | ||
| 241 | switch result := g! | ||
| 242 | when 1, 2 | ||
| 243 | print "small" | ||
| 244 | when 42 | ||
| 245 | print "life universe everything" | ||
| 246 | else | ||
| 247 | print "other #{result}" | ||
| 248 | -- output: life universe everything | ||
| 249 | |||
| 250 | do | ||
| 251 | check = -> | ||
| 252 | if true | ||
| 253 | "yes" | ||
| 254 | else | ||
| 255 | "no" | ||
| 256 | |||
| 257 | switch x := check! | ||
| 258 | when "yes" | ||
| 259 | print "affirmative" | ||
| 260 | else | ||
| 261 | print "negative" | ||
| 262 | -- output: affirmative | ||
| 263 | |||
| 264 | do | ||
| 265 | t = (): tb -> | ||
| 266 | tb = {a: 1} | ||
| 267 | tb.a = 2 | ||
| 268 | |||
| 269 | switch data := t! | ||
| 270 | when {a: 2} | ||
| 271 | print "matched" | ||
| 272 | else | ||
| 273 | print "not matched" | ||
| 274 | |||
| 275 | do | ||
| 276 | clientData = ["Meta", "CUST_1001", "CHK123"] | ||
| 277 | switch clientData | ||
| 278 | when [...metadata, customerId, checksum] | ||
| 279 | print metadata -- {"Meta"} | ||
| 280 | print customerId -- "CUST_1001" | ||
| 281 | print checksum -- "CHK123" | ||
| 282 | |||
| 283 | do | ||
| 284 | handlePath = (segments) -> | ||
| 285 | switch segments | ||
| 286 | when [..._, resource, action] | ||
| 287 | print "Resource:", resource | ||
| 288 | print "Action:", action | ||
| 289 | |||
| 290 | handlePath ["admin", "logs", "view"] | ||
| 291 | |||
| 292 | nil | ||
diff --git a/spec/inputs/syntax.yue b/spec/inputs/syntax.yue index a414d6f..eee518a 100644 --- a/spec/inputs/syntax.yue +++ b/spec/inputs/syntax.yue | |||
| @@ -392,7 +392,7 @@ invokeA( | |||
| 392 | v = { | 392 | v = { |
| 393 | a -1 | 393 | a -1 |
| 394 | a( | 394 | a( |
| 395 | -1) | 395 | -1) |
| 396 | a \ | 396 | a \ |
| 397 | - 1 | 397 | - 1 |
| 398 | a-1 | 398 | a-1 |
| @@ -405,7 +405,7 @@ v = { | |||
| 405 | 405 | ||
| 406 | a ~1 | 406 | a ~1 |
| 407 | a( | 407 | a( |
| 408 | ~1) | 408 | ~1) |
| 409 | a \ | 409 | a \ |
| 410 | ~ 1 | 410 | ~ 1 |
| 411 | a~1 | 411 | a~1 |
| @@ -478,5 +478,13 @@ do | |||
| 478 | f2 = -> | 478 | f2 = -> |
| 479 | -- | 479 | -- |
| 480 | 480 | ||
| 481 | do | ||
| 482 | return res if res ~= "" | ||
| 483 | |||
| 484 | |||
| 485 | do | ||
| 486 | return res if res ~= "" | ||
| 487 | -- | ||
| 488 | |||
| 481 | nil | 489 | nil |
| 482 | 490 | ||
diff --git a/spec/inputs/tables.yue b/spec/inputs/tables.yue index 0de8a8c..702e04a 100644 --- a/spec/inputs/tables.yue +++ b/spec/inputs/tables.yue | |||
| @@ -245,6 +245,24 @@ menus = | |||
| 245 | click: -> | 245 | click: -> |
| 246 | } | 246 | } |
| 247 | 247 | ||
| 248 | _ = | ||
| 249 | boolean: | ||
| 250 | - true | ||
| 251 | - false | ||
| 252 | float: | ||
| 253 | - 3.14 | ||
| 254 | - -6.8523015e+5 | ||
| 255 | int: | ||
| 256 | - 123 | ||
| 257 | - -0b1010_0111_0100_1010_1110 | ||
| 258 | null: | ||
| 259 | nodeName: 'node' | ||
| 260 | parent: nil | ||
| 261 | string: | ||
| 262 | - 'Hello world' | ||
| 263 | - "newline | ||
| 264 | newline2" | ||
| 265 | |||
| 248 | tb = {...other} | 266 | tb = {...other} |
| 249 | 267 | ||
| 250 | tbMix = { | 268 | tbMix = { |
diff --git a/spec/inputs/test/format_spec.yue b/spec/inputs/test/format_spec.yue index cbd9d22..3ad2c7f 100644 --- a/spec/inputs/test/format_spec.yue +++ b/spec/inputs/test/format_spec.yue | |||
| @@ -119,7 +119,7 @@ for file in *files | |||
| 119 | original_ast = yue.to_ast code | 119 | original_ast = yue.to_ast code |
| 120 | assert.is_not_nil original_ast | 120 | assert.is_not_nil original_ast |
| 121 | rewriteLineCol original_ast | 121 | rewriteLineCol original_ast |
| 122 | formated = yue.format code | 122 | formated = yue.format code, 0, false |
| 123 | ast = yue.to_ast formated | 123 | ast = yue.to_ast formated |
| 124 | assert.is_not_nil ast | 124 | assert.is_not_nil ast |
| 125 | rewriteLineCol ast | 125 | rewriteLineCol ast |
diff --git a/spec/inputs/try_catch.yue b/spec/inputs/try_catch.yue index 4e05bc6..6c29a52 100644 --- a/spec/inputs/try_catch.yue +++ b/spec/inputs/try_catch.yue | |||
| @@ -62,6 +62,126 @@ f = -> | |||
| 62 | do | 62 | do |
| 63 | <- x | 63 | <- x |
| 64 | local tb, a, b, c | 64 | local tb, a, b, c |
| 65 | f = -> try tb.f a, b, c | 65 | f1 = -> try tb.f a, b, c |
| 66 | |||
| 67 | do | ||
| 68 | f1 = -> do | ||
| 69 | ok, ... = try func! | ||
| 70 | ... | ||
| 71 | |||
| 72 | do | ||
| 73 | local func | ||
| 74 | a, b, c = try? func! | ||
| 75 | |||
| 76 | do | ||
| 77 | a, b, c = try? func! | ||
| 78 | |||
| 79 | do | ||
| 80 | a = (try? func!) ?? "default" | ||
| 81 | |||
| 82 | do | ||
| 83 | f try? func! | ||
| 84 | |||
| 85 | do | ||
| 86 | f try? | ||
| 87 | print 123 | ||
| 88 | func! | ||
| 89 | catch e | ||
| 90 | print e | ||
| 91 | e | ||
| 66 | 92 | ||
| 67 | nil | 93 | nil |
| 94 | |||
| 95 | do | ||
| 96 | try | ||
| 97 | func 1, 2, 3 | ||
| 98 | catch err | ||
| 99 | print err | ||
| 100 | |||
| 101 | try func 1, 2, 3 | ||
| 102 | catch err | ||
| 103 | print err | ||
| 104 | |||
| 105 | try | ||
| 106 | print "trying" | ||
| 107 | func 1, 2, 3 | ||
| 108 | |||
| 109 | do | ||
| 110 | success, result = try | ||
| 111 | func 1, 2, 3 | ||
| 112 | catch err | ||
| 113 | print err | ||
| 114 | |||
| 115 | success, result = try func 1, 2, 3 | ||
| 116 | |||
| 117 | tb = {} | ||
| 118 | |||
| 119 | try tb.func | ||
| 120 | try tb.func! | ||
| 121 | try tb.func() | ||
| 122 | try (tb.func!) | ||
| 123 | try (tb\func(1, 2, 3)) | ||
| 124 | |||
| 125 | try tb.func 1 | ||
| 126 | try tb.func(1) | ||
| 127 | |||
| 128 | if (try func 1 | ||
| 129 | catch err | ||
| 130 | print err) | ||
| 131 | print "OK" | ||
| 132 | |||
| 133 | if try (func 1) | ||
| 134 | catch err | ||
| 135 | print err | ||
| 136 | print "OK" | ||
| 137 | |||
| 138 | do | ||
| 139 | if success, result := try func "abc", 123 | ||
| 140 | print result | ||
| 141 | |||
| 142 | success, result = try func "abc", 123 | ||
| 143 | catch err | ||
| 144 | print err | ||
| 145 | |||
| 146 | print result if success, result := try func "abc", 123 | ||
| 147 | catch err | ||
| 148 | print err | ||
| 149 | |||
| 150 | do | ||
| 151 | try | ||
| 152 | func 1, 2, 3 | ||
| 153 | |||
| 154 | try func 1, 2, 3 | ||
| 155 | |||
| 156 | do | ||
| 157 | <- x | ||
| 158 | local tb, a, b, c | ||
| 159 | f1 = -> try tb.f a, b, c | ||
| 160 | |||
| 161 | do | ||
| 162 | f1 = -> do | ||
| 163 | ok, ... = try func! | ||
| 164 | ... | ||
| 165 | |||
| 166 | do | ||
| 167 | local func | ||
| 168 | a, b, c = try? func! | ||
| 169 | |||
| 170 | do | ||
| 171 | a, b, c = try? func! | ||
| 172 | |||
| 173 | do | ||
| 174 | a = (try? func!) ?? "default" | ||
| 175 | |||
| 176 | do | ||
| 177 | f try? func! | ||
| 178 | |||
| 179 | do | ||
| 180 | f try? | ||
| 181 | print 123 | ||
| 182 | func! | ||
| 183 | catch e | ||
| 184 | print e | ||
| 185 | e | ||
| 186 | |||
| 187 | nil | ||
diff --git a/spec/inputs/unicode/destructure.yue b/spec/inputs/unicode/destructure.yue index 3c3a369..a5ffd5d 100644 --- a/spec/inputs/unicode/destructure.yue +++ b/spec/inputs/unicode/destructure.yue | |||
| @@ -84,7 +84,7 @@ do | |||
| 84 | -- | 84 | -- |
| 85 | 85 | ||
| 86 | do | 86 | do |
| 87 | with {元素a,元素b} = 东西 | 87 | with {元素a,元素b} := 东西 |
| 88 | 打印 元素a, 元素b | 88 | 打印 元素a, 元素b |
| 89 | 89 | ||
| 90 | 90 | ||
diff --git a/spec/inputs/unicode/funcs.yue b/spec/inputs/unicode/funcs.yue index c563356..cb35500 100644 --- a/spec/inputs/unicode/funcs.yue +++ b/spec/inputs/unicode/funcs.yue | |||
| @@ -98,7 +98,7 @@ _无效变量 = -> 真名 if 某物 | |||
| 98 | 98 | ||
| 99 | 变量x = (参数a, | 99 | 变量x = (参数a, |
| 100 | 参数b) -> | 100 | 参数b) -> |
| 101 | 打印 "什么" | 101 | 打印 "什么" |
| 102 | 102 | ||
| 103 | 103 | ||
| 104 | 变量y = (参数a="hi", | 104 | 变量y = (参数a="hi", |
diff --git a/spec/inputs/unicode/import.yue b/spec/inputs/unicode/import.yue index c229edb..a7724ab 100644 --- a/spec/inputs/unicode/import.yue +++ b/spec/inputs/unicode/import.yue | |||
| @@ -23,7 +23,7 @@ do | |||
| 23 | 字段b, 字段c from 对象z | 23 | 字段b, 字段c from 对象z |
| 24 | 24 | ||
| 25 | do | 25 | do |
| 26 | import 字段a | 26 | import 字段a, |
| 27 | 字段b | 27 | 字段b |
| 28 | 字段c from 对象z | 28 | 字段c from 对象z |
| 29 | 29 | ||
diff --git a/spec/inputs/unicode/macro_export.yue b/spec/inputs/unicode/macro_export.yue index 3c9a942..56571cd 100644 --- a/spec/inputs/unicode/macro_export.yue +++ b/spec/inputs/unicode/macro_export.yue | |||
| @@ -37,8 +37,8 @@ export macro 复制 = (源, 目标, ...)-> | |||
| 37 | " | 37 | " |
| 38 | do | 38 | do |
| 39 | local _源_, _目标_ | 39 | local _源_, _目标_ |
| 40 | with _目标_ = #{目标} | 40 | with _目标_ := #{目标} |
| 41 | with _源_ = #{源} | 41 | with _源_ := #{源} |
| 42 | #{table.concat for 字段 in *{...} do " | 42 | #{table.concat for 字段 in *{...} do " |
| 43 | _目标_.#{字段} = _源_.#{字段} | 43 | _目标_.#{字段} = _源_.#{字段} |
| 44 | "}" | 44 | "}" |
diff --git a/spec/inputs/unicode/syntax.yue b/spec/inputs/unicode/syntax.yue index 8a98416..01d5c87 100644 --- a/spec/inputs/unicode/syntax.yue +++ b/spec/inputs/unicode/syntax.yue | |||
| @@ -376,7 +376,7 @@ with 对象 | |||
| 376 | 变量v = { | 376 | 变量v = { |
| 377 | 变量a -1 | 377 | 变量a -1 |
| 378 | 变量a( | 378 | 变量a( |
| 379 | -1) | 379 | -1) |
| 380 | 变量a \ | 380 | 变量a \ |
| 381 | - 1 | 381 | - 1 |
| 382 | 变量a-1 | 382 | 变量a-1 |
| @@ -389,7 +389,7 @@ with 对象 | |||
| 389 | 389 | ||
| 390 | 变量a ~1 | 390 | 变量a ~1 |
| 391 | 变量a( | 391 | 变量a( |
| 392 | ~1) | 392 | ~1) |
| 393 | 变量a \ | 393 | 变量a \ |
| 394 | ~ 1 | 394 | ~ 1 |
| 395 | 变量a~1 | 395 | 变量a~1 |
diff --git a/spec/inputs/unicode/vararg.yue b/spec/inputs/unicode/vararg.yue index e59e114..b508fbb 100644 --- a/spec/inputs/unicode/vararg.yue +++ b/spec/inputs/unicode/vararg.yue | |||
| @@ -55,12 +55,12 @@ | |||
| 55 | _ = -> | 55 | _ = -> |
| 56 | 列表 = {1, 2, 3, 4, 5} | 56 | 列表 = {1, 2, 3, 4, 5} |
| 57 | 函数名 = (确定) -> | 57 | 函数名 = (确定) -> |
| 58 | 确定, table.unpack 列表 | 58 | 确定, table.unpack 列表 |
| 59 | 确定, ... = 函数名 true | 59 | 确定, ... = 函数名 true |
| 60 | 打印 确定, ... | 60 | 打印 确定, ... |
| 61 | 61 | ||
| 62 | 多参数函数 = -> | 62 | 多参数函数 = -> |
| 63 | 10, nil, 20, nil, 30 | 63 | 10, nil, 20, nil, 30 |
| 64 | 64 | ||
| 65 | ... = 多参数函数! | 65 | ... = 多参数函数! |
| 66 | 打印 select "#", ... | 66 | 打印 select "#", ... |
diff --git a/spec/inputs/unicode/with.yue b/spec/inputs/unicode/with.yue index ecbfdab..3c15add 100644 --- a/spec/inputs/unicode/with.yue +++ b/spec/inputs/unicode/with.yue | |||
| @@ -45,19 +45,19 @@ do | |||
| 45 | with 变量a | 45 | with 变量a |
| 46 | 打印 .世界 | 46 | 打印 .世界 |
| 47 | 47 | ||
| 48 | 模块 = with _模块 = {} | 48 | 模块 = with _模块 := {} |
| 49 | .事物 = "你好" | 49 | .事物 = "你好" |
| 50 | 50 | ||
| 51 | with 变量a, 变量b = 东西, 布 | 51 | with 变量a, 变量b := 东西, 布 |
| 52 | 打印 .世界 | 52 | 打印 .世界 |
| 53 | 53 | ||
| 54 | 变量x = with 变量a, 变量b = 1, 2 | 54 | 变量x = with 变量a, 变量b := 1, 2 |
| 55 | 打印 变量a + 变量b | 55 | 打印 变量a + 变量b |
| 56 | 56 | ||
| 57 | 打印 with 变量a, 变量b = 1, 2 | 57 | 打印 with 变量a, 变量b := 1, 2 |
| 58 | 打印 变量a + 变量b | 58 | 打印 变量a + 变量b |
| 59 | 59 | ||
| 60 | p = with 你好!.字段x, 世界!.字段y = 1, 2 | 60 | p = with 你好!.字段x, 世界!.字段y := 1, 2 |
| 61 | 打印 变量a + 变量b | 61 | 打印 变量a + 变量b |
| 62 | 62 | ||
| 63 | -- | 63 | -- |
| @@ -68,16 +68,16 @@ do | |||
| 68 | 变量x\大写! | 68 | 变量x\大写! |
| 69 | 69 | ||
| 70 | do | 70 | do |
| 71 | with 变量k = "乔" | 71 | with 变量k := "乔" |
| 72 | 打印 \大写! | 72 | 打印 \大写! |
| 73 | 73 | ||
| 74 | do | 74 | do |
| 75 | with 变量a,变量b,变量c = "", "", "" | 75 | with 变量a,变量b,变量c := "", "", "" |
| 76 | 打印 \大写! | 76 | 打印 \大写! |
| 77 | 77 | ||
| 78 | do | 78 | do |
| 79 | 变量a = "床铺" | 79 | 变量a = "床铺" |
| 80 | with 变量a,变量b,变量c = "", "", "" | 80 | with 变量a,变量b,变量c := "", "", "" |
| 81 | 打印 \大写! | 81 | 打印 \大写! |
| 82 | 82 | ||
| 83 | do | 83 | do |
| @@ -85,7 +85,7 @@ do | |||
| 85 | 打印 \大写! | 85 | 打印 \大写! |
| 86 | 86 | ||
| 87 | do | 87 | do |
| 88 | with 变量k.变量j = "乔" | 88 | with 变量k.变量j := "乔" |
| 89 | 打印 \大写! | 89 | 打印 \大写! |
| 90 | 90 | ||
| 91 | do | 91 | do |
| @@ -96,7 +96,7 @@ do | |||
| 96 | 96 | ||
| 97 | do | 97 | do |
| 98 | with 变量a | 98 | with 变量a |
| 99 | with .b = 2 | 99 | with .b := 2 |
| 100 | 打印 .c | 100 | 打印 .c |
| 101 | 101 | ||
| 102 | do | 102 | do |
| @@ -131,12 +131,12 @@ do | |||
| 131 | 131 | ||
| 132 | do | 132 | do |
| 133 | global 掩码 | 133 | global 掩码 |
| 134 | with? 掩码 = 实心矩形 宽: w, 高: h, 颜色: 0x66000000 | 134 | with? 掩码 := 实心矩形 宽: w, 高: h, 颜色: 0x66000000 |
| 135 | .触摸启用 = true | 135 | .触摸启用 = true |
| 136 | .吞噬触摸 = true | 136 | .吞噬触摸 = true |
| 137 | 137 | ||
| 138 | do | 138 | do |
| 139 | with? 掩码 = 实心矩形 宽: w, 高: h, 颜色: 0x66000000 | 139 | with? 掩码 := 实心矩形 宽: w, 高: h, 颜色: 0x66000000 |
| 140 | .触摸启用 = true | 140 | .触摸启用 = true |
| 141 | .吞噬触摸 = true | 141 | .吞噬触摸 = true |
| 142 | 142 | ||
diff --git a/spec/inputs/vararg.yue b/spec/inputs/vararg.yue index 941cd97..4f8a0d7 100644 --- a/spec/inputs/vararg.yue +++ b/spec/inputs/vararg.yue | |||
| @@ -55,12 +55,12 @@ join = (...) -> | |||
| 55 | _ = -> | 55 | _ = -> |
| 56 | list = {1, 2, 3, 4, 5} | 56 | list = {1, 2, 3, 4, 5} |
| 57 | fn = (ok) -> | 57 | fn = (ok) -> |
| 58 | ok, table.unpack list | 58 | ok, table.unpack list |
| 59 | ok, ... = fn true | 59 | ok, ... = fn true |
| 60 | print ok, ... | 60 | print ok, ... |
| 61 | 61 | ||
| 62 | fn_many_args = -> | 62 | fn_many_args = -> |
| 63 | 10, nil, 20, nil, 30 | 63 | 10, nil, 20, nil, 30 |
| 64 | 64 | ||
| 65 | ... = fn_many_args! | 65 | ... = fn_many_args! |
| 66 | print select "#", ... | 66 | print select "#", ... |
| @@ -86,3 +86,47 @@ join = (...) -> | |||
| 86 | print ... | 86 | print ... |
| 87 | nil | 87 | nil |
| 88 | 88 | ||
| 89 | do | ||
| 90 | f1 = (...t) -> | ||
| 91 | print t.n | ||
| 92 | print #t | ||
| 93 | for i = 1, t.n | ||
| 94 | print t[i] | ||
| 95 | |||
| 96 | f1 1, 2, 3 | ||
| 97 | f1 "a", "b", "c", "d" | ||
| 98 | f1! | ||
| 99 | |||
| 100 | f2 = (...args) -> | ||
| 101 | print "args count:", args.n | ||
| 102 | print "args length:", #args | ||
| 103 | for i = 1, args.n | ||
| 104 | if args[i] == nil | ||
| 105 | print "position", i, "is nil" | ||
| 106 | else | ||
| 107 | print "position", i, ":", args[i] | ||
| 108 | |||
| 109 | f2 1, nil, 3, nil, 5 | ||
| 110 | |||
| 111 | f3 = (prefix, ...items) -> | ||
| 112 | result = {} | ||
| 113 | for i = 1, items.n | ||
| 114 | result[i] = prefix .. tostring items[i] | ||
| 115 | result | ||
| 116 | |||
| 117 | f3 "item_", 1, 2, 3 | ||
| 118 | |||
| 119 | f4 = (...empty) -> | ||
| 120 | print "empty count:", empty.n | ||
| 121 | print "empty length:", #empty | ||
| 122 | |||
| 123 | f4! | ||
| 124 | |||
| 125 | process = (...data) -> | ||
| 126 | sum = 0 | ||
| 127 | for i = 1, data.n | ||
| 128 | if type(data[i]) == "number" | ||
| 129 | sum += data[i] | ||
| 130 | sum | ||
| 131 | |||
| 132 | process 1, 2, 3, "skip", 5 | ||
diff --git a/spec/inputs/with.yue b/spec/inputs/with.yue index 3fee48e..19ed2a7 100644 --- a/spec/inputs/with.yue +++ b/spec/inputs/with.yue | |||
| @@ -48,21 +48,21 @@ do | |||
| 48 | with a -- only one value allowed | 48 | with a -- only one value allowed |
| 49 | print .world | 49 | print .world |
| 50 | 50 | ||
| 51 | mod = with _M = {} | 51 | mod = with _M := {} |
| 52 | .Thing = "hi" | 52 | .Thing = "hi" |
| 53 | 53 | ||
| 54 | -- operate on a only | 54 | -- operate on a only |
| 55 | with a, b = something, pooh | 55 | with a, b := something, pooh |
| 56 | print .world | 56 | print .world |
| 57 | 57 | ||
| 58 | x = with a, b = 1, 2 | 58 | x = with a, b := 1, 2 |
| 59 | print a + b | 59 | print a + b |
| 60 | 60 | ||
| 61 | print with a, b = 1, 2 | 61 | print with a, b := 1, 2 |
| 62 | print a + b | 62 | print a + b |
| 63 | 63 | ||
| 64 | -- assignment lhs must be evaluated in the order they appear | 64 | -- assignment lhs must be evaluated in the order they appear |
| 65 | p = with hello!.x, world!.y = 1, 2 | 65 | p = with hello!.x, world!.y := 1, 2 |
| 66 | print a + b | 66 | print a + b |
| 67 | 67 | ||
| 68 | -- | 68 | -- |
| @@ -73,16 +73,16 @@ do | |||
| 73 | x\upper! | 73 | x\upper! |
| 74 | 74 | ||
| 75 | do | 75 | do |
| 76 | with k = "jo" | 76 | with k := "jo" |
| 77 | print \upper! | 77 | print \upper! |
| 78 | 78 | ||
| 79 | do | 79 | do |
| 80 | with a,b,c = "", "", "" | 80 | with a,b,c := "", "", "" |
| 81 | print \upper! | 81 | print \upper! |
| 82 | 82 | ||
| 83 | do | 83 | do |
| 84 | a = "bunk" | 84 | a = "bunk" |
| 85 | with a,b,c = "", "", "" | 85 | with a,b,c := "", "", "" |
| 86 | print \upper! | 86 | print \upper! |
| 87 | 87 | ||
| 88 | do | 88 | do |
| @@ -90,7 +90,7 @@ do | |||
| 90 | print \upper! | 90 | print \upper! |
| 91 | 91 | ||
| 92 | do | 92 | do |
| 93 | with k.j = "jo" | 93 | with k.j := "jo" |
| 94 | print \upper! | 94 | print \upper! |
| 95 | 95 | ||
| 96 | do | 96 | do |
| @@ -103,7 +103,7 @@ do | |||
| 103 | do | 103 | do |
| 104 | with a | 104 | with a |
| 105 | -- nested `with`s with assignments should change the scope correctly | 105 | -- nested `with`s with assignments should change the scope correctly |
| 106 | with .b = 2 | 106 | with .b := 2 |
| 107 | print .c | 107 | print .c |
| 108 | 108 | ||
| 109 | do | 109 | do |
| @@ -138,12 +138,12 @@ do | |||
| 138 | 138 | ||
| 139 | do | 139 | do |
| 140 | global mask | 140 | global mask |
| 141 | with? mask = SolidRect width: w, height: h, color: 0x66000000 | 141 | with? mask := SolidRect width: w, height: h, color: 0x66000000 |
| 142 | .touchEnabled = true | 142 | .touchEnabled = true |
| 143 | .swallowTouches = true | 143 | .swallowTouches = true |
| 144 | 144 | ||
| 145 | do | 145 | do |
| 146 | with? mask = SolidRect width: w, height: h, color: 0x66000000 | 146 | with? mask := SolidRect width: w, height: h, color: 0x66000000 |
| 147 | .touchEnabled = true | 147 | .touchEnabled = true |
| 148 | .swallowTouches = true | 148 | .swallowTouches = true |
| 149 | 149 | ||
| @@ -161,4 +161,13 @@ do | |||
| 161 | if .v | 161 | if .v |
| 162 | break .a | 162 | break .a |
| 163 | 163 | ||
| 164 | a = while true | ||
| 165 | break with? tb | ||
| 166 | break 1 | ||
| 167 | |||
| 168 | do | ||
| 169 | a = for i = 1, 100 | ||
| 170 | with? x := tb[i] | ||
| 171 | break x if .id := 1 | ||
| 172 | |||
| 164 | nil | 173 | nil |
