Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String interning #39

Merged
merged 8 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 59 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/erars-ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ serde = { version = "1.0.142", features = ["derive"] }
smol_str = { version = "0.1.23", features = ["serde"] }
strum = { version = "0.24.1", features = ["derive"] }
anyhow = { version = "1" }
lasso = { version = "0.6.0", features = ["multi-threaded", "ahasher", "serialize"] }
once_cell = "1.15.0"

[dev-dependencies]
k9 = "0.11.1"
Expand Down
34 changes: 17 additions & 17 deletions crates/erars-ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ use strum::{Display, EnumString};

use crate::{
command::BuiltinMethod, Alignment, BinaryOperator, BuiltinCommand, BuiltinVariable, EventFlags,
EventType, LocalVariable, UnaryOperator, Value, Variable,
EventType, Interner, LocalVariable, StrKey, UnaryOperator, Value, Variable,
};

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Stmt {
Label(SmolStr),
Label(StrKey),
SelectCase(
Expr,
Vec<(Vec<SelectCaseCond>, Vec<StmtWithPos>)>,
Expand All @@ -23,7 +23,7 @@ pub enum Stmt {
PrintList(PrintFlags, Vec<Expr>),
PrintFormS(PrintFlags, Expr),
PrintData(PrintFlags, Option<Expr>, Vec<Vec<Expr>>),
ReuseLastLine(Box<str>),
ReuseLastLine(StrKey),
Assign(Variable, Option<BinaryOperator>, Expr),
Sif(Expr, Box<StmtWithPos>),
If(Vec<(Expr, Vec<StmtWithPos>)>, Vec<StmtWithPos>),
Expand Down Expand Up @@ -69,16 +69,16 @@ impl fmt::Display for ScriptPosition {
}
}

#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Function {
pub header: FunctionHeader,
pub body: Vec<StmtWithPos>,
}

#[derive(Clone, Default, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct FunctionHeader {
pub file_path: SmolStr,
pub name: SmolStr,
pub name: StrKey,
pub args: Vec<(Variable, Option<Value>)>,
pub infos: Vec<FunctionInfo>,
}
Expand All @@ -95,12 +95,12 @@ pub enum FunctionInfo {

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Expr {
String(Box<str>),
String(StrKey),
Int(i64),
FormText(FormText),
Var(Variable),
BuiltinVar(BuiltinVariable, Vec<Self>),
Method(Box<str>, Vec<Self>),
Method(StrKey, Vec<Self>),
BuiltinMethod(BuiltinMethod, Vec<Self>),
UnaryopExpr(Box<Self>, UnaryOperator),
/// ++/-- var ++/--
Expand All @@ -118,8 +118,8 @@ impl Expr {
Self::Int(i.into())
}

pub fn str(s: impl Into<Box<str>>) -> Self {
Self::String(s.into())
pub fn str(interner: &Interner, s: impl AsRef<str>) -> Self {
Self::String(interner.get_or_intern(s))
}

pub fn unary(op1: Self, op: UnaryOperator) -> Self {
Expand Down Expand Up @@ -171,12 +171,12 @@ impl fmt::Debug for FormExpr {

#[derive(Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct FormText {
pub first: String,
pub other: Vec<(FormExpr, String)>,
pub first: StrKey,
pub other: Vec<(FormExpr, StrKey)>,
}

impl FormText {
pub fn new(first: String) -> Self {
pub fn new(first: StrKey) -> Self {
Self {
first,
other: Vec::new(),
Expand All @@ -188,7 +188,7 @@ impl FormText {
expr: Expr,
padding: Option<Expr>,
align: Option<Alignment>,
text: String,
text: StrKey,
) {
self.other.push((
FormExpr {
Expand All @@ -203,10 +203,10 @@ impl FormText {

impl fmt::Debug for FormText {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.first)?;
write!(f, "{:?}", self.first)?;

for (expr, text) in self.other.iter() {
write!(f, "{{{:?}}}{}", expr, text)?;
write!(f, "{{{:?}}}{:?}", expr, text)?;
}

Ok(())
Expand All @@ -223,7 +223,7 @@ option_set::option_set! {
}
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize, EnumString, Display)]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, EnumString, Display, Serialize, Deserialize)]
pub enum BeginType {
#[strum(to_string = "TITLE")]
Title,
Expand Down
8 changes: 4 additions & 4 deletions crates/erars-ast/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use enum_map::Enum;
use serde::{Deserialize, Serialize};
use strum::{Display, EnumString, IntoStaticStr};

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Event {
pub ty: EventType,
pub flags: EventFlags,
Expand Down Expand Up @@ -38,7 +38,7 @@ impl Event {
}
}

#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum EventFlags {
None,
Pre,
Expand All @@ -51,14 +51,14 @@ pub enum EventFlags {
Clone,
Copy,
Debug,
Serialize,
Deserialize,
PartialEq,
Eq,
Hash,
Display,
EnumString,
IntoStaticStr,
Serialize,
Deserialize,
)]
pub enum EventType {
#[strum(to_string = "EVENTFIRST")]
Expand Down
Loading