diff options
author | Li Jin <dragon-fly@qq.com> | 2022-07-12 17:41:19 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2022-07-12 17:41:19 +0800 |
commit | 68e167e9f0b90968ea67b7f21fdc50a48d129173 (patch) | |
tree | 64612476c7cc5636d7a9eea68ea1e5b449bb9732 /spec/inputs | |
parent | 1a7c8e3c38fcf0af94ca799a3cd9fa7c4ef1bf08 (diff) | |
download | yuescript-68e167e9f0b90968ea67b7f21fdc50a48d129173.tar.gz yuescript-68e167e9f0b90968ea67b7f21fdc50a48d129173.tar.bz2 yuescript-68e167e9f0b90968ea67b7f21fdc50a48d129173.zip |
add table pattern matching syntax and fix issue #93, remove a confusing default value syntax for destructuring.
Diffstat (limited to 'spec/inputs')
-rw-r--r-- | spec/inputs/destructure.yue | 3 | ||||
-rw-r--r-- | spec/inputs/switch.yue | 86 |
2 files changed, 88 insertions, 1 deletions
diff --git a/spec/inputs/destructure.yue b/spec/inputs/destructure.yue index a235abd..3007adf 100644 --- a/spec/inputs/destructure.yue +++ b/spec/inputs/destructure.yue | |||
@@ -181,3 +181,6 @@ do | |||
181 | do | 181 | do |
182 | {_, a, _, b} = tb -- list placeholder | 182 | {_, a, _, b} = tb -- list placeholder |
183 | 183 | ||
184 | do | ||
185 | {x: a.b = 1, y: a.c = 2} = x.x.x | ||
186 | |||
diff --git a/spec/inputs/switch.yue b/spec/inputs/switch.yue index ac3dbea..36f9be6 100644 --- a/spec/inputs/switch.yue +++ b/spec/inputs/switch.yue | |||
@@ -58,7 +58,91 @@ switch hi | |||
58 | 58 | ||
59 | switch hi | 59 | switch hi |
60 | when 3+1, hello!, (-> 4)! | 60 | when 3+1, hello!, (-> 4)! |
61 | yello | 61 | _ = yello |
62 | else | 62 | else |
63 | print "cool" | 63 | print "cool" |
64 | 64 | ||
65 | do | ||
66 | dict = { | ||
67 | {} | ||
68 | {1, 2, 3} | ||
69 | a: b: c: 1 | ||
70 | x: y: z: 1 | ||
71 | } | ||
72 | |||
73 | switch dict | ||
74 | when { | ||
75 | first | ||
76 | {one, two, three} | ||
77 | a: b: :c | ||
78 | x: y: :z | ||
79 | } | ||
80 | print first, one, two, three, c, z | ||
81 | |||
82 | do | ||
83 | items = | ||
84 | * x: 100 | ||
85 | y: 200 | ||
86 | * width: 300 | ||
87 | height: 400 | ||
88 | * false | ||
89 | |||
90 | for item in *items | ||
91 | switch item | ||
92 | when :x, :y | ||
93 | print "Vec2 #{x}, #{y}" | ||
94 | when :width, :height | ||
95 | print "Size #{width}, #{height}" | ||
96 | when false | ||
97 | print "None" | ||
98 | when __class: cls | ||
99 | switch cls | ||
100 | when ClassA | ||
101 | print "Object A" | ||
102 | when ClassB | ||
103 | print "Object B" | ||
104 | when #: mt | ||
105 | print "A table with metatable" | ||
106 | else | ||
107 | print "item not accepted!" | ||
108 | |||
109 | do | ||
110 | tb = {} | ||
111 | switch tb | ||
112 | when {:a = 1, :b = 2} | ||
113 | print a, b | ||
114 | |||
115 | do | ||
116 | tb = x: "abc" | ||
117 | switch tb | ||
118 | when :x, :y | ||
119 | print "x: #{x} with y: #{y}" | ||
120 | when :x | ||
121 | print "x: #{x} only" | ||
122 | |||
123 | do | ||
124 | matched = switch tb | ||
125 | when 1 | ||
126 | "1" | ||
127 | when :x | ||
128 | x | ||
129 | when false | ||
130 | "false" | ||
131 | else | ||
132 | nil | ||
133 | |||
134 | do | ||
135 | return switch tb | ||
136 | when nil | ||
137 | "invalid" | ||
138 | when :a, :b | ||
139 | "#{a + b}" | ||
140 | when 1, 2, 3, 4, 5 | ||
141 | "number 1 - 5" | ||
142 | when {:alwaysMatch = "fallback"} | ||
143 | alwaysMatch | ||
144 | else | ||
145 | "should not reach here" | ||
146 | |||
147 | nil | ||
148 | |||