Skip to content

Commit

Permalink
[hir][refactor] Simpler global string representation (#1196)
Browse files Browse the repository at this point in the history
We can use the Heap PStr uniqueness guarantee to simplify
representation, and avoiding generating all these strings that might be
GCed anyways.
  • Loading branch information
SamChou19815 committed May 5, 2024
1 parent e031c87 commit cbae791
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 197 deletions.
13 changes: 5 additions & 8 deletions crates/samlang-ast/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ impl Expression {
pub fn debug_print(&self, heap: &samlang_heap::Heap) -> String {
match self {
Expression::IntLiteral(i) => i.to_string(),
Expression::StringName(n) => n.as_str(heap).to_string(),
Expression::StringName(n) => format!("\"{}\"", n.as_str(heap)),
Expression::Variable(v) => v.debug_print(heap),
}
}
Expand Down Expand Up @@ -661,14 +661,11 @@ impl Function {
}

#[derive(Debug, Clone, Copy)]
pub struct GlobalVariable {
pub name: PStr,
pub content: PStr,
}
pub struct GlobalString(pub PStr);

#[derive(Debug)]
pub struct Sources {
pub global_variables: Vec<GlobalVariable>,
pub global_variables: Vec<GlobalString>,
pub closure_types: Vec<ClosureTypeDefinition>,
pub type_definitions: Vec<TypeDefinition>,
pub main_function_names: Vec<FunctionName>,
Expand All @@ -678,8 +675,8 @@ pub struct Sources {
impl Sources {
pub fn debug_print(&self, heap: &samlang_heap::Heap) -> String {
let mut lines = vec![];
for v in &self.global_variables {
lines.push(format!("const {} = '{}';\n", v.name.as_str(heap), v.content.as_str(heap)));
for (i, v) in self.global_variables.iter().enumerate() {
lines.push(format!("const GLOBAL_STRING_{} = '{}';\n", i, v.0.as_str(heap)));
}
for d in &self.closure_types {
lines.push(d.pretty_print(heap));
Expand Down
14 changes: 5 additions & 9 deletions crates/samlang-ast/src/hir_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ mod tests {
Expression::var_name(PStr::LOWER_A, Type::new_id(PStr::UPPER_A, vec![(INT_TYPE)]))
.debug_print(heap)
);
assert_eq!("a", Expression::StringName(PStr::LOWER_A).clone().debug_print(heap));
assert_eq!("\"a\"", Expression::StringName(PStr::LOWER_A).clone().debug_print(heap));
}

#[test]
Expand Down Expand Up @@ -414,8 +414,8 @@ mod tests {
format!("{:?}", stmt.clone());
let expected = r#"let bar: int;
if 0 {
let baz: DUMMY_FooBar = [meggo];
let baz: DUMMY_Enum = [0, meggo];
let baz: DUMMY_FooBar = ["meggo"];
let baz: DUMMY_Enum = [0, "meggo"];
let closure: DUMMY_CCC = Closure { fun: (A$foo: (int) -> int), context: 0 };
let dd = 0 < 0;
let dd = 0 <= 0;
Expand Down Expand Up @@ -461,11 +461,7 @@ if 0 {
let heap = &mut Heap::new();

let sources1 = Sources {
global_variables: vec![GlobalVariable {
name: heap.alloc_str_for_test("dev_meggo"),
content: heap.alloc_str_for_test("vibez"),
}
.clone()],
global_variables: vec![GlobalString(heap.alloc_str_for_test("dev_meggo_vibez")).clone()],
closure_types: vec![ClosureTypeDefinition {
name: TypeName { module_reference: None, type_name: PStr::UPPER_C },
type_parameters: vec![],
Expand Down Expand Up @@ -504,7 +500,7 @@ if 0 {
.clone()],
};
format!("{sources1:?}");
let expected1 = r#"const dev_meggo = 'vibez';
let expected1 = r#"const GLOBAL_STRING_0 = 'dev_meggo_vibez';
closure type C = () -> int
object type Foo = [int, int]
Expand Down
Loading

0 comments on commit cbae791

Please sign in to comment.