From b0711ef63501b7c29cb7c2014f3ceeaa399e3481 Mon Sep 17 00:00:00 2001 From: David Li Date: Sun, 5 Nov 2017 09:27:10 -0500 Subject: Add stubbed out type checker --- src/semantic/environment.rs | 33 +++++++++++++++++++++++++++++++++ src/semantic/mod.rs | 3 +++ src/semantic/translate.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/semantic/types.rs | 7 +++++++ 4 files changed, 82 insertions(+) create mode 100644 src/semantic/environment.rs create mode 100644 src/semantic/mod.rs create mode 100644 src/semantic/translate.rs create mode 100644 src/semantic/types.rs (limited to 'src/semantic') diff --git a/src/semantic/environment.rs b/src/semantic/environment.rs new file mode 100644 index 0000000..fab16b3 --- /dev/null +++ b/src/semantic/environment.rs @@ -0,0 +1,33 @@ +use std::collections::HashMap; +use std::hash::Hash; + +pub struct Environment<'a, T: 'a, U: 'a> { + pub parent: Option<&'a Environment<'a, T, U>>, + pub bindings: HashMap, +} + +impl<'a, T: 'a, U: 'a> Environment<'a, T, U> + where T: Eq + Hash { + pub fn new(parent: Option<&'a Environment<'a, T, U>>) -> Environment<'a, T, U> { + Environment { + parent: parent, + bindings: HashMap::new(), + } + } + + pub fn add_binding(&mut self, key: T, value: U) { + self.bindings.insert(key, value); + } + + pub fn lookup(&self, key: &T) -> Option<&U> { + if let Some(v) = self.bindings.get(key) { + Some(v) + } + else if let Some(p) = self.parent { + p.lookup(key) + } + else { + None + } + } +} diff --git a/src/semantic/mod.rs b/src/semantic/mod.rs new file mode 100644 index 0000000..433a196 --- /dev/null +++ b/src/semantic/mod.rs @@ -0,0 +1,3 @@ +pub mod environment; +pub mod translate; +pub mod types; diff --git a/src/semantic/translate.rs b/src/semantic/translate.rs new file mode 100644 index 0000000..7bcd646 --- /dev/null +++ b/src/semantic/translate.rs @@ -0,0 +1,39 @@ +use ::ast::{self, WithLocation}; +use super::environment; +use super::types::Ty; + +#[derive(Debug)] +pub enum TypeError { + Unimplemented, +} + +type TypeEnvironment<'a> = environment::Environment<'a, String, Ty>; +pub type Result = ::std::result::Result>; + +pub fn translate(program: &ast::Program) -> Result { + trans_exp(TypeEnvironment::new(None), &*program.0) +} + +fn trans_exp<'a>(venv: TypeEnvironment<'a>, exp: &WithLocation) -> Result { + use ast::Expression::*; + + match &exp.value { + &Let(ref decls, ref body) => { + Err(WithLocation::new(TypeError::Unimplemented, exp.start, exp.end)) + }, + &UnaryOp(ref op, ref operand) => { + Err(WithLocation::new(TypeError::Unimplemented, exp.start, exp.end)) + }, + &BinOp(ref op, ref left, ref right) => { + Err(WithLocation::new(TypeError::Unimplemented, exp.start, exp.end)) + }, + &Number(_) => Ok(Ty::Int), + &String(_) => Ok(Ty::String), + &Name(ref name) => { + Err(WithLocation::new(TypeError::Unimplemented, exp.start, exp.end)) + }, + &Nil => { + Err(WithLocation::new(TypeError::Unimplemented, exp.start, exp.end)) + }, + } +} diff --git a/src/semantic/types.rs b/src/semantic/types.rs new file mode 100644 index 0000000..2d76387 --- /dev/null +++ b/src/semantic/types.rs @@ -0,0 +1,7 @@ +#[derive(Debug)] +pub enum Ty { + Int, + String, + Nil, + Unit, +} -- cgit v1.2.3