Skip to content

Commit 441a716

Browse files
committed
[assembler] Eliminate conversions from Vec<T> to OneOrMore<T>.
1 parent b1966f5 commit 441a716

File tree

3 files changed

+72
-85
lines changed

3 files changed

+72
-85
lines changed

assembler/src/asmlib/ast.rs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -966,26 +966,6 @@ impl From<OneOrMore<CommaDelimitedFragment>> for UntaggedProgramInstruction {
966966
}
967967
}
968968

969-
// Because this conversion only allows a runtime check of the
970-
// invariant we want to establish, we allow it only in tests.
971-
#[cfg(test)]
972-
impl From<Vec<CommaDelimitedFragment>> for UntaggedProgramInstruction {
973-
// TODO: move the establishment of the non-empty invariant into the callers.
974-
fn from(fragments: Vec<CommaDelimitedFragment>) -> Self {
975-
let mut it = fragments.into_iter();
976-
match it.next() {
977-
Some(head) => {
978-
let mut items = OneOrMore::new(head);
979-
items.extend(it);
980-
Self { fragments: items }
981-
}
982-
None => {
983-
panic!("input fragments should not be empty");
984-
}
985-
}
986-
}
987-
}
988-
989969
impl UntaggedProgramInstruction {
990970
fn symbol_uses(
991971
&self,
@@ -1096,30 +1076,33 @@ impl TaggedProgramInstruction {
10961076
frag_span: Span,
10971077
frag: InstructionFragment,
10981078
) -> TaggedProgramInstruction {
1099-
TaggedProgramInstruction::multiple(
1079+
TaggedProgramInstruction {
11001080
tags,
1101-
inst_span,
1102-
vec![CommaDelimitedFragment {
1081+
span: inst_span,
1082+
instruction: UntaggedProgramInstruction::from(OneOrMore::new(CommaDelimitedFragment {
11031083
span: frag_span,
11041084
leading_commas: None,
11051085
holdbit,
11061086
fragment: frag,
11071087
trailing_commas: None,
1108-
}],
1109-
)
1088+
})),
1089+
}
11101090
}
11111091

11121092
#[cfg(test)]
11131093
pub(crate) fn multiple(
11141094
tags: Vec<Tag>,
11151095
span: Span,
1116-
fragments: Vec<CommaDelimitedFragment>,
1096+
first_fragment: CommaDelimitedFragment,
1097+
more_fragments: Vec<CommaDelimitedFragment>,
11171098
) -> TaggedProgramInstruction {
1118-
assert!(!fragments.is_empty());
11191099
TaggedProgramInstruction {
11201100
tags,
11211101
span,
1122-
instruction: UntaggedProgramInstruction::from(fragments),
1102+
instruction: UntaggedProgramInstruction::from(OneOrMore::with_tail(
1103+
first_fragment,
1104+
more_fragments,
1105+
)),
11231106
}
11241107
}
11251108

assembler/src/asmlib/driver.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use chumsky::error::Rich;
1313
use tracing::{event, span, Level};
1414

1515
use super::ast::*;
16+
#[cfg(test)]
17+
use super::collections::OneOrMore;
1618
use super::eval::extract_final_equalities;
1719
use super::eval::Evaluate;
1820
use super::eval::HereValue;
@@ -706,7 +708,7 @@ fn test_assemble_pass1() {
706708
statements: vec![TaggedProgramInstruction {
707709
span: span(0..2),
708710
tags: Vec::new(),
709-
instruction: UntaggedProgramInstruction::from(vec![CommaDelimitedFragment {
711+
instruction: UntaggedProgramInstruction::from(OneOrMore::new(CommaDelimitedFragment {
710712
leading_commas: None,
711713
holdbit: HoldBit::Unspecified,
712714
span: span(0..2),
@@ -716,7 +718,7 @@ fn test_assemble_pass1() {
716718
u36!(0o14),
717719
)))),
718720
trailing_commas: None,
719-
}]),
721+
})),
720722
}]
721723
.into(),
722724
};

