
Introduction to Macros
Unlike macros in languages such as C, which are preprocessor-based, e macros are real syntactic rules. They actually modify the grammar of the language, introducing new derivation rules. Macros are used to add new construct to the language, possibly with new keywords and operators. The newly declared constructs are implemented by defining a syntactic expansion, reducing them to existing constructs.
Example - define as
1 <'
2 struct frame {
3 size : uint;
4 dest_address : uint;
5 source_address : uint;
6 // Method to print the frame
7 send() is {
8 print me using hex;
9 };
10 };
11
12 define
13 "send simple frame "
14 as {
15 var f: frame;
16 gen f keeping {
17 .size == ;
18 .dest_address == ;
19 .source_address == ;
20 };
21 f.send();
22 };
23
24 extend sys {
25 run() is also {
26 send simple frame 0x00fe 0x0010 0xff;
27 send simple frame 0x00ff 0x0011 0x0f;
28 };
29 };
---------------------------------------------- @macros1
0 size: 0xff
1 dest_address: 0xfe
2 source_address: 0x10
me = frame-@1: frame
---------------------------------------------- @macros1
0 size: 0xf
1 dest_address: 0xff
Â
Example - define as computed
1 <'
2 extend sys {
3 ! keyed_list: list (key: it) of string;
4 adder() is empty;
5 ! num_list: list of int (bits: 5);
6 run() is also {
7 adder();
8 print num_list;
9 };
10 };
11
12 define "add to list" as computed {
13 if sys.keyed_list.key_exists() {
14 return("{}");
15 } else {
16 sys.keyed_list.add();
17 result = append( "extend sys { adder() is also \
" 18 {num_list.add(",,")};}");
" 19 };
20 };
21
22 '>
1 <'
2
3 import macros2.e;
4
5 add 1 to list;
6 add 2 to list;
7 add 3 to list;
8 add 4 to list;
9 add 5 to list;
10
11 '>Output - define as computednum_list = (5 items, dec):
5 4 3 2 1 .0
Bạn Có Đam Mê Với Vi Mạch hay Nhúng - Bạn Muốn Trau Dồi Thêm Kĩ Năng
Mong Muốn Có Thêm Cơ Hội Trong Công Việc
Và Trở Thành Một Người Có Giá Trị Hơn
Mong Muốn Có Thêm Cơ Hội Trong Công Việc
Và Trở Thành Một Người Có Giá Trị Hơn