-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmacros.alu
84 lines (74 loc) · 1.93 KB
/
macros.alu
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
fn printf_impl(fmt: &[u8], args: &[&void]) {
let i = 0usize;
let buf: [u8; 32];
let buf: &mut [u8] = &buf;
let buf_pos = 0usize;
let in_escape = false;
// Macros are hygienic, but if defined in linear scopes,
// they can bind ambient variables (like buf, buf_pos here)
macro buf_append($val) {
buf[buf_pos] = $val;
buf_pos += 1;
if buf_pos == buf.len() {
print!("{}", buf);
buf_pos = 0;
}
}
macro pop_arg() {
if args.len() == 0 {
panic!("too few arguments");
}
let arg = args[0];
args = args[1..];
arg
}
while i < fmt.len() {
let ch = fmt[i];
if in_escape {
switch ch {
's' => {
print!("{}", buf[0..buf_pos]);
print!("{}", *(pop_arg!() as &&[u8]));
buf_pos = 0;
}
'd' => {
print!("{}", buf[0..buf_pos]);
print!("{}", *(pop_arg!() as &i32));
buf_pos = 0;
}
'%' => {
buf_append!(ch);
}
_ => {
panic!("unknown escape code")
}
}
in_escape = false;
} else {
if ch == '%' {
in_escape = true;
} else {
buf_append!(ch);
}
}
i += 1;
}
print!("{}", buf[0..buf_pos]);
if args.len() > 0 {
panic!("too many arguments");
}
}
macro printf($fmt, $expr...) {
printf_impl($fmt, &[(&$expr as &void)$...])
}
fn main() {
printf!(
"hello %s, you are %d years old, you have %d dogs and %d cats\n",
"user",
42,
3,
6
);
// Macros can also be invoked using universal call syntax
"%d + %d = %d\n".printf!(1, 2, 3);
}