Skip to content

Commit

Permalink
Remove reachable panics
Browse files Browse the repository at this point in the history
  • Loading branch information
jac3km4 committed Feb 13, 2021
1 parent a92d149 commit f6680d6
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 8 deletions.
6 changes: 4 additions & 2 deletions compiler/src/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Assembler {
match scope.resolve_reference(name.clone(), *pos)? {
Reference::Local(idx) => self.emit(Instr::Local(idx)),
Reference::Parameter(idx) => self.emit(Instr::Param(idx)),
_ => panic!("Shouldn't get here"),
_ => return Err(Error::CompileError("Expected a value".to_owned(), *pos)),
};
}
Expr::Constant(cons, _) => match cons {
Expand Down Expand Up @@ -345,7 +345,9 @@ impl Assembler {
Expr::This(_) | Expr::Super(_) => {
self.emit(Instr::This);
}
Expr::Goto(_, _) | Expr::Break => panic!("Not supported yet"),
Expr::Goto(_, pos) | Expr::Break(pos) => {
return Err(Error::CompileError("Not supported yet".to_owned(), *pos))
}
};
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ peg::parser! {
/ if_: if_() { if_ }
/ switch: switch() { switch }
/ pos:position!() keyword("return") _ val:expr()? ";" { Expr::Return(val.map(Box::new), Pos::new(pos)) }
/ keyword("break") _ ";" { Expr::Break }
/ pos:position!() keyword("break") _ ";" { Expr::Break(Pos::new(pos)) }
/ let_:let() { let_ }
/ expr:expr() _ ";" { expr }

Expand Down Expand Up @@ -406,7 +406,7 @@ mod tests {
.unwrap();
assert_eq!(
format!("{:?}", stmt),
r#"Switch(Ident(Ident("value"), Pos(7)), [SwitchCase(Constant(String(String, "0"), Pos(37)), Seq { exprs: [] }), SwitchCase(Constant(String(String, "1"), Pos(64)), Seq { exprs: [Call(Ident("Log"), [Constant(String(String, "0 or 1"), Pos(93))], Pos(89))] }), SwitchCase(Constant(String(String, "2"), Pos(126)), Seq { exprs: [Break] })], Some(Seq { exprs: [Call(Ident("Log"), [Constant(String(String, "default"), Pos(208))], Pos(204))] }))"#
r#"Switch(Ident(Ident("value"), Pos(7)), [SwitchCase(Constant(String(String, "0"), Pos(37)), Seq { exprs: [] }), SwitchCase(Constant(String(String, "1"), Pos(64)), Seq { exprs: [Call(Ident("Log"), [Constant(String(String, "0 or 1"), Pos(93))], Pos(89))] }), SwitchCase(Constant(String(String, "2"), Pos(126)), Seq { exprs: [Break(Pos(151))] })], Some(Seq { exprs: [Call(Ident("Log"), [Constant(String(String, "default"), Pos(208))], Pos(204))] }))"#
);
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ impl Scope {
Expr::While(_, _, _) => TypeId::Void,
Expr::Goto(_, _) => TypeId::Void,
Expr::If(_, _, _, _) => TypeId::Void,
Expr::Break => TypeId::Void,
Expr::Break(_) => TypeId::Void,
Expr::Return(_, _) => TypeId::Void,
Expr::Seq(_) => TypeId::Void,
Expr::Switch(_, _, _) => TypeId::Void,
Expand Down
2 changes: 1 addition & 1 deletion core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum Expr {
UnOp(Box<Expr>, UnOp, Pos),
This(Pos),
Super(Pos),
Break,
Break(Pos),
Null,
}

Expand Down
2 changes: 1 addition & 1 deletion decompiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'a> Decompiler<'a> {
let mut body = self.consume_path(exit)?;
if let Some(Expr::Goto(_, _)) = body.exprs.last() {
body.exprs.pop();
body.exprs.push(Expr::Break);
body.exprs.push(Expr::Break(Pos::ZERO));
}
cases.push(SwitchCase(matched, body));
}
Expand Down
2 changes: 1 addition & 1 deletion decompiler/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ fn write_expr<W: Write>(out: &mut W, expr: &Expr, verbose: bool, depth: usize) -
Expr::UnOp(val, op, _) => {
write_unop(out, val, *op, verbose)?;
}
Expr::Break => write!(out, "break")?,
Expr::Break(_) => write!(out, "break")?,
Expr::Null => write!(out, "null")?,
Expr::This(_) => write!(out, "this")?,
Expr::Super(_) => write!(out, "super")?,
Expand Down

0 comments on commit f6680d6

Please sign in to comment.