asmodeus 0.1.0

Complete assembler and emulator for Asmodeus architecture
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
use crate::formatter::core::format_assembly_content;

// ==========================
// HEADER AND COMMENT TESTS
// ==========================

#[test]
fn test_header_comments_no_indent() {
    let input = r#"; example: Test file
; This is a header comment
; Another header comment

start:
    POB #42
    STP"#;

    let expected = r#"; example: Test file
; This is a header comment
; Another header comment

start:
    POB #42
    STP
"#;

    let result = format_assembly_content(input);
    assert_eq!(result, expected);
}

#[test]
fn test_inline_comments_with_indent() {
    let input = r#"start:
    POB #42    ; this comment should be indented
    STP        ; this too"#;

    let expected = r#"start:
    POB #42              ; this comment should be indented
    STP                  ; this too

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

#[test]
fn test_comment_space_after_semicolon() {
    let input = r#"start:
    POB #42;no space after semicolon
    STP     ;also no space"#;

    let expected = r#"start:
    POB #42              ; no space after semicolon
    STP                  ; also no space

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

// ============
// LABEL TESTS
// ============

#[test]
fn test_label_instruction_separation() {
    let input = r#"start:POB first
end:STP"#;

    let expected = r#"start:
    POB first
end:
    STP

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

#[test]
fn test_label_instruction_with_comment_separation() {
    let input = r#"start:POB first;load value
loop:DOD second   ; add value"#;

    let expected = r#"start:
    POB first            ; load value
loop:
    DOD second           ; add value

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

#[test]
fn test_labels_always_left_aligned() {
    let input = r#"    start:
        loop_label:
            end_label:"#;

    let expected = r#"start:
loop_label:
end_label:

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

#[test]
fn test_simple_data_labels() {
    let input = r#"start:
    POB value
    STP

value:RST 42
message:   RST 100
buffer: RPA"#;

    let expected = r#"start:
    POB value
    STP

value: RST 42
message: RST 100
buffer: RPA

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

// ==================
// INSTRUCTION TESTS
// ==================

#[test]
fn test_instruction_formatting() {
    let input = r#"start:
POB    first
DOD      second
    WYJSCIE
        STP"#;

    let expected = r#"start:
    POB first
    DOD second
    WYJSCIE
    STP

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

#[test]
fn test_directives() {
    let input = r#".data
    .text
.section    .rodata"#;

    let expected = r#"    .data
    .text
    .section .rodata

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

// ============
// ARRAY TESTS
// ============

#[test]
fn test_array_with_commas() {
    let input = r#"array: RST 10,20,30,40"#;

    let expected = r#"array: RST 10
       RST 20
       RST 30
       RST 40

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

#[test]
fn test_multiline_array() {
    let input = r#"data_array: RST 10
RST 20
RST 30
RST 40
next_label: RST 50"#;

    let expected = r#"data_array: RST 10
            RST 20
            RST 30
            RST 40
next_label: RST 50

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

#[test]
fn test_mixed_arrays_and_labels() {
    let input = r#"first: RST 1
second: RST 2,3,4
third: RST 5
fourth: RST 6
RST 7
RST 8"#;

    let expected = r#"first: RST 1
second: RST 2
        RST 3
        RST 4
third: RST 5
fourth: RST 6
        RST 7
        RST 8

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

// ===============================
// WHITESPACE AND STRUCTURE TESTS
// ===============================

#[test]
fn test_empty_lines_handling() {
    let input = r#"; header


start:


    POB #42


    STP


"#;

    let expected = r#"; header


start:


    POB #42


    STP

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

#[test]
fn test_edge_cases() {
    // empty file
    assert_eq!(format_assembly_content(""), "\n");

    // only comments
    assert_eq!(format_assembly_content("; only comment"), "; only comment\n");

    // just label
    assert_eq!(format_assembly_content("start:"), "start:\n");

    // just instruction
    assert_eq!(format_assembly_content("POB #42"), "    POB #42\n");
}

// ==========================
// COMPLEX INTEGRATION TESTS
// ==========================

#[test]
fn test_complex_program() {
    let input = r#"; example: Complex test program
; Tests multiple formatting features

    start:POB first;load first
DOD    second ; add second
    WYJSCIE
STP

loop:    POB counter
SOZ end
    DOD #1
LAD counter
SOB loop

end:STP

first:RST 25
second:    RST 17
array: RST 1,2,3
buffer:RPA
multiline: RST 10
RST 20
RST 30"#;

    let expected = r#"; example: Complex test program
; Tests multiple formatting features

start:
    POB first            ; load first
    DOD second           ; add second
    WYJSCIE
    STP

loop:
    POB counter
    SOZ end
    DOD #1
    LAD counter
    SOB loop

end:
    STP

first: RST 25
second: RST 17
array: RST 1
       RST 2
       RST 3
buffer: RPA
multiline: RST 10
           RST 20
           RST 30

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

// ===========================
// INDIVIDUAL COMPONENT TESTS 
// ===========================

#[test]
fn test_various_instruction_formats() {
    let input = r#"start:
    POB    #42
    DOD   arg1   arg2
    MAKRO   name   param1   param2
    WYJSCIE
    STP"#;

    let expected = r#"start:
    POB #42
    DOD arg1 arg2
    MAKRO name param1 param2
    WYJSCIE
    STP

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

#[test]
fn test_data_with_different_formats() {
    let input = r#"decimal: RST 42
hex_val: RST 0x2A
negative: RST -10
buffer: RPA"#;

    let expected = r#"decimal: RST 42
hex_val: RST 0x2A
negative: RST -10
buffer: RPA

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

#[test]
fn test_comments_with_data() {
    let input = r#"value: RST 42;this is data
buffer: RPA   ; buffer space
array: RST 1,2,3   ;array data"#;

    let expected = r#"value: RST 42        ; this is data
buffer: RPA          ; buffer space
array: RST 1         ; array data
       RST 2
       RST 3

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

#[test]
fn test_mixed_label_types() {
    let input = r#"standalone:
attached:POB #42
data_label: RST 100
array_label: RST 1,2,3"#;

    let expected = r#"standalone:
attached:
    POB #42
data_label: RST 100
array_label: RST 1
             RST 2
             RST 3

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

// =================
// REGRESSION TESTS
// =================

#[test]
fn test_preserve_important_spacing() {
    let input = r#"; header comment

start:
    POB #42

    STP

; footer comment"#;

    let expected = r#"; header comment

start:
    POB #42

    STP

    ; footer comment

"#;

    let result = format_assembly_content(input);
    assert_eq!(result.trim_end(), expected.trim_end());
}

#[test]
fn test_no_extra_spaces_in_output() {
    let input = r#"start:
    POB #42
    STP"#;

    let result = format_assembly_content(input);
    
    // no line has trailing spaces
    for line in result.lines() {
        assert_eq!(line, line.trim_end(), "Line should not have trailing spaces: '{}'", line);
    }
}