diff options
author | David Li <li.davidm96@gmail.com> | 2017-11-26 20:03:22 -0500 |
---|---|---|
committer | David Li <li.davidm96@gmail.com> | 2017-11-26 20:03:22 -0500 |
commit | f4682d80de7bf7edc1a8be0aef0cdd0040149078 (patch) | |
tree | 48eee48faf22ba325b495d289665b0c38e1d30e0 /src | |
parent | 01daee374a299646c595dd245c8e4a4990769002 (diff) |
Translate variable references
Diffstat (limited to 'src')
-rw-r--r-- | src/semantic/analysis.rs | 28 | ||||
-rw-r--r-- | src/semantic/ir.rs | 6 | ||||
-rw-r--r-- | src/semantic/translate.rs | 2 |
3 files changed, 19 insertions, 17 deletions
diff --git a/src/semantic/analysis.rs b/src/semantic/analysis.rs index 152ede9..829e7c2 100644 --- a/src/semantic/analysis.rs +++ b/src/semantic/analysis.rs @@ -7,7 +7,7 @@ use ::ast::{self, WithLocation}; use super::environment; use super::frame; use super::ir; -use super::translate::{self, Level, Translate}; +use super::translate::{self, Expression, Level, Translate}; use super::types::{self, Ty}; #[derive(Debug)] @@ -24,6 +24,7 @@ pub enum TypeError { UnboundName, } +type VarEnvironment<'a> = environment::Environment<'a, String, (Expression, Ty)>; type TypeEnvironment<'a> = environment::Environment<'a, String, Ty>; pub type Result<T> = ::std::result::Result<T, WithLocation<TypeError>>; @@ -32,7 +33,7 @@ pub fn translate<'a, F: frame::Frame>( level: &mut Level<'a, F>, program: &ast::Program) -> Result<(translate::Expression, Ty)> { - let mut venv = TypeEnvironment::new(None); + let mut venv = VarEnvironment::new(None); let mut tenv = TypeEnvironment::new(None); tenv.add_binding("int".into(), Ty::Int); tenv.add_binding("string".into(), Ty::String); @@ -46,7 +47,7 @@ macro_rules! err { } fn trans_ty<'a>( - venv: &mut TypeEnvironment<'a>, + venv: &mut VarEnvironment<'a>, tenv: &mut TypeEnvironment<'a>, ty: &WithLocation<ast::Ty>) -> Result<Ty> { match ty.value { @@ -73,7 +74,7 @@ fn trans_ty<'a>( fn trans_decl<'a, 'b, F: frame::Frame>( tr: &mut Translate<F>, level: &mut Level<'b, F>, - venv: &mut TypeEnvironment<'a>, + venv: &mut VarEnvironment<'a>, tenv: &mut TypeEnvironment<'a>, decl: &WithLocation<ast::Declaration>) -> Result<(translate::Expression, Ty)> { match decl.declaration { @@ -82,13 +83,14 @@ fn trans_decl<'a, 'b, F: frame::Frame>( Some(ast::WithLocation::new(trans_ty(venv, tenv, &ty)?, ty.start, ty.end)) } else { None }; - let mut new_venv = TypeEnvironment::new(Some(venv)); + let mut new_venv = VarEnvironment::new(Some(venv)); let mut new_tenv = TypeEnvironment::new(Some(tenv)); let mut arg_types = vec![]; for param in params.iter() { let arg_ty = trans_ty(&mut new_venv, &mut new_tenv, ¶m.ty)?; arg_types.push(arg_ty.clone()); - new_venv.add_binding(param.name.value.clone(), arg_ty); + // TODO: + // new_venv.add_binding(param.name.value.clone(), arg_ty); } let (body_exp, body_ty) = @@ -130,7 +132,7 @@ fn trans_decl<'a, 'b, F: frame::Frame>( fn trans_exp<'a, 'b, F: frame::Frame>( tr: &mut Translate<F>, level: &mut Level<'b, F>, - venv: &mut TypeEnvironment<'a>, + venv: &mut VarEnvironment<'a>, tenv: &mut TypeEnvironment<'a>, exp: &WithLocation<ast::Expression>) -> Result<(translate::Expression, Ty)> { @@ -138,13 +140,13 @@ fn trans_exp<'a, 'b, F: frame::Frame>( match &exp.value { &Let(ref decls, ref body) => { - let mut new_venv = TypeEnvironment::new(Some(venv)); + let mut new_venv = VarEnvironment::new(Some(venv)); let mut new_tenv = TypeEnvironment::new(Some(tenv)); for decl in decls.iter() { let (decl_exp, decl_ty) = trans_decl(tr, level, &mut new_venv, &mut new_tenv, decl)?; match decl.declaration { ast::DeclarationBody::Fun { .. } | ast::DeclarationBody::Var { .. } => { - new_venv.add_binding(decl.name.clone(), decl_ty); + new_venv.add_binding(decl.name.clone(), (decl_exp, decl_ty)); } ast::DeclarationBody::Ty { .. } => { new_tenv.add_binding(decl.name.clone(), decl_ty); @@ -159,7 +161,7 @@ fn trans_exp<'a, 'b, F: frame::Frame>( arg_types.push(trans_exp(tr, level, venv, tenv, arg)?); } - let fun_ty = venv.lookup(name) + let &(ref fun_exp, ref fun_ty) = venv.lookup(name) .ok_or(WithLocation::new(TypeError::UnboundName, exp.start, exp.end))?; match fun_ty { @@ -257,9 +259,9 @@ fn trans_exp<'a, 'b, F: frame::Frame>( &String(_) => err!(exp, TypeError::Unimplemented), // &String(_) => Ok(Ty::String), &Name(ref name) => { - if let Some(ty) = venv.lookup(name) { - err!(exp, TypeError::Unimplemented) - // Ok(ty.clone()) + if let Some(&(ref exp, ref ty)) = venv.lookup(name) { + // TODO: clone might be expensive, Rc? + Ok((exp.clone(), ty.clone())) } else { err!(exp, TypeError::UnboundName) diff --git a/src/semantic/ir.rs b/src/semantic/ir.rs index f92cf27..a4ff875 100644 --- a/src/semantic/ir.rs +++ b/src/semantic/ir.rs @@ -1,11 +1,11 @@ use super::temp; -#[derive(Debug)] +#[derive(Clone,Debug)] pub enum BinOp { Plus, } -#[derive(Debug)] +#[derive(Clone,Debug)] pub enum Expression { Const(u64), Name(temp::TempLabel), @@ -16,7 +16,7 @@ pub enum Expression { Seq(Vec<Statement>, Box<Expression>), } -#[derive(Debug)] +#[derive(Clone,Debug)] pub enum Statement { Exp(Expression), MoveTemp(temp::TempName, Expression), diff --git a/src/semantic/translate.rs b/src/semantic/translate.rs index 5e68f36..f802c3f 100644 --- a/src/semantic/translate.rs +++ b/src/semantic/translate.rs @@ -2,7 +2,7 @@ use super::frame; use super::ir; use super::temp; -#[derive(Debug)] +#[derive(Clone,Debug)] pub enum Expression { Exp(ir::Expression), Void(ir::Statement), |