assembler/src/asmlib/parser/tests.rs

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ fn test_comment_in_rc_block() {
649649
TaggedProgramInstruction {
650650
span: span(1..2),
651651
tags: notags(),
652-
instruction: UntaggedProgramInstruction::from(vec![
652+
instruction: UntaggedProgramInstruction::from(OneOrMore::new(
653653
CommaDelimitedFragment {
654654
leading_commas: None,
655655
holdbit: HoldBit::Unspecified,
@@ -665,7 +665,7 @@ fn test_comment_in_rc_block() {
665665
),
666666
trailing_commas: None,
667667
}
668-
])
668+
))
669669
}
670670
))),
671671
)))
@@ -833,15 +833,15 @@ fn test_arrow() {
833833
},
834834
span: span(0..3),
835835
}],
836-
instruction: UntaggedProgramInstruction::from(vec![CommaDelimitedFragment {
836+
instruction: UntaggedProgramInstruction::from(OneOrMore::new(CommaDelimitedFragment {
837837
leading_commas: None,
838838
holdbit: HoldBit::Unspecified,
839839
span: span(5..6),
840840
fragment: InstructionFragment::Arithmetic(ArithmeticExpression::from(Atom::from(
841841
LiteralValue::from((span(5..6), Script::Normal, Unsigned36Bit::ZERO))
842842
))),
843843
trailing_commas: None
844-
}])
844+
}))
845845
}
846846
);
847847
assert_eq!(
@@ -854,15 +854,15 @@ fn test_arrow() {
854854
},
855855
span: span(0..3),
856856
}],
857-
instruction: UntaggedProgramInstruction::from(vec![CommaDelimitedFragment {
857+
instruction: UntaggedProgramInstruction::from(OneOrMore::new(CommaDelimitedFragment {
858858
leading_commas: None,
859859
holdbit: HoldBit::Unspecified,
860860
span: span(8..9),
861861
fragment: InstructionFragment::Arithmetic(ArithmeticExpression::from(Atom::from(
862862
LiteralValue::from((span(8..9), Script::Normal, u36!(1),))
863863
))),
864864
trailing_commas: None
865-
}])
865+
}))
866866
}
867867
);
868868
}
@@ -1012,13 +1012,13 @@ fn assignment_of_literal(name: &str, assignment_span: Span, literal: LiteralValu
10121012
name: SymbolName::from(name),
10131013
value: EqualityValue::from((
10141014
literal.span(),
1015-
UntaggedProgramInstruction::from(vec![CommaDelimitedFragment {
1015+
UntaggedProgramInstruction::from(OneOrMore::new(CommaDelimitedFragment {
10161016
leading_commas: None,
10171017
holdbit: HoldBit::Unspecified,
10181018
span: literal.span(),
10191019
fragment: atom_to_fragment(Atom::from(literal)),
10201020
trailing_commas: None,
1021-
}]),
1021+
})),
10221022
)),
10231023
}
10241024
}
@@ -1064,7 +1064,7 @@ fn test_assignment_superscript() {
10641064
name: SymbolName::from("FOO"),
10651065
value: EqualityValue::from((
10661066
val_span,
1067-
UntaggedProgramInstruction::from(vec![CommaDelimitedFragment {
1067+
UntaggedProgramInstruction::from(OneOrMore::new(CommaDelimitedFragment {
10681068
leading_commas: None,
10691069
holdbit: HoldBit::Unspecified,
10701070
span: span(*val_begin..*val_end),
@@ -1077,7 +1077,7 @@ fn test_assignment_superscript() {
10771077
)))
10781078
}),
10791079
trailing_commas: None
1080-
}])
1080+
}))
10811081
))
10821082
}
10831083
);
@@ -1766,7 +1766,7 @@ fn test_double_pipe_config_symbolic() {
17661766
let expected = TaggedProgramInstruction {
17671767
tags: notags(),
17681768
span: span(0..6),
1769-
instruction: UntaggedProgramInstruction::from(vec![
1769+
instruction: UntaggedProgramInstruction::from(OneOrMore::with_tail(
17701770
CommaDelimitedFragment {
17711771
leading_commas: None,
17721772
holdbit: HoldBit::Unspecified,
@@ -1781,16 +1781,16 @@ fn test_double_pipe_config_symbolic() {
17811781
}),
17821782
trailing_commas: None,
17831783
},
1784-
CommaDelimitedFragment {
1784+
vec![CommaDelimitedFragment {
17851785
leading_commas: None,
17861786
holdbit: HoldBit::Unspecified,
17871787
span: span(5..6),
17881788
fragment: InstructionFragment::Arithmetic(ArithmeticExpression::from(Atom::from(
17891789
(span(5..6), Script::Normal, SymbolName::from("Y")),
17901790
))),
17911791
trailing_commas: None,
1792-
},
1793-
]),
1792+
}],
1793+
)),
17941794
};
17951795
assert_eq!(got, expected);
17961796
}
@@ -1811,7 +1811,7 @@ fn test_double_pipe_config_expression() {
18111811
let expected = TaggedProgramInstruction {
18121812
tags: notags(),
18131813
span: span(0..12),
1814-
instruction: UntaggedProgramInstruction::from(vec![
1814+
instruction: UntaggedProgramInstruction::from(OneOrMore::with_tail(
18151815
CommaDelimitedFragment {
18161816
leading_commas: None,
18171817
holdbit: HoldBit::Unspecified,
@@ -1855,16 +1855,16 @@ fn test_double_pipe_config_expression() {
18551855
}),
18561856
trailing_commas: None,
18571857
},
1858-
CommaDelimitedFragment {
1858+
vec![CommaDelimitedFragment {
18591859
leading_commas: None,
18601860
holdbit: HoldBit::Unspecified,
18611861
span: span(11..12),
18621862
fragment: InstructionFragment::Arithmetic(ArithmeticExpression::from(Atom::from(
18631863
(span(11..12), Script::Normal, SymbolName::from("Y")),
18641864
))),
18651865
trailing_commas: None,
1866-
},
1867-
]),
1866+
}],
1867+
)),
18681868
};
18691869
assert_eq!(got, expected);
18701870
}
@@ -1925,22 +1925,22 @@ fn test_superscript_configuration_literal() {
19251925

19261926
assert_eq!(
19271927
parse_untagged_program_instruction(input),
1928-
UntaggedProgramInstruction::from(vec![
1928+
UntaggedProgramInstruction::from(OneOrMore::with_tail(
19291929
CommaDelimitedFragment {
19301930
leading_commas: None,
19311931
holdbit: HoldBit::Unspecified,
19321932
span: span(0..7),
19331933
fragment: config,
19341934
trailing_commas: None,
19351935
},
1936-
CommaDelimitedFragment {
1936+
vec![CommaDelimitedFragment {
19371937
leading_commas: None,
19381938
holdbit: HoldBit::Unspecified,
19391939
span: span(7..10),
19401940
fragment: aux,
19411941
trailing_commas: None,
1942-
},
1943-
])
1942+
},]
1943+
))
19441944
);
19451945
}
19461946

