citadel_frontend/ir/
mod.rs

1//! The AST (Abstract Syntax Tree) module for the Intermediary Representation (IR)
2//! This has the nodes (statements, expressions) for generating an IR
3//!
4//! It is recommended to use the [IR-Generator](frontend::ir::ir_gen) module for generating the IR
5//! but you can of course also generate the ir yourself (then you have to build your own typetable however)
6
7pub mod traits;
8pub mod irgen;
9
10pub const INT8_T: &str = "i8";
11pub const INT16_T: &str = "i16";
12pub const INT32_T: &str = "i32";
13pub const INT64_T: &str = "i64";
14pub const FLOAT32_T: &str = "f32";
15pub const FLOAT64_T: &str = "f64";
16
17#[derive(Debug, Clone, PartialEq)]
18pub enum IRStmt<'ir> {
19    Entry(BlockStmt<'ir>),
20
21    DeclaredFunction(DeclFuncStmt<'ir>),
22    Function(FuncStmt<'ir>),
23    Variable(VarStmt<'ir>),
24    Label(LabelStmt<'ir>),
25
26    Return(ReturnStmt<'ir>),
27    Exit(ExitStmt<'ir>),
28    Jump(JumpStmt<'ir>),
29    Call(CallExpr<'ir>),
30
31    Struct(StructStmt<'ir>),
32    Union(UnionStmt<'ir>),
33}
34
35#[derive(Debug, Clone, PartialEq)]
36pub enum IRExpr<'ir> {
37    Call(CallExpr<'ir>),
38    Literal(Literal<'ir>, Type<'ir>),
39    Ident(Ident<'ir>),
40
41    ArithOp(ArithOpExpr<'ir>),
42
43    StructInit(StructInitExpr<'ir>),
44}
45
46#[derive(Debug, Clone, Copy, PartialEq)]
47pub enum Type<'ir> {
48    Ident(Ident<'ir>),
49    Array(&'ir Type<'ir>, u32),
50}
51
52#[derive(Debug, Clone, PartialEq)]
53pub enum Literal<'ir> {
54    String(&'ir str),
55    Char(u8),
56
57    Float32(f32),
58    Float64(f64),
59    
60    /// Bool is an i1. 
61    Bool(bool),
62
63    Int8(i8),
64    Int16(i16),
65    Int32(i32),
66    Int64(i64),
67    Int128(i128),
68
69    Array(usize, Vec<IRExpr<'ir>>),
70    Vector(Vec<IRExpr<'ir>>),
71}
72
73#[derive(Debug, Clone, PartialEq)]
74pub enum Operator {
75    Add,
76    Sub,
77    Mul,
78    Div,
79}
80
81pub type Ident<'ir> = &'ir str;
82
83
84#[derive(Debug, Clone, PartialEq)]
85pub struct DeclFuncStmt<'ir> {
86    pub name: IRTypedIdent<'ir>,
87    pub args: Vec<IRTypedIdent<'ir>>,
88}
89
90#[derive(Debug, Clone, PartialEq)]
91pub struct FuncStmt<'ir> {
92    pub name: IRTypedIdent<'ir>,
93    pub args: Vec<IRTypedIdent<'ir>>,
94    pub block: BlockStmt<'ir>,
95}
96
97#[derive(Debug, Clone, PartialEq)]
98pub struct VarStmt<'ir> {
99    pub name: IRTypedIdent<'ir>,
100    pub val: IRExpr<'ir>,
101    pub is_const: bool,
102}
103
104#[derive(Debug, Clone, PartialEq)]
105pub struct StructStmt<'ir> {
106    pub name: Ident<'ir>,
107    pub fields: Vec<IRTypedIdent<'ir>>,
108}
109
110#[derive(Debug, Clone, PartialEq)]
111pub struct UnionStmt<'ir> {
112    pub name: Ident<'ir>,
113    pub variants: Vec<IRTypedIdent<'ir>>,
114}
115
116#[derive(Debug, Clone, Copy, PartialEq)]
117pub struct LabelStmt<'ir> {
118    pub name: Ident<'ir>,
119}
120
121#[derive(Debug, Clone, PartialEq)]
122pub struct ReturnStmt<'ir> {
123    pub ret_val: IRExpr<'ir>,
124}
125
126#[derive(Debug, Clone, PartialEq)]
127pub struct ExitStmt<'ir> {
128    pub exit_code: IRExpr<'ir>,
129}
130
131#[derive(Debug, Clone, PartialEq)]
132pub struct JumpStmt<'ir> {
133    pub label: Ident<'ir>,
134}
135
136#[derive(Debug, Clone, Copy, PartialEq)]
137pub struct IRTypedIdent<'ir> {
138    pub ident: Ident<'ir>,
139    pub _type: Type<'ir>,
140}
141
142#[derive(Debug, Clone, PartialEq)]
143pub struct BlockStmt<'ir> {
144    pub stmts: Vec<IRStmt<'ir>>,
145}
146
147#[derive(Debug, Clone, PartialEq)]
148pub struct CallExpr<'ir> {
149    pub name: Ident<'ir>,
150    pub args: Vec<IRExpr<'ir>>,
151}
152
153#[derive(Debug, Clone, PartialEq)]
154pub struct StructInitExpr<'ir> {
155    pub name: Ident<'ir>,
156    pub values: Vec<IRExpr<'ir>>,
157}
158
159#[derive(Debug, Clone, PartialEq)]
160pub struct ArithOpExpr<'ir> {
161    pub op: Operator,
162    pub values: (Box<IRExpr<'ir>>, Box<IRExpr<'ir>>)
163}