-
Notifications
You must be signed in to change notification settings - Fork 23
/
primer.en.robomarkup
387 lines (281 loc) · 8.07 KB
/
primer.en.robomarkup
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Pseudo markup format see ./README.md
#!unlock type{primer} id{loops}
# Loops
`loop` is used to repeat commands. It will repeat the commands in its scope forever. A loop's scope is the commands indented that come after a `loop` line.
```max_run=11
robo_forward() # called once
loop
robo_forward()
robo_left()
robo_left() # never called
```
#!unlock type{primer} id{comments}
# Comments
Text after a '`#`' symbol will not be used as code, these are just comments used to make notes.
```no_run
robo_left() # I think I'm starting to get this
```
#!unlock type{primer} id{conditionals}
# Conditionals
`if` can be used to execute commands, or not, based on a condition.
```
if robo_scan() is 1
# a tile is in front
robo_forward()
if robo_scan() is -1
# no tile is in front
robo_left()
```
#!unlock type{primer} id{variables}
# Variables
You can save a number for use later in a `var`.
```
# save a function result into a variable 'scan'
var scan = robo_scan()
# use & re-use the saved variable in conditions
if scan is 1
robo_forward()
if scan is -1
robo_left()
```
This can be used to avoid having to call a slow function multiple times just to check the same result. It can also allow saving a result for use much later.
#!unlock type{primer} id{conditionals-2}
# Conditions II
'`if expr`' simply checks if `expr` is non-zero. So `0` is interpreted as "false" while `1` or any other non-zero is "true". When `expr` is interpreted as "true" the indented commands will run.
```
if 0
robo_left() # not called
if 1
robo_forward() # called
```
#!unlock type{primer} id{is}
The keyword `is` compares two values producing `1` (true) when the values are the same or `0` (false) otherwise. This works naturally with `if`.
`not` can also be used to convert a `0` to a `1`, or any non-zero to `0`.
`not x is y ` may also be written ` x is not y ` producing `1` (true) when the values differ.
```
var scan = robo_scan()
if scan is 1
robo_forward()
if scan is not 1
robo_left()
```
#!unlock type{primer} id{comparison}
# Comparison
In addition to `is`, values can be compared with `>`, `>=`, `<`, `<=` operators.
` a > b # a is greater than b`
` a >= b # a is greater than or equal to b`
` a < b # a is less than b`
` a <= b # a is less than or equal to b`
```
var scan = robo_scan()
if scan >= 1
robo_forward()
if scan < 0
robo_left()
```
#!unlock type{primer} id{conditionals-3}
# Conditions III
After an `if` you can use `else` to deal with the 'otherwise' cases more easily.
An `else` block runs when the `if` above it doesn't.
```
var scan = robo_scan()
if scan is 1
robo_forward()
else
robo_left()
```
#!unlock type{primer} id{else-if}
Multiple `else if` can be chained together to deal with many possible exclusive cases.
This ensures only one indented block will be executed.
```
var example = 14
if example is 1
robo_left()
robo_forward()
else if example <= 3
robo_forward()
else if example > 12
robo_forward()
robo_forward()
else
robo_left()
```
An `else` section at the end will only run if each `if` and `else if` test above it fails.
#!unlock type{primer} id{scope}
# Scope
Indented code, like those that come after `loop` or `if` lines, represent "scope". A `var` can only be used in the scope it was declared in.
```no_run
if scan is 1
var foo = 123
if foo is 123 # works
robo_left()
if foo is 123 # fails! not available in this outer scope
robo_left()
```
To make a `var` more widely available it can be declared an outer scope. So, for example, top-level variables are accessible everywhere and will not be removed.
```no_run
var foo
if scan is 1
foo = 123
if foo is 123 # works
robo_left()
if foo is 123 # works
robo_left()
```
Variables are removed at the end of their scope, so `var` usage in a `loop` will result in a fresh variable for each iteration.
#!unlock type{primer} id{loops-2}
# Loops II
The `continue` keyword can be used to restart a loop mid-way, while the `break` keyword can be used to exit the current loop.
```
var count = 0
loop
if count is not 2
robo_forward()
count += 1
continue
break
robo_left()
robo_left()
```
#!unlock type{primer} id{loops-3}
# Loops III
Consider this use of `loop` that turns left 5 times.
```
# turn left 5 times
var loop_count = 1
loop
if loop_count > 5
break
robo_left()
loop_count = loop_count + 1
```
While any kind of looping logic can be written with `loop`, keywords `for` & `while` are also available and can help create simpler code that achieves the same result.
```
# execute block until while condition is false
var loop_count = 1
while loop_count <= 5
robo_left()
loop_count = loop_count + 1
```
```
# execute block with loop_count 1, 2, 3, 4 & 5
for loop_count in 1,2,3,4,5
robo_left()
```
#!unlock type{primer} id{fun}
# Functions
The `robo_left()`, `robo_scan()`, etc commands are functions. A function is a block of code that can be run later. They are defined with the `fun` keyword.
```
# define a function
fun go_right()
robo_left()
robo_left()
go_right() # call it
go_right()
```
#!unlock type{primer} id{fun-b}
Functions are useful for reducing repeated code and putting a name to a more complex operation.
Functions can also call other functions. For example you could keep a count of `robo_left()` calls by wrapping it in another function and using that.
```
var left_count
fun left_and_count()
robo_left()
left_count += 1 # increment the count
while left_count < 3
left_and_count()
```
#!unlock type{primer} id{fun-2}
# Functions II
Functions can also take in numbers and/or output a number.
```
fun loneliest_number()
1 # last expression is returned
# functions can take arguments
fun largest(a, b)
if b > a
# can return early
return b
a
var three = largest(2, 3)
var one = largest(-5, loneliest_number())
```
#!unlock type{primer} id{bool}
# Boolean operators
The `and` & `or` operators can be used to combine zero & non-zero values. They can be used to produce more complex logical statements.
An '`and`' expression returns `1` if both sides are non-zero.
```
var scan = robo_scan()
var adjacent = robo_detect_adjacent()
if scan < 0 and adjacent is 3
robo_forward()
```
An '`or`' expression returns `1` if either side is non-zero.
```
var scan = robo_scan()
var safe = scan is 1 or scan is 2
if safe
robo_forward()
```
#!unlock type{primer} id{seq}
# Sequences
`seq` is used to define a sequence of values. The values within can be accessed and modified by their index, starting with `0`.
```
seq list[] # new empty seq
seq numbers[] = 11, 22, 33
var first = numbers[0]
var third = numbers[2]
numbers[1] = 77 # overwrite index 1 to 77 (was 22)
if numbers[1] > 40
robo_left()
```
#!unlock type{primer} id{seq-b}
The `add(sv)` function is used to add additional values to the end of the sequence. `remove(sv)` to remove a value by index. `size(s)` returns the number of values in the sequence.
```
seq list[]
var size = list[].size()
list[].add(11) # add to end
list[].add(33)
var el = list[0]
var size = list[].size()
list[].remove(0) # remove index 0
var el = list[0]
var size = list[].size()
```
#!unlock type{primer} id{loop-seq}
# Looping sequences
`for` can be used to loop over each value inside a sequence.
```
seq nums[] = 1, 234, 55, -23
for number in nums[]
if number < 100
robo_use()
else
robo_left()
```
#!unlock type{primer} id{fun-3}
# Functions III
Functions can have `seq` arguments.
```
# function with a single seq argument
fun sum(list[])
var sum
for n in list[]
sum += n
sum
seq nums[] = 1, 2, 3
var total = sum(nums[])
total is 6
```
#!unlock type{primer} id{dot-call}
There is another way to call functions with arguments.
```no_run
fun example(a, b, c, d)
# ...
example(2, 3, 4, 5) # normal call syntax
2.example(3, 4, 5) # dot call syntax
```
The second style may suit certain usages, for example the built-in `add`, `remove` & `size` functions.
```no_run
nums[].size() # 3 equivalent to size(nums[])
nums[].sum() # 6
```