schala/src/schala_lang/typechecking.rs

66 lines
1.6 KiB
Rust
Raw Normal View History

2018-02-21 02:31:28 -08:00
use std::rc::Rc;
use schala_lang::parsing;
pub struct TypeContext { }
2018-02-21 03:39:40 -08:00
#[derive(Debug, PartialEq, Clone)]
2018-02-21 02:31:28 -08:00
pub enum Type {
Unit,
2018-02-21 03:39:40 -08:00
Int,
Float,
StringT,
Bool,
Custom(Rc<String>),
2018-02-21 02:31:28 -08:00
Void
}
type TypeResult<T> = Result<T, String>;
impl TypeContext {
pub fn new() -> TypeContext {
TypeContext { }
}
pub fn type_check_ast(&mut self, ast: &parsing::AST) -> TypeResult<Type> {
use self::Type::*;
let mut ret_type = Unit;
for statement in ast.0.iter() {
ret_type = self.type_check_statement(statement)?;
}
Ok(ret_type)
}
fn type_check_statement(&mut self, statement: &parsing::Statement) -> TypeResult<Type> {
2018-02-21 03:39:40 -08:00
println!("statement should be: {:?}", statement);
2018-02-21 02:31:28 -08:00
use self::parsing::Statement::*;
match statement {
2018-02-21 03:39:40 -08:00
&ExpressionStatement(ref expr) => self.type_infer(expr),
2018-02-21 02:31:28 -08:00
&Declaration(ref decl) => self.type_check_declaration(decl),
}
}
fn type_check_declaration(&mut self, decl: &parsing::Declaration) -> TypeResult<Type> {
use self::Type::*;
Ok(Unit)
}
2018-02-21 03:39:40 -08:00
fn type_infer(&mut self, expr: &parsing::Expression) -> TypeResult<Type> {
use self::parsing::Expression;
use self::parsing::ExpressionType::*;
2018-02-21 02:31:28 -08:00
use self::Type::*;
2018-02-21 03:39:40 -08:00
match expr {
&Expression(ref e, Some(ref ty)) => Err(format!("Anno not implemented")),
&Expression(ref e, None) => match e {
&IntLiteral(_) => Ok(Int),
&FloatLiteral(_) => Ok(Float),
&StringLiteral(_) => Ok(StringT),
&BoolLiteral(_) => Ok(Bool),
_ => Err(format!("Type not yet implemented"))
}
}
}
fn unify(&mut self, t1: Type, t2: Type) -> TypeResult<Type> {
use self::Type::*;
Ok(Unit)
2018-02-21 02:31:28 -08:00
}
}