diff options
author | David Li <li.davidm96@gmail.com> | 2017-11-07 19:09:42 -0500 |
---|---|---|
committer | David Li <li.davidm96@gmail.com> | 2017-11-07 19:09:42 -0500 |
commit | 00945be8de227e2a6f56819e5864b58559e37ee6 (patch) | |
tree | a69bc1f54dd61274762eb4ddfd5daf1ec4934c7b /src/semantic | |
parent | 29c94bef2237f9b730ab298da61f8095a052cb91 (diff) |
Parameterize translation on frame type
Diffstat (limited to 'src/semantic')
-rw-r--r-- | src/semantic/analysis.rs | 15 | ||||
-rw-r--r-- | src/semantic/translate.rs | 36 |
2 files changed, 39 insertions, 12 deletions
diff --git a/src/semantic/analysis.rs b/src/semantic/analysis.rs index edc229f..cf19351 100644 --- a/src/semantic/analysis.rs +++ b/src/semantic/analysis.rs @@ -5,6 +5,7 @@ use ::ast::{self, WithLocation}; use super::environment; +use super::frame; use super::translate::{self, Translate}; use super::types::{self, Ty}; @@ -25,8 +26,10 @@ pub enum TypeError { type TypeEnvironment<'a> = environment::Environment<'a, String, Ty>; pub type Result<T> = ::std::result::Result<T, WithLocation<TypeError>>; -pub fn translate(translate: &mut Translate, program: &ast::Program) - -> Result<(translate::Expression, Ty)> { +pub fn translate<F: frame::Frame>( + translate: &mut Translate<F>, + program: &ast::Program) + -> Result<(translate::Expression, Ty)> { let mut venv = TypeEnvironment::new(None); let mut tenv = TypeEnvironment::new(None); tenv.add_binding("int".into(), Ty::Int); @@ -65,8 +68,8 @@ fn trans_ty<'a>( } } -fn trans_decl<'a>( - tr: &mut Translate, +fn trans_decl<'a, F: frame::Frame>( + tr: &mut Translate<F>, venv: &mut TypeEnvironment<'a>, tenv: &mut TypeEnvironment<'a>, decl: &WithLocation<ast::Declaration>) -> Result<Ty> { @@ -119,8 +122,8 @@ fn trans_decl<'a>( } } -fn trans_exp<'a>( - tr: &mut Translate, +fn trans_exp<'a, F: frame::Frame>( + tr: &mut Translate<F>, venv: &mut TypeEnvironment<'a>, tenv: &mut TypeEnvironment<'a>, exp: &WithLocation<ast::Expression>) diff --git a/src/semantic/translate.rs b/src/semantic/translate.rs index cc5aec0..179eb62 100644 --- a/src/semantic/translate.rs +++ b/src/semantic/translate.rs @@ -1,4 +1,6 @@ +use super::frame; use super::ir; +use super::temp; #[derive(Debug)] pub enum Expression { @@ -6,18 +8,40 @@ pub enum Expression { Void(ir::Statement), } -pub struct Translate { - +pub struct Translate<F> { + level_counter: usize, + _frametype: ::std::marker::PhantomData<F>, } -pub enum Level { +pub enum Level<'a, F> + where F: 'a + frame::Frame { Top, - Level, + Level { + parent: &'a Level<'a, F>, + frame: F, + unique_id: usize, + }, } -impl Translate { - pub fn new() -> Translate { +impl<F: frame::Frame> Translate<F> { + pub fn new() -> Translate<F> { Translate { + level_counter: 0, + _frametype: ::std::marker::PhantomData, + } + } + + pub fn make_level<'a>( + &mut self, + parent: &'a Level<F>, + name: temp::TempLabel, + formals: Vec<frame::Escape>) -> Level<'a, F> { + let id = self.level_counter; + self.level_counter += 1; + Level::Level { + parent: parent, + frame: F::new(name, formals), + unique_id: id, } } |