gaia_assembler/program/
mod.rs

1use crate::{instruction::GaiaInstruction, types::GaiaType};
2use serde::{Deserialize, Serialize};
3
4/// Gaia 程序
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub struct GaiaProgram {
7    /// 程序名
8    pub name: String,
9    /// 函数列表
10    pub functions: Vec<GaiaFunction>,
11    /// 常量池(名称,值)
12    pub constants: Vec<(String, GaiaConstant)>,
13    /// 全局变量列表(可选)
14    pub globals: Option<Vec<GaiaGlobal>>,
15}
16
17impl Default for GaiaProgram {
18    fn default() -> Self {
19        Self { name: "untitled".to_string(), functions: Vec::new(), constants: Vec::new(), globals: None }
20    }
21}
22
23impl GaiaProgram {
24    /// 创建新的空程序
25    pub fn new(name: impl Into<String>) -> Self {
26        Self { name: name.into(), functions: Vec::new(), constants: Vec::new(), globals: None }
27    }
28
29    /// 添加函数
30    pub fn add_function(&mut self, function: GaiaFunction) {
31        self.functions.push(function);
32    }
33
34    /// 添加常量
35    pub fn add_constant(&mut self, name: impl Into<String>, value: GaiaConstant) {
36        self.constants.push((name.into(), value));
37    }
38
39    /// 添加全局变量
40    pub fn add_global(&mut self, global: GaiaGlobal) {
41        if let Some(ref mut globals) = self.globals {
42            globals.push(global);
43        }
44        else {
45            self.globals = Some(vec![global]);
46        }
47    }
48}
49
50/// Gaia 常量值
51#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
52pub enum GaiaConstant {
53    /// 8位整数常量
54    Integer8(i8),
55    /// 16位整数常量
56    Integer16(i16),
57    /// 32位整数常量
58    Integer32(i32),
59    /// 64位整数常量
60    Integer64(i64),
61    /// 32位浮点常量
62    Float32(f32),
63    /// 64位浮点常量
64    Float64(f64),
65    /// 布尔常量
66    Boolean(bool),
67    /// 字符串常量
68    String(String),
69    /// 空值
70    Null,
71}
72
73/// Gaia 全局变量
74#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
75pub struct GaiaGlobal {
76    /// 变量名
77    pub name: String,
78    /// 变量类型
79    pub var_type: GaiaType,
80    /// 初始值(可选)
81    pub initial_value: Option<GaiaConstant>,
82    /// 是否为常量
83    pub is_constant: bool,
84}
85
86/// Gaia 函数定义
87#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
88pub struct GaiaFunction {
89    /// 函数名
90    pub name: String,
91    /// 参数类型列表
92    pub parameters: Vec<GaiaType>,
93    /// 返回类型
94    pub return_type: Option<GaiaType>,
95    /// 指令序列
96    pub instructions: Vec<GaiaInstruction>,
97    /// 局部变量类型列表
98    pub locals: Vec<GaiaType>,
99}
100
101impl GaiaFunction {
102    /// 创建新函数
103    pub fn new(name: impl Into<String>) -> Self {
104        Self { name: name.into(), parameters: Vec::new(), return_type: None, instructions: Vec::new(), locals: Vec::new() }
105    }
106
107    /// 添加参数
108    pub fn add_parameter(&mut self, param_type: GaiaType) {
109        self.parameters.push(param_type);
110    }
111
112    /// 设置返回类型
113    pub fn set_return_type(&mut self, return_type: GaiaType) {
114        self.return_type = Some(return_type);
115    }
116
117    /// 添加指令
118    pub fn add_instruction(&mut self, instruction: GaiaInstruction) {
119        self.instructions.push(instruction);
120    }
121
122    /// 添加局部变量
123    pub fn add_local(&mut self, local_type: GaiaType) {
124        self.locals.push(local_type);
125    }
126}