Syntax by Example
The following examples will help you understand the syntax of Evy. We also have a one page Cheat Sheet that you can print and keep handy.
For a more formal definition of the syntax, see the
Language Specification. Built-in functions, such as
print
and circle
, are documented in the
Built-ins section.
#Comment
// This is a comment
#Declaration
x:num // or string, bool, any, []num, {}string
y := 1 // type inference (num)
print x y // 0 1
#Assignment
z:num
z = 5
print z // 5
#Expression
Arithmetic, number expressions
pi := 3.1415
x := 5 * (3 + pi) - 2 / 7.6
print x // 30.44434210526316
Logical, boolean expressions
trace := false
debug := true
level := "error"
b := !trace and debug or level == ""
print b // true
#Strings
Concatenation, indexing and slicing
str := "abc" + "๐ฅช123" // "abc๐ฅช123" - concatenation
s2 := str[0] // "a" - indexing
s3 := str[1:5] // "bc๐ฅช1" - slicing
print str s2 s3
Newline, indentation and escaping
str := "newline: \n indentation: \t"
print str
print "quotation mark : \" " // escaping
#if
statements
x := 6
if x > 10
print "huge"
else if x > 5
print "medium"
else
print "small"
end
#Nested if
str := "abc"
if (len str) > 2
if (startswith str "a")
print "string starting with 'a'"
else
print "string not starting with 'a'"
end
else
print "single character or empty string"
end
#Loop statements
#while
loop
x := 0
while x < 10
print x // 0 1 2 ... 9
x = x + 1
end
#for
โฆ
range
number
for x := range 5
print x // 0 1 2 3 4
end
for x := range 5 10
print x // 5 6 7 8 9
end
for x := range 1 10 2 // from to step
print x // 1 3 5 7 9
end
for x := range -10
print x // nothing. step is 1 by default.
end
#for
โฆ
range
array
for x := range [1 2 3]
print x // 1 2 3
end
#for
โฆ
range
map
m := {name:"Mali" sport:"climbing"}
for key := range m
print key m[key]
end
#break
x := 0
while true
print "tick... "
sleep 1
if x > 2
print "๐ฅ"
break // breaks out of the innermost loop
end
x = x + 1
end
#Function definition
func add:num a:num b:num
return a + b
end
#No return type
func foxprint s:string
print "๐ฆ "+s
end
#Variadic
func list args:any...
for arg := range args[:-1]
printf "%v, " arg
end
printf "%v" args[-1]
end
#Function calls
n := add 1 2
print n // 3
foxprint "๐พ" // ๐ฆ ๐พ
list 2 true "blue" // [2 true blue]
// previous function definitions
func add:num a:num b:num
return a + b
end
func foxprint s:string
print "๐ฆ "+s
end
func list args:any...
print args
end
#Array
Typed declaration
a1:[]num
a2:[][]string
a1 = [1 2 3 4] // type: num[]
a2 = [["1" "2"] ["a" "b"]] // type: string[][]
print a1 a2
Declaration with inference
a1 := [true false] // type: bool[]
a2 := ["s1" // line break allowed
"s2"] // type: string[]
print a1 a2
any
arrays
a1:[]any
a2 := ["chars" 123] // type: any[]
print a1 a2
#Array element access
a1 := [1 2 3 4]
a2 := [["1" "2"] ["a" "b"]]
print a1[1] // 2
print a2[1][0] // "a"
print a1[-1] // 4
#Concatenation
a := [1 2 3 4]
a = a + [100] // [1 2 3 4 100]; optional extra whitespace
a = [0] + a + [101 102] // [0 1 2 3 4 100 101 102]
#Repetition
a := [0] * 5 // [0 0 0 0 0]
a = [1 2] * 2 + a + [3] * 3 // [1 2 1 2 0 0 0 0 0 3 3 3]
n := 3
b := ["hello"] * n
print b // ["hello" "hello" "hello"]
#Slicing
a := [1 2 3]
b := a[:2] // [1 2]
b = a[1:2] // [2]
b = a[-2:] // [2 3]
#Map
Any map
m:{}any // keys used in literals or with `.` must be identifiers.
m.name = "fox"
m.age = 42
m["key with space"] = "๐๐ช"
print m // {name:fox age:42 key with space:๐๐ช}
Typed map
m1 := {letters:"abc" name:"Jill"} // type: {}string
m2 := {
letters:"abc" // line break allowed
name:"Jill"
}
print m1 m2
Empty map
m1:{}string // {}string
m2 := {} // {}any
print m1 m2 // {} {}
Nested map
m1:{}[]num
m2 := {a:{}}
print m1 m2 // {} {a:{}}
#Map value access
m := {letters:"abc" name:"Jill"}
s := "letters"
print m.letters // abc
print m[s] // abc
print m["letters"] // abc
#any
Zero value of any is false
.
x:any
m1:{}any
m2 := {letter:"a" number:1} // {}any
print x m1 m2 // false {} {letter:a number:1}
a1:[]any
a2 := ["b" 2] // []any
print a1 a2 // [] [b 2]
#Type inspection with typeof
print (typeof "abc") // "string"
print (typeof true) // "bool"
print (typeof [1 2]) // "[]num"
print (typeof [[1 2] [3 4]]) // "[][]num"
#Type assertion
x:any
print x (typeof x) // flase bool
x = [1 2 3 4]
s := x.([]num) // type assertion
print s (typeof s) // [1 2 3 4] []num
#Type inspection and assertion
v:any
v = "๐"
if (typeof v) == "string"
s := v.(string) // type assertion
print s+s // ๐๐
end
#Event handling
on key k:string
print "key:" k
end
Evy can only handle a limited set of events, such as key presses, pointer movements, or periodic screen redraws.