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 /// This will *not* attempt to recursively collect locals, and will only stay on the base statement
/// level. /// level.
pub struct CollectLocals<'c, 't> { pub struct CollectLocals<'c> {
compile: &'c mut Compile<'t>, compile: &'c mut Compile,
} }
// - Python's LEGB methodology seems to be good. Look up variables in this order: // - 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. // - Scan assignments first. If anything is assigned (even before usage), it's local.
// - Everything else should just do a lookup // - Everything else should just do a lookup
impl<'c, 't> CollectLocals<'c, 't> { impl<'c> CollectLocals<'c> {
pub fn new(compile: &'c mut Compile<'t>) -> Self { pub fn new(compile: &'c mut Compile) -> Self {
Self { compile } Self { compile }
} }
@@ -27,7 +27,7 @@ impl<'c, 't> CollectLocals<'c, 't> {
} }
} }
impl Visit for CollectLocals<'_, '_> { impl Visit for CollectLocals<'_> {
type Out = (); type Out = ();
fn visit_body(&mut self, body: &Body) -> Self::Out { DefaultAccept::default_accept(body, self); } 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 crate::{syn::ast::Body, obj::prelude::*, vm::consts::*};
use std::collections::{BTreeMap, HashMap}; use std::collections::{BTreeMap, HashMap};
pub struct Compile<'t> { pub struct Compile {
text: &'t str,
const_data: ConstData, const_data: ConstData,
globals: BTreeMap<Sym, Local>, globals: BTreeMap<Sym, Local>,
locals: Vec<BTreeMap<Sym, Local>>, locals: Vec<BTreeMap<Sym, Local>>,
next_local: Local, next_local: Local,
} }
impl<'t> Compile<'t> { impl Compile {
pub fn new(text: &'t str) -> Self { /// Creates a new compiler using the given text.
pub fn new() -> Self {
Compile { Compile {
text,
const_data: Default::default(), const_data: Default::default(),
globals: Default::default(), globals: Default::default(),
locals: Default::default(), locals: Default::default(),
@@ -25,14 +24,12 @@ impl<'t> Compile<'t> {
} }
} }
pub fn text(&self) -> &'t str { /// Gets the constant data that is interned in this compile session.
self.text
}
pub fn const_data(&self) -> &ConstData { pub fn const_data(&self) -> &ConstData {
&self.const_data &self.const_data
} }
/// Mutably gets the constant data that is interned in this compile session.
pub fn const_data_mut(&mut self) -> &mut ConstData { pub fn const_data_mut(&mut self) -> &mut ConstData {
&mut self.const_data &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. /// 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"); let locals = self.locals.last_mut().expect("scope");
if let Some(local) = locals.get(&sym) { if let Some(local) = locals.get(&sym) {
*local *local
@@ -88,17 +85,17 @@ impl<'t> Compile<'t> {
} }
/// Pushes an empty scope layer of local variables. /// 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()); self.locals.push(Default::default());
} }
/// Pops a scope layer of local variables, if any are available. /// 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() self.locals.pop()
} }
/// Collects local variables for the given AST body non-recursively. /// 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); 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 /// 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 /// 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. /// allows for shrinking blocks of code without having to recalculate jump addresses.
pub struct CompileBody<'c, 't> { pub struct CompileBody<'c> {
compile: &'c mut Compile<'t>, compile: &'c mut Compile,
} }
impl<'c, 't> CompileBody<'c, 't> { impl<'c> CompileBody<'c> {
pub fn new(compile: &'c mut Compile<'t>) -> Self { pub fn new(compile: &'c mut Compile) -> Self {
CompileBody { compile } CompileBody { compile }
} }
@@ -224,7 +224,7 @@ impl<'c, 't> CompileBody<'c, 't> {
// impl Visit for CompileBody // impl Visit for CompileBody
// //
impl Visit for CompileBody<'_, '_> { impl Visit for CompileBody<'_> {
// XXX // XXX
// Trying to "future-proof" by using Result<_> in case there's some reason that an error // 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(_) // may need to be thrown in the future so I don't have to wrap every return value in Ok(_)