diff --git a/package/origlang-ast/src/after_parse.rs b/package/origlang-ast/src/after_parse.rs index 7d25f2b..b78f359 100644 --- a/package/origlang-ast/src/after_parse.rs +++ b/package/origlang-ast/src/after_parse.rs @@ -13,7 +13,7 @@ pub enum Expression { /// 真偽値リテラル BooleanLiteral(bool), /// 文字列リテラル - StringLiteral(String), + StringLiteral(Box), /// ユニットリテラル UnitLiteral, /// 変数 diff --git a/package/origlang-ast/src/lib.rs b/package/origlang-ast/src/lib.rs index 0469f2a..3864eed 100644 --- a/package/origlang-ast/src/lib.rs +++ b/package/origlang-ast/src/lib.rs @@ -67,20 +67,20 @@ pub enum Statement { #[derive(Eq, PartialEq, Clone, Debug)] pub struct Comment { - pub content: String, + pub content: Box, } #[derive(Eq, PartialEq, Clone, Debug, Hash)] -pub struct Identifier(String); +pub struct Identifier(Box); impl Identifier { - #[must_use = "If you don't use it, it will drop entire String"] - pub const fn new(s: String) -> Self { + #[must_use = "If you don't use it, it will drop entire string"] + pub const fn new(s: Box) -> Self { Self(s) } - #[must_use = "If you don't use it, it will drop entire String"] - pub fn name(self) -> String { + #[must_use = "If you don't use it, it will drop entire string"] + pub fn name(self) -> Box { self.0 } diff --git a/package/origlang-compiler-entrypoint/src/lib.rs b/package/origlang-compiler-entrypoint/src/lib.rs index 4d7a8c6..fb17f03 100644 --- a/package/origlang-compiler-entrypoint/src/lib.rs +++ b/package/origlang-compiler-entrypoint/src/lib.rs @@ -63,8 +63,8 @@ impl TheCompiler { self } - pub fn compile(&self, source: String) -> Result, PartialCompilation> { - let x = Parser::create(&source); + pub fn compile(&self, source: &str) -> Result, PartialCompilation> { + let x = Parser::create(source); let root = x.parse()?; let typeck = TypeChecker::new().check(root)?; let ir0_seq = typeck.into_ir(); diff --git a/package/origlang-compiler-scanner-example/src/lib.rs b/package/origlang-compiler-scanner-example/src/lib.rs index c95c892..2dba939 100644 --- a/package/origlang-compiler-scanner-example/src/lib.rs +++ b/package/origlang-compiler-scanner-example/src/lib.rs @@ -77,7 +77,7 @@ mod tests { s.ir1_scanner.push(PreOrPost::Pre(Box::new(MyScanner))); }) .register_diagnostic_receiver(Box::new(tdr)) - .compile("var foo = 1i32\n".to_string()); + .compile("var foo = 1i32\n"); assert!(a.is_ok()); } diff --git a/package/origlang-interop/src/lib.rs b/package/origlang-interop/src/lib.rs index 2768d19..bf815ba 100644 --- a/package/origlang-interop/src/lib.rs +++ b/package/origlang-interop/src/lib.rs @@ -35,6 +35,7 @@ struct PseudoStdout; impl OutputAccumulator for PseudoStdout { fn output(&mut self, tb: TypeBox) { + // TODO: this shall be Box whenever possible. let s: String = match tb { TypeBox::NonCoercedInteger(x) => x.to_string(), TypeBox::Int8(i) => i.to_string(), @@ -44,7 +45,7 @@ impl OutputAccumulator for PseudoStdout { TypeBox::Float32(f) => f.to_string(), TypeBox::Float64(f) => f.to_string(), TypeBox::Boolean(i) => i.to_string(), - TypeBox::String(s) => s, + TypeBox::String(s) => s.to_string(), TypeBox::Unit => "()".to_string(), TypeBox::Tuple(t) => t.to_string(), TypeBox::Record(r) => r.to_string(), diff --git a/package/origlang-ir/src/lib.rs b/package/origlang-ir/src/lib.rs index ef14d4f..a416547 100644 --- a/package/origlang-ir/src/lib.rs +++ b/package/origlang-ir/src/lib.rs @@ -103,7 +103,7 @@ pub enum CompiledTypedExpression { IntLiteral(TypedIntLiteral), BooleanLiteral(bool), FloatLiteral(TypedFloatLiteral), - StringLiteral(String), + StringLiteral(Box), UnitLiteral, Variable { ident: Identifier, diff --git a/package/origlang-lexer/src/lib.rs b/package/origlang-lexer/src/lib.rs index d797139..6725378 100644 --- a/package/origlang-lexer/src/lib.rs +++ b/package/origlang-lexer/src/lib.rs @@ -151,7 +151,7 @@ impl Lexer<'_> { "type" => Token::KeywordType, "_" => Token::SymUnderscore, other => Token::Reserved { - matched: other.to_string(), + matched: other.to_string().into_boxed_str(), }, } } else { @@ -275,7 +275,7 @@ impl Lexer<'_> { .unwrap_or(self.source.len() - start); self.advance_bytes(rel_pos + 1); - let s = self.source[start..(start + rel_pos)].to_string(); + let s = self.source[start..(start + rel_pos)].to_string().into_boxed_str(); Token::StringLiteral(s) } @@ -353,7 +353,7 @@ impl Lexer<'_> { let rel_pos = self.source[start..].find('\n').unwrap_or(self.source.len()); self.advance_bytes(rel_pos); - let content = self.source[start..(start + rel_pos)].to_string(); + let content = self.source[start..(start + rel_pos)].into(); Token::Comment { content: Comment { content }, } @@ -435,7 +435,7 @@ impl Lexer<'_> { debug!("lexer:identifier: length of {plus}"); let start = self.source_bytes_nth.get().as_usize(); - let s = Identifier::new(self.source[start..(start + plus)].to_string()); + let s = Identifier::new(self.source[start..(start + plus)].into()); self.advance_bytes(plus); Ok(Some(s)) diff --git a/package/origlang-lexer/src/tests.rs b/package/origlang-lexer/src/tests.rs index 9f291a6..23fa8ac 100644 --- a/package/origlang-lexer/src/tests.rs +++ b/package/origlang-lexer/src/tests.rs @@ -9,11 +9,11 @@ fn test(str_lit: &str) { assert_eq!( p.next().data, Token::Identifier { - inner: Identifier::new("x".to_string()), + inner: Identifier::new("x".to_string().into_boxed_str()), } ); assert_eq!(p.next().data, Token::SymEq); - assert_eq!(p.next().data, Token::StringLiteral(str_lit.to_string())); + assert_eq!(p.next().data, Token::StringLiteral(str_lit.to_string().into_boxed_str())); } #[test] @@ -117,7 +117,7 @@ fn token_location() { lexer.next(), Pointed { data: Token::Identifier { - inner: Identifier::new("x".to_string()) + inner: Identifier::new("x".to_string().into_boxed_str()) }, position: SourcePosition::try_new((1, 5)).unwrap() } @@ -162,7 +162,7 @@ fn token_location() { lexer.next(), Pointed { data: Token::Identifier { - inner: Identifier::new("y".to_string()) + inner: Identifier::new("y".to_string().into_boxed_str()) }, position: SourcePosition::try_new((2, 5)).unwrap() } diff --git a/package/origlang-lexer/src/token.rs b/package/origlang-lexer/src/token.rs index c0a1ddc..06410cd 100644 --- a/package/origlang-lexer/src/token.rs +++ b/package/origlang-lexer/src/token.rs @@ -97,10 +97,10 @@ pub enum Token { Comment { content: Comment, }, - StringLiteral(String), + StringLiteral(Box), /// reserved for future use. Reserved { - matched: String, + matched: Box, }, /// `_` SymUnderscore, diff --git a/package/origlang-runtime/src/lib.rs b/package/origlang-runtime/src/lib.rs index 6614cba..599fb64 100644 --- a/package/origlang-runtime/src/lib.rs +++ b/package/origlang-runtime/src/lib.rs @@ -95,7 +95,7 @@ pub enum TypeBox { Boolean(bool), #[display(fmt = "{_0}")] #[from] - String(String), + String(Box), #[display(fmt = "()")] #[from] Unit, @@ -406,11 +406,11 @@ fn evaluate_bin_op( f!(lhs, operator, rhs as Coerced) } (TypeBox::String(lhs), TypeBox::String(rhs)) => { - let mut ret = lhs; + let mut ret = lhs.to_string(); // give hint to compiler ret.reserve_exact(rhs.len()); - ret += rhs.as_str(); - Ok(ret.into()) + ret += rhs.as_ref(); + Ok(ret.into_boxed_str().into()) } _ => indicate_type_checker_bug!( context = "type checker must deny operator application between different type" diff --git a/package/origlang-runtime/src/tests.rs b/package/origlang-runtime/src/tests.rs index 80e1f34..f4c7f58 100644 --- a/package/origlang-runtime/src/tests.rs +++ b/package/origlang-runtime/src/tests.rs @@ -6,7 +6,7 @@ mod display { #[test] fn empty() { let x = DisplayRecordValue { - name: Identifier::new("abcdef".to_string()), + name: Identifier::new("abcdef".to_string().into_boxed_str()), values: vec![], }; @@ -17,9 +17,9 @@ mod display { #[test] fn one() { let x = DisplayRecordValue { - name: Identifier::new("abcdef".to_string()), + name: Identifier::new("abcdef".to_string().into_boxed_str()), values: vec![ - TypeBox::String("defg".to_string()) + TypeBox::String("defg".to_string().into_boxed_str()) ], }; @@ -30,11 +30,11 @@ mod display { #[test] fn many() { let x = DisplayRecordValue { - name: Identifier::new("abcdef".to_string()), + name: Identifier::new("abcdef".to_string().into_boxed_str()), values: vec![ - TypeBox::String("abcdef".to_string()), - TypeBox::String("ghijkl".to_string()), - TypeBox::String("alice".to_string()) + TypeBox::String("abcdef".to_string().into_boxed_str()), + TypeBox::String("ghijkl".to_string().into_boxed_str()), + TypeBox::String("alice".to_string().into_boxed_str()) ], }; @@ -59,7 +59,7 @@ mod display { #[test] fn one() { let x = DisplayTupleValue { - boxes: Box::new([TypeBox::String("abcd".to_string())]), + boxes: Box::new([TypeBox::String("abcd".to_string().into_boxed_str())]), }; let x = format!("{x}"); @@ -69,7 +69,7 @@ mod display { #[test] fn many() { let x = DisplayTupleValue { - boxes: Box::new([TypeBox::String("abcd".to_string()), TypeBox::String("defg".to_string())]), + boxes: Box::new([TypeBox::String("abcd".to_string().into_boxed_str()), TypeBox::String("defg".to_string().into_boxed_str())]), }; let x = format!("{x}"); diff --git a/package/origlang-slice-in-box/src/lib.rs b/package/origlang-slice-in-box/src/lib.rs index 2043a4a..82d7036 100644 --- a/package/origlang-slice-in-box/src/lib.rs +++ b/package/origlang-slice-in-box/src/lib.rs @@ -1,3 +1,4 @@ +use std::alloc::{alloc, Layout}; use std::mem::MaybeUninit; pub type BoxedSlice = Box<[I]>; @@ -26,4 +27,4 @@ unsafe fn assume_every_elements_are_initialized(half_baked_slice_in_box: Boxe let slice = ptr as *mut [I]; unsafe { Box::from_raw(slice) } -} \ No newline at end of file +} diff --git a/package/origlang-testsuite/src/main.rs b/package/origlang-testsuite/src/main.rs index 9422b14..7d7ca8e 100644 --- a/package/origlang-testsuite/src/main.rs +++ b/package/origlang-testsuite/src/main.rs @@ -454,7 +454,7 @@ impl Test { fn test_string_literal() { assert_eq!( Self::evaluated_expressions("print \"123\"\n").expect("properly parsed and typed"), - type_boxes!("123".to_string() => String) + type_boxes!("123".to_string().into_boxed_str() => String) ); } @@ -462,7 +462,7 @@ impl Test { assert_eq!( Self::evaluated_expressions("print \"123\" + \"456\"\n") .expect("properly parsed and typed"), - type_boxes!("123456".to_string() => String) + type_boxes!("123456".to_string().into_boxed_str() => String) ); } @@ -633,7 +633,7 @@ print a .expect("properly parsed") .statement, [Statement::VariableDeclaration { - pattern: AtomicPattern::Bind(Identifier::new("a".to_string())), + pattern: AtomicPattern::Bind(Identifier::new("a".into())), expression: Expression::Tuple { expressions: vec![ Expression::IntLiteral { @@ -647,8 +647,8 @@ print a ], }, type_annotation: Some(TypeSignature::Tuple(vec![ - TypeSignature::Simple(Identifier::new("Int32".to_string())), - TypeSignature::Simple(Identifier::new("Int32".to_string())), + TypeSignature::Simple(Identifier::new("Int32".into())), + TypeSignature::Simple(Identifier::new("Int32".into())), ])), }] ); @@ -667,7 +667,7 @@ print 1 statement: vec![ Statement::Comment { content: Comment { - content: "Hello, World!".to_string() + content: "Hello, World!".into() }, }, Statement::Print { @@ -696,7 +696,7 @@ print 1 }, Statement::Comment { content: Comment { - content: "Hello, World!".to_string() + content: "Hello, World!".into() }, }, ] @@ -745,8 +745,8 @@ print 1 .expect("properly parsed") .statement, [Statement::TypeAliasDeclaration { - new_name: Identifier::new("Ik".to_string()), - replace_with: TypeSignature::Simple(Identifier::new("Int32".to_string())) + new_name: Identifier::new("Ik".into()), + replace_with: TypeSignature::Simple(Identifier::new("Int32".into())) }] ); assert_eq!( @@ -764,7 +764,7 @@ print 1 assert_eq!( Self::ast("var a = 1 << 2\n").expect("fail").statement, [Statement::VariableDeclaration { - pattern: AtomicPattern::Bind(Identifier::new("a".to_owned())), + pattern: AtomicPattern::Bind(Identifier::new("a".into())), expression: Expression::BinaryOperator { lhs: Box::new(Expression::IntLiteral { value: 1, @@ -788,7 +788,7 @@ print 1 assert_eq!( Self::ast("var a = 4 >> 2\n").expect("fail").statement, [Statement::VariableDeclaration { - pattern: AtomicPattern::Bind(Identifier::new("a".to_owned())), + pattern: AtomicPattern::Bind(Identifier::new("a".into())), expression: Expression::BinaryOperator { lhs: Box::new(Expression::IntLiteral { value: 4, diff --git a/package/origlang-typecheck/src/type_check.rs b/package/origlang-typecheck/src/type_check.rs index cf0e386..bbdda4a 100644 --- a/package/origlang-typecheck/src/type_check.rs +++ b/package/origlang-typecheck/src/type_check.rs @@ -208,7 +208,7 @@ impl TryIntoTypeCheckedForm for Expression { } } else { Err(TypeCheckError::GenericTypeMismatch { - context: "The condition of if-expression".to_string(), + context: "The condition of if-expression".to_string().into_boxed_str(), expected_type: Type::Boolean, actual_type: cond_type, }) @@ -490,7 +490,7 @@ impl TryIntoTypeCheckedForm for Statement { }]) } else { Err(TypeCheckError::GenericTypeMismatch { - context: "variable assignment".to_string(), + context: "variable assignment".to_string().into_boxed_str(), expected_type, actual_type: checked.actual_type(), }) @@ -597,7 +597,7 @@ impl TypeChecker { m[7].clamp(b'a', b'z'), ]; - Identifier::new(core::str::from_utf8(&m).expect("not panic").to_owned()) + Identifier::new(core::str::from_utf8(&m).expect("not panic").into()) } } diff --git a/package/origlang-typecheck/src/type_check/error.rs b/package/origlang-typecheck/src/type_check/error.rs index ca9b97f..a04d176 100644 --- a/package/origlang-typecheck/src/type_check/error.rs +++ b/package/origlang-typecheck/src/type_check/error.rs @@ -30,7 +30,7 @@ pub enum TypeCheckError { }, #[error("{context} must be {expected_type}, got {actual_type}")] GenericTypeMismatch { - context: String, + context: Box, expected_type: Type, actual_type: Type, }, diff --git a/package/origlang-typesystem-model/src/lib.rs b/package/origlang-typesystem-model/src/lib.rs index 86c52d5..f10ec35 100644 --- a/package/origlang-typesystem-model/src/lib.rs +++ b/package/origlang-typesystem-model/src/lib.rs @@ -177,7 +177,7 @@ pub enum TypedExpression { IntLiteral(TypedIntLiteral), BooleanLiteral(bool), FloatLiteral(TypedFloatLiteral), - StringLiteral(String), + StringLiteral(Box), UnitLiteral, Variable { ident: Identifier,