summaryrefslogtreecommitdiff
path: root/src/semantic
diff options
context:
space:
mode:
Diffstat (limited to 'src/semantic')
-rw-r--r--src/semantic/translate.rs26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/semantic/translate.rs b/src/semantic/translate.rs
index 7e0d2b8..617c832 100644
--- a/src/semantic/translate.rs
+++ b/src/semantic/translate.rs
@@ -64,8 +64,30 @@ fn trans_decl<'a>(
tenv: &mut TypeEnvironment<'a>,
decl: &WithLocation<ast::Declaration>) -> Result {
match decl.declaration {
- ast::DeclarationBody::Fun { .. } => {
- err!(decl, TypeError::Unimplemented)
+ ast::DeclarationBody::Fun { ref ty, ref params, ref body } => {
+ let declared_ty = if let &Some(ref ty) = ty {
+ 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_tenv = TypeEnvironment::new(Some(tenv));
+ for param in params.iter() {
+ let decl_ty = trans_ty(&mut new_venv, &mut new_tenv, &param.ty)?;
+ new_venv.add_binding(param.name.value.clone(), decl_ty);
+ }
+
+ let body_ty = trans_exp(&mut new_venv, &mut new_tenv, &*body)?;
+
+ if let Some(decl_ty) = declared_ty {
+ if &decl_ty.value != &body_ty {
+ return err!(decl_ty, TypeError::Mismatch {
+ expected: decl_ty.value,
+ actual: body_ty,
+ });
+ }
+ }
+
+ Ok(body_ty)
}
ast::DeclarationBody::Ty { ref ty } => {
trans_ty(venv, tenv, ty)