more generally consider NEGATIVE_SIGN a non-op term (#2401)

This commit is contained in:
Mark Thom
2024-05-13 14:37:32 -06:00
parent 42a0d686a5
commit 070f1e8dfa
3 changed files with 18 additions and 17 deletions

View File

@@ -49,13 +49,13 @@ macro_rules! fixnum {
macro_rules! is_term { macro_rules! is_term {
($x:expr) => { ($x:expr) => {
($x as u32 & $crate::parser::ast::TERM) != 0 ($x as u32 & $crate::parser::ast::TERM) != 0 || is_negate!($x)
}; };
} }
macro_rules! is_lterm { macro_rules! is_lterm {
($x:expr) => { ($x:expr) => {
($x as u32 & $crate::parser::ast::LTERM) != 0 ($x as u32 & $crate::parser::ast::LTERM) != 0 || is_negate!($x)
}; };
} }

View File

@@ -150,6 +150,7 @@ pub fn get_op_desc(name: Atom, op_dir: &CompositeOpDir) -> Option<CompositeOpDes
op_desc.pre = pri as usize; op_desc.pre = pri as usize;
op_desc.spec |= spec as u32; op_desc.spec |= spec as u32;
} else if name == atom!("-") { } else if name == atom!("-") {
// used to denote a negative sign that should be treated as an atom and not an operator
op_desc.spec |= NEGATIVE_SIGN; op_desc.spec |= NEGATIVE_SIGN;
} }
} }
@@ -693,7 +694,7 @@ impl<'a, R: CharRead> Parser<'a, R> {
// expect a term or non-comma operator. // expect a term or non-comma operator.
if let TokenType::Comma = desc.tt { if let TokenType::Comma = desc.tt {
return None; return None;
} else if is_term!(desc.spec) || is_op!(desc.spec) { } else if is_term!(desc.spec) || is_op!(desc.spec) || is_negate!(desc.spec) {
arity += 1; arity += 1;
} else { } else {
return None; return None;
@@ -911,9 +912,6 @@ impl<'a, R: CharRead> Parser<'a, R> {
// can't be prefix, so either inf == 0 // can't be prefix, so either inf == 0
// or post == 0. // or post == 0.
self.reduce_op(inf + post); self.reduce_op(inf + post);
// let fixity = if inf > 0 { Fixity::In } else { Fixity::Post };
self.promote_atom_op(name, inf + post, spec & (XFX | XFY | YFX | YF | XF)); self.promote_atom_op(name, inf + post, spec & (XFX | XFY | YFX | YF | XF));
} }
_ => { _ => {
@@ -927,12 +925,12 @@ impl<'a, R: CharRead> Parser<'a, R> {
inf + post, inf + post,
spec & (XFX | XFY | YFX | XF | YF), spec & (XFX | XFY | YFX | XF | YF),
); );
} else {
self.promote_atom_op(name, pre, spec & (FX | FY | NEGATIVE_SIGN)); return Ok(true);
} }
} else {
self.promote_atom_op(name, pre, spec & (FX | FY | NEGATIVE_SIGN));
} }
self.promote_atom_op(name, pre, spec & (FX | FY | NEGATIVE_SIGN));
} }
} }
} else { } else {

View File

@@ -230,13 +230,16 @@ test_61 :- integer('-'/*.*/1).
test_62 :- atom(-/*.*/-). test_62 :- atom(-/*.*/-).
test_63_180_64 :- setup_call_cleanup(( current_op(P,fy,-), test_63_180_64_328 :- setup_call_cleanup(( current_op(P,fy,-),
op(0,fy,-) op(0,fy,-)
), ),
( integer(-1), ( integer(-1),
integer(- 1) integer(- 1),
), read_from_chars("writeq_term_to_chars([-]).", Writer),
op(P,fy,-)). call(Writer, Cs),
Cs == "[-]"
),
op(P,fy,-)).
test_135 :- writeq_term_to_chars(-(1), Chars), test_135 :- writeq_term_to_chars(-(1), Chars),
Chars == "- (1)". Chars == "- (1)".