swamp_script_code_gen_program/
lib.rs1use swamp_script_code_gen::{CodeGenState, Error, GenOptions};
2use swamp_script_compile::Program;
3use swamp_script_modules::prelude::ModuleRef;
4use swamp_script_semantic::Function;
5use swamp_script_source_map_lookup::SourceMapWrapper;
6use swamp_script_types::Type;
7
8pub fn code_gen_program<'a>(
9 program: &Program,
10 main_module: &ModuleRef,
11 source_map_lookup: &'a SourceMapWrapper,
12) -> Result<CodeGenState, Error> {
13 let mut code_gen = CodeGenState::new();
14
15 code_gen.reserve_space_for_constants(&program.state.constants_in_dependency_order)?;
16
17 if let Some(found_main_expression) = &main_module.main_expression {
18 let halt_function = GenOptions {
19 is_halt_function: true,
20 };
21 code_gen.gen_main_function(found_main_expression, &halt_function, source_map_lookup)?;
22 }
23
24 let normal_function = GenOptions {
25 is_halt_function: false,
26 };
27
28 for internal_function_def in &main_module.symbol_table.internal_functions() {
29 code_gen.gen_function_def(internal_function_def, &normal_function, source_map_lookup)?;
30 }
31
32 for (associated_on_type, impl_functions) in
33 &program.state.instantiator.associated_impls.functions
34 {
35 if !associated_on_type.is_concrete() {
36 continue;
37 }
38 if associated_on_type == &Type::Int
39 || associated_on_type == &Type::Float
40 || associated_on_type == &Type::Bool
41 || associated_on_type == &Type::String
42 {
43 continue;
44 }
45
46 for (_name, func) in &impl_functions.functions {
47 if func.name().clone().starts_with("instantiated ") {
48 continue;
49 }
50 match &**func {
51 Function::Internal(int_fn) => {
52 code_gen.gen_function_def(int_fn, &normal_function, source_map_lookup)?;
53 }
54
55 Function::External(_ext_fn) => {}
56 }
57 }
58 }
59
60 code_gen.gen_constants_expression_functions_in_order(
61 &program.state.constants_in_dependency_order,
62 source_map_lookup,
63 )?;
64
65 code_gen.finalize();
66
67 Ok(code_gen)
68}