gaia_assembler/program/
mod.rs1use crate::{instruction::GaiaInstruction, types::GaiaType};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub struct GaiaProgram {
7 pub name: String,
9 pub functions: Vec<GaiaFunction>,
11 pub constants: Vec<(String, GaiaConstant)>,
13 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 pub fn new(name: impl Into<String>) -> Self {
26 Self { name: name.into(), functions: Vec::new(), constants: Vec::new(), globals: None }
27 }
28
29 pub fn add_function(&mut self, function: GaiaFunction) {
31 self.functions.push(function);
32 }
33
34 pub fn add_constant(&mut self, name: impl Into<String>, value: GaiaConstant) {
36 self.constants.push((name.into(), value));
37 }
38
39 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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
52pub enum GaiaConstant {
53 Integer8(i8),
55 Integer16(i16),
57 Integer32(i32),
59 Integer64(i64),
61 Float32(f32),
63 Float64(f64),
65 Boolean(bool),
67 String(String),
69 Null,
71}
72
73#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
75pub struct GaiaGlobal {
76 pub name: String,
78 pub var_type: GaiaType,
80 pub initial_value: Option<GaiaConstant>,
82 pub is_constant: bool,
84}
85
86#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
88pub struct GaiaFunction {
89 pub name: String,
91 pub parameters: Vec<GaiaType>,
93 pub return_type: Option<GaiaType>,
95 pub instructions: Vec<GaiaInstruction>,
97 pub locals: Vec<GaiaType>,
99}
100
101impl GaiaFunction {
102 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 pub fn add_parameter(&mut self, param_type: GaiaType) {
109 self.parameters.push(param_type);
110 }
111
112 pub fn set_return_type(&mut self, return_type: GaiaType) {
114 self.return_type = Some(return_type);
115 }
116
117 pub fn add_instruction(&mut self, instruction: GaiaInstruction) {
119 self.instructions.push(instruction);
120 }
121
122 pub fn add_local(&mut self, local_type: GaiaType) {
124 self.locals.push(local_type);
125 }
126}