From 7f09596bcb48b32fa18ad6d5c0af9583fb85cad3 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Mon, 28 Dec 2020 13:15:46 +0800 Subject: [PATCH] primitives and some lifetime issue --- nac3type/src/inference.rs | 1 - nac3type/src/primitives.rs | 69 +++++++++++++++++++++++++++++++++++++- nac3type/src/types.rs | 2 +- 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/nac3type/src/inference.rs b/nac3type/src/inference.rs index bc875b6f..6cdc28a0 100644 --- a/nac3type/src/inference.rs +++ b/nac3type/src/inference.rs @@ -1,4 +1,3 @@ -use super::primitives::*; use super::types::{Type::*, *}; use std::collections::HashMap; use std::rc::Rc; diff --git a/nac3type/src/primitives.rs b/nac3type/src/primitives.rs index 8357a4a0..321088cb 100644 --- a/nac3type/src/primitives.rs +++ b/nac3type/src/primitives.rs @@ -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) { 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 } diff --git a/nac3type/src/types.rs b/nac3type/src/types.rs index e48f85ee..8828439b 100644 --- a/nac3type/src/types.rs +++ b/nac3type/src/types.rs @@ -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); }