Skip to content

Commit

Permalink
Prepare for native functions in bytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
Quaqqer committed Jan 17, 2024
1 parent 3a3ab9d commit 50a6779
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
20 changes: 10 additions & 10 deletions crates/saft-ast-to-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ impl Error {
}
}

enum LowererItem {
Ir_(Spanned<ir::Item>),
enum LowererItem<N> {
Ir_(Spanned<ir::Item<N>>),
Unlowered,
}

pub struct Lowerer {
items: Vec<LowererItem>,
pub struct Lowerer<N> {
items: Vec<LowererItem<N>>,
scopes: Vec<HashMap<String, ir::Ref>>,
scope_base: usize,
var_counter: usize,
}

impl Lowerer {
impl<N> Lowerer<N> {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
Expand Down Expand Up @@ -110,7 +110,7 @@ impl Lowerer {
}
}

fn resolve_item(&mut self, item: &Spanned<&ast::Item>) -> Result<Spanned<ir::Item>, Error> {
fn resolve_item(&mut self, item: &Spanned<&ast::Item>) -> Result<Spanned<ir::Item<N>>, Error> {
let Spanned { s, v: item } = item;

Ok(s.spanned(match item {
Expand All @@ -132,7 +132,7 @@ impl Lowerer {
})
}

pub fn lower_module(mut self, module: &ast::Module) -> Result<ir::Module, Error> {
pub fn lower_module(mut self, module: &ast::Module) -> Result<ir::Module<N>, Error> {
self.resolve_module_items(module)?;

let stmts = module
Expand Down Expand Up @@ -207,8 +207,8 @@ impl Lowerer {
}

fn lower_expr(&mut self, expr: &Spanned<ast::Expr>) -> Result<Spanned<ir::Expr>, Error> {
fn binary(
lowerer: &mut Lowerer,
fn binary<N>(
lowerer: &mut Lowerer<N>,
lhs: &Spanned<ast::Expr>,
rhs: &Spanned<ast::Expr>,
op: ir::BinaryOp,
Expand Down Expand Up @@ -418,7 +418,7 @@ impl Lowerer {
ref_
}

fn replace_item(&mut self, ref_: ir::ItemRef, item: Spanned<ir::Item>) {
fn replace_item(&mut self, ref_: ir::ItemRef, item: Spanned<ir::Item<N>>) {
self.items[ref_.0] = LowererItem::Ir_(item);
}

Expand Down
5 changes: 3 additions & 2 deletions crates/saft-bytecode/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use codespan_reporting::diagnostic::{Diagnostic, Label};
use saft_common::span::{Span, Spanned};
use saft_ir as ir;

use crate::{chunk::Chunk, item::Item, op::Op, value::SaftFunction};
use crate::{chunk::Chunk, item::{Item, NativeFunction}, op::Op, value::SaftFunction};

pub enum Error {
Exotic {
Expand Down Expand Up @@ -79,7 +79,7 @@ impl Compiler {
}
}

pub fn compile_module(&mut self, module: &ir::Module) -> Result<(Chunk, Vec<Item>), Error> {
pub fn compile_module(&mut self, module: &ir::Module<NativeFunction>) -> Result<(Chunk, Vec<Item>), Error> {
let mut chunk = Chunk::new();

let mut items = module
Expand All @@ -90,6 +90,7 @@ impl Compiler {
ir::Item::Function(function) => {
Item::SaftFunction(self.compile_fn(item.s.spanned(function))?)
}
ir::Item::NativeFunction(_) => todo!(),
})
})
.try_collect::<Vec<_>>()?;
Expand Down
6 changes: 5 additions & 1 deletion crates/saft-bytecode/src/item.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::value::SaftFunction;
use crate::{value::SaftFunction, vm};

#[derive(Clone, Debug)]
pub enum Item {
SaftFunction(SaftFunction),
}

pub struct NativeFunction {
f: fn(&mut vm::Vm) -> Result<(), vm::Error>,
}
7 changes: 4 additions & 3 deletions crates/saft-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ pub struct ItemRef(pub usize);
pub struct VarRef(pub usize);

#[derive(Debug)]
pub struct Module {
pub items: Vec<Spanned<Item>>,
pub struct Module<NativeFunction> {
pub items: Vec<Spanned<Item<NativeFunction>>>,
pub stmts: Vec<Spanned<Stmt>>,
}

#[derive(Debug)]
pub enum Item {
pub enum Item<NativeFunction> {
Function(Function),
NativeFunction(NativeFunction),
}

#[derive(Debug)]
Expand Down
9 changes: 7 additions & 2 deletions crates/saft/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ impl Saft {
}
}

fn try_lower(&mut self, fname: &str, s: &str, module: &ast::Module) -> Option<ir::Module> {
fn try_lower(
&mut self,
fname: &str,
s: &str,
module: &ast::Module,
) -> Option<ir::Module<bytecode::item::NativeFunction>> {
let mut files = SimpleFiles::new();
let id = files.add(fname, s);

Expand All @@ -92,7 +97,7 @@ impl Saft {
&mut self,
fname: &str,
s: &str,
module: &ir::Module,
module: &ir::Module<bytecode::item::NativeFunction>,
) -> Option<(bytecode::chunk::Chunk, Vec<bytecode::item::Item>)> {
let mut files = SimpleFiles::new();
let id = files.add(fname, s);
Expand Down

0 comments on commit 50a6779

Please sign in to comment.