From 50a6779bd437b7e96b994d5d96727f8b0ef350ad Mon Sep 17 00:00:00 2001 From: Empa Date: Wed, 17 Jan 2024 23:25:37 +0100 Subject: [PATCH] Prepare for native functions in bytecode --- crates/saft-ast-to-ir/src/lib.rs | 20 ++++++++++---------- crates/saft-bytecode/src/compiler.rs | 5 +++-- crates/saft-bytecode/src/item.rs | 6 +++++- crates/saft-ir/src/lib.rs | 7 ++++--- crates/saft/src/main.rs | 9 +++++++-- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/crates/saft-ast-to-ir/src/lib.rs b/crates/saft-ast-to-ir/src/lib.rs index 85ef67e..1cc48b7 100644 --- a/crates/saft-ast-to-ir/src/lib.rs +++ b/crates/saft-ast-to-ir/src/lib.rs @@ -51,19 +51,19 @@ impl Error { } } -enum LowererItem { - Ir_(Spanned), +enum LowererItem { + Ir_(Spanned>), Unlowered, } -pub struct Lowerer { - items: Vec, +pub struct Lowerer { + items: Vec>, scopes: Vec>, scope_base: usize, var_counter: usize, } -impl Lowerer { +impl Lowerer { #[allow(clippy::new_without_default)] pub fn new() -> Self { Self { @@ -110,7 +110,7 @@ impl Lowerer { } } - fn resolve_item(&mut self, item: &Spanned<&ast::Item>) -> Result, Error> { + fn resolve_item(&mut self, item: &Spanned<&ast::Item>) -> Result>, Error> { let Spanned { s, v: item } = item; Ok(s.spanned(match item { @@ -132,7 +132,7 @@ impl Lowerer { }) } - pub fn lower_module(mut self, module: &ast::Module) -> Result { + pub fn lower_module(mut self, module: &ast::Module) -> Result, Error> { self.resolve_module_items(module)?; let stmts = module @@ -207,8 +207,8 @@ impl Lowerer { } fn lower_expr(&mut self, expr: &Spanned) -> Result, Error> { - fn binary( - lowerer: &mut Lowerer, + fn binary( + lowerer: &mut Lowerer, lhs: &Spanned, rhs: &Spanned, op: ir::BinaryOp, @@ -418,7 +418,7 @@ impl Lowerer { ref_ } - fn replace_item(&mut self, ref_: ir::ItemRef, item: Spanned) { + fn replace_item(&mut self, ref_: ir::ItemRef, item: Spanned>) { self.items[ref_.0] = LowererItem::Ir_(item); } diff --git a/crates/saft-bytecode/src/compiler.rs b/crates/saft-bytecode/src/compiler.rs index 60e927b..dd30e2c 100644 --- a/crates/saft-bytecode/src/compiler.rs +++ b/crates/saft-bytecode/src/compiler.rs @@ -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 { @@ -79,7 +79,7 @@ impl Compiler { } } - pub fn compile_module(&mut self, module: &ir::Module) -> Result<(Chunk, Vec), Error> { + pub fn compile_module(&mut self, module: &ir::Module) -> Result<(Chunk, Vec), Error> { let mut chunk = Chunk::new(); let mut items = module @@ -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::>()?; diff --git a/crates/saft-bytecode/src/item.rs b/crates/saft-bytecode/src/item.rs index 55a5da4..7185c96 100644 --- a/crates/saft-bytecode/src/item.rs +++ b/crates/saft-bytecode/src/item.rs @@ -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>, +} diff --git a/crates/saft-ir/src/lib.rs b/crates/saft-ir/src/lib.rs index 41788c2..90b4e3e 100644 --- a/crates/saft-ir/src/lib.rs +++ b/crates/saft-ir/src/lib.rs @@ -14,14 +14,15 @@ pub struct ItemRef(pub usize); pub struct VarRef(pub usize); #[derive(Debug)] -pub struct Module { - pub items: Vec>, +pub struct Module { + pub items: Vec>>, pub stmts: Vec>, } #[derive(Debug)] -pub enum Item { +pub enum Item { Function(Function), + NativeFunction(NativeFunction), } #[derive(Debug)] diff --git a/crates/saft/src/main.rs b/crates/saft/src/main.rs index 2db99d8..f3e4022 100644 --- a/crates/saft/src/main.rs +++ b/crates/saft/src/main.rs @@ -69,7 +69,12 @@ impl Saft { } } - fn try_lower(&mut self, fname: &str, s: &str, module: &ast::Module) -> Option { + fn try_lower( + &mut self, + fname: &str, + s: &str, + module: &ast::Module, + ) -> Option> { let mut files = SimpleFiles::new(); let id = files.add(fname, s); @@ -92,7 +97,7 @@ impl Saft { &mut self, fname: &str, s: &str, - module: &ir::Module, + module: &ir::Module, ) -> Option<(bytecode::chunk::Chunk, Vec)> { let mut files = SimpleFiles::new(); let id = files.add(fname, s);