ahk 0.3.2

The ahk language: a lexer, an AST, a parser and an interpreter for AutoHotkey-style scripts
Documentation
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! The `Buffer` class, against a fake desktop.
//!
//! A buffer is the one class that does not touch the machine: the
//! desktop is here because `Desktop` is the sum of the capabilities,
//! and the runtime drives one.
//!
//! v2.0.19 gives a buffer a size, an address and a constructor, and
//! nothing else. The reads and the writes of bytes belong to the file,
//! which reads into a buffer and writes out of one. Measured on the
//! binary of v2.0.19 on 2026-10-27, with `HasProp` and `HasMethod`.

use ahk::desktop::{
    Clipboard, Desktop, Dialogs, Files, Hotkeys, Keyboard, Machine, Native, Network, Pictures,
    Pointer, Processes, Screen, Sound, Timers, Windows,
};
use ahk::parse;
use ahk::runtime::{Report, Runtime, Value};
use std::time::Duration;

/// A desktop that answers nothing: a buffer does not ask it anything.
#[derive(Debug, Default)]
struct Fake;

impl Desktop for Fake {}
impl Pictures for Fake {}
impl Network for Fake {}
impl Sound for Fake {}
impl Machine for Fake {}
impl Dialogs for Fake {}

impl Files for Fake {}

impl Clipboard for Fake {}

impl Timers for Fake {}

impl Hotkeys for Fake {}

impl Windows for Fake {}

impl Keyboard for Fake {
    fn type_text(&mut self, _text: &str) -> Result<String, String> {
        Ok(String::new())
    }

    fn press_key(&mut self, _name: &str, _modifiers: &[String]) -> Result<String, String> {
        Ok(String::new())
    }
}

impl Pointer for Fake {
    fn click(&mut self, _x: i64, _y: i64, _button: &str, _count: u32) -> Result<String, String> {
        Ok(String::new())
    }
}

impl Screen for Fake {
    fn capture(
        &mut self,
        _x: i64,
        _y: i64,
        _width: i64,
        _height: i64,
        _filename: &str,
    ) -> Result<String, String> {
        Ok(String::new())
    }

    fn pixel(&mut self, _x: i64, _y: i64) -> Result<String, String> {
        Ok("#000000".into())
    }
}

impl Processes for Fake {
    fn run(&mut self, _command: &str, _window: &str) -> Result<(i32, String, String), String> {
        Ok((0, String::new(), String::new()))
    }
}

impl Native for Fake {}

/// Run a script and answer the report of it.
fn run(source: &str) -> Report {
    let mut desktop = Fake;
    let statements = parse(source).expect("parses");
    Runtime::new(&mut desktop, Duration::from_secs(10)).run(&statements)
}

#[test]
fn a_buffer_is_as_big_as_it_was_asked_for() {
    let report = run("b := Buffer(4)\nMsgBox \"%b.Size%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["4"]);
}

#[test]
fn a_buffer_has_an_address() {
    let report = run("b := Buffer(4)\naddress := b.Ptr");
    assert!(report.ok, "{:?}", report.error);
    match report.variables.get("address") {
        Some(Value::Int(number)) => assert!(*number > 0, "the address is {number}"),
        other => panic!("the address is {other:?}"),
    }
}

#[test]
fn a_buffer_may_be_made_again_by_its_constructor() {
    let report = run("b := Buffer(2)\nb.__New(3)\nMsgBox \"%b.Size%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3"]);
}

#[test]
fn a_buffer_of_a_negative_size_fails() {
    // Measured on the binary of v2.0.19 on 2026-10-30: `Buffer(-1)`
    // answers "Invalid value.", of the class `ValueError`, and the
    // constructor is named in the `What`.
    let report = run(
        "try {\n    b := Buffer(-1)\n} catch Any as e {\n    MsgBox e.Message \" | \" e.What \" | \" e.Extra\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec!["Invalid value. | Buffer.Prototype.__New | "]
    );
}

#[test]
fn a_buffer_may_be_made_of_another_size() {
    let report = run("b := Buffer(2)\nb.Size := 4\nMsgBox \"%b.Size%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["4"]);
}

#[test]
fn a_buffer_has_no_position() {
    // v2.0.19 does not give a buffer one: the position was never there.
    let report = run("b := Buffer(4)\nposition := b.Pos");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Buffer\" has no property named \"Pos\".")
    );
}

#[test]
fn a_buffer_does_not_read_bytes() {
    // The reads and the writes belong to the file, which reads into a
    // buffer and writes out of one.
    let report = run("b := Buffer(4)\nb.ReadUChar()");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Buffer\" has no method named \"ReadUChar\".")
    );
    assert_eq!(report.error_line, Some(2));
}