@@ -1973,7 +1973,7 @@ fn test_pipe_construct() {
19731973
rc_word_value: RegisterContaining::from(TaggedProgramInstruction {
19741974
span: span(21..38),
19751975
tags: notags(),
1976-
instruction: UntaggedProgramInstruction::from(vec![
1976+
instruction: UntaggedProgramInstruction::from(OneOrMore::with_tail(
19771977
CommaDelimitedFragment {
19781978
leading_commas: None,
19791979
holdbit: HoldBit::Unspecified,
@@ -1992,7 +1992,7 @@ fn test_pipe_construct() {
19921992
}),
19931993
trailing_commas: None,
19941994
},
1995-
CommaDelimitedFragment {
1995+
vec![CommaDelimitedFragment {
19961996
leading_commas: None,
19971997
holdbit: HoldBit::Unspecified,
19981998
span: span(21..31),
@@ -2009,8 +2009,8 @@ fn test_pipe_construct() {
20092009
tail: Vec::new(),
20102010
}),
20112011
trailing_commas: None,
2012-
},
2013-
]),
2012+
}],
2013+
)),
20142014
}),
20152015
};
20162016
assert_eq!(got, expected, "incorrect parse of input {input}");
@@ -2036,18 +2036,18 @@ fn test_commas_instruction() {
20362036
let expected = TaggedProgramInstruction::multiple(
20372037
notags(),
20382038
span(0..16),
2039+
CommaDelimitedFragment {
2040+
leading_commas: None,
2041+
holdbit: HoldBit::Unspecified,
2042+
span: span(0..4),
2043+
fragment: InstructionFragment::from((
2044+
span(0..3),
2045+
Script::Normal,
2046+
Unsigned36Bit::from(0o200u8),
2047+
)),
2048+
trailing_commas: Some(Commas::One(span(3..4))),
2049+
},
20392050
vec![
2040-
CommaDelimitedFragment {
2041-
leading_commas: None,
2042-
holdbit: HoldBit::Unspecified,
2043-
span: span(0..4),
2044-
fragment: InstructionFragment::from((
2045-
span(0..3),
2046-
Script::Normal,
2047-
Unsigned36Bit::from(0o200u8),
2048-
)),
2049-
trailing_commas: Some(Commas::One(span(3..4))),
2050-
},
20512051
CommaDelimitedFragment {
20522052
leading_commas: Some(Commas::One(span(3..4))),
20532053
holdbit: HoldBit::Unspecified,
@@ -2503,7 +2503,7 @@ fn test_mnemonic_suz() {
25032503
},
25042504
span: span(0..5),
25052505
}],
2506-
instruction: UntaggedProgramInstruction::from(vec![
2506+
instruction: UntaggedProgramInstruction::from(OneOrMore::with_tail(
25072507
CommaDelimitedFragment {
25082508
leading_commas: None,
25092509
holdbit: HoldBit::Unspecified,
@@ -2515,7 +2515,7 @@ fn test_mnemonic_suz() {
25152515
)),
25162516
trailing_commas: None,
25172517
},
2518-
CommaDelimitedFragment {
2518+
vec![CommaDelimitedFragment {
25192519
leading_commas: None,
25202520
holdbit: HoldBit::Unspecified,
25212521
span: span(13..19),
@@ -2525,8 +2525,8 @@ fn test_mnemonic_suz() {
25252525
u36!(0o000_000_200_013)
25262526
)),
25272527
trailing_commas: None,
2528-
},
2529-
])
2528+
},]
2529+
))
25302530
}
25312531
);
25322532
}
@@ -2711,20 +2711,22 @@ mod macro_tests {
27112711
body: vec![TaggedProgramInstruction {
27122712
span: span(0..24),
27132713
tags: notags(),
2714-
instruction: UntaggedProgramInstruction::from(vec![CommaDelimitedFragment {
2715-
leading_commas: None,
2716-
holdbit: HoldBit::Unspecified,
2717-
span: span(16..20),
2718-
fragment: InstructionFragment::Config(ConfigValue {
2719-
already_superscript: false,
2720-
expr: ArithmeticExpression::from(Atom::from((
2721-
span(4..5),
2722-
Script::Normal,
2723-
SymbolName::from("X"),
2724-
))),
2725-
}),
2726-
trailing_commas: None,
2727-
}]),
2714+
instruction: UntaggedProgramInstruction::from(OneOrMore::new(
2715+
CommaDelimitedFragment {
2716+
leading_commas: None,
2717+
holdbit: HoldBit::Unspecified,
2718+
span: span(16..20),
2719+
fragment: InstructionFragment::Config(ConfigValue {
2720+
already_superscript: false,
2721+
expr: ArithmeticExpression::from(Atom::from((
2722+
span(4..5),
2723+
Script::Normal,
2724+
SymbolName::from("X"),
2725+
))),
2726+
}),
2727+
trailing_commas: None,
2728+
},
2729+
)),
27282730
}]
27292731
.into(),
27302732
span: span(20..40),

0 commit comments

Comments
 (0)