Skip to content

Commit 6193d7f

Browse files
committed
[feature] added fixed-body-comment-indent for lc3fmt
1 parent 85322b2 commit 6193d7f

File tree

9 files changed

+91
-4
lines changed

9 files changed

+91
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lc3-toolchain"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
edition = "2024"
55
description = "Toolchain for LC-3 Assembly Code, designed for ECE109 Spring 2025"
66
authors = ["Finn Sheng@NCState Class of 2028"]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# lc3-toolchain
22

3-
![Version](https://img.shields.io/badge/version-0.3.0-blue)
3+
![Version](https://img.shields.io/badge/version-0.3.1-blue)
44
![Edition](https://img.shields.io/badge/edition-2024-orange)
55

66
LC-3 Assembly Toolchain, designed for ECE109 Spring 2025.

src/bin/fmt.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ space-comment-stick-to-body = 0
5858
space-from-label-block = 1
5959
space-from-start-end-block = 1
6060
colon-after-label = true
61+
fixed-body-comment-indent = true
6162
```

src/bin/fmt.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ const DEFAULT_STYLE: FormatStyle = FormatStyle {
166166
space_from_label_block: 1,
167167
space_from_start_end_block: 1,
168168
colon_after_label: true,
169+
fixed_body_comment_indent: true,
169170
};
170171

171172
const CONFIG_FILENAME: &str = "lc3-format.toml";
@@ -271,6 +272,7 @@ pub struct ConfigFormatStyle {
271272
pub space_from_label_block: Option<u8>,
272273
pub space_from_start_end_block: Option<u8>,
273274
pub colon_after_label: Option<bool>,
275+
pub fixed_body_comment_indent: Option<bool>,
274276
}
275277

276278
fn read_style(filepath_opt: Option<PathBuf>) -> FormatStyle {
@@ -344,6 +346,9 @@ fn config_format_style_to_format_style(
344346
colon_after_label: config_format_style
345347
.colon_after_label
346348
.unwrap_or(default.colon_after_label),
349+
fixed_body_comment_indent: config_format_style
350+
.fixed_body_comment_indent
351+
.unwrap_or(default.fixed_body_comment_indent),
347352
}
348353
}
349354

src/fmt/formatter.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub struct FormatStyle {
2222
pub space_from_label_block: u8, // vertical //done
2323
pub space_from_start_end_block: u8, // vertical // done
2424
pub colon_after_label: bool,
25+
pub fixed_body_comment_indent: bool,
2526
}
2627

2728
pub struct Formatter<'a> {
@@ -65,7 +66,12 @@ impl<'a> Formatter<'a> {
6566
.for_each(|e| label.push_str(e.as_str()));
6667
self.buffer.append(&mut label.into_bytes());
6768
self.buffer.append(&mut body.into_bytes());
68-
self.add_indent(missing_indent);
69+
self.add_indent(
70+
self.style
71+
.fixed_body_comment_indent
72+
.then_some(missing_indent)
73+
.unwrap_or(1), // default indent between block and comment
74+
);
6975
match comment {
7076
None => {}
7177
Some(comment) => {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
;LC-3 Assembly Test File
2+
;This program adds two numbers stored in memory
3+
.ORIG x3000
4+
5+
LD R1, NUM1 ;Load first number into R1
6+
LD R2, NUM2 ;Load second number into R2
7+
ADD R3, R1, R2 ;Add R1 and R2, store in R3
8+
ST R3, RESULT ;Store result in memory
9+
HALT ;Stop execution
10+
11+
input:
12+
.FILL x0005 ;First number (5)
13+
in_asd:
14+
.FILL x0003 ;Second number (3)
15+
br_:
16+
.FILL x0003 ;Second number (3)
17+
brnzp_:
18+
.FILL x0003 ;Second number (3)
19+
br1:
20+
.FILL x0003 ;Second number (3)
21+
l:
22+
.BLKW 1 ;Reserve space for result
23+
b1:
24+
.BLKW 1 ;Reserve space for result
25+
26+
.END
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
;LC-3 Assembly Test File
2+
;This program adds two numbers stored in memory
3+
.ORIG x3000
4+
5+
LD R1, NUM1 ;Load first number into R1
6+
LD R2, NUM2 ;Load second number into R2
7+
ADD R3, R1, R2 ;Add R1 and R2, store in R3
8+
ST R3, RESULT ;Store result in memory
9+
HALT ;Stop execution
10+
11+
input:
12+
.FILL x0005 ;First number (5)
13+
in_asd:
14+
.FILL x0003 ;Second number (3)
15+
br_:
16+
.FILL x0003 ;Second number (3)
17+
brnzp_:
18+
.FILL x0003 ;Second number (3)
19+
br1:
20+
.FILL x0003 ;Second number (3)
21+
l:
22+
.BLKW 1 ;Reserve space for result
23+
b1:
24+
.BLKW 1 ;Reserve space for result
25+
26+
.END

test/test_fmt.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod test_fmt {
1919
space_from_label_block: 1,
2020
space_from_start_end_block: 1,
2121
colon_after_label: true,
22+
fixed_body_comment_indent: true,
2223
};
2324

2425
const NO_COLON_STYLE: FormatStyle = FormatStyle {
@@ -31,6 +32,20 @@ mod test_fmt {
3132
space_from_label_block: 1,
3233
space_from_start_end_block: 1,
3334
colon_after_label: false,
35+
fixed_body_comment_indent: true,
36+
};
37+
38+
const FLEXIBLE_BODY_COMMENT_INDENT: FormatStyle = FormatStyle {
39+
indent_directive: 3,
40+
indent_instruction: 4,
41+
indent_label: 0,
42+
indent_min_comment_from_block: 1,
43+
space_block_to_comment: 1,
44+
space_comment_stick_to_body: 0,
45+
space_from_label_block: 1,
46+
space_from_start_end_block: 1,
47+
colon_after_label: true,
48+
fixed_body_comment_indent: false,
3449
};
3550

3651
fn assert_true(style: &FormatStyle, path: &'static str) {
@@ -108,4 +123,12 @@ mod test_fmt {
108123
fn test_no_colon_labels() {
109124
assert_true(&NO_COLON_STYLE, "fmt/no_colon_labels.asm")
110125
}
126+
127+
#[test]
128+
fn test_flexible_block_comment_indent() {
129+
assert_true(
130+
&FLEXIBLE_BODY_COMMENT_INDENT,
131+
"fmt/flexible_comment_indent.asm",
132+
)
133+
}
111134
}

0 commit comments

Comments
 (0)