1use crate::utils::location::Location;
2
3#[derive(Debug, Clone)]
4pub enum Literal {
5 Int(i64),
6 String(String),
7 Char(char),
8 Bool(bool),
9 Arr { elements: Vec<Expr> },
10}
11impl Literal {
12 pub fn to_i64(&self) -> i64 {
13 match self {
14 Literal::Int(n) => *n,
15 _ => panic!("Expected integer literal"),
16 }
17 }
18}
19
20#[derive(Clone, Debug, PartialEq)]
21pub enum Type {
22 Int,
23 UInt,
24 Int8,
25 UInt8,
26 Bool,
27 Str,
28 Char,
29 Void,
30 Ptr(Box<Type>),
31 Array {
32 element_type: Box<Type>,
33 size: usize,
34 },
35 Struct(String),
36
37 GenericInstance {
39 name: String,
40 args: Vec<Type>,
41 },
42 GenericParam(String),
43
44 Any,
45}
46
47#[derive(Debug, Clone)]
48pub enum BinaryOp {
49 Add,
50 Sub,
51 Mul,
52 Div,
53 Mod,
54
55 Eq,
56 NEq,
57 Gt,
58 GtE,
59 Lt,
60 LtE,
61 And,
62 Or,
63}
64#[derive(Debug, Clone)]
65pub enum UnaryOp {
66 Positive,
67 Negative,
68 AddressOf,
69 Deref,
70 Not,
71}
72
73#[derive(Debug, Clone)]
74pub struct Identifier {
75 pub value: String,
76 pub location: Location,
77}
78
79#[derive(Debug, Clone)]
80pub enum ExprKind {
81 Literal(Literal),
82 Identifier(String),
83
84 Index {
86 base: Box<Expr>,
87 index: Box<Expr>,
88 },
89
90 Field {
92 base: Box<Expr>,
93 field: String,
94 },
95 StructLiteral {
96 struct_name: String,
97 generic_args: Vec<Type>,
98 fields: Vec<(String, Expr)>,
99 },
100
101 Binary {
103 left: Box<Expr>,
104 op: BinaryOp,
105 right: Box<Expr>,
106 },
107
108 Cast {
109 left: Box<Expr>,
110 right: Type,
111 },
112
113 Unary {
114 op: UnaryOp,
115 expr: Box<Expr>,
116 },
117
118 Call {
119 callee: Identifier,
120 generic_args: Vec<Type>,
121 args: Vec<Expr>,
122 },
123 Sizeof {
124 ty: Type,
125 },
126}
127
128#[derive(Debug, Clone)]
129pub struct Expr {
130 pub kind: ExprKind,
131 pub span: Location,
132}
133
134#[derive(Debug, Clone)]
135pub struct Parameter {
136 pub name: Identifier,
137 pub ptype: Option<Type>,
138}
139
140#[derive(Debug, Clone)]
141pub enum Stmt {
142 Assignment {
143 ident: Identifier,
144 vtype: Option<Type>,
145 expr: Option<Expr>,
146 },
147 Constant {
148 name: Identifier,
149 vtype: Option<Type>,
150 expr: Expr,
151 },
152 Reassignment {
153 ident: Identifier,
154 expr: Expr,
155 },
156 DerefReassignment {
157 target: Expr,
158 expr: Expr,
159 },
160 Expr(Expr),
161 If {
162 cond: Expr,
163 then_branch: Vec<Stmt>,
164 else_if_branches: Vec<(Expr, Vec<Stmt>)>,
165 else_branch: Option<Vec<Stmt>>,
166 },
167 While {
168 cond: Expr,
169 body: Vec<Stmt>,
170 },
171 For {
172 init: Box<Stmt>,
173 cond: Expr,
174 step: Box<Stmt>,
175 body: Vec<Stmt>,
176 },
177 Return {
178 value: Option<Expr>,
179 span: Location,
180 },
181 Use {
182 path: Vec<String>,
183 },
184 Struct {
185 name: Identifier,
186 generic_params: Vec<String>,
187 fields: Vec<Parameter>,
188 },
189 Function {
190 name: Identifier,
191 public: bool,
192 rttype: Option<Type>,
193 generic_params: Vec<String>,
194 params: Vec<Parameter>,
195 body: Vec<Stmt>,
196 },
197 Extern {
198 name: Identifier,
199 rttype: Option<Type>,
200 generic_params: Vec<String>,
201 params: Vec<Parameter>,
202 },
203 Break {
204 location: Location,
205 },
206}
207
208#[derive(Debug)]
209pub struct Program {
210 pub statements: Vec<Stmt>,
211}
212
213#[derive(Debug)]
214pub enum ParserErrorType {
215 MalformedStatementError,
216 UnexpectedTokenTypeError,
217 UnimplementedError,
218}
219
220#[derive(Debug)]
221pub struct ParserError {
222 pub etype: ParserErrorType,
223 pub message: String,
224 pub location: Location,
225}
226impl std::fmt::Display for ParserError {
227 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
228 write!(
229 f,
230 "! Parser Error :{}: {:?}: {}",
231 self.location, self.etype, self.message
232 )
233 }
234}