summaryrefslogtreecommitdiffhomepage
path: root/easylang.html.markdown
blob: ba33dbf2455e5b6086875b975c940cd65c7883c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
---
language: Easylang
contributors:
    - ["chkas", "https://github.com/chkas"]
filename: easylang.el
---

**Easylang** is a simple programming language with built-in graphical functions and an easy-to-use and offline usable browser IDE. Its simple syntax and semantics make it well suited as a teaching and learning programming language. You can also use it to write graphical applications that you can embed in a web page. 

*Easylang* is statically typed and has as data types only strings and numbers (floating point), resizeable arrays of strings and numbers and arrays of arrays.

[The browser IDE](https://easylang.online/ide/) includes various tutorials, including one for beginners.

```
print "Hello world"
#
# number variable (64 bit floating point)
#
h = 3.14
print h
#
# string variable
#
str$ = "monkey"
# strings can grow
str$ &= " circus" 
print str$
#
# blocks end with 'end' or a dot, a newline has no
# other meaning than a space
#
for i = 1 to 5
  sum += i * i
.
print sum
#
# functions have value and reference
# parameters, no return values
#
func gcd a b . res .
  # a and b are value parameters
  # res is a reference parameter
  while b <> 0
    # h is a local variable, because 
    # it is first used in the function
    h = b
    b = a mod b
    a = h
  .
  res = a
.
call gcd 120 35 r
print r
#
# strings can be concatenated and numbers are
# automatically converted to strings
#
print "1 + 2 = " & 1 + 2
#
# array of numbers
#
a[] = [ 2.1 3.14 3 ]
#
# arrays can grow
a[] &= 4
print a[]
#
# arrays, strings and numbers are copied by value
#
b[] = a[]
a[] &= 4
print a[] ; print b[]
#
# array swapping ist fast
#
swap a[] b[]
print a[] ; print b[]
# 
# array of strings
#
fruits$[] = [ "apple" "banana" "orange" ]
#
# for-in iterates over the elements of an array
#
for fruit$ in fruits$[]
  print fruit$
.
#
# strings are also used for single characters
#
letters$[] = str_chars "ping"
print letters$[]
letters$[1] = "o"
print str_join letters$[]
#
# 2-dimensional arrays are arrays of arrays
# this defines 3 arrays with length 4
#
len a[][] 3
for i range len a[][]
  len a[i][] 4
.
a[1][2] = 99
print a[][]
#
# builtin functions
if sin 90 = 1
  print "angles are in degree"
.
print pow 2 8
# seconds since 1970
print floor sys_time
# random numbers
print randomf
print random 6 + 1
# 
# hour and minutes
print substr time_str sys_time 11 5
# 
print str_ord "A"
print str_chr 65
# 
# set number format
numfmt 0 4
print sqrt 2
print pi
print logn 10
# 
a$[] = str_split "10,15,22" ","
print a$[]
print 2 * number a$[0]
print len a$[]
print len "Hello"
#
# With 'break n' you can leave nested loops and a function
#
names$[] = [ ]
func name2id name$ . id .
  for id range len names$[]
    if names$[id] = name$
      # leave loop and function
      break 2
    .
  .
  names$[] &= name$
.
call name2id "alice" id ; print id
call name2id "bob" id ; print id
call name2id "alice" id ; print i
#
# with 'repeat' you can make loops, which you can leave
# in the loop body using 'until'
#
sum = 0
repeat
  s$ = input
  until s$ = ""
  sum += number s$
.
print "sum: " & sum
#
# "input" reads a string from the "input_data" section, 
# if it exists, otherwise via a prompt.
#
input_data
10
-2
6
```

Built-in graphic primitives and event-driven programming

```
# simple drawing with the mouse
# 
set_linewidth 4
set_color 900
# the colors are coded from 0 to 999, with 
# the left digit specifying the red component,
# the middle digit the green component and
# the right digit the blue component. 
# 
on mouse_down
  down = 1
  move_pen mouse_x mouse_y
  # moves the drawing pen to the actual mouse position
  draw_circle 2
.
on mouse_up
  down = 0
.
on mouse_move
  if down = 1
    draw_line mouse_x mouse_y
  .
.
```

```
# an animated pendulum
#
on animate
  # The animate event occurs after each screen refresh.
  #
  clear_screen
  move_pen 50 50
  draw_circle 1
  x = 50 + 40 * sin ang
  y = 50 - 40 * cos ang
  draw_line x y
  draw_circle 5
  vel += sin ang / 5
  ang += vel
.
ang = 10
```

* [More about Easylang](https://easylang.online/)

* [Source code](https://github.com/chkas/easylang)