schala/src/schala_lang/type_check.rs

115 lines
2.8 KiB
Rust
Raw Normal View History

2017-10-08 12:22:04 -07:00
use std::collections::HashMap;
2017-10-08 13:51:56 -07:00
use std::rc::Rc;
2017-10-08 12:22:04 -07:00
2017-10-04 02:07:30 -07:00
use schala_lang::parsing::{AST, Statement, Declaration, Expression, ExpressionType, Operation, TypeAnno};
2017-10-08 12:22:04 -07:00
#[derive(Debug, PartialEq, Eq, Hash)]
2017-10-08 13:51:56 -07:00
struct PathSpecifier {
name: Rc<String>,
kind: &'static str,
constant: bool,
}
2017-10-08 12:22:04 -07:00
struct SymbolTable {
2017-10-08 12:22:04 -07:00
map: HashMap<PathSpecifier, Expression>,
}
impl SymbolTable {
fn new() -> SymbolTable {
2017-10-08 12:22:04 -07:00
SymbolTable { map: HashMap::new() }
}
fn add_symbols(&mut self, ast: &AST) {
2017-10-08 13:51:56 -07:00
use self::Declaration::*;
for statement in ast.0.iter() {
match statement {
&Statement::ExpressionStatement(_) => (),
&Statement::Declaration(ref d) => {
match d {
&FuncDecl { .. } => (),
&TypeDecl { .. } => (),
&TypeAlias { .. } => (),
&Binding {ref name, ref constant, ref expr} => {
let spec = PathSpecifier {
name: name.clone(),
kind: "binding",
constant: *constant
};
let binding_contents = (*expr).clone();
self.map.insert(spec, binding_contents);
},
&Impl { .. } => (),
}
}
}
}
}
}
2017-10-04 02:07:30 -07:00
pub struct TypeContext {
symbol_table: SymbolTable,
2017-10-04 02:07:30 -07:00
}
impl TypeContext {
pub fn new() -> TypeContext {
TypeContext { symbol_table: SymbolTable::new() }
2017-10-04 02:07:30 -07:00
}
pub fn add_symbols(&mut self, ast: &AST) {
self.symbol_table.add_symbols(ast)
}
2017-10-08 13:57:43 -07:00
pub fn debug_symbol_table(&self) -> String {
format!("Symbol table:\n {:?}", self.symbol_table.map)
}
2017-10-04 02:07:30 -07:00
}
2017-10-07 22:08:48 -07:00
pub struct SchalaType {
2017-10-04 02:07:30 -07:00
}
2017-10-07 22:08:48 -07:00
type TypeCheckResult = Result<SchalaType, String>;
2017-10-04 02:07:30 -07:00
// from Niko's talk
/* fn type_check(expression, expected_ty) -> Ty {
let ty = bare_type_check(expression, expected_type);
if ty icompatible with expected_ty {
try_coerce(expression, ty, expected_ty)
} else {
ty
}
}
fn bare_type_check(exprssion, expected_type) -> Ty { ... }
*/
impl TypeContext {
pub fn type_check(&mut self, ast: &AST) -> TypeCheckResult {
use self::ExpressionType::*;
2017-10-04 02:07:30 -07:00
for statement in ast.0.iter() {
match statement {
&Statement::Declaration(ref _decl) => {
2017-10-07 22:08:48 -07:00
return Err(format!("Declarations not supported"));
2017-10-04 02:07:30 -07:00
},
&Statement::ExpressionStatement(ref expr) => {
2017-10-08 12:22:04 -07:00
self.expr_type_check(expr)?;
2017-10-04 02:07:30 -07:00
}
}
}
2017-10-07 22:08:48 -07:00
Ok(SchalaType { })
2017-10-04 02:07:30 -07:00
}
2017-10-08 12:22:04 -07:00
fn expr_type_check(&mut self, expr: &Expression) -> TypeCheckResult {
use self::ExpressionType::*;
match (&expr.0, &expr.1) {
(&IntLiteral(_), &Some(ref t)) => {
match t {
&TypeAnno::Singleton { ref name, ref params } if **name == "Int" && params.len() == 0 => (),
t => return Err(format!("Bad type {:?} for int literal", t)),
}
},
_ => (),
}
Ok(SchalaType { })
}
2017-10-04 02:07:30 -07:00
}