#[test]
fn a_buffer_has_no_item() {
    let report = run("b := Buffer(2)\nbyte := b[1]");
    assert!(!report.ok);
    // The buffer of v2 has no item either: the name of the item is the
    // name of the property that carries it.
    assert_eq!(
        report.error.as_deref(),
        Some("This value of type \"Buffer\" has no property named \"__Item\".")
    );
    assert_eq!(report.error_line, Some(2));
}

#[test]
fn the_fill_of_a_buffer_is_the_byte_of_the_number() {
    // Measured on the binary of v2.0.19 on 2026-10-31: a number that is
    // not a byte is not refused, and the byte of it is the fill.
    let report = run("b := Buffer(3, 0x414243)\nMsgBox b.Size \" \" NumGet(b, 0, \"UChar\")");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["3 67"]);
}

#[test]
fn a_buffer_has_the_methods_of_the_base_object() {
    let report = run(
        "b := Buffer(4)\nmethod := b.HasMethod(\"ReadUChar\")\nprop := b.HasProp(\"Size\")\nbase := b.HasBase(Object.Prototype)\nMsgBox \"%method% %prop% %base%\"",
    );
    assert!(report.ok, "{:?}", report.error);
    // The buffer has no method of its own but the constructor, so the
    // read is not one of them.
    assert_eq!(report.output, vec!["0 1 1"]);
}

#[test]
fn the_inside_of_a_buffer_is_not_a_property_of_it() {
    let report = run(
        "b := Buffer(4)\ncount := ObjOwnPropCount(b)\ninside := b.HasOwnProp(\"buffer state\")\nMsgBox \"%count% %inside%\"",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["0 0"]);
}

#[test]
fn a_buffer_may_have_a_property_of_its_own() {
    let report = run(
        "b := Buffer(4)\nb.DefineProp(\"x\", {Value: 1})\nvalue := b.x\ngone := b.DeleteProp(\"x\")\nMsgBox \"%value% %gone%\"",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1 1"]);
}

// -- the numbers --------------------------------------------------------------

#[test]
fn a_number_goes_into_a_buffer_and_comes_back() {
    let report = run(
        "b := Buffer(8)\nNumPut(\"Int\", 42, b)\nvalue := NumGet(b, \"Int\")\nMsgBox \"%value%\"",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["42"]);
}

#[test]
fn every_type_of_a_number_is_a_number() {
    let report = run("b := Buffer(64)\n\
         NumPut(\"UInt\", 4294967295, b, 0)\n\
         NumPut(\"Int\", -2, b, 4)\n\
         NumPut(\"Int64\", -3, b, 8)\n\
         NumPut(\"Short\", -4, b, 16)\n\
         NumPut(\"UShort\", 65535, b, 18)\n\
         NumPut(\"Char\", -5, b, 20)\n\
         NumPut(\"UChar\", 255, b, 21)\n\
         NumPut(\"Float\", 1.5, b, 22)\n\
         NumPut(\"Double\", 2.25, b, 26)\n\
         u := NumGet(b, 0, \"UInt\")\n\
         i := NumGet(b, 4, \"Int\")\n\
         i64 := NumGet(b, 8, \"Int64\")\n\
         s := NumGet(b, 16, \"Short\")\n\
         us := NumGet(b, 18, \"UShort\")\n\
         c := NumGet(b, 20, \"Char\")\n\
         uc := NumGet(b, 21, \"UChar\")\n\
         f := NumGet(b, 22, \"Float\")\n\
         d := NumGet(b, 26, \"Double\")\n\
         MsgBox \"%u% %i% %i64% %s% %us% %c% %uc% %f% %d%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec!["4294967295 -2 -3 -4 65535 -5 255 1.5 2.25"]
    );
}

#[test]
fn a_pointer_is_a_number_of_the_size_of_a_pointer() {
    let report = run(
        "b := Buffer(16)\nNumPut(\"Ptr\", 1234, b, 0)\nNumPut(\"UPtr\", 4321, b, 8)\n\
         first := NumGet(b, 0, \"Ptr\")\nsecond := NumGet(b, 8, \"UPtr\")\nMsgBox \"%first% %second%\"",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1234 4321"]);
}

#[test]
fn a_number_is_at_the_offset_it_is_written_at() {
    let report = run(
        "b := Buffer(8)\nNumPut(\"Int\", 42, b, 4)\nvalue := NumGet(b, 4, \"Int\")\n\
         empty := NumGet(b, 0, \"Int\")\nMsgBox \"%value% %empty%\"",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["42 0"]);
}

#[test]
fn a_write_takes_several_numbers() {
    let report = run("b := Buffer(8)\nNumPut(\"Int\", 1, \"Short\", 2, b)\n\
         first := NumGet(b, 0, \"Int\")\nsecond := NumGet(b, 4, \"Short\")\nMsgBox \"%first% %second%\"");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["1 2"]);
}

#[test]
fn a_write_answers_the_address_to_the_right_of_the_last_number() {
    let report = run("b := Buffer(8)\naddress := NumPut(\"Int\", 42, b)\nright := address - b.Ptr");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.variables.get("right"), Some(&Value::Int(4)));
}

