1use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
9pub struct Program {
10 pub name: String,
11 pub uses: Vec<String>,
12 pub block: Block,
13}
14
15#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
16pub struct Unit {
17 pub name: String,
18 pub uses: Vec<String>,
19 pub interface: UnitInterface,
20 pub implementation: UnitImplementation,
21}
22
23#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
24pub struct Library {
25 pub name: String,
26 pub uses: Vec<String>,
27 pub exports: Vec<String>,
28 pub block: Block,
29}
30
31#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
32pub struct Package {
33 pub name: String,
34 pub uses: Vec<String>,
35 pub requires: Vec<String>,
36 pub contains: Vec<String>,
37 pub block: Block,
38}
39
40#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
41pub struct UnitInterface {
42 pub uses: Vec<String>,
43 pub types: Vec<TypeDecl>,
44 pub constants: Vec<ConstDecl>,
45 pub variables: Vec<VariableDecl>,
46 pub procedures: Vec<ProcedureDecl>,
47 pub functions: Vec<FunctionDecl>,
48 pub classes: Vec<ClassDecl>,
49 pub interfaces: Vec<InterfaceDecl>,
50}
51
52#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
53pub struct UnitImplementation {
54 pub uses: Vec<String>,
55 pub types: Vec<TypeDecl>,
56 pub constants: Vec<ConstDecl>,
57 pub variables: Vec<VariableDecl>,
58 pub procedures: Vec<ProcedureDecl>,
59 pub functions: Vec<FunctionDecl>,
60 pub classes: Vec<ClassDecl>,
61 pub interfaces: Vec<InterfaceDecl>,
62 pub initialization: Option<Vec<Statement>>,
63 pub finalization: Option<Vec<Statement>>,
64}
65
66#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
69pub enum SimpleType {
70 Integer,
71 Real,
72 Boolean,
73 Char,
74 String,
75}
76
77#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
78pub enum Type {
79 Simple(SimpleType),
81
82 Integer,
84 Real,
85 Boolean,
86 Char,
87 String,
88 WideString,
89
90 Alias {
92 name: String,
93 target_type: Box<Type>,
94 },
95 Array {
96 index_type: Box<Type>,
97 element_type: Box<Type>,
98 range: Option<(i64, i64)>,
99 },
100 Record {
101 fields: HashMap<String, Box<Type>>,
102 is_packed: bool,
103 },
104 Set {
105 base_type: Box<Type>,
106 },
107 File {
108 element_type: Option<Box<Type>>,
109 },
110 Pointer(Box<Type>),
111
112 Generic {
114 name: String,
115 constraints: Vec<String>,
116 },
117 GenericInstance {
118 base_type: String,
119 type_arguments: Vec<Type>,
120 },
121}
122
123#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
124pub struct ArrayDimension {
125 pub lower_bound: i64,
126 pub upper_bound: i64,
127}
128
129#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
130pub struct FieldDecl {
131 pub name: String,
132 pub field_type: Type,
133 pub visibility: FieldVisibility,
134}
135
136#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
137pub enum FieldVisibility {
138 Private,
139 Protected,
140 Public,
141 Published,
142}
143
144#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
147pub enum Statement {
148 Assignment {
149 target: String,
150 value: Expression,
151 },
152 If {
153 condition: Expression,
154 then_branch: Vec<Statement>,
155 else_branch: Option<Vec<Statement>>,
156 },
157 While {
158 condition: Expression,
159 body: Vec<Statement>,
160 },
161 Repeat {
162 body: Vec<Statement>,
163 until_condition: Expression,
164 },
165 For {
166 var_name: String,
167 start: Expression,
168 end: Expression,
169 body: Vec<Statement>,
170 direction: ForDirection,
171 },
172 Case {
173 expression: Expression,
174 branches: Vec<CaseBranch>,
175 else_branch: Option<Vec<Statement>>,
176 },
177 ProcedureCall {
178 name: String,
179 arguments: Vec<Expression>,
180 },
181 Block(Block),
182
183 Try {
185 try_block: Vec<Statement>,
186 except_clauses: Vec<ExceptClause>,
187 finally_block: Option<Vec<Statement>>,
188 },
189 Raise {
190 exception: Option<Expression>,
191 message: Option<Expression>,
192 },
193 Goto {
194 label: String,
195 },
196 Label {
197 name: String,
198 },
199 With {
200 variable: Expression,
201 statements: Vec<Statement>,
202 },
203
204 Empty,
205}
206
207#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
208pub enum ForDirection {
209 To,
210 DownTo,
211}
212
213#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
214pub struct CaseBranch {
215 pub values: Vec<Expression>,
216 #[serde(default)]
218 pub guard: Option<Expression>,
219 pub body: Vec<Statement>,
220}
221
222#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
223pub struct ExceptClause {
224 pub exception_type: Option<String>,
225 pub variable: Option<String>,
226 pub body: Vec<Statement>,
227}
228
229#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
232pub enum Expression {
233 Literal(Literal),
234 Variable(String),
235 BinaryOp {
236 operator: String,
237 left: Box<Expression>,
238 right: Box<Expression>,
239 },
240 UnaryOp {
241 operator: String,
242 operand: Box<Expression>,
243 },
244 FunctionCall {
245 name: String,
246 arguments: Vec<Expression>,
247 },
248
249 Set {
251 elements: Vec<Expression>,
252 },
253 TypeCast {
254 target_type: Type,
255 expression: Box<Expression>,
256 },
257 SizeOf {
258 type_or_expression: Box<Expression>,
259 },
260 AddressOf {
261 expression: Box<Expression>,
262 },
263 Dereference {
264 expression: Box<Expression>,
265 },
266 Inherited {
267 member: Option<String>,
268 },
269 Is {
270 expression: Box<Expression>,
271 type_name: String,
272 },
273 As {
274 expression: Box<Expression>,
275 type_name: String,
276 },
277}
278
279#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
282pub enum Literal {
283 Integer(i64),
284 Real(f64),
285 String(String),
286 WideString(String),
287 Char(char),
288 Boolean(bool),
289 Nil,
290 Set(Vec<i64>),
291}
292
293#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
296pub struct Block {
297 pub consts: Vec<ConstDecl>,
298 pub types: Vec<TypeDecl>,
299 pub vars: Vec<VariableDecl>,
300 pub procedures: Vec<ProcedureDecl>,
301 pub functions: Vec<FunctionDecl>,
302 pub classes: Vec<ClassDecl>,
303 pub statements: Vec<Statement>,
304}
305
306impl Block {
307 pub fn empty() -> Self {
308 Self {
309 consts: vec![],
310 types: vec![],
311 vars: vec![],
312 procedures: vec![],
313 functions: vec![],
314 classes: vec![],
315 statements: vec![],
316 }
317 }
318
319 pub fn with_statements(statements: Vec<Statement>) -> Self {
320 Self {
321 consts: vec![],
322 types: vec![],
323 vars: vec![],
324 procedures: vec![],
325 functions: vec![],
326 classes: vec![],
327 statements,
328 }
329 }
330}
331
332#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
333pub struct ConstDecl {
334 pub name: String,
335 pub value: Literal,
336 pub visibility: FieldVisibility,
337}
338
339#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
340pub struct TypeDecl {
341 pub name: String,
342 pub type_definition: Type,
343 pub visibility: FieldVisibility,
344}
345
346#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
347pub struct VariableDecl {
348 pub name: String,
349 pub variable_type: Type,
350 pub initial_value: Option<Expression>,
351 pub visibility: FieldVisibility,
352 pub is_absolute: bool,
353 pub absolute_address: Option<i64>,
354}
355
356#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
357pub struct FunctionDecl {
358 pub name: String,
359 pub parameters: Vec<Parameter>,
360 pub return_type: Type,
361 pub block: Block,
362 pub visibility: FieldVisibility,
363 pub is_external: bool,
364 pub external_name: Option<String>,
365 pub is_inline: bool,
366 pub is_forward: bool,
367 pub is_class_method: bool,
368 pub is_virtual: bool,
369 pub is_override: bool,
370 pub is_overload: bool,
371}
372
373#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
374pub struct ProcedureDecl {
375 pub name: String,
376 pub parameters: Vec<Parameter>,
377 pub block: Block,
378 pub visibility: FieldVisibility,
379 pub is_external: bool,
380 pub external_name: Option<String>,
381 pub is_inline: bool,
382 pub is_forward: bool,
383 pub is_class_method: bool,
384 pub is_virtual: bool,
385 pub is_override: bool,
386 pub is_overload: bool,
387}
388
389#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
390pub struct Parameter {
391 pub name: String,
392 pub param_type: Type,
393 pub is_var: bool,
394 pub is_const: bool,
395 pub is_out: bool,
396 pub default_value: Option<Expression>,
397}
398
399#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
402pub struct ClassDecl {
403 pub name: String,
404 pub parent: Option<String>,
405 pub interfaces: Vec<String>,
406 pub fields: Vec<FieldDecl>,
407 pub methods: Vec<MethodDecl>,
408 pub properties: Vec<PropertyDecl>,
409 pub visibility: FieldVisibility,
410 pub is_abstract: bool,
411 pub is_sealed: bool,
412}
413
414#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
415pub struct InterfaceDecl {
416 pub name: String,
417 pub parent: Option<String>,
418 pub methods: Vec<MethodDecl>,
419 pub properties: Vec<PropertyDecl>,
420 pub visibility: FieldVisibility,
421}
422
423#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
424pub struct MethodDecl {
425 pub name: String,
426 pub parameters: Vec<Parameter>,
427 pub return_type: Option<Type>,
428 pub block: Option<Block>,
429 pub visibility: FieldVisibility,
430 pub is_class_method: bool,
431 pub is_virtual: bool,
432 pub is_abstract: bool,
433 pub is_override: bool,
434 pub is_overload: bool,
435 pub is_static: bool,
436 pub is_constructor: bool,
437 pub is_destructor: bool,
438}
439
440#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
441pub struct PropertyDecl {
442 pub name: String,
443 pub property_type: Type,
444 pub read_specifier: Option<MethodSpecifier>,
445 pub write_specifier: Option<MethodSpecifier>,
446 pub stored_field: Option<String>,
447 pub default_value: Option<Expression>,
448 pub visibility: FieldVisibility,
449 pub is_indexed: bool,
450 pub index_parameters: Vec<Parameter>,
451}
452
453#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
454pub enum MethodSpecifier {
455 Field(String),
456 Method(String),
457}
458
459#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
462pub struct Module {
463 pub name: String,
464 pub unit: Unit,
465 pub dependencies: Vec<String>,
466}
467
468#[derive(Debug, Clone, PartialEq)]
469pub enum ModuleError {
470 ModuleNotFound(String),
471 CircularDependency(Vec<String>),
472 LoadError(String, String),
473 ParseError(String),
474}
475
476impl std::fmt::Display for ModuleError {
477 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
478 match self {
479 ModuleError::ModuleNotFound(name) => write!(f, "Module not found: {}", name),
480 ModuleError::CircularDependency(cycle) => {
481 write!(f, "Circular dependency detected: {}", cycle.join(" -> "))
482 }
483 ModuleError::LoadError(name, err) => {
484 write!(f, "Error loading module {}: {}", name, err)
485 }
486 ModuleError::ParseError(msg) => write!(f, "Parse error: {}", msg),
487 }
488 }
489}
490
491impl std::error::Error for ModuleError {}
492
493pub type ModuleResult<T> = Result<T, ModuleError>;
494
495#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
498pub enum CallingConvention {
499 Cdecl,
500 Stdcall,
501 Fastcall,
502 Pascal,
503 Register,
504 Safecall,
505}
506
507pub type Expr = Expression;
510pub type Stmt = Statement;