xasm_rs/init.rs
1#[derive(Debug, Clone)]
2/// FileDescriptor represents a standard file stream in a system.
3///
4/// This enum is used to specify which standard file descriptor to use
5/// for input and output operations. It is typically used in conjunction
6/// with tokens that require a file descriptor to perform operations like
7/// printing or reading data.
8///
9/// # Variants
10///
11/// * `STDIN` - Represents the standard input stream.
12/// * `STDOUT` - Represents the standard output stream.
13/// * `STDERR` - Represents the standard error stream.
14pub enum FileDescriptor {
15 STDIN = 0,
16 STDOUT = 1,
17 STDERR = 2,
18}
19#[derive(Debug, Clone)]
20/// Xasm is the main struct used to generate assembly code.
21///
22/// Xasm holds all the information needed to generate assembly code.
23/// It contains information about variables, functions, and tokens.
24///
25/// The information is stored in the following fields:
26///
27/// * `vars` - A vector of `Vars`, which represents variables declared in the source code.
28/// * `mut_vars` - A vector of `MutVars`, which represents mutable variables declared in the source code.
29/// * `funcs` - A vector of `Func`, which represents functions declared in the source code.
30/// * `tokens` - A vector of `Tokens`, which represents tokens declared in the source code.
31///
32/// To generate assembly code, the `genasm` function should be called.
33/// The `genasm` function takes a `Xasm` object and an `OsConfig` object as arguments,
34/// and returns a string representing the assembly code.
35pub struct Xasm {
36 pub vars: Vec<Vars>,
37 pub mut_vars: Vec<MutVars>,
38 pub funcs: Vec<Func>,
39 pub tokens: Vec<Tokens>,
40}
41#[allow(non_camel_case_types)]
42#[derive(Debug, Clone)]
43pub enum Tokens {
44 /// Represents a token to print a string to a file descriptor.
45 ///
46 /// The `print` token is used to send a formatted string to a specified
47 /// file descriptor, such as STDOUT, STDERR, or a custom file. The string
48 /// is provided as a vector of characters.
49 ///
50 /// # Arguments
51 ///
52 /// * `file_descriptor` - The file descriptor to which the string will be printed.
53 /// * `string` - A vector of characters representing the string to be printed.
54 ///
55 /// # Examples
56 ///
57 /// ```
58 /// let token = Tokens::print(FileDescriptor::STDOUT, "Hello, world!\n".chars().collect());
59 /// ```
60 print(FileDescriptor, Vec<char>),
61}
62#[derive(Debug, Clone)]
63pub enum Vars {
64 /// Represents an immutable 8-bit signed integer variable.
65 ///
66 /// The first argument is the name of the variable, and the second argument is the value of the variable.
67 I8(String, i8),
68 /// Represents an immutable 16-bit signed integer variable.
69 ///
70 /// The first argument is the name of the variable, and the second argument is the value of the variable.
71 I16(String, i16),
72 /// Represents an immutable 32-bit signed integer variable.
73 ///
74 /// The first argument is the name of the variable, and the second argument is the value of the variable.
75 I32(String, i32),
76 /// Represents an immutable 64-bit signed integer variable.
77 ///
78 /// The first argument is the name of the variable, and the second argument is the value of the variable.
79 I64(String, i64),
80 /// Represents an immutable 32-bit floating-point number variable.
81 ///
82 /// The first argument is the name of the variable, and the second argument is the value of the variable.
83 F32(String, f32),
84 /// Represents an immutable 64-bit floating-point number variable.
85 ///
86 /// The first argument is the name of the variable, and the second argument is the value of the variable.
87 F64(String, f64),
88 /// Represents an immutable character variable.
89 ///
90 /// The first argument is the name of the variable, and the second argument is the value of the variable.
91 Char(String, char),
92 /// Represents an immutable string variable.
93 ///
94 /// The first argument is the name of the variable, and the second argument is the value of the variable.
95 String(String, String),
96}
97#[derive(Debug, Clone)]
98pub enum MutVars {
99 /// Represents a mutable 8-bit signed integer variable.
100 ///
101 /// The first argument is the name of the variable, and the second argument is the value of the variable.
102 I8(String, i8),
103 /// Represents a mutable 16-bit signed integer variable.
104 ///
105 /// The first argument is the name of the variable, and the second argument is the value of the variable.
106 I16(String, i16),
107 /// Represents a mutable 32-bit signed integer variable.
108 ///
109 /// The first argument is the name of the variable, and the second argument is the value of the variable.
110 I32(String, i32),
111 /// Represents a mutable 64-bit signed integer variable.
112 ///
113 /// The first argument is the name of the variable, and the second argument is the value of the variable.
114 I64(String, i64),
115 /// Represents a mutable 32-bit floating-point number variable.
116 ///
117 /// The first argument is the name of the variable, and the second argument is the value of the variable.
118 F32(String, f32),
119 /// Represents a mutable 64-bit floating-point number variable.
120 ///
121 /// The first argument is the name of the variable, and the second argument is the value of the variable.
122 F64(String, f64),
123 /// Represents a mutable character variable.
124 ///
125 /// The first argument is the name of the variable, and the second argument is the value of the variable.
126 Char(String, char),
127 /// Represents a mutable string variable.
128 ///
129 /// The first argument is the name of the variable, and the second argument is the value of the variable.
130 String(String, String),
131}
132#[derive(Debug, Clone)]
133/// Represents a function in the Xasm assembly code.
134///
135/// The `Func` struct is used to define a function, including its name, arguments, body,
136/// and return value. It provides the necessary structure to represent a function in the
137/// assembly code generated by Xasm.
138///
139/// # Fields
140///
141/// * `name` - A `String` representing the name of the function.
142/// * `args` - An `Option<Vec<Vars>>` representing the function's arguments as immutable variables.
143/// * `mut_args` - An `Option<Vec<MutVars>>` representing the function's arguments as mutable variables.
144/// * `body` - An `Xasm` struct representing the body of the function, which contains tokens, variables,
145/// and other elements that make up the function's implementation.
146/// * `ret` - An `Option<Vars>` representing the return value of the function, if any.
147pub struct Func {
148 pub name: String,
149 pub args: Option<Vec<Vars>>,
150 pub mut_args: Option<Vec<MutVars>>,
151 pub body: Xasm,
152 pub ret: Option<Vars>,
153}
154impl Xasm {
155 /// Creates a new `Xasm` object with empty fields.
156 ///
157 /// Returns a new `Xasm` object with empty vectors for `vars`, `mut_vars`, `funcs`, and `tokens`.
158 pub fn new() -> Xasm {
159 Xasm {
160 vars: Vec::new(),
161 mut_vars: Vec::new(),
162 funcs: Vec::new(),
163 tokens: Vec::new(),
164 }
165 }
166}