#[test]
fn a_number_that_does_not_fit_in_the_buffer_fails() {
    let report = run("b := Buffer(2)\nNumPut(\"Int\", 42, b)");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("the bytes do not fit in the buffer.")
    );
    assert_eq!(report.error_line, Some(2));
}

#[test]
fn a_number_that_is_not_in_the_buffer_fails() {
    let report = run("b := Buffer(2)\nNumGet(b, 0, \"Int\")");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("the bytes do not fit in the buffer.")
    );
    assert_eq!(report.error_line, Some(2));
}

#[test]
fn a_number_is_not_read_from_the_memory_of_an_address() {
    let report = run("NumGet(1234, \"Int\")");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some(
            "this runtime does not read the memory of an address: the source of a number is a buffer."
        )
    );
    assert_eq!(report.error_line, Some(1));
}

#[test]
fn a_number_is_not_written_to_the_memory_of_an_address() {
    let report = run("NumPut(\"Int\", 42, 1234)");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some(
            "this runtime does not write to the memory of an address: the target of a number is a buffer."
        )
    );
    assert_eq!(report.error_line, Some(1));
}

#[test]
fn a_number_is_not_read_from_a_value_that_is_not_a_buffer() {
    let report = run("a := Array()\nNumGet(a, \"Int\")");
    assert!(!report.ok);
    assert_eq!(report.error.as_deref(), Some("this value is not a buffer."));
    assert_eq!(report.error_line, Some(2));
}

#[test]
fn a_type_that_is_not_a_type_of_a_number_fails() {
    let report = run("b := Buffer(4)\nNumGet(b, \"Word\")");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some(
            "the type of a number is one of UInt, Int, Int64, Short, UShort, Char, UChar, Double, Float, Ptr or UPtr."
        )
    );
    assert_eq!(report.error_line, Some(2));
}

#[test]
fn a_number_that_is_not_a_number_fails() {
    let report = run("b := Buffer(4)\nNumPut(\"Int\", \"word\", b)");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Parameter #2 of NumPut requires a Number, but received a String.")
    );
    assert_eq!(report.error_line, Some(2));
}

#[test]
fn num_get_wants_a_source_and_a_type() {
    let report = run("b := Buffer(4)\nNumGet(b)");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Too few parameters passed to function.\n     Specifically: NumGet")
    );
    assert_eq!(report.error_line, Some(2));
}

#[test]
fn num_put_wants_a_type_a_number_and_a_target() {
    let report = run("NumPut(\"Int\", 42)");
    assert!(!report.ok);
    assert_eq!(
        report.error.as_deref(),
        Some("Too few parameters passed to function.\n     Specifically: NumPut")
    );
    assert_eq!(report.error_line, Some(1));
}

#[test]
fn the_number_of_the_arguments_of_the_constructor_of_a_buffer_is_checked() {
    // Measured on the binary of v2.0.19 on 2026-10-30: the call is in the
    // `What` of the error, and the constructor is in the `Extra`.
    let report = run(
        "try {\n    x := Buffer(1, 2, 3)\n} catch Any as e {\n    MsgBox e.Message \" | \" e.What \" | \" e.Extra\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec!["Too many parameters passed to function. | Buffer.Call | Buffer.Prototype.__New"]
    );
}

#[test]
fn the_size_of_a_buffer_is_a_number() {
    let report = run(
        "try {\n    x := Buffer(\"x\")\n} catch Any as e {\n    MsgBox e.Message \" | \" e.What \" | \" e.Extra\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(
        report.output,
        vec![
            "Parameter #1 of Buffer.Prototype.__New requires a Number, but received a String. | Buffer.Prototype.__New | x"
        ]
    );
}

#[test]
fn the_ptr_of_a_buffer_may_not_be_written() {
    // Measured on the binary of v2.0.19 on 2026-10-31: the pointer of a
    // buffer is a property that may not be written, and the write of it is
    // refused with "Property is read-only.", the name of the property is in
    // the Extra, and the What is empty.
    let report = run(
        "try {\n    b := Buffer(8)\n    b.Ptr := 1\n} catch Any as e {\n    MsgBox e.Message \" | \" e.What \" | \" e.Extra\n}",
    );
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["Property is read-only. |  | Ptr"]);
}

#[test]
fn the_size_of_a_buffer_may_be_written() {
    // Measured on the binary of v2.0.19 on 2026-10-31: the size of a buffer
    // is a property that may be written, and the write of it changes it.
    let report = run("b := Buffer(8)\nb.Size := 4\nMsgBox b.Size");
    assert!(report.ok, "{:?}", report.error);
    assert_eq!(report.output, vec!["4"]);
}