rue_compiler/compile/
document.rs1use rue_ast::{AstItem, AstSymbolItem};
2use rue_hir::{ModuleDeclarations, ScopeId};
3
4use crate::{
5 Compiler, compile_module_symbols, compile_module_types, compile_symbol_item, compile_type_item,
6 declare_module_symbols, declare_module_types, declare_symbol_item, declare_type_item,
7};
8
9pub fn declare_type_items(
10 ctx: &mut Compiler,
11 scope: ScopeId,
12 items: impl Iterator<Item = AstItem>,
13 declarations: &mut ModuleDeclarations,
14) {
15 ctx.push_scope(scope);
16
17 for item in items {
18 match item {
19 AstItem::TypeItem(item) => declarations.types.push(declare_type_item(ctx, &item)),
20 AstItem::SymbolItem(item) => {
21 if let AstSymbolItem::ModuleItem(item) = item {
22 declarations.symbols.push(declare_module_types(ctx, &item));
23 }
24 }
25 }
26 }
27
28 ctx.pop_scope();
29}
30
31pub fn declare_symbol_items(
32 ctx: &mut Compiler,
33 scope: ScopeId,
34 items: impl Iterator<Item = AstItem>,
35 declarations: &mut ModuleDeclarations,
36) {
37 ctx.push_scope(scope);
38
39 let mut index = 0;
40
41 for item in items {
42 match item {
43 AstItem::TypeItem(_) => {}
44 AstItem::SymbolItem(item) => {
45 if let AstSymbolItem::ModuleItem(item) = item {
46 declare_module_symbols(ctx, &item, declarations.symbols[index]);
47 index += 1;
48 } else {
49 declarations.symbols.push(declare_symbol_item(ctx, &item));
50 }
51 }
52 }
53 }
54
55 ctx.pop_scope();
56}
57
58pub fn compile_type_items(
59 ctx: &mut Compiler,
60 scope: ScopeId,
61 items: impl Iterator<Item = AstItem>,
62 declarations: &ModuleDeclarations,
63) {
64 ctx.push_scope(scope);
65
66 let mut index = 0;
67 let mut module_index = 0;
68
69 for item in items {
70 match item {
71 AstItem::TypeItem(item) => {
72 let (ty, scope) = declarations.types[index];
73 compile_type_item(ctx, &item, ty, scope);
74 index += 1;
75 }
76 AstItem::SymbolItem(item) => {
77 if let AstSymbolItem::ModuleItem(item) = item {
78 compile_module_types(ctx, &item, declarations.symbols[module_index]);
79 module_index += 1;
80 }
81 }
82 }
83 }
84
85 ctx.pop_scope();
86}
87
88pub fn compile_symbol_items(
89 ctx: &mut Compiler,
90 scope: ScopeId,
91 items: impl Iterator<Item = AstItem>,
92 declarations: &ModuleDeclarations,
93) {
94 ctx.push_scope(scope);
95
96 let mut index = 0;
97
98 for item in items {
99 match item {
100 AstItem::TypeItem(_) => {}
101 AstItem::SymbolItem(item) => {
102 if let AstSymbolItem::ModuleItem(item) = item {
103 compile_module_symbols(ctx, &item, declarations.symbols[index]);
104 } else {
105 let symbol = declarations.symbols[index];
106 compile_symbol_item(ctx, &item, symbol);
107 }
108 index += 1;
109 }
110 }
111 }
112
113 ctx.pop_scope();
114}