Reduce true/false conditionals down to just 'else' and warn if else is used in non-final branch

master
Nat Lasseter 4 years ago
parent 12e37d4335
commit 0006cd06ec
  1. 7
      crsn/src/asm/instr/cond.rs
  2. 4
      crsn/src/asm/instr/flatten.rs
  3. 11
      examples/test_cond.csn

@ -138,8 +138,7 @@ pub fn parse_cond(text: &str, pos: &SourcePosition) -> Result<Cond, CrsnError> {
"neof" => Cond::NotEof, "neof" => Cond::NotEof,
"ov" => Cond::Overflow, "ov" => Cond::Overflow,
"nov" => Cond::NotOverflow, "nov" => Cond::NotOverflow,
"true" | "always" | "else" => Cond::True, "else" => Cond::True,
"false" | "never" => Cond::False,
_ => { _ => {
return Err(CrsnError::Parse(format!("Unknown cond: {}", text).into(), pos.clone())); return Err(CrsnError::Parse(format!("Unknown cond: {}", text).into(), pos.clone()));
} }
@ -180,8 +179,8 @@ impl Display for Cond {
Cond::Valid => "valid", Cond::Valid => "valid",
Cond::Eof => "eof", Cond::Eof => "eof",
Cond::NotEof => "neof", Cond::NotEof => "neof",
Cond::True => "true", Cond::True => "else",
Cond::False => "false", Cond::False => "never",
}) })
} }
} }

@ -49,6 +49,10 @@ impl Flatten for InstrWithBranches {
return Err(CrsnError::Asm(AsmError::ConditionalAlreadyUsed(cond), branch.pos())); return Err(CrsnError::Asm(AsmError::ConditionalAlreadyUsed(cond), branch.pos()));
} }
if cnt != branch_count - 1 && cond == Cond::True {
warn!("\"Else\" conditional used in non-final branch at {}:{}", branch.pos().line, branch.pos().column);
}
let next_lbl = if cnt == branch_count - 1 { let next_lbl = if cnt == branch_count - 1 {
end_lbl.clone() end_lbl.clone()
} else { } else {

@ -2,7 +2,7 @@
(ld r8 0) (ld r8 0)
(ld r0 1 (ld r0 1
(true? (add r8 1)) (else? (add r8 1))
) )
(sub r0 1 (sub r0 1
@ -10,15 +10,8 @@
(else? (add r8 1)) (else? (add r8 1))
) )
(nop
(always? (add r8 1))
)
(nop
(never? (fault))
)
(cmp r8 3 (cmp r8 2
(ne? (fault)) (ne? (fault))
) )
) )

Loading…
Cancel
Save