BRANCHING (TURING COMPLETE????)
This commit is contained in:
@@ -1,24 +1,20 @@
|
||||
use super::{CompilerMsg, CompilerOutput, FileSpan, FnLowerable, Node, PFunction};
|
||||
use crate::{
|
||||
ir::{
|
||||
FnDef, FnID, IRInstructions, IRUFunction, IRUInstruction, Idents, NamespaceGuard, Origin,
|
||||
Type, VarDef, VarInst,
|
||||
FnDef, FnID, IRUFunction, IRUInstrInst, IRUInstruction, IRUProgram, Idents, Origin, Type,
|
||||
VarDef, VarInst,
|
||||
},
|
||||
parser,
|
||||
};
|
||||
|
||||
impl Node<PFunction> {
|
||||
pub fn lower_header(
|
||||
&self,
|
||||
map: &mut NamespaceGuard,
|
||||
output: &mut CompilerOutput,
|
||||
) -> Option<FnID> {
|
||||
pub fn lower_header(&self, map: &mut IRUProgram, output: &mut CompilerOutput) -> Option<FnID> {
|
||||
self.as_ref()?.lower_header(map, output)
|
||||
}
|
||||
pub fn lower_body(
|
||||
&self,
|
||||
id: FnID,
|
||||
map: &mut NamespaceGuard,
|
||||
map: &mut IRUProgram,
|
||||
output: &mut CompilerOutput,
|
||||
) -> Option<IRUFunction> {
|
||||
Some(self.as_ref()?.lower_body(id, map, output))
|
||||
@@ -26,11 +22,7 @@ impl Node<PFunction> {
|
||||
}
|
||||
|
||||
impl PFunction {
|
||||
pub fn lower_header(
|
||||
&self,
|
||||
map: &mut NamespaceGuard,
|
||||
output: &mut CompilerOutput,
|
||||
) -> Option<FnID> {
|
||||
pub fn lower_header(&self, map: &mut IRUProgram, output: &mut CompilerOutput) -> Option<FnID> {
|
||||
let header = self.header.as_ref()?;
|
||||
let name = header.name.as_ref()?;
|
||||
let args = header
|
||||
@@ -58,44 +50,43 @@ impl PFunction {
|
||||
pub fn lower_body(
|
||||
&self,
|
||||
id: FnID,
|
||||
map: &mut NamespaceGuard,
|
||||
map: &mut IRUProgram,
|
||||
output: &mut CompilerOutput,
|
||||
) -> IRUFunction {
|
||||
let mut instructions = IRInstructions::new();
|
||||
let def = map.get_fn(id).clone();
|
||||
let args = def.args.iter().map(|a| map.named_var(a.clone())).collect();
|
||||
let mut ctx = FnLowerCtx {
|
||||
instructions: &mut instructions,
|
||||
map,
|
||||
instructions: Vec::new(),
|
||||
program: map,
|
||||
output,
|
||||
span: self.body.span,
|
||||
};
|
||||
if let Some(src) = self.body.lower(&mut ctx) {
|
||||
instructions.push(IRUInstruction::Ret { src }, src.span);
|
||||
ctx.instructions.push(IRUInstrInst {
|
||||
i: IRUInstruction::Ret { src },
|
||||
span: src.span,
|
||||
});
|
||||
}
|
||||
IRUFunction {
|
||||
name: def.name.clone(),
|
||||
args,
|
||||
ret: def.ret,
|
||||
instructions: ctx.instructions,
|
||||
}
|
||||
IRUFunction::new(def.name.clone(), args, def.ret, instructions)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FnLowerCtx<'a, 'n> {
|
||||
pub map: &'a mut NamespaceGuard<'n>,
|
||||
pub instructions: &'a mut IRInstructions,
|
||||
pub struct FnLowerCtx<'a> {
|
||||
pub program: &'a mut IRUProgram,
|
||||
pub instructions: Vec<IRUInstrInst>,
|
||||
pub output: &'a mut CompilerOutput,
|
||||
pub span: FileSpan,
|
||||
}
|
||||
|
||||
impl<'n> FnLowerCtx<'_, 'n> {
|
||||
pub fn span<'b>(&'b mut self, span: FileSpan) -> FnLowerCtx<'b, 'n> {
|
||||
FnLowerCtx {
|
||||
map: self.map,
|
||||
instructions: self.instructions,
|
||||
output: self.output,
|
||||
span,
|
||||
}
|
||||
}
|
||||
impl FnLowerCtx<'_> {
|
||||
pub fn get(&mut self, node: &Node<parser::PIdent>) -> Option<Idents> {
|
||||
let name = node.inner.as_ref()?;
|
||||
let res = self.map.get(name);
|
||||
let res = self.program.get(name);
|
||||
if res.is_none() {
|
||||
self.err_at(node.span, format!("Identifier '{}' not found", name));
|
||||
}
|
||||
@@ -124,18 +115,18 @@ impl<'n> FnLowerCtx<'_, 'n> {
|
||||
self.output.err(CompilerMsg::from_span(span, msg))
|
||||
}
|
||||
pub fn temp(&mut self, ty: Type) -> VarInst {
|
||||
self.map.temp_var(self.span, ty)
|
||||
self.program.temp_var(self.span, ty)
|
||||
}
|
||||
pub fn push(&mut self, i: IRUInstruction) {
|
||||
self.instructions.push(i, self.span);
|
||||
self.instructions.push(IRUInstrInst { i, span: self.span });
|
||||
}
|
||||
pub fn push_at(&mut self, i: IRUInstruction, span: FileSpan) {
|
||||
self.instructions.push(i, span);
|
||||
self.instructions.push(IRUInstrInst { i, span });
|
||||
}
|
||||
pub fn sub<'b>(&'b mut self) -> FnLowerCtx<'b, 'n> {
|
||||
pub fn branch<'a>(&'a mut self) -> FnLowerCtx<'a> {
|
||||
FnLowerCtx {
|
||||
map: self.map,
|
||||
instructions: self.instructions,
|
||||
program: self.program,
|
||||
instructions: Vec::new(),
|
||||
output: self.output,
|
||||
span: self.span,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user