Skip to content

Commit

Permalink
fire: wip: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
CohenArthur committed Apr 23, 2023
1 parent e310511 commit 3fee9b6
Showing 1 changed file with 55 additions and 29 deletions.
84 changes: 55 additions & 29 deletions fire/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::HashMap;
// FIXME: How do we use the value returned by a function call for example?
// where x = id(15);

use error::{ErrKind, Error};
use error::Error;
use fir::{Fallible, Fir, Kind, Mapper, Node, OriginIdx, RefIdx, Traversal};
use flatten::FlattenData;

Expand Down Expand Up @@ -41,7 +41,8 @@ type Type = &'static str;
// needs to be hashable
// we need the type of the value - it needs to be known at all times
// FIXME: We can probably improve this type by specializing it more - turning it into a sum type differentiating between
#[derive(PartialEq, Eq, Debug)]
// FIXME: We need to be very careful about what a "Clone" means here
#[derive(PartialEq, Eq, Debug, Clone)]
struct Instance {
pub(crate) ty: Type,
pub(crate) data: Vec<u8>,
Expand All @@ -56,12 +57,25 @@ struct Fire {
}

impl Fire {
fn allocate(&mut self, key: OriginIdx, value: Instance) {
// if we allocate the same value twice, this is an interpreter error
assert!(self.values.insert(key, value).is_none());
}

fn copy(&mut self, to_copy: &RefIdx, key: OriginIdx) {
// if we haven't allocated this value beforehand, this is an interpreter error
let value = self.values.get(&to_copy.unwrap()).unwrap();

// FIXME: This is very invalid
self.allocate(key, value.clone());
}

fn perform_extern_call(
&self,
_fir: &Fir<FlattenData<'_>>,
node: &Node<FlattenData<'_>>,
args: &[RefIdx],
) -> Result<Option<Instance>, Error> {
) -> Option<Instance> {
let ast = node.data.ast.node();
let name = match &ast.node {
ast::Node::Function {
Expand All @@ -84,18 +98,19 @@ impl Fire {
})
}

Ok(None)
None
}

fn execute_stmts(
&mut self,
fir: &Fir<FlattenData<'_>>,
node: &RefIdx,
) -> Result<Option<Instance>, Error> {
// FIXME: Sholud this return a Result<Option<Instance>, Error>?
fn run_block(&mut self, fir: &Fir<FlattenData<'_>>, node: &RefIdx) -> Option<Instance> {
let stmts = &fir.nodes[&node.unwrap()];
let stmts = match &stmts.kind {
Kind::Statements(stmts) => stmts,
_ => unreachable!(),
_ => unreachable!(
"{}:{}: expected list of statements for node {node:?}. this is an interpreter error.",
file!(),
line!(),
),
};

let errs = stmts.iter().fold(Vec::new(), |mut errs, node| {
Expand All @@ -109,12 +124,9 @@ impl Fire {
}
});

if errs.is_empty() {
// FIXME: Invalid
Ok(None)
} else {
Err(Error::new(ErrKind::Multiple(errs)))
}
// FIXME: This is invalid, isn't it?
// FIXME: or should we just return () here?
None
}

// TODO: It would be good to have a "copy/move" function which actually performs the behavior we want (copy or move)
Expand All @@ -135,20 +147,21 @@ impl Traversal<FlattenData<'_>, Error> for Fire {
args: &[RefIdx],
) -> Fallible<Error> {
let def = &fir.nodes[&_to.unwrap()];
let (block, def_args) = match &def.kind {
Kind::Function { block, args, .. } => (block, args),
_ => unreachable!(),
};

args.iter()
.for_each(|arg| self.traverse_node(fir, &fir.nodes[&arg.unwrap()]).unwrap());
args.iter().enumerate().for_each(|(i, arg)| {
self.traverse_node(fir, &fir.nodes[&arg.unwrap()]).unwrap();
self.copy(arg, def_args[i].unwrap());
});

let _block = match &def.kind {
Kind::Function { block: None, .. } => self.perform_extern_call(fir, def, args),
Kind::Function {
block: Some(block), ..
} => {
self.execute_stmts(fir, block)
// self.perform_extern_call(_fir, def, args),
}
_ => unreachable!(),
match block {
None => self.perform_extern_call(fir, def, args),
Some(block) => self.run_block(fir, block),
};
// FIXME: We need to add bindings here between the function's variables and the arguments given to the call

Ok(())
}
Expand Down Expand Up @@ -176,7 +189,7 @@ impl Traversal<FlattenData<'_>, Error> for Fire {
};

// FIXME: Handle result here
self.values.insert(node.origin, instance);
self.allocate(node.origin, instance);

Ok(())
}
Expand Down Expand Up @@ -210,11 +223,24 @@ impl Traversal<FlattenData<'_>, Error> for Fire {
};

// FIXME: Handle result here
self.values.insert(node.origin, instance);
self.allocate(node.origin, instance);

Ok(())
}

fn traverse_binding(
&mut self,
_fir: &Fir<FlattenData<'_>>,
node: &Node<FlattenData<'_>>,
to: &RefIdx,
) -> Fallible<Error> {
// FIXME: This is very invalid
self.copy(to, node.origin);

Ok(())
}

// FIXME: Remove this function. Use Fire::run_block instead
fn traverse(&mut self, fir: &Fir<FlattenData<'_>>) -> Fallible<Vec<Error>> {
// FIXME: No unwrap here
let entry_point = fir.nodes.last_key_value().unwrap();
Expand Down

0 comments on commit 3fee9b6

Please sign in to comment.