From 7eb40e25dd1a979d81034cb19cd9354cfd188d24 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Mon, 4 Jan 2021 14:49:48 +0800 Subject: [PATCH] added typedef --- nac3core/src/lib.rs | 1 + nac3core/src/typedef.rs | 60 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 nac3core/src/typedef.rs diff --git a/nac3core/src/lib.rs b/nac3core/src/lib.rs index 69faf0be..53d5d36e 100644 --- a/nac3core/src/lib.rs +++ b/nac3core/src/lib.rs @@ -5,6 +5,7 @@ extern crate num_bigint; extern crate inkwell; extern crate rustpython_parser; +pub mod typedef; use std::error::Error; use std::fmt; use std::path::Path; diff --git a/nac3core/src/typedef.rs b/nac3core/src/typedef.rs new file mode 100644 index 00000000..bec61fd1 --- /dev/null +++ b/nac3core/src/typedef.rs @@ -0,0 +1,60 @@ +use std::collections::HashMap; +use std::rc::Rc; + +#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)] +pub struct PrimitiveId(pub(crate) usize); + +#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)] +pub struct ClassId(pub(crate) usize); + +#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)] +pub struct ParamId(pub(crate) usize); + +#[derive(PartialEq, Eq, Copy, Clone, Hash, Debug)] +pub struct VariableId(pub(crate) usize); + +#[derive(PartialEq, Eq, Clone, Hash, Debug)] +pub enum TypeEnum { + BotType, + SelfType, + PrimitiveType(PrimitiveId), + ClassType(ClassId), + VirtualClassType(ClassId), + ParametricType(ParamId, Vec>), + TypeVariable(VariableId), +} + +pub type Type = Rc; + +#[derive(Clone)] +pub struct FnDef { + // we assume methods first argument to be SelfType, + // so the first argument is not contained here + pub args: Vec, + pub result: Option, +} + +#[derive(Clone)] +pub struct TypeDef<'a> { + pub name: &'a str, + pub fields: HashMap<&'a str, Type>, + pub methods: HashMap<&'a str, FnDef>, +} + +#[derive(Clone)] +pub struct ClassDef<'a> { + pub base: TypeDef<'a>, + pub parents: Vec, +} + +#[derive(Clone)] +pub struct ParametricDef<'a> { + pub base: TypeDef<'a>, + pub params: Vec, +} + +#[derive(Clone)] +pub struct VarDef<'a> { + pub name: &'a str, + pub bound: Vec, +}