Skip to main content

code_transpiler/
ir.rs

1#[derive(Debug, Clone, PartialEq)]
2pub struct Program {
3    pub structs: Vec<StructDef>,
4    pub functions: Vec<Function>,
5}
6
7#[derive(Debug, Clone, PartialEq)]
8pub struct StructDef {
9    pub name: String,
10    pub fields: Vec<StructField>,
11}
12
13#[derive(Debug, Clone, PartialEq)]
14pub struct StructField {
15    pub name: String,
16    pub ty: Type,
17}
18
19#[derive(Debug, Clone, PartialEq)]
20pub struct Function {
21    pub name: String,
22    pub params: Vec<Param>,
23    pub return_type: Option<Type>,
24    pub body: Vec<Stmt>,
25}
26
27#[derive(Debug, Clone, PartialEq)]
28pub struct Param {
29    pub name: String,
30    pub ty: Type,
31}
32
33#[derive(Debug, Clone, PartialEq, Eq)]
34pub enum Type {
35    Unit,
36    Bool,
37    String,
38    Int,
39    Int8,
40    Int16,
41    Int32,
42    Int64,
43    UInt,
44    UInt8,
45    UInt16,
46    UInt32,
47    UInt64,
48    Float32,
49    Float64,
50    Slice(Box<Type>),
51    Map(Box<Type>, Box<Type>),
52    Tuple(Vec<Type>),
53    Named(String),
54}
55
56#[derive(Debug, Clone, PartialEq)]
57pub enum Stmt {
58    Let {
59        name: String,
60        ty: Option<Type>,
61        value: Expr,
62        mutable: bool,
63    },
64    MultiLet {
65        names: Vec<String>,
66        value: Expr,
67        mutable: bool,
68    },
69    Assign {
70        target: String,
71        op: String,
72        value: Expr,
73    },
74    IndexAssign {
75        collection: Expr,
76        index: Expr,
77        op: String,
78        value: Expr,
79        map_value_type: Option<Type>,
80    },
81    MultiAssign {
82        targets: Vec<String>,
83        value: Expr,
84    },
85    Expr(Expr),
86    Print {
87        newline: bool,
88        args: Vec<Expr>,
89    },
90    Return(Option<Expr>),
91    If {
92        cond: Expr,
93        then_body: Vec<Stmt>,
94        else_body: Vec<Stmt>,
95    },
96    While {
97        cond: Expr,
98        body: Vec<Stmt>,
99    },
100    ForEach {
101        value: String,
102        iter: Expr,
103        body: Vec<Stmt>,
104    },
105    Switch {
106        value: Option<Expr>,
107        cases: Vec<SwitchCase>,
108        default: Vec<Stmt>,
109    },
110    Break,
111    Continue,
112}
113
114#[derive(Debug, Clone, PartialEq)]
115pub struct SwitchCase {
116    pub values: Vec<Expr>,
117    pub body: Vec<Stmt>,
118}
119
120#[derive(Debug, Clone, PartialEq)]
121pub enum Expr {
122    Ident(String),
123    Int(String),
124    Float(String),
125    String(String),
126    Bool(bool),
127    Nil,
128    Unary {
129        op: String,
130        value: Box<Expr>,
131    },
132    Binary {
133        left: Box<Expr>,
134        op: String,
135        right: Box<Expr>,
136    },
137    Call {
138        function: String,
139        args: Vec<Expr>,
140    },
141    Index {
142        value: Box<Expr>,
143        index: Box<Expr>,
144    },
145    Array {
146        items: Vec<Expr>,
147        element_type: Option<Box<Type>>,
148    },
149    Map {
150        key_type: Box<Type>,
151        value_type: Box<Type>,
152        entries: Vec<(Expr, Expr)>,
153    },
154    MapIndex {
155        value: Box<Expr>,
156        index: Box<Expr>,
157        value_type: Box<Type>,
158    },
159    MapLookupOk {
160        value: Box<Expr>,
161        index: Box<Expr>,
162        value_type: Box<Type>,
163    },
164    Tuple(Vec<Expr>),
165    StructLiteral {
166        ty: String,
167        fields: Vec<(String, Expr)>,
168        positional: Vec<Expr>,
169    },
170    Raw(String),
171}