Remove 'text' member from Compile struct, since we can return file positions instead

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-09-16 17:36:40 -07:00
parent 099ee5ea0d
commit 61ac00ae39
3 changed files with 20 additions and 23 deletions

View File

@@ -4,8 +4,8 @@ use crate::{compile::Compile, obj::prelude::*, syn::{ast::*, visit::*}};
///
/// This will *not* attempt to recursively collect locals, and will only stay on the base statement
/// level.
pub struct CollectLocals<'c, 't> {
compile: &'c mut Compile<'t>,
pub struct CollectLocals<'c> {
compile: &'c mut Compile,
}
// - Python's LEGB methodology seems to be good. Look up variables in this order:
@@ -17,8 +17,8 @@ pub struct CollectLocals<'c, 't> {
// - Scan assignments first. If anything is assigned (even before usage), it's local.
// - Everything else should just do a lookup
impl<'c, 't> CollectLocals<'c, 't> {
pub fn new(compile: &'c mut Compile<'t>) -> Self {
impl<'c> CollectLocals<'c> {
pub fn new(compile: &'c mut Compile) -> Self {
Self { compile }
}
@@ -27,7 +27,7 @@ impl<'c, 't> CollectLocals<'c, 't> {
}
}
impl Visit for CollectLocals<'_, '_> {
impl Visit for CollectLocals<'_> {
type Out = ();
fn visit_body(&mut self, body: &Body) -> Self::Out { DefaultAccept::default_accept(body, self); }

View File

@@ -6,18 +6,17 @@ pub mod thunk;
use crate::{syn::ast::Body, obj::prelude::*, vm::consts::*};
use std::collections::{BTreeMap, HashMap};
pub struct Compile<'t> {
text: &'t str,
pub struct Compile {
const_data: ConstData,
globals: BTreeMap<Sym, Local>,
locals: Vec<BTreeMap<Sym, Local>>,
next_local: Local,
}
impl<'t> Compile<'t> {
pub fn new(text: &'t str) -> Self {
impl Compile {
/// Creates a new compiler using the given text.
pub fn new() -> Self {
Compile {
text,
const_data: Default::default(),
globals: Default::default(),
locals: Default::default(),
@@ -25,14 +24,12 @@ impl<'t> Compile<'t> {
}
}
pub fn text(&self) -> &'t str {
self.text
}
/// Gets the constant data that is interned in this compile session.
pub fn const_data(&self) -> &ConstData {
&self.const_data
}
/// Mutably gets the constant data that is interned in this compile session.
pub fn const_data_mut(&mut self) -> &mut ConstData {
&mut self.const_data
}
@@ -62,7 +59,7 @@ impl<'t> Compile<'t> {
}
/// Creates a new local variable if it does not exist in the current local scope.
pub fn create_local(&mut self, sym: Sym) -> Local {
pub(crate) fn create_local(&mut self, sym: Sym) -> Local {
let locals = self.locals.last_mut().expect("scope");
if let Some(local) = locals.get(&sym) {
*local
@@ -88,17 +85,17 @@ impl<'t> Compile<'t> {
}
/// Pushes an empty scope layer of local variables.
pub fn push_scope_layer(&mut self) {
pub(crate) fn push_scope_layer(&mut self) {
self.locals.push(Default::default());
}
/// Pops a scope layer of local variables, if any are available.
pub fn pop_scope_layer(&mut self) -> Option<BTreeMap<Sym, Local>> {
pub(crate) fn pop_scope_layer(&mut self) -> Option<BTreeMap<Sym, Local>> {
self.locals.pop()
}
/// Collects local variables for the given AST body non-recursively.
pub fn collect_locals(&mut self, body: &Body) {
pub(crate) fn collect_locals(&mut self, body: &Body) {
locals::CollectLocals::new(self).collect(body);
}
}

View File

@@ -203,12 +203,12 @@ impl Flatten {
/// Thunks are the basic building blocks of the IR. Thunks form a chain of decision paths that may
/// be taken, which allows an optimizer to remove dead code, detect endless loops, and so on. This
/// allows for shrinking blocks of code without having to recalculate jump addresses.
pub struct CompileBody<'c, 't> {
compile: &'c mut Compile<'t>,
pub struct CompileBody<'c> {
compile: &'c mut Compile,
}
impl<'c, 't> CompileBody<'c, 't> {
pub fn new(compile: &'c mut Compile<'t>) -> Self {
impl<'c> CompileBody<'c> {
pub fn new(compile: &'c mut Compile) -> Self {
CompileBody { compile }
}
@@ -224,7 +224,7 @@ impl<'c, 't> CompileBody<'c, 't> {
// impl Visit for CompileBody
//
impl Visit for CompileBody<'_, '_> {
impl Visit for CompileBody<'_> {
// XXX
// Trying to "future-proof" by using Result<_> in case there's some reason that an error
// may need to be thrown in the future so I don't have to wrap every return value in Ok(_)