more function things

This commit is contained in:
nora 2022-01-16 19:58:52 +01:00
parent e15967e24c
commit ff6b4703ab
11 changed files with 77 additions and 55 deletions

View file

@ -13,8 +13,7 @@ pub struct Ident {
pub span: Span,
}
#[derive(Debug, PartialEq)]
pub struct Program<'ast>(pub Vec<'ast, Stmt<'ast>>);
pub type Program<'ast> = Block<'ast>;
#[derive(Debug, PartialEq)]
pub struct Block<'ast> {

View file

@ -78,7 +78,7 @@ pub struct FnBlock<'bc> {
pub arity: u8,
}
#[cfg(feature = "pretty")]
#[cfg(feature = "_debug")]
impl debug2::Debug for FnBlock<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FnBlock")
@ -94,7 +94,7 @@ pub type Function = usize;
/// A bytecode instruction. For more details on the structure of the bytecode, read the module level docs [`bytecode`](`self`)
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
#[cfg_attr(feature = "_debug", derive(debug2::Debug))]
pub enum Instr {
/// An operation that does nothing.
Nop,

View file

@ -11,7 +11,6 @@ use crate::vm::Value;
use crate::{HashMap, RtAlloc};
use bumpalo::collections::Vec;
use bumpalo::Bump;
use std::borrow::BorrowMut;
use std::cell::RefCell;
use std::rc::Rc;
@ -95,13 +94,17 @@ impl<'bc, 'gc> Compiler<'bc, 'gc> {
self.blocks.push(global_block);
self.current_block = self.blocks.len() - 1;
// padding for backwards jumps
self.push_instr(Instr::Nop, StackChange::None, Span::dummy());
self.compile_stmts(&ast.0)?;
self.compile_fn(ast)?;
Ok(())
}
fn compile_fn(&mut self, block: &Block) -> CResult {
// padding for backwards jumps
self.push_instr(Instr::Nop, StackChange::None, block.span);
self.compile_stmts(&block.stmts)
}
fn compile_stmts(&mut self, stmts: &[Stmt]) -> CResult {
for stmt in stmts {
match stmt {
@ -168,8 +171,27 @@ impl<'bc, 'gc> Compiler<'bc, 'gc> {
})?,
};
let new_block_idx = self.blocks.len();
self.blocks.push(block);
self.current_block = self.blocks.len() - 1;
let old_block = self.current_block;
self.current_block = new_block_idx;
self.compile_fn(&decl.body)?;
self.current_block = old_block;
self.push_instr(
Instr::PushVal(Value::Function(new_block_idx)),
StackChange::Grow,
decl.span,
);
let stack_pos = self.current_stack_top();
self.env
.borrow_mut()
.locals
.insert(decl.name.sym, stack_pos);
Ok(())
}

View file

@ -13,7 +13,7 @@ pub use span::Span;
mod span {
#[derive(Debug, Default, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
#[cfg_attr(feature = "_debug", derive(debug2::Debug))]
pub struct Span {
pub start: usize,
pub end: usize,

View file

@ -30,14 +30,14 @@ impl<T: ?Sized> Deref for Gc<T> {
}
}
#[cfg(feature = "pretty")]
#[cfg(feature = "_debug")]
impl<T: debug2::Debug> debug2::Debug for Gc<T> {
fn fmt(&self, f: &mut debug2::Formatter<'_>) -> std::fmt::Result {
T::fmt(&*self, f)
}
}
#[cfg(feature = "pretty")]
#[cfg(feature = "_debug")]
impl debug2::Debug for Gc<str> {
fn fmt(&self, f: &mut debug2::Formatter<'_>) -> std::fmt::Result {
let str = self.deref();
@ -61,7 +61,7 @@ impl<T: ?Sized> Copy for Gc<T> {}
/// An reference to an interned String. Hashing and Equality are O(1) and just look at the pointer address
#[derive(Clone, Copy)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
#[cfg_attr(feature = "_debug", derive(debug2::Debug))]
pub struct Symbol {
gc: Gc<str>,
}
@ -74,20 +74,20 @@ type ObjectMap = HashMap<Symbol, Value>;
/// ```
/// This is inside the local x now.
#[derive(Clone, Copy)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
#[cfg_attr(feature = "_debug", derive(debug2::Debug))]
pub struct Object {
gc: Gc<HeapObject>,
}
#[derive(Debug)]
#[repr(C)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
#[cfg_attr(feature = "_debug", derive(debug2::Debug))]
struct HeapObject {
kind: HeapObjectKind,
}
#[derive(Debug)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
#[cfg_attr(feature = "_debug", derive(debug2::Debug))]
enum HeapObjectKind {
String(Gc<str>),
Object(ObjectMap),

View file

@ -68,11 +68,11 @@ fn process_ast(program: &str, ast: &Program, mut runtime: RtAlloc, cfg: &mut Con
match bytecode {
Ok(code) => {
if cfg.debug {
#[cfg(feature = "pretty")]
#[cfg(feature = "_debug")]
{
println!("Bytecode:\n{}\n", debug2::pprint(code));
}
#[cfg(not(feature = "pretty"))]
#[cfg(not(feature = "_debug"))]
{
println!("Bytecode:\n{:#?}\n", code);
}

View file

@ -78,7 +78,10 @@ where
const MAX_DEPTH: usize = 100;
fn program(&mut self) -> ParseResult<Program<'ast>> {
Ok(Program(self.statement_list()?))
Ok(Block {
stmts: self.statement_list()?,
span: Span::dummy(),
})
}
fn too_nested_error(&mut self) -> ParseResult<()> {

View file

@ -29,7 +29,7 @@ pub fn execute<'bc>(
}
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "pretty", derive(debug2::Debug))]
#[cfg_attr(feature = "_debug", derive(debug2::Debug))]
pub enum Value {
/// `null`
Null,
@ -278,7 +278,7 @@ impl Display for Value {
}
}
#[cfg(feature = "pretty")]
#[cfg(feature = "_debug")]
impl debug2::Debug for Ptr {
fn fmt(&self, f: &mut debug2::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Ptr").finish()