Base compilation of some kind of file to a list of instructions seems to work(!)

I'm able to compile some basic expressions into instructions using the
`not` binary. Big first step, now we need to introduce branches and
loops to the syntax.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-09-16 18:01:40 -07:00
parent 61ac00ae39
commit e72cbe2b8c
5 changed files with 47 additions and 19 deletions

View File

@@ -3,9 +3,10 @@ pub mod error;
mod locals;
pub mod thunk;
use crate::{syn::ast::Body, obj::prelude::*, vm::consts::*};
use crate::{syn::ast::Body, obj::prelude::*, vm::{consts::*, inst::Inst}};
use std::collections::{BTreeMap, HashMap};
#[derive(Default)]
pub struct Compile {
const_data: ConstData,
globals: BTreeMap<Sym, Local>,
@@ -16,12 +17,16 @@ pub struct Compile {
impl Compile {
/// Creates a new compiler using the given text.
pub fn new() -> Self {
Compile {
const_data: Default::default(),
globals: Default::default(),
locals: Default::default(),
next_local: Default::default(),
}
Default::default()
}
/// Compiles the given AST body.
pub fn compile<'c>(&'c mut self, body: &Body) -> error::Result<(Vec<Inst>, &'c ConstPool)> {
let main = thunk::CompileBody::new(self)
.compile(body)?
.flatten()
.to_vec();
Ok((main, &self.const_data.const_pool))
}
/// Gets the constant data that is interned in this compile session.