Extract 'compile' function from compiler struct
This commit is contained in:
parent
a6eb232b48
commit
b6a960862c
|
@ -23,34 +23,6 @@ use crate::namespace::Namespace;
|
||||||
use crate::types::Expression;
|
use crate::types::Expression;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
// pub fn create_compiler<'ctx>() -> Compiler<'ctx> {
|
|
||||||
// let default_ns_name = "user";
|
|
||||||
// // let builder = context.create_builder();
|
|
||||||
// let context = Context::create();
|
|
||||||
// //let user_ns = Namespace::new(&context, default_ns_name);
|
|
||||||
// //namespaces.insert(default_ns_name, &user_ns);
|
|
||||||
// // let fpm = PassManager::create(&user_ns.module);
|
|
||||||
|
|
||||||
// // fpm.add_instruction_combining_pass();
|
|
||||||
// // fpm.add_reassociate_pass();
|
|
||||||
// // fpm.add_gvn_pass();
|
|
||||||
// // fpm.add_cfg_simplification_pass();
|
|
||||||
// // fpm.add_basic_alias_analysis_pass();
|
|
||||||
// // fpm.add_promote_memory_to_register_pass();
|
|
||||||
// // fpm.add_instruction_combining_pass();
|
|
||||||
// // fpm.add_reassociate_pass();
|
|
||||||
|
|
||||||
// // fpm.initialize();
|
|
||||||
// //, builder, fpm, namespaces, Some(&default_ns_name)
|
|
||||||
// //Compiler::new(context)
|
|
||||||
// let builder = context.create_builder();
|
|
||||||
// Compiler {
|
|
||||||
// builder: builder,
|
|
||||||
// context: context,
|
|
||||||
// namespaces: HashMap::new(),
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
pub struct Compiler<'ctx> {
|
pub struct Compiler<'ctx> {
|
||||||
pub context: &'ctx Context,
|
pub context: &'ctx Context,
|
||||||
pub builder: Builder<'ctx>,
|
pub builder: Builder<'ctx>,
|
||||||
|
@ -126,26 +98,54 @@ impl<'ctx> Compiler<'ctx> {
|
||||||
|
|
||||||
// // builder.build_alloca(self.context.f64_type(), name)
|
// // builder.build_alloca(self.context.f64_type(), name)
|
||||||
// // }
|
// // }
|
||||||
|
|
||||||
pub fn compile(
|
|
||||||
&'ctx self,
|
|
||||||
exprs: Vec<&impl Expression>,
|
|
||||||
) -> Vec<Result<AnyValueEnum<'ctx>, String>> {
|
|
||||||
match self.current_ns() {
|
|
||||||
Some(ns) => ns,
|
|
||||||
None => panic!("Current namespace is not set."),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut generated_code = vec![];
|
|
||||||
|
|
||||||
for expr in &exprs {
|
|
||||||
generated_code.push(expr.code_gen(&self));
|
|
||||||
}
|
|
||||||
|
|
||||||
generated_code
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_context() -> Context {
|
pub fn create_context() -> Context {
|
||||||
return Context::create();
|
return Context::create();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn compile<'ctx>(
|
||||||
|
compiler: &'ctx Compiler,
|
||||||
|
exprs: Vec<impl Expression>,
|
||||||
|
) -> Vec<Result<AnyValueEnum<'ctx>, String>> {
|
||||||
|
match compiler.current_ns() {
|
||||||
|
Some(ns) => ns,
|
||||||
|
None => panic!("Current namespace is not set."),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut generated_code = vec![];
|
||||||
|
|
||||||
|
for expr in &exprs {
|
||||||
|
generated_code.push(expr.code_gen(compiler));
|
||||||
|
}
|
||||||
|
|
||||||
|
generated_code
|
||||||
|
}
|
||||||
|
|
||||||
|
// pub fn create_compiler<'ctx>() -> Compiler<'ctx> {
|
||||||
|
// let default_ns_name = "user";
|
||||||
|
// // let builder = context.create_builder();
|
||||||
|
// let context = Context::create();
|
||||||
|
// //let user_ns = Namespace::new(&context, default_ns_name);
|
||||||
|
// //namespaces.insert(default_ns_name, &user_ns);
|
||||||
|
// // let fpm = PassManager::create(&user_ns.module);
|
||||||
|
|
||||||
|
// // fpm.add_instruction_combining_pass();
|
||||||
|
// // fpm.add_reassociate_pass();
|
||||||
|
// // fpm.add_gvn_pass();
|
||||||
|
// // fpm.add_cfg_simplification_pass();
|
||||||
|
// // fpm.add_basic_alias_analysis_pass();
|
||||||
|
// // fpm.add_promote_memory_to_register_pass();
|
||||||
|
// // fpm.add_instruction_combining_pass();
|
||||||
|
// // fpm.add_reassociate_pass();
|
||||||
|
|
||||||
|
// // fpm.initialize();
|
||||||
|
// //, builder, fpm, namespaces, Some(&default_ns_name)
|
||||||
|
// //Compiler::new(context)
|
||||||
|
// let builder = context.create_builder();
|
||||||
|
// Compiler {
|
||||||
|
// builder: builder,
|
||||||
|
// context: context,
|
||||||
|
// namespaces: HashMap::new(),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
|
@ -46,11 +46,16 @@ fn main() -> io::Result<()> {
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
f.read_to_string(&mut buf)?;
|
f.read_to_string(&mut buf)?;
|
||||||
match reader::read_string(&buf) {
|
match reader::read_string(&buf) {
|
||||||
Ok(v) => println!("{:?}", v),
|
Ok(v) => {
|
||||||
|
println!("AST: {:?}", v);
|
||||||
|
let g = compiler::compile(&compiler, v);
|
||||||
|
println!("GEN: {:?}", g)
|
||||||
|
}
|
||||||
Err(e) => println!(">> error {:?}", e),
|
Err(e) => println!(">> error {:?}", e),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
println!("Input file is missing.")
|
println!("Input file is missing.")
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue