Some work on binops

This commit is contained in:
greg 2018-02-23 01:49:37 -08:00
parent 23af2b1455
commit 1caccc6ae2
1 changed files with 17 additions and 7 deletions

View File

@ -1,6 +1,10 @@
use std::rc::Rc; use std::rc::Rc;
use std::collections::HashMap; use std::collections::HashMap;
macro_rules! bx {
($e:expr) => { Box::new($e) }
}
use schala_lang::parsing; use schala_lang::parsing;
pub struct TypeContext { pub struct TypeContext {
@ -10,7 +14,7 @@ pub struct TypeContext {
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, PartialEq, Clone)]
pub enum Type { pub enum Type {
Const(TConst), Const(TConst),
Func(Vec<Type>), Func(Box<Type>, Box<Type>),
Void Void
} }
@ -78,11 +82,17 @@ impl TypeContext {
&FloatLiteral(_) => Ok(Const(Float)), &FloatLiteral(_) => Ok(Const(Float)),
&StringLiteral(_) => Ok(Const(StringT)), &StringLiteral(_) => Ok(Const(StringT)),
&BoolLiteral(_) => Ok(Const(Bool)), &BoolLiteral(_) => Ok(Const(Bool)),
&BinExp(ref op, ref lhs, ref rhs) => { &BinExp(ref op, ref lhs, ref rhs) => { /* remember there are both the haskell convention talk and the write you a haskell ways to do this! */
let _op_ty = self.infer_optype(op); match self.infer_optype(op)? {
let _lhs_ty = self.infer(lhs); Func(box t1, box Func(box t2, box t3)) => {
let _rhs_ty = self.infer(rhs); let lhs_ty = self.infer(lhs)?;
Ok(Const(Unit)) let rhs_ty = self.infer(rhs)?;
self.unify(t1, lhs_ty)?;
self.unify(t2, rhs_ty)?;
Ok(t3)
},
other => return Err(format!("{:?} is not a binary function type", other))
}
}, },
/* /*
BinExp(Operation, Box<Expression>, Box<Expression>), BinExp(Operation, Box<Expression>, Box<Expression>),
@ -107,7 +117,7 @@ impl TypeContext {
fn infer_optype(&mut self, _op: &parsing::Operation) -> TypeResult<Type> { fn infer_optype(&mut self, _op: &parsing::Operation) -> TypeResult<Type> {
use self::Type::*; use self::TConst::*; use self::Type::*; use self::TConst::*;
//this is a shim; not all ops are binops from int -> int -> int //this is a shim; not all ops are binops from int -> int -> int
Ok(Func(vec![Const(Int), Const(Int), Const(Int)])) Ok(Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int))))))
} }
fn type_from_anno(&mut self, anno: &parsing::TypeName) -> TypeResult<Type> { fn type_from_anno(&mut self, anno: &parsing::TypeName) -> TypeResult<Type> {
use self::parsing::TypeSingletonName; use self::parsing::TypeSingletonName;