primitives and some lifetime issue

archive
pca006132 2020-12-28 13:15:46 +08:00 committed by pca006132
parent 81f4be60c7
commit 7f09596bcb
3 changed files with 69 additions and 3 deletions

View File

@ -1,4 +1,3 @@
use super::primitives::*;
use super::types::{Type::*, *};
use std::collections::HashMap;
use std::rc::Rc;

View File

@ -7,7 +7,8 @@ pub const LIST_TYPE: ParamId = ParamId(1);
pub const BOOL_TYPE: PrimitiveId = PrimitiveId(0);
pub const INT32_TYPE: PrimitiveId = PrimitiveId(1);
pub const FLOAT_TYPE: PrimitiveId = PrimitiveId(2);
pub const INT64_TYPE: PrimitiveId = PrimitiveId(2);
pub const FLOAT_TYPE: PrimitiveId = PrimitiveId(3);
fn impl_math(def: &mut TypeDef, ty: &Rc<Type>) {
let bin = Rc::new(ParametricType(
@ -99,6 +100,11 @@ pub fn basic_ctx() -> GlobalContext<'static> {
fields: HashMap::new(),
methods: HashMap::new(),
},
TypeDef {
name: "int64",
fields: HashMap::new(),
methods: HashMap::new(),
},
TypeDef {
name: "float",
fields: HashMap::new(),
@ -117,11 +123,72 @@ pub fn basic_ctx() -> GlobalContext<'static> {
impl_bits(int32_def, &int32);
impl_order(int32_def, &int32);
impl_eq(int32_def, &int32);
let int64_def = ctx.get_primitive_mut(INT64_TYPE);
let int64 = PrimitiveType(INT64_TYPE).into();
impl_math(int64_def, &int64);
impl_bits(int64_def, &int64);
impl_order(int64_def, &int64);
impl_eq(int64_def, &int64);
let float_def = ctx.get_primitive_mut(FLOAT_TYPE);
let float = PrimitiveType(FLOAT_TYPE).into();
impl_math(float_def, &float);
impl_order(float_def, &float);
impl_eq(float_def, &float);
let t = ctx.add_variable_private(VarDef {
name: "T",
bound: vec![],
});
ctx.add_parametric(ParametricDef {
base: TypeDef {
name: "tuple",
fields: HashMap::new(),
methods: HashMap::new(),
},
// we have nothing for tuple, so no param def
params: vec![],
});
ctx.add_parametric(ParametricDef {
base: TypeDef {
name: "list",
fields: HashMap::new(),
methods: HashMap::new(),
},
params: vec![t],
});
let i = ctx.add_variable_private(VarDef {
name: "I",
bound: vec![
PrimitiveType(INT32_TYPE).into(),
PrimitiveType(INT64_TYPE).into(),
PrimitiveType(FLOAT_TYPE).into(),
],
});
let args = Rc::new(ParametricType(TUPLE_TYPE, vec![TypeVariable(i).into()]));
ctx.add_fn(
"int32",
FnDef {
args: args.clone(),
result: Some(PrimitiveType(INT32_TYPE).into()),
},
);
ctx.add_fn(
"int64",
FnDef {
args: args.clone(),
result: Some(PrimitiveType(INT64_TYPE).into()),
},
);
ctx.add_fn(
"float",
FnDef {
args: args.clone(),
result: Some(PrimitiveType(FLOAT_TYPE).into()),
},
);
ctx
}

View File

@ -116,7 +116,7 @@ impl<'a> GlobalContext<'a> {
VariableId(self.var_defs.len() - 1)
}
pub fn add_fn(&'a mut self, name: &'a str, def: FnDef) {
pub fn add_fn(&mut self, name: &'a str, def: FnDef) {
self.fn_table.insert(name, def);
}