11# Evy Syntax by Example
22
3- The following examples on various aspects of ` evy ` 's syntax give an
4- intuitive understanding of the language . For a formal language
5- specification see the [ syntax grammar] .
3+ The following examples give an intuitive understanding of various
4+ aspects of ` evy ` 's syntax . For a formal language specification see the
5+ [ syntax grammar] ( syntax_grammar.md ) .
66
7- [ syntax grammar ] : syntax_grammar.md
87
98## Comment
109
1110 // This is comment
1211
1312## Declaration
1413
15- x:num // declaration: num, string, bool, any, [] {}
14+ x:num // declaration: num, string, bool, any, []num, {}string
1615 y := 1 // declaration through type inference (num)
1716
1817## Assignment
@@ -55,8 +54,6 @@ specification see the [syntax grammar].
5554
5655## Loop statements
5756
58-
59-
6057### ` while ` loop
6158
6259 x := 0
@@ -80,18 +77,18 @@ specification see the [syntax grammar].
8077 end
8178
8279 for x := range -10
83- print x // nothing. step always 1 by default.
80+ print x // nothing. step is 1 by default.
8481 end
8582
8683### ` for ` … ` range ` array
8784
88- for x := range num[ 1 2 3 ]
85+ for x := range [ 1 2 3]
8986 print x // 1 2 3
9087 end
9188
9289### ` for ` … ` range ` map
9390
94- m := string { name:"Mali" sport:"climbing" }
91+ m := { name:"Mali" sport:"climbing" }
9592 for key := range m
9693 print key m[key]
9794 end
@@ -133,53 +130,63 @@ specification see the [syntax grammar].
133130
134131## Array
135132
136- a1:num[]
137- a2:string[][]
138- a1 = num[ 1 2 3 4 ]
139- a2 = string[ ["1" "2"] ["a" "b"] ]
140- a3 := bool[ true false ]
141- a4 := num[ "s1"
142- "s2" ] // linebreak allowed
143- a5 := any[ "chars" 123 ]
133+ a1:[]num
134+ a2:[][]string
135+ a1 = [1 2 3 4] // type: num[]
136+ a2 = [["1" "2"]["a" "b"]] // type: string[][]
137+ a3 := [true false] // type: bool[]
138+ a4 := ["s1" // line break allowed
139+ "s2"] // type: string[]
140+ a5 := ["chars" 123] // type: any[]
141+ a6:[]any // type: any[]
144142
145143### Array element access
146144
145+ a1 := [1 2 3 4]
146+ a2 := [["1" "2"]["a" "b"]]
147147 print a1[1] // 2
148148 print a2[1][0] // "a"
149149
150150### Concatenation, append, prepend
151151
152- z = z + num[ 100 ] // [ 1 2 3 4 5 6 100 ]
153- z = append z 101 // [ 1 2 3 4 5 6 100 101 ]
154- z = prepend z 0 // [ 0 1 2 3 4 5 6 100 101 ]
152+ z = z + [ 100 ] // z: [1 2 3 4 5 6 100]; optional extra whitespace
153+ z = z + [101] // z: [1 2 3 4 5 6 100 101]
154+ append z 102 // z: [1 2 3 4 5 6 100 101 102]
155+ prepend z 0 // z: [0 1 2 3 4 5 6 100 101 102]
155156
156157### Slicing
157158
158- x1 := x[:2] // [ 1 2 ]
159+ x := [1 2 3]
160+ x1 := x[:2] // [1 2]
159161 x2 = x[2] // [3]
160162 x2 = x[1:2] // [2]
161- x2 = x[-1] // [3]`
162- x2 = x[-2:] // [ 2 3 ]
163+ x2 = x[-1] // [3]
164+ x2 = x[-2:] // [2 3]
163165
164166## Map
165167
166- m:any{} // keys can only be identifiers
167- m .name = "fox"
168- m .age = 42
168+ m1:{}any // keys can only be identifiers, any value allowed
169+ m2 .name = "fox"
170+ m2 .age = 42
169171
170- m1 := string{ letters:"abc" nums:"asdf" }
171- m2 := {} // short for any{}
172- m3 := any {
173- letters:"abc"
172+ m3 := { letters:"abc" name:"Jill"} // type: {}string
173+ m4 := {} // type: {}any
174+ m5 := {
175+ letters:"abc" // line break allowed
174176 nums:123
175- } // linebreak allowed
177+ } // type: {}any
178+ m6:{}[]num // map of array of numbers
179+ m6.digits = [1 2 3]
180+ m7:{}num
181+ m7.x = "y" // invalid, only num values allows
176182
177183### Map value access
178184
185+ m := {letters:"abc" name:"Jill"}
179186 s := "letters"
180- print m2 .letters // abc
181- print m2 [s] // abc
182- print m2 ["letters"] // abc
187+ print m .letters // abc
188+ print m [s] // abc
189+ print m ["letters"] // abc
183190
184191### Map value existence
185192
@@ -190,33 +197,34 @@ specification see the [syntax grammar].
190197
191198## Any
192199
193- x:any // any type
194- m1:{} // any map value type
200+ x:any // any type
201+ m1:{}any // map with any value type
195202 m2 := { letter:"a" number:1 }
196- arr := any[ "b" 2 ]
203+ arr1:[]any
204+ arr2 := [ "b" 2 ]
197205
198206## Type reflection
199207
200- reflect "abc" // {type: "string"}
201- reflect true // {type: "bool"}
202- reflect num [ 1 2 ] // {type: "array",
203- // sub: {type: "num"}
204- // }
205- reflect num[ [1 2] [3 4] ] // {
206- // type: "array",
207- // sub: {
208- // type: "array"
209- // sub: {
210- // type: "num"
211- // }
212- // }
213- // }
208+ reflect "abc" // {type: "string"}
209+ reflect true // {type: "bool"}
210+ reflect [ 1 2 ] // {type: "array",
211+ // sub: {type: "num"}
212+ // }
213+ reflect [ [1 2] [3 4]] // {
214+ // type: "array",
215+ // sub: {
216+ // type: "array"
217+ // sub: {
218+ // type: "num"
219+ // }
220+ // }
221+ // }
214222
215223### Type reflection Usage Example
216224
217225 v:any
218226 v = "asdf"
219- if (reflect v).type == "string"
227+ if (reflect v) == {type: "string"}
220228 print "v is a string:" v
221229 end
222230
@@ -230,8 +238,8 @@ specification see the [syntax grammar].
230238## Type assertion
231239
232240 x:any
233- x = num [ 1 2 3 4 ] // concrete type num[]
234- s := num[] x
241+ x = [ 1 2 3 4 ] // concrete type num[]
242+ s := x.([]num)
235243
236244## Variadic functions
237245
@@ -249,7 +257,7 @@ specification see the [syntax grammar].
249257
250258## Event handling
251259
252- on animate
260+ on frame
253261 draw
254262 end
255263
@@ -265,16 +273,22 @@ specification see the [syntax grammar].
265273
266274### Print
267275
268- print "abc" 123 // abc 123 \n
276+ print "abc" 123 // abc 123\n
269277 prints "abc" 123 // print string: abc123
270- printq "abc" // print quoted, reuse as value: "abc"
278+ printq "abc" 123 // print quoted, reuse as value: "abc"
279+
280+ Returning a string:
281+
282+ sprint "abc" 123 // returns "abc 123\n"
283+ sprints "abc" 123 // returns "abc123"
284+ sprintq "abc" // returns "\"abc\""
271285
272286### Strings
273287
274288 "Hello"[2] // "l"
275289 "Hello world"[1:5] // "ello"
276290 join [ "one" "two" ] ", " // "one, two"
277- split "hi there" " " // [ "hi" "there" ]
291+ split "hi there" // [ "hi" "there" ]
278292
279293### Length
280294
@@ -285,6 +299,13 @@ specification see the [syntax grammar].
285299 append x 100 // [ 1 2 3 100 ]
286300 prepend x -100 // [ -100 1 2 3 100 ]
287301
302+ ### Maps
303+
304+ m := {name: "abc"}
305+ has m "abc" // true
306+ del m "abc"
307+ has m "abc" // false
308+
288309### Conversion
289310
290311 str2bool "true" // true
@@ -303,9 +324,9 @@ specification see the [syntax grammar].
303324
304325 now // return unix time in seconds
305326 format_time now // "2022-08-28T23:59:05Z" Z is time zone zero
306- format_time2 now "06/01/02 15:04" // "22/01/02 15:04"
327+ format_timef now "06/01/02 15:04" // "22/01/02 15:04"
307328 parse_time "2022-08-28T23:59:05Z" // internal representation as unix seconds
308- parse_time2 value format
329+ parse_timef value format
309330 sleep 10 // sleep 10 seconds
310331
311332See Go's [ time.Layout] for further details on formatting and parsing.
@@ -327,9 +348,9 @@ No support for custom events.
327348 circle radius
328349 line end_x end_y
329350 rect width height
330- curve ?
331- polygon num[ x1 y2 x2 y2 x3 y3 ]
332- polyline num[ x1 y1 x2 y2 x3 y3 ]
351+ curve // TBD
352+ polygon [ x1 y2] [ x2 y2] [ x3 y3]
353+ polyline [ x1 y1] [ x2 y2] [ x3 y3]
333354
334355 color 900 // CSS #ff0000
335356 colors "red" // CSS color keywords: #ff0000
0 commit comments