Skip to content

Instantly share code, notes, and snippets.

@AcrylicShrimp
Last active January 30, 2020 02:44
Show Gist options
  • Save AcrylicShrimp/07b78c176e3d8ee03555a42d2e461315 to your computer and use it in GitHub Desktop.
Save AcrylicShrimp/07b78c176e3d8ee03555a42d2e461315 to your computer and use it in GitHub Desktop.
pub struct FuncGen<'ctx> {
pub function: FunctionValue<'ctx>,
pub prototype: FuncPrototype<'ctx>,
pub statement_stack: Vec<StatementGen<'ctx>>,
pub scope_stack: Vec<ScopeGen<'ctx>>,
}
pub struct StatementGen<'ctx> {
pub name: String,
pub entry_block_gen: BlockGen<'ctx>,
pub exit_block_gen: BlockGen<'ctx>,
}
impl<'stmt, 'fnc: 'stmt, 'mdl: 'fnc, 'ctx: 'mdl> FuncGen<'ctx> {
pub fn create_statement(
&'fnc mut self,
context: &'ctx Context,
name: String,
) -> &'stmt mut StatementGen<'ctx> {
let parent_statement = self.statement_stack.last_mut().unwrap();
let entry = parent_statement.create_block(context, "entry".to_owned());
let exit = parent_statement.create_block(context, "exit".to_owned());
let statement_gen = StatementGen {
name: name,
entry_block_gen: entry,
exit_block_gen: exit,
};
statement_gen.create_block(context, "body".to_owned());
self.statement_stack.push(statement_gen);
self.statement_stack.last_mut().unwrap()
}
pub fn end_statement(&'fnc mut self, context: &'ctx Context) {
self.statement_stack.pop().unwrap().end_statement(context);
}
pub fn generate_code(
&'fnc mut self,
context: &'ctx Context,
module: &'mdl mut ModuleGen<'ctx>,
ast: &AST,
) {
if ast.name != "statement-list" {
panic!("statement-list AST expected, got {}.", ast.name);
}
if ast.children.is_empty() {
return;
}
let mut statement_ast_stack: Vec<&AST> = ast.children.iter().rev().collect();
while statement_ast_stack.last().unwrap().name == "statement-list" {
let mut statement_vec: Vec<&AST> = statement_ast_stack
.pop()
.unwrap()
.children
.iter()
.rev()
.collect();
statement_ast_stack.append(&mut statement_vec);
}
for ast in statement_ast_stack.into_iter().rev() {
// let statement = self.create_statement(context, ast.children[0].name.clone());
let statement = self.statement_stack.last_mut().unwrap();
statement.generate_code(context, module, self, &ast.children[0]); // PROBLEM
self.end_statement(context);
}
}
}
impl<'stmt, 'fnc: 'stmt, 'mdl: 'fnc, 'ctx: 'mdl> StatementGen<'ctx> {
pub fn generate_code(
&'fnc mut self,
context: &'ctx Context,
module: &'mdl mut ModuleGen<'ctx>,
function: &'fnc mut FuncGen<'ctx>,
ast: &AST,
) {
// Some logics to do here!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment