Add mode to ast::parse

This commit is contained in:
Jeong YunWon 2019-08-28 03:39:06 +09:00
parent 2326b114c2
commit f2b696a75b
3 changed files with 42 additions and 30 deletions

View file

@ -6,6 +6,7 @@
//! https://github.com/micropython/micropython/blob/master/py/compile.c
use crate::error::{CompileError, CompileErrorType};
pub use crate::mode::Mode;
use crate::output_stream::{CodeObjectStream, OutputStream};
use crate::peephole::PeepholeOptimizer;
use crate::symboltable::{
@ -105,36 +106,6 @@ pub fn compile_program_single(
})
}
#[derive(Clone, Copy)]
pub enum Mode {
Exec,
Eval,
Single,
}
impl std::str::FromStr for Mode {
type Err = ModeParseError;
fn from_str(s: &str) -> Result<Self, ModeParseError> {
match s {
"exec" => Ok(Mode::Exec),
"eval" => Ok(Mode::Eval),
"single" => Ok(Mode::Single),
_ => Err(ModeParseError { _priv: () }),
}
}
}
#[derive(Debug)]
pub struct ModeParseError {
_priv: (),
}
impl std::fmt::Display for ModeParseError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, r#"mode should be "exec", "eval", or "single""#)
}
}
impl<O> Default for Compiler<O>
where
O: OutputStream,