mirror of
https://github.com/MorizzG/rlox.git
synced 2025-12-06 12:22:42 +00:00
replaced Box<str> with SmolStr
This commit is contained in:
parent
fb88595b6c
commit
da6a820638
21 changed files with 137 additions and 85 deletions
|
|
@ -1,4 +1,5 @@
|
|||
use phf::phf_map;
|
||||
use smol_str::SmolStr;
|
||||
|
||||
use super::{CodePos, LexerError, Token, TokenType};
|
||||
|
||||
|
|
@ -130,8 +131,9 @@ impl Lexer {
|
|||
// advance until either source is empty or newline if found
|
||||
while !self.source_is_empty() && self.advance() != '\n' {}
|
||||
|
||||
let comment: Box<str> =
|
||||
self.source[self.start + 2..self.current].iter().collect();
|
||||
let comment = SmolStr::from_iter(
|
||||
self.source[self.start + 2..self.current].iter().cloned(),
|
||||
);
|
||||
|
||||
self.push_token(TokenType::Comment(comment));
|
||||
} else if self.consume('*') {
|
||||
|
|
@ -170,9 +172,11 @@ impl Lexer {
|
|||
self.advance();
|
||||
}
|
||||
|
||||
let comment: Box<str> = self.source[self.start + 2..self.current - 2]
|
||||
.iter()
|
||||
.collect();
|
||||
let comment = SmolStr::from_iter(
|
||||
self.source[self.start + 2..self.current - 2]
|
||||
.iter()
|
||||
.cloned(),
|
||||
);
|
||||
|
||||
self.push_token(TokenType::Comment(comment));
|
||||
} else {
|
||||
|
|
@ -289,11 +293,6 @@ impl Lexer {
|
|||
}
|
||||
}
|
||||
|
||||
// let string_literal: Box<str> = self.source[self.start + 1..self.current - 1]
|
||||
// .iter()
|
||||
// .collect();
|
||||
|
||||
// Some(TokenType::String(Box::new(string_literal)))
|
||||
self.tokens.push(Token::new_string(s, self.code_pos));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
use std::fmt::{Debug, Display};
|
||||
|
||||
use smol_str::SmolStr;
|
||||
|
||||
use super::CodePos;
|
||||
|
||||
#[repr(u8)]
|
||||
|
|
@ -17,13 +19,13 @@ pub enum TokenType {
|
|||
Less, LessEqual,
|
||||
|
||||
// Identifier and literals
|
||||
Identifier(Box<str>), String(Box<str>), Number(f64),
|
||||
Identifier(SmolStr), String(SmolStr), Number(f64),
|
||||
|
||||
// Keywords
|
||||
And, Break, Class, Else, False, Fun, For, If, Nil, Or,
|
||||
Print, Return, Super, This, True, Var, While,
|
||||
|
||||
Comment(Box<str>),
|
||||
Comment(SmolStr),
|
||||
|
||||
#[allow(clippy::upper_case_acronyms)]
|
||||
EOF
|
||||
|
|
@ -42,14 +44,14 @@ impl Token {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn new_string(s: impl Into<Box<str>>, code_pos: CodePos) -> Self {
|
||||
pub fn new_string(s: impl Into<SmolStr>, code_pos: CodePos) -> Self {
|
||||
Token {
|
||||
token_type: TokenType::String(s.into()),
|
||||
code_pos,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_identifier(name: impl Into<Box<str>>, code_pos: CodePos) -> Self {
|
||||
pub fn new_identifier(name: impl Into<SmolStr>, code_pos: CodePos) -> Self {
|
||||
Token {
|
||||
token_type: TokenType::Identifier(name.into()),
|
||||
code_pos,
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
use smol_str::SmolStr;
|
||||
|
||||
use crate::lexer::{Token, TokenType};
|
||||
use crate::parser::expr::BinaryOp;
|
||||
|
||||
|
|
@ -481,7 +483,7 @@ impl Parser {
|
|||
Ok(Expr::function(name, param_names, body))
|
||||
}
|
||||
|
||||
fn collect_params(&mut self) -> ParserResult<Vec<Box<str>>> {
|
||||
fn collect_params(&mut self) -> ParserResult<Vec<SmolStr>> {
|
||||
assert_eq!(self.next_token().token_type, TokenType::LeftParen);
|
||||
|
||||
if self.peek_token().token_type == TokenType::RightParen {
|
||||
|
|
@ -825,7 +827,7 @@ impl Parser {
|
|||
})
|
||||
}
|
||||
|
||||
fn identifier(&mut self, msg: &str) -> ParserResult<Box<str>> {
|
||||
fn identifier(&mut self, msg: &str) -> ParserResult<SmolStr> {
|
||||
match self.peek_token().token_type {
|
||||
TokenType::Identifier(_) => match self.next_token().token_type {
|
||||
TokenType::Identifier(s) => Ok(s),
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
use itertools::Itertools;
|
||||
use smol_str::SmolStr;
|
||||
|
||||
use super::Stmt;
|
||||
|
||||
|
|
@ -27,14 +28,14 @@ pub enum Expr {
|
|||
expr: Box<Expr>,
|
||||
},
|
||||
Variable {
|
||||
name: Box<str>,
|
||||
name: SmolStr,
|
||||
},
|
||||
LocalVariable {
|
||||
name: Box<str>,
|
||||
name: SmolStr,
|
||||
level: usize,
|
||||
},
|
||||
GlobalVariable {
|
||||
name: Box<str>,
|
||||
name: SmolStr,
|
||||
},
|
||||
Assignment {
|
||||
target: Box<Expr>,
|
||||
|
|
@ -46,21 +47,21 @@ pub enum Expr {
|
|||
},
|
||||
Get {
|
||||
target: Box<Expr>,
|
||||
name: Box<str>,
|
||||
name: SmolStr,
|
||||
},
|
||||
Set {
|
||||
target: Box<Expr>,
|
||||
name: Box<str>,
|
||||
name: SmolStr,
|
||||
value: Box<Expr>,
|
||||
},
|
||||
Function {
|
||||
name: Box<str>,
|
||||
param_names: Vec<Box<str>>,
|
||||
closure_vars: Vec<(Box<str>, usize)>,
|
||||
name: SmolStr,
|
||||
param_names: Vec<SmolStr>,
|
||||
closure_vars: Vec<(SmolStr, usize)>,
|
||||
body: Box<Stmt>,
|
||||
},
|
||||
Class {
|
||||
name: Box<str>,
|
||||
name: SmolStr,
|
||||
superclass: Option<Box<Expr>>,
|
||||
methods: Box<Vec<Expr>>,
|
||||
},
|
||||
|
|
@ -68,12 +69,12 @@ pub enum Expr {
|
|||
Super {
|
||||
super_var: Box<Expr>,
|
||||
this_var: Box<Expr>,
|
||||
method: Box<str>,
|
||||
method: SmolStr,
|
||||
},
|
||||
}
|
||||
|
||||
impl Expr {
|
||||
pub fn string(s: impl Into<Box<str>>) -> Self {
|
||||
pub fn string(s: impl Into<SmolStr>) -> Self {
|
||||
let s = s.into();
|
||||
Expr::Literal {
|
||||
literal: Literal::String(s),
|
||||
|
|
@ -120,17 +121,17 @@ impl Expr {
|
|||
Expr::Grouping { expr }
|
||||
}
|
||||
|
||||
pub fn variable(name: impl Into<Box<str>>) -> Self {
|
||||
pub fn variable(name: impl Into<SmolStr>) -> Self {
|
||||
let name = name.into();
|
||||
Expr::Variable { name }
|
||||
}
|
||||
|
||||
pub fn local_variable(name: impl Into<Box<str>>, level: usize) -> Self {
|
||||
pub fn local_variable(name: impl Into<SmolStr>, level: usize) -> Self {
|
||||
let name = name.into();
|
||||
Expr::LocalVariable { name, level }
|
||||
}
|
||||
|
||||
pub fn global_variable(name: impl Into<Box<str>>) -> Self {
|
||||
pub fn global_variable(name: impl Into<SmolStr>) -> Self {
|
||||
let name = name.into();
|
||||
Expr::GlobalVariable { name }
|
||||
}
|
||||
|
|
@ -146,13 +147,13 @@ impl Expr {
|
|||
Expr::Call { callee, args }
|
||||
}
|
||||
|
||||
pub fn get(target: Expr, name: impl Into<Box<str>>) -> Self {
|
||||
pub fn get(target: Expr, name: impl Into<SmolStr>) -> Self {
|
||||
let target = Box::new(target);
|
||||
let name = name.into();
|
||||
Expr::Get { target, name }
|
||||
}
|
||||
|
||||
pub fn function(name: impl Into<Box<str>>, param_names: Vec<Box<str>>, body: Stmt) -> Self {
|
||||
pub fn function(name: impl Into<SmolStr>, param_names: Vec<SmolStr>, body: Stmt) -> Self {
|
||||
let name = name.into();
|
||||
#[allow(clippy::box_default)]
|
||||
// let closure_vars = Box::new(Vec::new());
|
||||
|
|
@ -166,7 +167,7 @@ impl Expr {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn class(name: impl Into<Box<str>>, methods: Vec<Expr>, superclass: Option<Expr>) -> Self {
|
||||
pub fn class(name: impl Into<SmolStr>, methods: Vec<Expr>, superclass: Option<Expr>) -> Self {
|
||||
let superclass = superclass.map(Box::new);
|
||||
let name = name.into();
|
||||
let methods = Box::new(methods);
|
||||
|
|
@ -181,7 +182,7 @@ impl Expr {
|
|||
pub fn super_(
|
||||
super_var: impl Into<Box<Expr>>,
|
||||
this_var: impl Into<Box<Expr>>,
|
||||
method: impl Into<Box<str>>,
|
||||
method: impl Into<SmolStr>,
|
||||
) -> Self {
|
||||
let super_var = super_var.into();
|
||||
let this_var = this_var.into();
|
||||
|
|
@ -267,7 +268,7 @@ impl Display for Expr {
|
|||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Literal {
|
||||
String(Box<str>),
|
||||
String(SmolStr),
|
||||
Number(f64),
|
||||
Bool(bool),
|
||||
Nil,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
use smol_str::SmolStr;
|
||||
|
||||
use super::misc::indent;
|
||||
use super::Expr;
|
||||
|
||||
|
|
@ -18,7 +20,7 @@ pub enum Stmt {
|
|||
body: Box<Stmt>,
|
||||
},
|
||||
VarDecl {
|
||||
name: Box<str>,
|
||||
name: SmolStr,
|
||||
initializer: Box<Expr>,
|
||||
},
|
||||
Block {
|
||||
|
|
@ -62,7 +64,7 @@ impl Stmt {
|
|||
Stmt::While { condition, body }
|
||||
}
|
||||
|
||||
pub fn var_decl(name: impl Into<Box<str>>, initializer: impl Into<Box<Expr>>) -> Self {
|
||||
pub fn var_decl(name: impl Into<SmolStr>, initializer: impl Into<Box<Expr>>) -> Self {
|
||||
let name = name.into();
|
||||
|
||||
let initializer = initializer.into();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue