33## Syntax
44
55In the following, a word in ALLCAPS refers to a non-terminal in
6- MicroCode's grammar. All other words are terminal symbols and
7- may have dashes and other symbols in them, with the following
8- exceptions: <float > is a floating point number; <pos > is an
9- integer greater than zero; // designates a comment.
6+ MicroCode's grammar. All other words are terminal symbols, with
7+ the following exceptions: <float > is a floating point number;
8+ <pos > is an integer greater than zero; // designates a comment;
9+ the symbols \s, \* , (, ), [ , ] , and | are part of the grammar
10+ specification. Words are always separated by whitespace.
1011
11- A program (PROG) consists of 5 pages, number 1-5, each with a possibly empty sequence of
12- rules RULE:
12+ A program (PROG) consists of 5 pages, numbered 1-5, each with a possibly
13+ empty sequence of rules RULE:
1314
14- PROG := page-1 RULE* page-2 RULE* page-3 RULE* page-4 RULE* page-5 RULE*
15+ PROG := page_1 RULE\* page_2 RULE\* page_3 RULE\* page_4 RULE\* page_5 RULE\ *
1516
16- Each rule has an option when section WHEN and an optional do section DO.
17+ Each rule has an optional section WHEN and an optional section DO.
1718
1819 RULE := when [WHEN] do [DO]
1920
20- The WHEN section specifies an event of interest and, optionally, a filter on that event .
21+ The WHEN section specifies a signal of interest and, optionally, a filter on that signal .
2122The DO section specifies an action and, optionally, parameters to that action.
22- Some actions can be repeated.
2323
2424 WHEN :=
25- | page-start [TS] // fires (once) when control transitions to this page, with optional delay
26- | timer [TS] // set a timer to fire after a delay, execute repeatedly after associated action completes
25+ | page_start [TS] // fires (once) when control transitions to this page, with optional delay
26+ | timer [TS] // set a timer to fire after a delay, execute repeatedly
2727 | press [PK] // fire on press of specified button PK
2828 | release [PK] // fire on release of specified button PK
2929 | move [MK] // fire on specified accelerometer event MK
3030 | sound [loud | quiet | C] // fire on loud/quiet event or comparison C of current sound level (0-255)
31- | temperature [UD | C] // fire on UD event or comparison C of current temperature (in Celcius )
31+ | temperature [UD | C] // fire on UD event or comparison C of current temperature (in Celsius )
3232 | light [UD | C] // fire on UD event or comparison C of current light level (0-255)
3333 | magnet [UD | C] // fire on UD event or comparison C of current magnetic level
34- | radio-receive [C] // fire when number arrives via radio, subject to optional comparison C
35- | variable-X-set [C] // fire after variable X has been assigned, subject to optional comparison C
36- | variable-Y-set [C] // fire after variable Y has been assigned, subject to optional comparison C
37- | variable-Z-set [C] // fire after variable Z has been assigned, subject to optional comparison C
34+ | radio_receive [C] // fire when number arrives via radio, subject to optional comparison C
35+ | variable_X_set [C] // fire after variable X has been assigned, subject to optional comparison C
36+ | variable_Y_set [C] // fire after variable Y has been assigned, subject to optional comparison C
37+ | variable_Z_set [C] // fire after variable Z has been assigned, subject to optional comparison C
3838
3939 UD := up | down
40- TS := (1/4-second | 1-second | 1-random-second | 5-seconds) * // sum the sequence of times
41- PK := button-A | button-B | logo | pin-0 | pin-1 | pin-2
42- MK := shake | tilt-left | tilt-right | ...
40+ TS := (1/4_second | 1_second | 1_random_second | 5_seconds)\ * // sum the sequence of times
41+ PK := button_A | button_B | logo | touch_pin_0 | touch_pin_1 | touch_pin_2
42+ MK := shake | tilt_left | tilt_right | tilt_up | tilt_down | face_down | face_up
4343
4444Sensors and variables may be compared to values using C; sensors may also have events
4545
@@ -49,11 +49,11 @@ Comparison operators CO are as follows:
4949
5050 CO :=
5151 | equals
52- | not-equals
53- | less-then
54- | less-then-or-equal
55- | greater-than
56- | greater-than-or-equal
52+ | not_equals
53+ | less_then
54+ | less_then_or_equal
55+ | greater_than
56+ | greater_than_or_equal
5757
5858An expression E is either atomic A, a binary expression, or a randomly chosen value:
5959
@@ -63,16 +63,16 @@ An expression E is either atomic A, a binary expression, or a randomly chosen va
6363 | A / E
6464 | A - E
6565 | A * E
66- | random PE
66+ | random_number PE
6767
6868An atomic value A is either a floating point number, one of the three variables,
6969or the current value of one of the four sensors, or the last value received over radio:
7070
7171 A :=
7272 | <float>
73- | var-X | var-Y | var-Z
74- | light-value | sound-value | temp-value | magnet-value
75- | radio-value
73+ | var_X | var_Y | var_Z
74+ | light_value | sound_value | temp_value | magnet_value
75+ | radio_value
7676
7777A positive (integer) expression PE is
7878
@@ -84,30 +84,96 @@ A positive (integer) expression PE is
8484A DO action
8585
8686 DO :=
87- | show-number [V]
88- | show-image (IMAGE)* [repeat [PE]]
89- | play-sound (SND)* [repeat [PE]]
90- | play- music (NOTES)* [repeat [PE]]
91- | radio-send [V]
92- | radio-set-group [PE]
93- | set-variable-X [V]
94- | set-variable-Y [V]
95- | set-variable-Z [V]
96- | switch-page [PAGE]
87+ | show_number [V]
88+ | show_image (IMAGE)\ * [repeat [PE]]
89+ | play_sound (SND)\ * [repeat [PE]]
90+ | music (NOTES)* [repeat [PE]]
91+ | radio_send [V]
92+ | radio_set_group [PE]
93+ | set_variable_X [V]
94+ | set_variable_Y [V]
95+ | set_variable_Z [V]
96+ | switch_page [PAGE]
9797
98- PAGE := | page-1 | page-2 | page-3 | page-4 | page-5
99- SND := | giggle | happy | hello | mysterious | sad | slide | soaring
98+ PAGE := | page_1 | page_2 | page_3 | page_4 | page_5
99+ SND := | giggle | happy | hello | mysterious | sad | slide | soaring
100100
101- An led image is specified inside quotes and consists of 25 digits , either 0 or 1, separated by
102- a space :
101+ An led image occurs inside backquotes ` and consists of 25 characters , either . or 1, separated by
102+ whitespace :
103103
104- IMAGE := LED- image `(0\s | 1\s)25`
104+ IMAGE := image ` (.\s+ | 1\s+ )25 `
105105
106+ A melody fragment consists of a sequence of 4 characters from the set { C, D, E, F, G, -},
107+ where - denotes a rest, inside backquotes:
106108
107- NOTES := (C | D | E | F | G)4
109+ NOTES := melody ` (C | D | E | F | G | -)4 `
108110
109111## Semantics
110112
111- Before execution starts, all variables are initialized to 0 and
112- the current value of all micro: bit /jacdac sensors is cached.
113+ Before execution starts, all variables (X, Y, and Z) are initialized to 0
114+ and the current value of all micro: bit /jacdac sensors is cached.
115+ The initial value of the radio is undefined.
116+ The radio group is initialized to 1.
113117Execution begins by transitioning to page 1.
118+
119+ ### Defaults
120+
121+ The absence of an optional element results in the use of a default, as follows:
122+
123+ - A rule with an empty do section never executes (no matching performed
124+ on it).
125+ - A rule with an empty when section defaults to page_start signal with no delay.
126+ - Signal defaults are as follows
127+ - page_start defaults to no delay
128+ - timer defaults to 1 second delay
129+ - press defaults to wildcard (any element in PK)
130+ - release defaults to wildcard (any element in PK)
131+ - move defaults to shake event
132+ - sound defaults to loud event
133+ - temperature, light, and magnet default to up event
134+ - radio and variable signals have no default
135+ - Action defaults are as follows
136+ - show_number defaults to 0
137+ - show_image defaults to smiley face
138+ - play_sound defaults to giggle
139+ - music defaults to ` C E G C `
140+ - radio_send defaults to 0
141+ - radio_set_group defaults to 1
142+ - set of a variable defaults to 0
143+ - switch page with no page specified is a NOOP
144+
145+ ### Signals
146+
147+ Execution of rules on the current page is driven by the reception
148+ of _ signals_ , which may come from the external environment (for
149+ example, when the user presses a button, shakes the micro: bit ,
150+ or a sensor provides a new value) or may be generated by the
151+ MicroCode program itself (for example, by assigning to a variable
152+ or switching to another page).
153+
154+ Regardless of their origin, signals are ordered and processed one
155+ at a time using a FIFO queue. Each signal has a name corresponding to
156+ the signals named in the when section; a signal carries a payload, which
157+ may be an event or a value. Some special kinds of signals are
158+ used for timers, as detailed later.
159+
160+ ### Resources and conflicts
161+
162+ Actions have effects through writing to resources, which are as follows:
163+
164+ - Screen
165+ - Speaker
166+ - Radio group
167+ - Radio
168+ - variable X
169+ - variable Y
170+ - variable Z
171+
172+ If two rules have actions that target the same resource, they are said
173+ to be in _ conflict_ if it is possible for them to execute at the same time.
174+
175+ ### Rule matching
176+
177+ Upon reception of a signal, it is first necessary to find the
178+ rules that are enabled for execution by the signal. The first
179+ step is to find the rules that mention the named signal.
0 commit comments