Make Token smaller by boxing LexError.

This commit is contained in:
Stephen Chung 2020-03-24 09:49:37 +08:00
parent bcab024d22
commit 3677bd3651
2 changed files with 39 additions and 30 deletions

View File

@ -591,29 +591,25 @@ pub fn optimize_into_ast(
functions functions
.into_iter() .into_iter()
.map(|mut fn_def| { .map(|mut fn_def| {
match engine.optimization_level { if engine.optimization_level != OptimizationLevel::None {
OptimizationLevel::None => (), let pos = fn_def.body.position();
OptimizationLevel::Simple | OptimizationLevel::Full => {
let pos = fn_def.body.position();
// Optimize the function body // Optimize the function body
let mut body = optimize(vec![fn_def.body], engine, &Scope::new()); let mut body = optimize(vec![fn_def.body], engine, &Scope::new());
// {} -> Noop // {} -> Noop
fn_def.body = match body.pop().unwrap_or_else(|| Stmt::Noop(pos)) { fn_def.body = match body.pop().unwrap_or_else(|| Stmt::Noop(pos)) {
// { return val; } -> val // { return val; } -> val
Stmt::ReturnWithVal(Some(val), ReturnType::Return, _) => { Stmt::ReturnWithVal(Some(val), ReturnType::Return, _) => Stmt::Expr(val),
Stmt::Expr(val) // { return; } -> ()
} Stmt::ReturnWithVal(None, ReturnType::Return, pos) => {
// { return; } -> () Stmt::Expr(Box::new(Expr::Unit(pos)))
Stmt::ReturnWithVal(None, ReturnType::Return, pos) => { }
Stmt::Expr(Box::new(Expr::Unit(pos))) // All others
} stmt => stmt,
// All others };
stmt => stmt,
};
}
} }
Arc::new(fn_def) Arc::new(fn_def)
}) })
.collect(), .collect(),

View File

@ -516,7 +516,7 @@ pub enum Token {
XOrAssign, XOrAssign,
ModuloAssign, ModuloAssign,
PowerOfAssign, PowerOfAssign,
LexError(LexError), LexError(Box<LexError>),
} }
impl Token { impl Token {
@ -955,7 +955,9 @@ impl<'a> TokenIterator<'a> {
INT::from_str_radix(&out, radix) INT::from_str_radix(&out, radix)
.map(Token::IntegerConstant) .map(Token::IntegerConstant)
.unwrap_or_else(|_| { .unwrap_or_else(|_| {
Token::LexError(LERR::MalformedNumber(result.iter().collect())) Token::LexError(Box::new(LERR::MalformedNumber(
result.iter().collect(),
)))
}), }),
pos, pos,
)); ));
@ -969,7 +971,9 @@ impl<'a> TokenIterator<'a> {
return Some(( return Some((
num.unwrap_or_else(|_| { num.unwrap_or_else(|_| {
Token::LexError(LERR::MalformedNumber(result.iter().collect())) Token::LexError(Box::new(LERR::MalformedNumber(
result.iter().collect(),
)))
}), }),
pos, pos,
)); ));
@ -1000,7 +1004,10 @@ impl<'a> TokenIterator<'a> {
let identifier: String = result.iter().collect(); let identifier: String = result.iter().collect();
if !is_valid_identifier { if !is_valid_identifier {
return Some((Token::LexError(LERR::MalformedIdentifier(identifier)), pos)); return Some((
Token::LexError(Box::new(LERR::MalformedIdentifier(identifier))),
pos,
));
} }
return Some(( return Some((
@ -1031,7 +1038,7 @@ impl<'a> TokenIterator<'a> {
'"' => { '"' => {
return match self.parse_string_literal('"') { return match self.parse_string_literal('"') {
Ok(out) => Some((Token::StringConst(out), pos)), Ok(out) => Some((Token::StringConst(out), pos)),
Err(e) => Some((Token::LexError(e.0), e.1)), Err(e) => Some((Token::LexError(Box::new(e.0)), e.1)),
} }
} }
// ' - character literal // ' - character literal
@ -1042,17 +1049,23 @@ impl<'a> TokenIterator<'a> {
return Some(( return Some((
if let Some(first_char) = chars.next() { if let Some(first_char) = chars.next() {
if chars.count() != 0 { if chars.count() != 0 {
Token::LexError(LERR::MalformedChar(format!("'{}'", result))) Token::LexError(Box::new(LERR::MalformedChar(format!(
"'{}'",
result
))))
} else { } else {
Token::CharConstant(first_char) Token::CharConstant(first_char)
} }
} else { } else {
Token::LexError(LERR::MalformedChar(format!("'{}'", result))) Token::LexError(Box::new(LERR::MalformedChar(format!(
"'{}'",
result
))))
}, },
pos, pos,
)); ));
} }
Err(e) => return Some((Token::LexError(e.0), e.1)), Err(e) => return Some((Token::LexError(Box::new(e.0)), e.1)),
}, },
// Braces // Braces
@ -1313,7 +1326,7 @@ impl<'a> TokenIterator<'a> {
)) ))
} }
x if x.is_whitespace() => (), x if x.is_whitespace() => (),
x => return Some((Token::LexError(LERR::UnexpectedChar(x)), pos)), x => return Some((Token::LexError(Box::new(LERR::UnexpectedChar(x))), pos)),
} }
} }
@ -1336,7 +1349,7 @@ impl<'a> Iterator for TokenIterator<'a> {
/// Tokenize an input text stream. /// Tokenize an input text stream.
pub fn lex(input: &str) -> TokenIterator<'_> { pub fn lex(input: &str) -> TokenIterator<'_> {
TokenIterator { TokenIterator {
last: Token::LexError(LERR::InputError("".into())), last: Token::LexError(Box::new(LERR::InputError("".into()))),
pos: Position::new(1, 0), pos: Position::new(1, 0),
char_stream: input.chars().peekable(), char_stream: input.chars().peekable(),
} }