Skip to main content

mysz_core/parse/
parsing.rs

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    // Generics
38    GenericInstance {
39        name: String,
40        args: Vec<Type>,
41    },
42    GenericParam(String),
43
44    VariadicPack {
45        name: String,
46        types: Vec<Type>,
47    },
48
49    Any,
50}
51
52#[derive(Debug, Clone)]
53pub enum BinaryOp {
54    Add,
55    Sub,
56    Mul,
57    Div,
58    Mod,
59
60    Eq,
61    NEq,
62    Gt,
63    GtE,
64    Lt,
65    LtE,
66    And,
67    Or,
68}
69#[derive(Debug, Clone)]
70pub enum UnaryOp {
71    Positive,
72    Negative,
73    AddressOf,
74    Deref,
75    Not,
76}
77
78#[derive(Debug, Clone)]
79pub struct Identifier {
80    pub value: String,
81    pub location: Location,
82}
83
84#[derive(Debug, Clone)]
85pub enum ExprKind {
86    Literal(Literal),
87    Identifier(String),
88
89    // array indexing
90    Index {
91        base: Box<Expr>,
92        index: Box<Expr>,
93    },
94
95    // struct literal
96    Field {
97        base: Box<Expr>,
98        field: String,
99    },
100    StructLiteral {
101        struct_name: String,
102        generic_args: Vec<Type>,
103        fields: Vec<(String, Expr)>,
104    },
105
106    // basic maths
107    Binary {
108        left: Box<Expr>,
109        op: BinaryOp,
110        right: Box<Expr>,
111    },
112
113    Cast {
114        left: Box<Expr>,
115        right: Type,
116    },
117
118    Unary {
119        op: UnaryOp,
120        expr: Box<Expr>,
121    },
122
123    Call {
124        callee: Identifier,
125        generic_args: Vec<Type>,
126        args: Vec<Expr>,
127    },
128    Sizeof {
129        ty: Type,
130    },
131    Typeof {
132        expr: Box<Expr>,
133    },
134}
135
136#[derive(Debug, Clone)]
137pub struct Expr {
138    pub kind: ExprKind,
139    pub span: Location,
140}
141
142#[derive(Debug, Clone)]
143pub struct Parameter {
144    pub name: Identifier,
145    pub ptype: Option<Type>,
146    pub is_variadic: bool,
147}
148
149#[derive(Debug, Clone)]
150pub enum Stmt {
151    Assignment {
152        ident: Identifier,
153        vtype: Option<Type>,
154        expr: Option<Expr>,
155    },
156    Constant {
157        name: Identifier,
158        vtype: Option<Type>,
159        expr: Expr,
160    },
161    Reassignment {
162        ident: Identifier,
163        expr: Expr,
164    },
165    DerefReassignment {
166        target: Expr,
167        expr: Expr,
168    },
169    Expr(Expr),
170    If {
171        cond: Expr,
172        then_branch: Vec<Stmt>,
173        else_if_branches: Vec<(Expr, Vec<Stmt>)>,
174        else_branch: Option<Vec<Stmt>>,
175    },
176    While {
177        cond: Expr,
178        body: Vec<Stmt>,
179    },
180    For {
181        init: Box<Stmt>,
182        cond: Expr,
183        step: Box<Stmt>,
184        body: Vec<Stmt>,
185    },
186    ForIn {
187        field_ident: Identifier,
188        target_expr: Expr,
189        body: Vec<Stmt>,
190    },
191    Return {
192        value: Option<Expr>,
193        span: Location,
194    },
195    Use {
196        path: Vec<String>,
197    },
198    Struct {
199        name: Identifier,
200        generic_params: Vec<String>,
201        fields: Vec<Parameter>,
202    },
203    Function {
204        name: Identifier,
205        public: bool,
206        rttype: Option<Type>,
207        generic_params: Vec<String>,
208        params: Vec<Parameter>,
209        body: Vec<Stmt>,
210    },
211    Extern {
212        name: Identifier,
213        rttype: Option<Type>,
214        generic_params: Vec<String>,
215        params: Vec<Parameter>,
216    },
217    Break {
218        location: Location,
219    },
220}
221
222#[derive(Debug)]
223pub struct Program {
224    pub statements: Vec<Stmt>,
225}
226
227#[derive(Debug)]
228pub enum ParserErrorType {
229    MalformedStatementError,
230    UnexpectedTokenTypeError,
231    UnimplementedError,
232}
233
234#[derive(Debug)]
235pub struct ParserError {
236    pub etype: ParserErrorType,
237    pub message: String,
238    pub location: Location,
239}
240impl std::fmt::Display for ParserError {
241    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
242        write!(
243            f,
244            "! Parser Error :{}: {:?}: {}",
245            self.location, self.etype, self.message
246        )
247    }
248}