swamp_script_code_gen/
ctx.rs

1use crate::alloc::FrameMemoryRegion;
2use swamp_vm_types::{FrameMemoryAddress, MemoryOffset, MemorySize};
3
4pub struct Context {
5    target_info: FrameMemoryRegion,
6    comment: String,
7}
8
9impl Context {
10    pub(crate) fn comment(&self) -> &str {
11        &self.comment
12    }
13}
14
15impl Context {
16    pub(crate) const fn target(&self) -> FrameMemoryRegion {
17        self.target_info
18    }
19}
20
21impl Context {
22    pub(crate) fn with_offset(&self, offset: MemoryOffset, memory_size: MemorySize) -> Self {
23        Self {
24            target_info: FrameMemoryRegion {
25                addr: self.addr().add_offset(offset),
26                size: memory_size,
27            },
28            comment: "".to_string(),
29        }
30    }
31}
32
33impl Context {
34    #[must_use]
35    pub fn new(target_info: FrameMemoryRegion) -> Self {
36        Self {
37            target_info,
38            comment: String::new(),
39        }
40    }
41
42    pub const fn addr(&self) -> FrameMemoryAddress {
43        self.target_info.addr
44    }
45    pub const fn target_size(&self) -> MemorySize {
46        self.target_info.size
47    }
48
49    #[must_use]
50    pub fn with_target(&self, target_info: FrameMemoryRegion, comment: &str) -> Self {
51        Self {
52            target_info,
53            comment: comment.to_string(),
54        }
55    }
56
57    #[must_use]
58    pub fn create_scope(&self) -> Self {
59        Self {
60            target_info: self.target_info,
61            comment: self.comment.clone(),
62        }
63    }
64
65    #[must_use]
66    pub fn create_function_scope(&self, return_target: FrameMemoryRegion, comment: &str) -> Self {
67        Self {
68            target_info: self.target_info,
69            comment: comment.to_string(),
70        }
71    }
72}