Skip to content

Commit

Permalink
Separate constants and compiled items
Browse files Browse the repository at this point in the history
  • Loading branch information
Quaqqer committed Feb 2, 2024
1 parent be4de81 commit d802311
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
6 changes: 6 additions & 0 deletions crates/saft-bytecode/src/compiled_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::constant::ConstantRef;

#[derive(Clone, Debug)]
pub(crate) enum CompiledItem {
Function(ConstantRef),
}
60 changes: 44 additions & 16 deletions crates/saft-bytecode/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use saft_ir as ir;

use crate::{
chunk::Chunk,
constant::Constant,
compiled_item::CompiledItem,
constant::{Constant, ConstantRef},
op::Op,
value::{Function, NativeFunction, SaftFunction},
};
Expand Down Expand Up @@ -72,6 +73,7 @@ pub struct Compiler {
stack_i: usize,
scopes: Vec<Scope>,
ref_offsets: HashMap<ir::VarRef, usize>,
compiled_items: Vec<CompiledItem>,
pub constants: Vec<Constant>,
}

Expand All @@ -82,30 +84,51 @@ impl Compiler {
stack_i: 0,
scopes: vec![Scope::new(0)],
ref_offsets: HashMap::new(),
compiled_items: Vec::new(),
constants: vec![],
}
}

fn compile_items(
&mut self,
items: &[Option<Spanned<ir::Item<NativeFunction>>>],
) -> Result<(), Error> {
for item in items.iter().skip(self.constants.len()) {
let item = item.as_ref().expect("Should not be none");
items: &[&Spanned<ir::Item<NativeFunction>>],
) -> Result<Option<ConstantRef>, Error> {
let mut new_compiled_items = items
.iter()
.skip(self.constants.len())
.map(|item| self.compile_item(item))
.try_collect::<Vec<_>>()?;
self.compiled_items.append(&mut new_compiled_items);

Ok(None)
}

let constant = match &item.v {
ir::Item::Function(fun) => Constant::Function(Function::SaftFunction(
fn compile_item(
&mut self,
item: &Spanned<ir::Item<NativeFunction>>,
) -> Result<CompiledItem, Error> {
let compiled_item = match &item.v {
ir::Item::Function(fun) => {
let constant = Constant::Function(Function::SaftFunction(
self.compile_fn(item.s.spanned(fun))?,
)),
ir::Item::Builtin(native) => {
Constant::Function(Function::NativeFunction(native.clone()))
}
};
));
let ref_ = self.add_constant(constant);
CompiledItem::Function(ref_)
}
ir::Item::Builtin(builtin) => {
let constant = Constant::Function(Function::NativeFunction(builtin.clone()));
let ref_ = self.add_constant(constant);
CompiledItem::Function(ref_)
}
};

self.constants.push(constant);
}
Ok(compiled_item)
}

Ok(())
fn add_constant(&mut self, constant: Constant) -> ConstantRef {
let i = self.constants.len();
self.constants.push(constant);
ConstantRef(i)
}

pub fn compile_module(
Expand All @@ -115,7 +138,12 @@ impl Compiler {
) -> Result<Chunk, Error> {
let mut chunk = Chunk::new();

self.compile_items(items)?;
self.compile_items(
&items
.iter()
.map(|opt| opt.as_ref().unwrap())
.collect::<Vec<_>>(),
)?;

for stmt in &module.stmts {
self.compile_stmt_(stmt, &mut chunk)?
Expand Down
3 changes: 3 additions & 0 deletions crates/saft-bytecode/src/constant.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::value::Function;

#[derive(Debug, Clone)]
pub struct ConstantRef(pub usize);

#[derive(Clone, Debug)]
pub enum Constant {
Function(Function),
Expand Down
2 changes: 1 addition & 1 deletion crates/saft-bytecode/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![feature(box_patterns, iterator_try_collect)]

pub mod chunk;
pub mod compiled_item;
pub mod compiler;
pub mod constant;
pub mod natives;
Expand Down

0 comments on commit d802311

Please sign in to comment.