Merge pull request #1318 from youknowone/ast-module

ast module
This commit is contained in:
Windel Bouwman 2019-10-12 12:51:26 +02:00 committed by GitHub
commit 31b6e815f6
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,

View file

@ -8,6 +8,7 @@ extern crate log;
pub mod compile;
pub mod error;
pub mod mode;
pub(crate) mod output_stream;
pub mod peephole;
pub mod symboltable;

40
src/mode.rs Normal file
View file

@ -0,0 +1,40 @@
use rustpython_parser::parser;
#[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: () }),
}
}
}
impl Mode {
pub fn to_parser_mode(self) -> parser::Mode {
match self {
Self::Exec | Self::Single => parser::Mode::Program,
Self::Eval => parser::Mode::Statement,
}
}
}
#[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""#)
}
}