forked from M-Labs/nac3
Compare commits
19 Commits
Author | SHA1 | Date | |
---|---|---|---|
f691fbb4dc | |||
44f4b7cfc7 | |||
584d8a490f | |||
67963fc21f | |||
2b2e220723 | |||
fa2fc1db54 | |||
8ade8c7b1f | |||
dea987dfde | |||
231bcd95fc | |||
44ccae6959 | |||
0d20b36974 | |||
0269dcee84 | |||
8fd164bda2 | |||
e625bd4ad2 | |||
f1b8e73703 | |||
38a3240e6e | |||
9cba777b3b | |||
bd67b3f387 | |||
59453d3785 |
228
nac3core/src/context/inference_context.rs
Normal file
228
nac3core/src/context/inference_context.rs
Normal file
@ -0,0 +1,228 @@
|
||||
use super::TopLevelContext;
|
||||
use crate::typedef::*;
|
||||
use std::boxed::Box;
|
||||
use std::collections::HashMap;
|
||||
|
||||
struct ContextStack<'a> {
|
||||
/// stack level, starts from 0
|
||||
level: u32,
|
||||
/// stack of variable definitions containing (id, def, level) where `def` is the original
|
||||
/// definition in `level-1`.
|
||||
var_defs: Vec<(usize, VarDef<'a>, u32)>,
|
||||
/// stack of symbol definitions containing (name, level) where `level` is the smallest level
|
||||
/// where the name is assigned a value
|
||||
sym_def: Vec<(&'a str, u32)>,
|
||||
}
|
||||
|
||||
pub struct InferenceContext<'a> {
|
||||
/// top level context
|
||||
top_level: TopLevelContext<'a>,
|
||||
|
||||
/// list of primitive instances
|
||||
primitives: Vec<Type>,
|
||||
/// list of variable instances
|
||||
variables: Vec<Type>,
|
||||
/// identifier to (type, readable) mapping.
|
||||
/// an identifier might be defined earlier but has no value (for some code path), thus not
|
||||
/// readable.
|
||||
sym_table: HashMap<&'a str, (Type, bool)>,
|
||||
/// resolution function reference, that may resolve unbounded identifiers to some type
|
||||
resolution_fn: Box<dyn FnMut(&str) -> Result<Type, String>>,
|
||||
/// stack
|
||||
stack: ContextStack<'a>,
|
||||
}
|
||||
|
||||
// non-trivial implementations here
|
||||
impl<'a> InferenceContext<'a> {
|
||||
/// return a new `InferenceContext` from `TopLevelContext` and resolution function.
|
||||
pub fn new(
|
||||
top_level: TopLevelContext,
|
||||
resolution_fn: Box<dyn FnMut(&str) -> Result<Type, String>>,
|
||||
) -> InferenceContext {
|
||||
let primitives = (0..top_level.primitive_defs.len())
|
||||
.map(|v| TypeEnum::PrimitiveType(PrimitiveId(v)).into())
|
||||
.collect();
|
||||
let variables = (0..top_level.var_defs.len())
|
||||
.map(|v| TypeEnum::TypeVariable(VariableId(v)).into())
|
||||
.collect();
|
||||
InferenceContext {
|
||||
top_level,
|
||||
primitives,
|
||||
variables,
|
||||
sym_table: HashMap::new(),
|
||||
resolution_fn,
|
||||
stack: ContextStack {
|
||||
level: 0,
|
||||
var_defs: Vec::new(),
|
||||
sym_def: Vec::new(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// execute the function with new scope.
|
||||
/// variable assignment would be limited within the scope (not readable outside), and type
|
||||
/// variable type guard would be limited within the scope.
|
||||
/// returns the list of variables assigned within the scope, and the result of the function
|
||||
pub fn with_scope<F, R>(&mut self, f: F) -> (Vec<&'a str>, R)
|
||||
where
|
||||
F: FnOnce(&mut Self) -> R,
|
||||
{
|
||||
self.stack.level += 1;
|
||||
let result = f(self);
|
||||
self.stack.level -= 1;
|
||||
while !self.stack.var_defs.is_empty() {
|
||||
let (_, _, level) = self.stack.var_defs.last().unwrap();
|
||||
if *level > self.stack.level {
|
||||
let (id, def, _) = self.stack.var_defs.pop().unwrap();
|
||||
self.top_level.var_defs[id] = def;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let mut poped_names = Vec::new();
|
||||
while !self.stack.sym_def.is_empty() {
|
||||
let (_, level) = self.stack.sym_def.last().unwrap();
|
||||
if *level > self.stack.level {
|
||||
let (name, _) = self.stack.sym_def.pop().unwrap();
|
||||
self.sym_table.remove(name).unwrap();
|
||||
poped_names.push(name);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
(poped_names, result)
|
||||
}
|
||||
|
||||
/// assign a type to an identifier.
|
||||
/// may return error if the identifier was defined but with different type
|
||||
pub fn assign(&mut self, name: &'a str, ty: Type) -> Result<Type, String> {
|
||||
if let Some((t, x)) = self.sym_table.get_mut(name) {
|
||||
if t == &ty {
|
||||
if !*x {
|
||||
self.stack.sym_def.push((name, self.stack.level));
|
||||
}
|
||||
*x = true;
|
||||
Ok(ty)
|
||||
} else {
|
||||
Err("different types".into())
|
||||
}
|
||||
} else {
|
||||
self.stack.sym_def.push((name, self.stack.level));
|
||||
self.sym_table.insert(name, (ty.clone(), true));
|
||||
Ok(ty)
|
||||
}
|
||||
}
|
||||
|
||||
/// check if an identifier is already defined
|
||||
pub fn defined(&self, name: &str) -> bool {
|
||||
self.sym_table.get(name).is_some()
|
||||
}
|
||||
|
||||
/// get the type of an identifier
|
||||
/// may return error if the identifier is not defined, and cannot be resolved with the
|
||||
/// resolution function.
|
||||
pub fn resolve(&mut self, name: &str) -> Result<Type, String> {
|
||||
if let Some((t, x)) = self.sym_table.get(name) {
|
||||
if *x {
|
||||
Ok(t.clone())
|
||||
} else {
|
||||
Err("may not have value".into())
|
||||
}
|
||||
} else {
|
||||
self.resolution_fn.as_mut()(name)
|
||||
}
|
||||
}
|
||||
|
||||
/// restrict the bound of a type variable by replacing its definition.
|
||||
/// used for implementing type guard
|
||||
pub fn restrict(&mut self, id: VariableId, mut def: VarDef<'a>) {
|
||||
std::mem::swap(self.top_level.var_defs.get_mut(id.0).unwrap(), &mut def);
|
||||
self.stack.var_defs.push((id.0, def, self.stack.level));
|
||||
}
|
||||
}
|
||||
|
||||
// trivial getters:
|
||||
impl<'a> InferenceContext<'a> {
|
||||
pub fn get_primitive(&self, id: PrimitiveId) -> Type {
|
||||
self.primitives.get(id.0).unwrap().clone()
|
||||
}
|
||||
pub fn get_variable(&self, id: VariableId) -> Type {
|
||||
self.variables.get(id.0).unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn get_fn_def(&self, name: &str) -> Option<&FnDef> {
|
||||
self.top_level.fn_table.get(name)
|
||||
}
|
||||
pub fn get_primitive_def(&self, id: PrimitiveId) -> &TypeDef {
|
||||
self.top_level.primitive_defs.get(id.0).unwrap()
|
||||
}
|
||||
pub fn get_class_def(&self, id: ClassId) -> &ClassDef {
|
||||
self.top_level.class_defs.get(id.0).unwrap()
|
||||
}
|
||||
pub fn get_parametric_def(&self, id: ParamId) -> &ParametricDef {
|
||||
self.top_level.parametric_defs.get(id.0).unwrap()
|
||||
}
|
||||
pub fn get_variable_def(&self, id: VariableId) -> &VarDef {
|
||||
self.top_level.var_defs.get(id.0).unwrap()
|
||||
}
|
||||
pub fn get_type(&self, name: &str) -> Option<Type> {
|
||||
self.top_level.get_type(name)
|
||||
}
|
||||
}
|
||||
|
||||
impl TypeEnum {
|
||||
pub fn subst(&self, map: &HashMap<VariableId, Type>) -> TypeEnum {
|
||||
match self {
|
||||
TypeEnum::TypeVariable(id) => map.get(id).map(|v| v.as_ref()).unwrap_or(self).clone(),
|
||||
TypeEnum::ParametricType(id, params) => TypeEnum::ParametricType(
|
||||
*id,
|
||||
params
|
||||
.iter()
|
||||
.map(|v| v.as_ref().subst(map).into())
|
||||
.collect(),
|
||||
),
|
||||
_ => self.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inv_subst(&self, map: &[(Type, Type)]) -> Type {
|
||||
for (from, to) in map.iter() {
|
||||
if self == from.as_ref() {
|
||||
return to.clone();
|
||||
}
|
||||
}
|
||||
match self {
|
||||
TypeEnum::ParametricType(id, params) => TypeEnum::ParametricType(
|
||||
*id,
|
||||
params.iter().map(|v| v.as_ref().inv_subst(map)).collect(),
|
||||
),
|
||||
_ => self.clone(),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
pub fn get_subst(&self, ctx: &InferenceContext) -> HashMap<VariableId, Type> {
|
||||
match self {
|
||||
TypeEnum::ParametricType(id, params) => {
|
||||
let vars = &ctx.get_parametric_def(*id).params;
|
||||
vars.iter()
|
||||
.zip(params)
|
||||
.map(|(v, p)| (*v, p.as_ref().clone().into()))
|
||||
.collect()
|
||||
}
|
||||
// if this proves to be slow, we can use option type
|
||||
_ => HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_base<'b: 'a, 'a>(&'a self, ctx: &'b InferenceContext) -> Option<&'b TypeDef> {
|
||||
match self {
|
||||
TypeEnum::PrimitiveType(id) => Some(ctx.get_primitive_def(*id)),
|
||||
TypeEnum::ClassType(id) | TypeEnum::VirtualClassType(id) => {
|
||||
Some(&ctx.get_class_def(*id).base)
|
||||
}
|
||||
TypeEnum::ParametricType(id, _) => Some(&ctx.get_parametric_def(*id).base),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
4
nac3core/src/context/mod.rs
Normal file
4
nac3core/src/context/mod.rs
Normal file
@ -0,0 +1,4 @@
|
||||
mod inference_context;
|
||||
mod top_level_context;
|
||||
pub use inference_context::InferenceContext;
|
||||
pub use top_level_context::TopLevelContext;
|
136
nac3core/src/context/top_level_context.rs
Normal file
136
nac3core/src/context/top_level_context.rs
Normal file
@ -0,0 +1,136 @@
|
||||
use crate::typedef::*;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// Structure for storing top-level type definitions.
|
||||
/// Used for collecting type signature from source code.
|
||||
/// Can be converted to `InferenceContext` for type inference in functions.
|
||||
pub struct TopLevelContext<'a> {
|
||||
/// List of primitive definitions.
|
||||
pub(super) primitive_defs: Vec<TypeDef<'a>>,
|
||||
/// List of class definitions.
|
||||
pub(super) class_defs: Vec<ClassDef<'a>>,
|
||||
/// List of parametric type definitions.
|
||||
pub(super) parametric_defs: Vec<ParametricDef<'a>>,
|
||||
/// List of type variable definitions.
|
||||
pub(super) var_defs: Vec<VarDef<'a>>,
|
||||
/// Function name to signature mapping.
|
||||
pub(super) fn_table: HashMap<&'a str, FnDef>,
|
||||
/// Type name to type mapping.
|
||||
pub(super) sym_table: HashMap<&'a str, Type>,
|
||||
|
||||
primitives: Vec<Type>,
|
||||
variables: Vec<Type>,
|
||||
}
|
||||
|
||||
impl<'a> TopLevelContext<'a> {
|
||||
pub fn new(primitive_defs: Vec<TypeDef<'a>>) -> TopLevelContext {
|
||||
let mut sym_table = HashMap::new();
|
||||
let mut primitives = Vec::new();
|
||||
for (i, t) in primitive_defs.iter().enumerate() {
|
||||
primitives.push(TypeEnum::PrimitiveType(PrimitiveId(i)).into());
|
||||
sym_table.insert(t.name, TypeEnum::PrimitiveType(PrimitiveId(i)).into());
|
||||
}
|
||||
TopLevelContext {
|
||||
primitive_defs,
|
||||
class_defs: Vec::new(),
|
||||
parametric_defs: Vec::new(),
|
||||
var_defs: Vec::new(),
|
||||
fn_table: HashMap::new(),
|
||||
sym_table,
|
||||
primitives,
|
||||
variables: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_class(&mut self, def: ClassDef<'a>) -> ClassId {
|
||||
self.sym_table.insert(
|
||||
def.base.name,
|
||||
TypeEnum::ClassType(ClassId(self.class_defs.len())).into(),
|
||||
);
|
||||
self.class_defs.push(def);
|
||||
ClassId(self.class_defs.len() - 1)
|
||||
}
|
||||
|
||||
pub fn add_parametric(&mut self, def: ParametricDef<'a>) -> ParamId {
|
||||
let params = def
|
||||
.params
|
||||
.iter()
|
||||
.map(|&v| Rc::new(TypeEnum::TypeVariable(v)))
|
||||
.collect();
|
||||
self.sym_table.insert(
|
||||
def.base.name,
|
||||
TypeEnum::ParametricType(ParamId(self.parametric_defs.len()), params).into(),
|
||||
);
|
||||
self.parametric_defs.push(def);
|
||||
ParamId(self.parametric_defs.len() - 1)
|
||||
}
|
||||
|
||||
pub fn add_variable(&mut self, def: VarDef<'a>) -> VariableId {
|
||||
self.sym_table.insert(
|
||||
def.name,
|
||||
TypeEnum::TypeVariable(VariableId(self.var_defs.len())).into(),
|
||||
);
|
||||
self.add_variable_private(def)
|
||||
}
|
||||
|
||||
pub fn add_variable_private(&mut self, def: VarDef<'a>) -> VariableId {
|
||||
self.var_defs.push(def);
|
||||
self.variables
|
||||
.push(TypeEnum::TypeVariable(VariableId(self.var_defs.len() - 1)).into());
|
||||
VariableId(self.var_defs.len() - 1)
|
||||
}
|
||||
|
||||
pub fn add_fn(&mut self, name: &'a str, def: FnDef) {
|
||||
self.fn_table.insert(name, def);
|
||||
}
|
||||
|
||||
pub fn get_fn_def(&self, name: &str) -> Option<&FnDef> {
|
||||
self.fn_table.get(name)
|
||||
}
|
||||
|
||||
pub fn get_primitive_def_mut(&mut self, id: PrimitiveId) -> &mut TypeDef<'a> {
|
||||
self.primitive_defs.get_mut(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_primitive_def(&self, id: PrimitiveId) -> &TypeDef {
|
||||
self.primitive_defs.get(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_class_def_mut(&mut self, id: ClassId) -> &mut ClassDef<'a> {
|
||||
self.class_defs.get_mut(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_class_def(&self, id: ClassId) -> &ClassDef {
|
||||
self.class_defs.get(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_parametric_def_mut(&mut self, id: ParamId) -> &mut ParametricDef<'a> {
|
||||
self.parametric_defs.get_mut(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_parametric_def(&self, id: ParamId) -> &ParametricDef {
|
||||
self.parametric_defs.get(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_variable_def_mut(&mut self, id: VariableId) -> &mut VarDef<'a> {
|
||||
self.var_defs.get_mut(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_variable_def(&self, id: VariableId) -> &VarDef {
|
||||
self.var_defs.get(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_primitive(&self, id: PrimitiveId) -> Type {
|
||||
self.primitives.get(id.0).unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn get_variable(&self, id: VariableId) -> Type {
|
||||
self.variables.get(id.0).unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn get_type(&self, name: &str) -> Option<Type> {
|
||||
// TODO: handle parametric types
|
||||
self.sym_table.get(name).cloned()
|
||||
}
|
||||
}
|
966
nac3core/src/expression_inference.rs
Normal file
966
nac3core/src/expression_inference.rs
Normal file
@ -0,0 +1,966 @@
|
||||
use crate::context::InferenceContext;
|
||||
use crate::inference_core::resolve_call;
|
||||
use crate::magic_methods::*;
|
||||
use crate::primitives::*;
|
||||
use crate::typedef::{Type, TypeEnum::*};
|
||||
use rustpython_parser::ast::{
|
||||
Comparison, Comprehension, ComprehensionKind, Expression, ExpressionType, Operator,
|
||||
UnaryOperator,
|
||||
};
|
||||
use std::convert::TryInto;
|
||||
|
||||
type ParserResult = Result<Option<Type>, String>;
|
||||
|
||||
pub fn infer_expr<'b: 'a, 'a>(
|
||||
ctx: &mut InferenceContext<'a>,
|
||||
expr: &'b Expression,
|
||||
) -> ParserResult {
|
||||
match &expr.node {
|
||||
ExpressionType::Number { value } => infer_constant(ctx, value),
|
||||
ExpressionType::Identifier { name } => infer_identifier(ctx, name),
|
||||
ExpressionType::List { elements } => infer_list(ctx, elements),
|
||||
ExpressionType::Tuple { elements } => infer_tuple(ctx, elements),
|
||||
ExpressionType::Attribute { value, name } => infer_attribute(ctx, value, name),
|
||||
ExpressionType::BoolOp { values, .. } => infer_bool_ops(ctx, values),
|
||||
ExpressionType::Binop { a, b, op } => infer_bin_ops(ctx, op, a, b),
|
||||
ExpressionType::Unop { op, a } => infer_unary_ops(ctx, op, a),
|
||||
ExpressionType::Compare { vals, ops } => infer_compare(ctx, vals, ops),
|
||||
ExpressionType::Call {
|
||||
args,
|
||||
function,
|
||||
keywords,
|
||||
} => {
|
||||
if !keywords.is_empty() {
|
||||
Err("keyword is not supported".into())
|
||||
} else {
|
||||
infer_call(ctx, &args, &function)
|
||||
}
|
||||
}
|
||||
ExpressionType::Subscript { a, b } => infer_subscript(ctx, a, b),
|
||||
ExpressionType::IfExpression { test, body, orelse } => {
|
||||
infer_if_expr(ctx, &test, &body, orelse)
|
||||
}
|
||||
ExpressionType::Comprehension { kind, generators } => match kind.as_ref() {
|
||||
ComprehensionKind::List { element } => {
|
||||
if generators.len() == 1 {
|
||||
infer_list_comprehension(ctx, element, &generators[0])
|
||||
} else {
|
||||
Err("only 1 generator statement is supported".into())
|
||||
}
|
||||
}
|
||||
_ => Err("only list comprehension is supported".into()),
|
||||
},
|
||||
ExpressionType::True | ExpressionType::False => Ok(Some(ctx.get_primitive(BOOL_TYPE))),
|
||||
_ => Err("not supported".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn infer_constant(
|
||||
ctx: &mut InferenceContext,
|
||||
value: &rustpython_parser::ast::Number,
|
||||
) -> ParserResult {
|
||||
use rustpython_parser::ast::Number;
|
||||
match value {
|
||||
Number::Integer { value } => {
|
||||
let int32: Result<i32, _> = value.try_into();
|
||||
if int32.is_ok() {
|
||||
Ok(Some(ctx.get_primitive(INT32_TYPE)))
|
||||
} else {
|
||||
let int64: Result<i64, _> = value.try_into();
|
||||
if int64.is_ok() {
|
||||
Ok(Some(ctx.get_primitive(INT64_TYPE)))
|
||||
} else {
|
||||
Err("integer out of range".into())
|
||||
}
|
||||
}
|
||||
}
|
||||
Number::Float { .. } => Ok(Some(ctx.get_primitive(FLOAT_TYPE))),
|
||||
_ => Err("not supported".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn infer_identifier(ctx: &mut InferenceContext, name: &str) -> ParserResult {
|
||||
Ok(Some(ctx.resolve(name)?))
|
||||
}
|
||||
|
||||
fn infer_list<'b: 'a, 'a>(
|
||||
ctx: &mut InferenceContext<'a>,
|
||||
elements: &'b [Expression],
|
||||
) -> ParserResult {
|
||||
if elements.is_empty() {
|
||||
return Ok(Some(ParametricType(LIST_TYPE, vec![BotType.into()]).into()));
|
||||
}
|
||||
|
||||
let mut types = elements.iter().map(|v| infer_expr(ctx, v));
|
||||
|
||||
let head = types.next().unwrap()?;
|
||||
if head.is_none() {
|
||||
return Err("list elements must have some type".into());
|
||||
}
|
||||
for v in types {
|
||||
if v? != head {
|
||||
return Err("inhomogeneous list is not allowed".into());
|
||||
}
|
||||
}
|
||||
Ok(Some(ParametricType(LIST_TYPE, vec![head.unwrap()]).into()))
|
||||
}
|
||||
|
||||
fn infer_tuple<'b: 'a, 'a>(
|
||||
ctx: &mut InferenceContext<'a>,
|
||||
elements: &'b [Expression],
|
||||
) -> ParserResult {
|
||||
let types: Result<Option<Vec<_>>, String> =
|
||||
elements.iter().map(|v| infer_expr(ctx, v)).collect();
|
||||
if let Some(t) = types? {
|
||||
Ok(Some(ParametricType(TUPLE_TYPE, t).into()))
|
||||
} else {
|
||||
Err("tuple elements must have some type".into())
|
||||
}
|
||||
}
|
||||
|
||||
fn infer_attribute<'a>(
|
||||
ctx: &mut InferenceContext<'a>,
|
||||
value: &'a Expression,
|
||||
name: &str,
|
||||
) -> ParserResult {
|
||||
let value = infer_expr(ctx, value)?.ok_or_else(|| "no value".to_string())?;
|
||||
if let TypeVariable(id) = value.as_ref() {
|
||||
let v = ctx.get_variable_def(*id);
|
||||
if v.bound.is_empty() {
|
||||
return Err("no fields on unbounded type variable".into());
|
||||
}
|
||||
let ty = v.bound[0].get_base(ctx).and_then(|v| v.fields.get(name));
|
||||
if ty.is_none() {
|
||||
return Err("unknown field".into());
|
||||
}
|
||||
for x in v.bound[1..].iter() {
|
||||
let ty1 = x.get_base(ctx).and_then(|v| v.fields.get(name));
|
||||
if ty1 != ty {
|
||||
return Err("unknown field (type mismatch between variants)".into());
|
||||
}
|
||||
}
|
||||
return Ok(Some(ty.unwrap().clone()));
|
||||
}
|
||||
|
||||
match value.get_base(ctx) {
|
||||
Some(b) => match b.fields.get(name) {
|
||||
Some(t) => Ok(Some(t.clone())),
|
||||
None => Err("no such field".into()),
|
||||
},
|
||||
None => Err("this object has no fields".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn infer_bool_ops<'a>(ctx: &mut InferenceContext<'a>, values: &'a [Expression]) -> ParserResult {
|
||||
assert_eq!(values.len(), 2);
|
||||
let left = infer_expr(ctx, &values[0])?.ok_or_else(|| "no value".to_string())?;
|
||||
let right = infer_expr(ctx, &values[1])?.ok_or_else(|| "no value".to_string())?;
|
||||
|
||||
let b = ctx.get_primitive(BOOL_TYPE);
|
||||
if left == b && right == b {
|
||||
Ok(Some(b))
|
||||
} else {
|
||||
Err("bool operands must be bool".into())
|
||||
}
|
||||
}
|
||||
|
||||
fn infer_bin_ops<'b: 'a, 'a>(
|
||||
ctx: &mut InferenceContext<'a>,
|
||||
op: &Operator,
|
||||
left: &'b Expression,
|
||||
right: &'b Expression,
|
||||
) -> ParserResult {
|
||||
let left = infer_expr(ctx, left)?.ok_or_else(|| "no value".to_string())?;
|
||||
let right = infer_expr(ctx, right)?.ok_or_else(|| "no value".to_string())?;
|
||||
let fun = binop_name(op);
|
||||
resolve_call(ctx, Some(left), fun, &[right])
|
||||
}
|
||||
|
||||
fn infer_unary_ops<'b: 'a, 'a>(
|
||||
ctx: &mut InferenceContext<'a>,
|
||||
op: &UnaryOperator,
|
||||
obj: &'b Expression,
|
||||
) -> ParserResult {
|
||||
let ty = infer_expr(ctx, obj)?.ok_or_else(|| "no value".to_string())?;
|
||||
if let UnaryOperator::Not = op {
|
||||
if ty == ctx.get_primitive(BOOL_TYPE) {
|
||||
Ok(Some(ty))
|
||||
} else {
|
||||
Err("logical not must be applied to bool".into())
|
||||
}
|
||||
} else {
|
||||
resolve_call(ctx, Some(ty), unaryop_name(op), &[])
|
||||
}
|
||||
}
|
||||
|
||||
fn infer_compare<'b: 'a, 'a>(
|
||||
ctx: &mut InferenceContext<'a>,
|
||||
vals: &'b [Expression],
|
||||
ops: &'b [Comparison],
|
||||
) -> ParserResult {
|
||||
let types: Result<Option<Vec<_>>, _> = vals.iter().map(|v| infer_expr(ctx, v)).collect();
|
||||
let types = types?;
|
||||
if types.is_none() {
|
||||
return Err("comparison operands must have type".into());
|
||||
}
|
||||
let types = types.unwrap();
|
||||
let boolean = ctx.get_primitive(BOOL_TYPE);
|
||||
let left = &types[..types.len() - 1];
|
||||
let right = &types[1..];
|
||||
|
||||
for ((a, b), op) in left.iter().zip(right.iter()).zip(ops.iter()) {
|
||||
let fun = comparison_name(op).ok_or_else(|| "unsupported comparison".to_string())?;
|
||||
let ty = resolve_call(ctx, Some(a.clone()), fun, &[b.clone()])?;
|
||||
if ty.is_none() || ty.unwrap() != boolean {
|
||||
return Err("comparison result must be boolean".into());
|
||||
}
|
||||
}
|
||||
Ok(Some(boolean))
|
||||
}
|
||||
|
||||
fn infer_call<'b: 'a, 'a>(
|
||||
ctx: &mut InferenceContext<'a>,
|
||||
args: &'b [Expression],
|
||||
function: &'b Expression,
|
||||
) -> ParserResult {
|
||||
let types: Result<Option<Vec<_>>, _> = args.iter().map(|v| infer_expr(ctx, v)).collect();
|
||||
let types = types?;
|
||||
if types.is_none() {
|
||||
return Err("function params must have type".into());
|
||||
}
|
||||
|
||||
let (obj, fun) = match &function.node {
|
||||
ExpressionType::Identifier { name } => (None, name),
|
||||
ExpressionType::Attribute { value, name } => (
|
||||
Some(infer_expr(ctx, &value)?.ok_or_else(|| "no value".to_string())?),
|
||||
name,
|
||||
),
|
||||
_ => return Err("not supported".into()),
|
||||
};
|
||||
resolve_call(ctx, obj, fun.as_str(), &types.unwrap())
|
||||
}
|
||||
|
||||
fn infer_subscript<'b: 'a, 'a>(
|
||||
ctx: &mut InferenceContext<'a>,
|
||||
a: &'b Expression,
|
||||
b: &'b Expression,
|
||||
) -> ParserResult {
|
||||
let a = infer_expr(ctx, a)?.ok_or_else(|| "no value".to_string())?;
|
||||
let t = if let ParametricType(LIST_TYPE, ls) = a.as_ref() {
|
||||
ls[0].clone()
|
||||
} else {
|
||||
return Err("subscript is not supported for types other than list".into());
|
||||
};
|
||||
|
||||
match &b.node {
|
||||
ExpressionType::Slice { elements } => {
|
||||
let int32 = ctx.get_primitive(INT32_TYPE);
|
||||
let types: Result<Option<Vec<_>>, _> = elements
|
||||
.iter()
|
||||
.map(|v| {
|
||||
if let ExpressionType::None = v.node {
|
||||
Ok(Some(int32.clone()))
|
||||
} else {
|
||||
infer_expr(ctx, v)
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let types = types?.ok_or_else(|| "slice must have type".to_string())?;
|
||||
if types.iter().all(|v| v == &int32) {
|
||||
Ok(Some(a))
|
||||
} else {
|
||||
Err("slice must be int32 type".into())
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let b = infer_expr(ctx, b)?.ok_or_else(|| "no value".to_string())?;
|
||||
if b == ctx.get_primitive(INT32_TYPE) {
|
||||
Ok(Some(t))
|
||||
} else {
|
||||
Err("index must be either slice or int32".into())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn infer_if_expr<'b: 'a, 'a>(
|
||||
ctx: &mut InferenceContext<'a>,
|
||||
test: &'b Expression,
|
||||
body: &'b Expression,
|
||||
orelse: &'b Expression,
|
||||
) -> ParserResult {
|
||||
let test = infer_expr(ctx, test)?.ok_or_else(|| "no value".to_string())?;
|
||||
if test != ctx.get_primitive(BOOL_TYPE) {
|
||||
return Err("test should be bool".into());
|
||||
}
|
||||
|
||||
let body = infer_expr(ctx, body)?;
|
||||
let orelse = infer_expr(ctx, orelse)?;
|
||||
if body.as_ref() == orelse.as_ref() {
|
||||
Ok(body)
|
||||
} else {
|
||||
Err("divergent type".into())
|
||||
}
|
||||
}
|
||||
|
||||
fn infer_simple_binding<'a: 'b, 'b>(
|
||||
ctx: &mut InferenceContext<'b>,
|
||||
name: &'a Expression,
|
||||
ty: Type,
|
||||
) -> Result<(), String> {
|
||||
match &name.node {
|
||||
ExpressionType::Identifier { name } => {
|
||||
if name == "_" {
|
||||
Ok(())
|
||||
} else if ctx.defined(name.as_str()) {
|
||||
Err("duplicated naming".into())
|
||||
} else {
|
||||
ctx.assign(name.as_str(), ty)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
ExpressionType::Tuple { elements } => {
|
||||
if let ParametricType(TUPLE_TYPE, ls) = ty.as_ref() {
|
||||
if elements.len() == ls.len() {
|
||||
for (a, b) in elements.iter().zip(ls.iter()) {
|
||||
infer_simple_binding(ctx, a, b.clone())?;
|
||||
}
|
||||
Ok(())
|
||||
} else {
|
||||
Err("different length".into())
|
||||
}
|
||||
} else {
|
||||
Err("not supported".into())
|
||||
}
|
||||
}
|
||||
_ => Err("not supported".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn infer_list_comprehension<'b: 'a, 'a>(
|
||||
ctx: &mut InferenceContext<'a>,
|
||||
element: &'b Expression,
|
||||
comprehension: &'b Comprehension,
|
||||
) -> ParserResult {
|
||||
if comprehension.is_async {
|
||||
return Err("async is not supported".into());
|
||||
}
|
||||
|
||||
let iter = infer_expr(ctx, &comprehension.iter)?.ok_or_else(|| "no value".to_string())?;
|
||||
if let ParametricType(LIST_TYPE, ls) = iter.as_ref() {
|
||||
ctx.with_scope(|ctx| {
|
||||
infer_simple_binding(ctx, &comprehension.target, ls[0].clone())?;
|
||||
|
||||
let boolean = ctx.get_primitive(BOOL_TYPE);
|
||||
for test in comprehension.ifs.iter() {
|
||||
let result =
|
||||
infer_expr(ctx, test)?.ok_or_else(|| "no value in test".to_string())?;
|
||||
if result != boolean {
|
||||
return Err("test must be bool".into());
|
||||
}
|
||||
}
|
||||
let result = infer_expr(ctx, element)?.ok_or_else(|| "no value")?;
|
||||
Ok(Some(ParametricType(LIST_TYPE, vec![result]).into()))
|
||||
})
|
||||
.1
|
||||
} else {
|
||||
Err("iteration is supported for list only".into())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use crate::context::*;
|
||||
use crate::typedef::*;
|
||||
use rustpython_parser::parser::parse_expression;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
fn get_inference_context(ctx: TopLevelContext) -> InferenceContext {
|
||||
InferenceContext::new(ctx, Box::new(|_| Err("unbounded identifier".into())))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_constants() {
|
||||
let ctx = basic_ctx();
|
||||
let mut ctx = get_inference_context(ctx);
|
||||
|
||||
let ast = parse_expression("123").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(INT32_TYPE));
|
||||
|
||||
let ast = parse_expression("2147483647").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(INT32_TYPE));
|
||||
|
||||
let ast = parse_expression("2147483648").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(INT64_TYPE));
|
||||
|
||||
let ast = parse_expression("9223372036854775807").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(INT64_TYPE));
|
||||
|
||||
let ast = parse_expression("9223372036854775808").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("integer out of range".into()));
|
||||
|
||||
let ast = parse_expression("123.456").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(FLOAT_TYPE));
|
||||
|
||||
let ast = parse_expression("True").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(BOOL_TYPE));
|
||||
|
||||
let ast = parse_expression("False").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(BOOL_TYPE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_identifier() {
|
||||
let ctx = basic_ctx();
|
||||
let mut ctx = get_inference_context(ctx);
|
||||
ctx.assign("abc", ctx.get_primitive(INT32_TYPE)).unwrap();
|
||||
|
||||
let ast = parse_expression("abc").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(INT32_TYPE));
|
||||
|
||||
let ast = parse_expression("ab").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("unbounded identifier".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_list() {
|
||||
let mut ctx = basic_ctx();
|
||||
ctx.add_fn(
|
||||
"foo",
|
||||
FnDef {
|
||||
args: vec![],
|
||||
result: None,
|
||||
},
|
||||
);
|
||||
let mut ctx = get_inference_context(ctx);
|
||||
ctx.assign("abc", ctx.get_primitive(INT32_TYPE)).unwrap();
|
||||
// def is reserved...
|
||||
ctx.assign("efg", ctx.get_primitive(INT32_TYPE)).unwrap();
|
||||
ctx.assign("xyz", ctx.get_primitive(FLOAT_TYPE)).unwrap();
|
||||
|
||||
let ast = parse_expression("[]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(
|
||||
result.unwrap().unwrap(),
|
||||
ParametricType(LIST_TYPE, vec![BotType.into()]).into()
|
||||
);
|
||||
|
||||
let ast = parse_expression("[abc]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(
|
||||
result.unwrap().unwrap(),
|
||||
ParametricType(LIST_TYPE, vec![ctx.get_primitive(INT32_TYPE)]).into()
|
||||
);
|
||||
|
||||
let ast = parse_expression("[abc, efg]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(
|
||||
result.unwrap().unwrap(),
|
||||
ParametricType(LIST_TYPE, vec![ctx.get_primitive(INT32_TYPE)]).into()
|
||||
);
|
||||
|
||||
let ast = parse_expression("[abc, efg, xyz]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("inhomogeneous list is not allowed".into()));
|
||||
|
||||
let ast = parse_expression("[foo()]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("list elements must have some type".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tuple() {
|
||||
let mut ctx = basic_ctx();
|
||||
ctx.add_fn(
|
||||
"foo",
|
||||
FnDef {
|
||||
args: vec![],
|
||||
result: None,
|
||||
},
|
||||
);
|
||||
let mut ctx = get_inference_context(ctx);
|
||||
ctx.assign("abc", ctx.get_primitive(INT32_TYPE)).unwrap();
|
||||
ctx.assign("efg", ctx.get_primitive(FLOAT_TYPE)).unwrap();
|
||||
|
||||
let ast = parse_expression("(abc, efg)").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(
|
||||
result.unwrap().unwrap(),
|
||||
ParametricType(
|
||||
TUPLE_TYPE,
|
||||
vec![ctx.get_primitive(INT32_TYPE), ctx.get_primitive(FLOAT_TYPE)]
|
||||
)
|
||||
.into()
|
||||
);
|
||||
|
||||
let ast = parse_expression("(abc, efg, foo())").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("tuple elements must have some type".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_attribute() {
|
||||
let mut ctx = basic_ctx();
|
||||
ctx.add_fn(
|
||||
"none",
|
||||
FnDef {
|
||||
args: vec![],
|
||||
result: None,
|
||||
},
|
||||
);
|
||||
let int32 = ctx.get_primitive(INT32_TYPE);
|
||||
let float = ctx.get_primitive(FLOAT_TYPE);
|
||||
|
||||
let foo = ctx.add_class(ClassDef {
|
||||
base: TypeDef {
|
||||
name: "Foo",
|
||||
fields: HashMap::new(),
|
||||
methods: HashMap::new(),
|
||||
},
|
||||
parents: vec![],
|
||||
});
|
||||
let foo_def = ctx.get_class_def_mut(foo);
|
||||
foo_def.base.fields.insert("a", int32.clone());
|
||||
foo_def.base.fields.insert("b", ClassType(foo).into());
|
||||
foo_def.base.fields.insert("c", int32.clone());
|
||||
|
||||
let bar = ctx.add_class(ClassDef {
|
||||
base: TypeDef {
|
||||
name: "Bar",
|
||||
fields: HashMap::new(),
|
||||
methods: HashMap::new(),
|
||||
},
|
||||
parents: vec![],
|
||||
});
|
||||
let bar_def = ctx.get_class_def_mut(bar);
|
||||
bar_def.base.fields.insert("a", int32);
|
||||
bar_def.base.fields.insert("b", ClassType(bar).into());
|
||||
bar_def.base.fields.insert("c", float);
|
||||
|
||||
let v0 = ctx.add_variable(VarDef {
|
||||
name: "v0",
|
||||
bound: vec![],
|
||||
});
|
||||
|
||||
let v1 = ctx.add_variable(VarDef {
|
||||
name: "v1",
|
||||
bound: vec![ClassType(foo).into(), ClassType(bar).into()],
|
||||
});
|
||||
|
||||
let mut ctx = get_inference_context(ctx);
|
||||
ctx.assign("foo", Rc::new(ClassType(foo))).unwrap();
|
||||
ctx.assign("bar", Rc::new(ClassType(bar))).unwrap();
|
||||
ctx.assign("foobar", Rc::new(VirtualClassType(foo)))
|
||||
.unwrap();
|
||||
ctx.assign("v0", ctx.get_variable(v0)).unwrap();
|
||||
ctx.assign("v1", ctx.get_variable(v1)).unwrap();
|
||||
ctx.assign("bot", Rc::new(BotType)).unwrap();
|
||||
|
||||
let ast = parse_expression("foo.a").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(INT32_TYPE));
|
||||
|
||||
let ast = parse_expression("foo.d").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("no such field".into()));
|
||||
|
||||
let ast = parse_expression("foobar.a").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(INT32_TYPE));
|
||||
|
||||
let ast = parse_expression("v0.a").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("no fields on unbounded type variable".into()));
|
||||
|
||||
let ast = parse_expression("v1.a").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(INT32_TYPE));
|
||||
|
||||
// shall we support this?
|
||||
let ast = parse_expression("v1.b").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(
|
||||
result,
|
||||
Err("unknown field (type mismatch between variants)".into())
|
||||
);
|
||||
// assert_eq!(result.unwrap().unwrap(), TypeVariable(v1).into());
|
||||
|
||||
let ast = parse_expression("v1.c").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(
|
||||
result,
|
||||
Err("unknown field (type mismatch between variants)".into())
|
||||
);
|
||||
|
||||
let ast = parse_expression("v1.d").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("unknown field".into()));
|
||||
|
||||
let ast = parse_expression("none().a").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("no value".into()));
|
||||
|
||||
let ast = parse_expression("bot.a").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("this object has no fields".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ops() {
|
||||
let mut ctx = basic_ctx();
|
||||
ctx.add_fn(
|
||||
"none",
|
||||
FnDef {
|
||||
args: vec![],
|
||||
result: None,
|
||||
},
|
||||
);
|
||||
let mut ctx = get_inference_context(ctx);
|
||||
|
||||
let ast = parse_expression("True and False").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(BOOL_TYPE));
|
||||
|
||||
let ast = parse_expression("True and none()").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("no value".into()));
|
||||
|
||||
let ast = parse_expression("True and 123").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("bool operands must be bool".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bin_ops() {
|
||||
let mut ctx = basic_ctx();
|
||||
let v0 = ctx.add_variable(VarDef {
|
||||
name: "v0",
|
||||
bound: vec![ctx.get_primitive(INT32_TYPE), ctx.get_primitive(INT64_TYPE)],
|
||||
});
|
||||
let mut ctx = get_inference_context(ctx);
|
||||
ctx.assign("a", TypeVariable(v0).into()).unwrap();
|
||||
|
||||
let ast = parse_expression("1 + 2 + 3").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(INT32_TYPE));
|
||||
|
||||
let ast = parse_expression("a + a + a").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), TypeVariable(v0).into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_unary_ops() {
|
||||
let mut ctx = basic_ctx();
|
||||
let v0 = ctx.add_variable(VarDef {
|
||||
name: "v0",
|
||||
bound: vec![ctx.get_primitive(INT32_TYPE), ctx.get_primitive(INT64_TYPE)],
|
||||
});
|
||||
let mut ctx = get_inference_context(ctx);
|
||||
ctx.assign("a", TypeVariable(v0).into()).unwrap();
|
||||
|
||||
let ast = parse_expression("-(123)").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(INT32_TYPE));
|
||||
|
||||
let ast = parse_expression("-a").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), TypeVariable(v0).into());
|
||||
|
||||
let ast = parse_expression("not True").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(BOOL_TYPE));
|
||||
|
||||
let ast = parse_expression("not (1)").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("logical not must be applied to bool".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_compare() {
|
||||
let mut ctx = basic_ctx();
|
||||
let v0 = ctx.add_variable(VarDef {
|
||||
name: "v0",
|
||||
bound: vec![ctx.get_primitive(INT32_TYPE), ctx.get_primitive(INT64_TYPE)],
|
||||
});
|
||||
let mut ctx = get_inference_context(ctx);
|
||||
ctx.assign("a", TypeVariable(v0).into()).unwrap();
|
||||
|
||||
let ast = parse_expression("a == a == a").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(BOOL_TYPE));
|
||||
|
||||
let ast = parse_expression("a == a == 1").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("not equal".into()));
|
||||
|
||||
let ast = parse_expression("True > False").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("no such function".into()));
|
||||
|
||||
let ast = parse_expression("True in False").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("unsupported comparison".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_call() {
|
||||
let mut ctx = basic_ctx();
|
||||
ctx.add_fn(
|
||||
"none",
|
||||
FnDef {
|
||||
args: vec![],
|
||||
result: None,
|
||||
},
|
||||
);
|
||||
|
||||
let foo = ctx.add_class(ClassDef {
|
||||
base: TypeDef {
|
||||
name: "Foo",
|
||||
fields: HashMap::new(),
|
||||
methods: HashMap::new(),
|
||||
},
|
||||
parents: vec![],
|
||||
});
|
||||
let foo_def = ctx.get_class_def_mut(foo);
|
||||
foo_def.base.methods.insert(
|
||||
"a",
|
||||
FnDef {
|
||||
args: vec![],
|
||||
result: Some(Rc::new(ClassType(foo))),
|
||||
},
|
||||
);
|
||||
|
||||
let bar = ctx.add_class(ClassDef {
|
||||
base: TypeDef {
|
||||
name: "Bar",
|
||||
fields: HashMap::new(),
|
||||
methods: HashMap::new(),
|
||||
},
|
||||
parents: vec![],
|
||||
});
|
||||
let bar_def = ctx.get_class_def_mut(bar);
|
||||
bar_def.base.methods.insert(
|
||||
"a",
|
||||
FnDef {
|
||||
args: vec![],
|
||||
result: Some(Rc::new(ClassType(bar))),
|
||||
},
|
||||
);
|
||||
|
||||
let v0 = ctx.add_variable(VarDef {
|
||||
name: "v0",
|
||||
bound: vec![],
|
||||
});
|
||||
let v1 = ctx.add_variable(VarDef {
|
||||
name: "v1",
|
||||
bound: vec![ClassType(foo).into(), ClassType(bar).into()],
|
||||
});
|
||||
let v2 = ctx.add_variable(VarDef {
|
||||
name: "v2",
|
||||
bound: vec![
|
||||
ClassType(foo).into(),
|
||||
ClassType(bar).into(),
|
||||
ctx.get_primitive(INT32_TYPE),
|
||||
],
|
||||
});
|
||||
let mut ctx = get_inference_context(ctx);
|
||||
ctx.assign("foo", Rc::new(ClassType(foo))).unwrap();
|
||||
ctx.assign("bar", Rc::new(ClassType(bar))).unwrap();
|
||||
ctx.assign("foobar", Rc::new(VirtualClassType(foo)))
|
||||
.unwrap();
|
||||
ctx.assign("v0", ctx.get_variable(v0)).unwrap();
|
||||
ctx.assign("v1", ctx.get_variable(v1)).unwrap();
|
||||
ctx.assign("v2", ctx.get_variable(v2)).unwrap();
|
||||
ctx.assign("bot", Rc::new(BotType)).unwrap();
|
||||
|
||||
let ast = parse_expression("foo.a()").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ClassType(foo).into());
|
||||
|
||||
let ast = parse_expression("v1.a()").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), TypeVariable(v1).into());
|
||||
|
||||
let ast = parse_expression("foobar.a()").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ClassType(foo).into());
|
||||
|
||||
let ast = parse_expression("none().a()").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("no value".into()));
|
||||
|
||||
let ast = parse_expression("bot.a()").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("not supported".into()));
|
||||
|
||||
let ast = parse_expression("[][0].a()").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("not supported".into()));
|
||||
|
||||
let ast = parse_expression("v0.a()").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("unbounded type var".into()));
|
||||
|
||||
let ast = parse_expression("v2.a()").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("no such function".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn infer_subscript() {
|
||||
let mut ctx = basic_ctx();
|
||||
ctx.add_fn(
|
||||
"none",
|
||||
FnDef {
|
||||
args: vec![],
|
||||
result: None,
|
||||
},
|
||||
);
|
||||
let mut ctx = get_inference_context(ctx);
|
||||
|
||||
let ast = parse_expression("[1, 2, 3][0]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(INT32_TYPE));
|
||||
|
||||
let ast = parse_expression("[[1]][0][0]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(INT32_TYPE));
|
||||
|
||||
let ast = parse_expression("[1, 2, 3][1:2]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(
|
||||
result.unwrap().unwrap(),
|
||||
ParametricType(LIST_TYPE, vec![ctx.get_primitive(INT32_TYPE)]).into()
|
||||
);
|
||||
|
||||
let ast = parse_expression("[1, 2, 3][1:2:2]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(
|
||||
result.unwrap().unwrap(),
|
||||
ParametricType(LIST_TYPE, vec![ctx.get_primitive(INT32_TYPE)]).into()
|
||||
);
|
||||
|
||||
let ast = parse_expression("[1, 2, 3][1:1.2]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("slice must be int32 type".into()));
|
||||
|
||||
let ast = parse_expression("[1, 2, 3][1:none()]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("slice must have type".into()));
|
||||
|
||||
let ast = parse_expression("[1, 2, 3][1.2]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("index must be either slice or int32".into()));
|
||||
|
||||
let ast = parse_expression("[1, 2, 3][none()]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("no value".into()));
|
||||
|
||||
let ast = parse_expression("none()[1.2]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("no value".into()));
|
||||
|
||||
let ast = parse_expression("123[1]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(
|
||||
result,
|
||||
Err("subscript is not supported for types other than list".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_if_expr() {
|
||||
let mut ctx = basic_ctx();
|
||||
ctx.add_fn(
|
||||
"none",
|
||||
FnDef {
|
||||
args: vec![],
|
||||
result: None,
|
||||
},
|
||||
);
|
||||
let mut ctx = get_inference_context(ctx);
|
||||
|
||||
let ast = parse_expression("1 if True else 0").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), ctx.get_primitive(INT32_TYPE));
|
||||
|
||||
let ast = parse_expression("none() if True else none()").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap(), None);
|
||||
|
||||
let ast = parse_expression("none() if 1 else none()").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("test should be bool".into()));
|
||||
|
||||
let ast = parse_expression("1 if True else none()").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("divergent type".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_list_comp() {
|
||||
let mut ctx = basic_ctx();
|
||||
ctx.add_fn(
|
||||
"none",
|
||||
FnDef {
|
||||
args: vec![],
|
||||
result: None,
|
||||
},
|
||||
);
|
||||
let int32 = ctx.get_primitive(INT32_TYPE);
|
||||
let mut ctx = get_inference_context(ctx);
|
||||
ctx.assign("z", int32.clone()).unwrap();
|
||||
|
||||
let ast = parse_expression("[x for x in [(1, 2), (2, 3), (3, 4)]][0]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(
|
||||
result.unwrap().unwrap(),
|
||||
ParametricType(TUPLE_TYPE, vec![int32.clone(), int32.clone()]).into()
|
||||
);
|
||||
|
||||
let ast = parse_expression("[x for (x, y) in [(1, 2), (2, 3), (3, 4)]][0]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), int32);
|
||||
|
||||
let ast =
|
||||
parse_expression("[x for (x, y) in [(1, 2), (2, 3), (3, 4)] if x > 0][0]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result.unwrap().unwrap(), int32);
|
||||
|
||||
let ast = parse_expression("[x for (x, y) in [(1, 2), (2, 3), (3, 4)] if x][0]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("test must be bool".into()));
|
||||
|
||||
let ast = parse_expression("[y for x in []][0]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("unbounded identifier".into()));
|
||||
|
||||
let ast = parse_expression("[none() for x in []][0]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("no value".into()));
|
||||
|
||||
let ast = parse_expression("[z for z in []][0]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(result, Err("duplicated naming".into()));
|
||||
|
||||
let ast = parse_expression("[x for x in [] for y in []]").unwrap();
|
||||
let result = infer_expr(&mut ctx, &ast);
|
||||
assert_eq!(
|
||||
result,
|
||||
Err("only 1 generator statement is supported".into())
|
||||
);
|
||||
}
|
||||
}
|
601
nac3core/src/inference_core.rs
Normal file
601
nac3core/src/inference_core.rs
Normal file
@ -0,0 +1,601 @@
|
||||
use crate::context::InferenceContext;
|
||||
use crate::typedef::{TypeEnum::*, *};
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn find_subst(
|
||||
ctx: &InferenceContext,
|
||||
valuation: &Option<(VariableId, Type)>,
|
||||
sub: &mut HashMap<VariableId, Type>,
|
||||
mut a: Type,
|
||||
mut b: Type,
|
||||
) -> Result<(), String> {
|
||||
// TODO: fix error messages later
|
||||
if let TypeVariable(id) = a.as_ref() {
|
||||
if let Some((assumption_id, t)) = valuation {
|
||||
if assumption_id == id {
|
||||
a = t.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut substituted = false;
|
||||
if let TypeVariable(id) = b.as_ref() {
|
||||
if let Some(c) = sub.get(&id) {
|
||||
b = c.clone();
|
||||
substituted = true;
|
||||
}
|
||||
}
|
||||
|
||||
match (a.as_ref(), b.as_ref()) {
|
||||
(BotType, _) => Ok(()),
|
||||
(TypeVariable(id_a), TypeVariable(id_b)) => {
|
||||
if substituted {
|
||||
return if id_a == id_b {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("different variables".to_string())
|
||||
};
|
||||
}
|
||||
let v_a = ctx.get_variable_def(*id_a);
|
||||
let v_b = ctx.get_variable_def(*id_b);
|
||||
if !v_b.bound.is_empty() {
|
||||
if v_a.bound.is_empty() {
|
||||
return Err("unbounded a".to_string());
|
||||
} else {
|
||||
let diff: Vec<_> = v_a
|
||||
.bound
|
||||
.iter()
|
||||
.filter(|x| !v_b.bound.contains(x))
|
||||
.collect();
|
||||
if !diff.is_empty() {
|
||||
return Err("different domain".to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
sub.insert(*id_b, a.clone());
|
||||
Ok(())
|
||||
}
|
||||
(TypeVariable(id_a), _) => {
|
||||
let v_a = ctx.get_variable_def(*id_a);
|
||||
if v_a.bound.len() == 1 && v_a.bound[0].as_ref() == b.as_ref() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("different domain".to_string())
|
||||
}
|
||||
}
|
||||
(_, TypeVariable(id_b)) => {
|
||||
let v_b = ctx.get_variable_def(*id_b);
|
||||
if v_b.bound.is_empty() || v_b.bound.contains(&a) {
|
||||
sub.insert(*id_b, a.clone());
|
||||
Ok(())
|
||||
} else {
|
||||
Err("different domain".to_string())
|
||||
}
|
||||
}
|
||||
(_, VirtualClassType(id_b)) => {
|
||||
let mut parents;
|
||||
match a.as_ref() {
|
||||
ClassType(id_a) => {
|
||||
parents = [*id_a].to_vec();
|
||||
}
|
||||
VirtualClassType(id_a) => {
|
||||
parents = [*id_a].to_vec();
|
||||
}
|
||||
_ => {
|
||||
return Err("cannot substitute non-class type into virtual class".to_string());
|
||||
}
|
||||
};
|
||||
while !parents.is_empty() {
|
||||
if *id_b == parents[0] {
|
||||
return Ok(());
|
||||
}
|
||||
let c = ctx.get_class_def(parents.remove(0));
|
||||
parents.extend_from_slice(&c.parents);
|
||||
}
|
||||
Err("not subtype".to_string())
|
||||
}
|
||||
(ParametricType(id_a, param_a), ParametricType(id_b, param_b)) => {
|
||||
if id_a != id_b || param_a.len() != param_b.len() {
|
||||
Err("different parametric types".to_string())
|
||||
} else {
|
||||
for (x, y) in param_a.iter().zip(param_b.iter()) {
|
||||
find_subst(ctx, valuation, sub, x.clone(), y.clone())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
(_, _) => {
|
||||
if a == b {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("not equal".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_call_rec(
|
||||
ctx: &InferenceContext,
|
||||
valuation: &Option<(VariableId, Type)>,
|
||||
obj: Option<Type>,
|
||||
func: &str,
|
||||
args: &[Type],
|
||||
) -> Result<Option<Type>, String> {
|
||||
let mut subst = obj
|
||||
.as_ref()
|
||||
.map(|v| v.get_subst(ctx))
|
||||
.unwrap_or_else(HashMap::new);
|
||||
|
||||
let fun = match &obj {
|
||||
Some(obj) => {
|
||||
let base = match obj.as_ref() {
|
||||
TypeVariable(id) => {
|
||||
let v = ctx.get_variable_def(*id);
|
||||
if v.bound.is_empty() {
|
||||
return Err("unbounded type var".to_string());
|
||||
}
|
||||
let results: Result<Vec<_>, String> = v
|
||||
.bound
|
||||
.iter()
|
||||
.map(|ins| {
|
||||
resolve_call_rec(
|
||||
ctx,
|
||||
&Some((*id, ins.clone())),
|
||||
Some(ins.clone()),
|
||||
func,
|
||||
args.clone(),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let results = results?;
|
||||
if results.iter().all(|v| v == &results[0]) {
|
||||
return Ok(results[0].clone());
|
||||
}
|
||||
let mut results = results.iter().zip(v.bound.iter()).map(|(r, ins)| {
|
||||
r.as_ref()
|
||||
.map(|v| v.inv_subst(&[(ins.clone(), obj.clone())]))
|
||||
});
|
||||
let first = results.next().unwrap();
|
||||
if results.all(|v| v == first) {
|
||||
return Ok(first);
|
||||
} else {
|
||||
return Err("divergent type after substitution".to_string());
|
||||
}
|
||||
}
|
||||
PrimitiveType(id) => &ctx.get_primitive_def(*id),
|
||||
ClassType(id) | VirtualClassType(id) => &ctx.get_class_def(*id).base,
|
||||
ParametricType(id, _) => &ctx.get_parametric_def(*id).base,
|
||||
_ => return Err("not supported".to_string()),
|
||||
};
|
||||
base.methods.get(func)
|
||||
}
|
||||
None => ctx.get_fn_def(func),
|
||||
}
|
||||
.ok_or_else(|| "no such function".to_string())?;
|
||||
|
||||
if args.len() != fun.args.len() {
|
||||
return Err("incorrect parameter number".to_string());
|
||||
}
|
||||
for (a, b) in args.iter().zip(fun.args.iter()) {
|
||||
find_subst(ctx, valuation, &mut subst, a.clone(), b.clone())?;
|
||||
}
|
||||
let result = fun.result.as_ref().map(|v| v.subst(&subst));
|
||||
Ok(result.map(|result| {
|
||||
if let SelfType = result {
|
||||
obj.unwrap()
|
||||
} else {
|
||||
result.into()
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn resolve_call(
|
||||
ctx: &InferenceContext,
|
||||
obj: Option<Type>,
|
||||
func: &str,
|
||||
args: &[Type],
|
||||
) -> Result<Option<Type>, String> {
|
||||
resolve_call_rec(ctx, &None, obj, func, args)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::context::TopLevelContext;
|
||||
use crate::primitives::*;
|
||||
use std::rc::Rc;
|
||||
|
||||
fn get_inference_context(ctx: TopLevelContext) -> InferenceContext {
|
||||
InferenceContext::new(ctx, Box::new(|_| Err("unbounded identifier".into())))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple_generic() {
|
||||
let mut ctx = basic_ctx();
|
||||
let v1 = ctx.add_variable(VarDef {
|
||||
name: "V1",
|
||||
bound: vec![ctx.get_primitive(INT32_TYPE), ctx.get_primitive(FLOAT_TYPE)],
|
||||
});
|
||||
let v1 = ctx.get_variable(v1);
|
||||
let v2 = ctx.add_variable(VarDef {
|
||||
name: "V2",
|
||||
bound: vec![
|
||||
ctx.get_primitive(BOOL_TYPE),
|
||||
ctx.get_primitive(INT32_TYPE),
|
||||
ctx.get_primitive(FLOAT_TYPE),
|
||||
],
|
||||
});
|
||||
let v2 = ctx.get_variable(v2);
|
||||
let ctx = get_inference_context(ctx);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "int32", &[ctx.get_primitive(FLOAT_TYPE)]),
|
||||
Ok(Some(ctx.get_primitive(INT32_TYPE)))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "int32", &[ctx.get_primitive(INT32_TYPE)],),
|
||||
Ok(Some(ctx.get_primitive(INT32_TYPE)))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "float", &[ctx.get_primitive(INT32_TYPE)]),
|
||||
Ok(Some(ctx.get_primitive(FLOAT_TYPE)))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "float", &[ctx.get_primitive(BOOL_TYPE)]),
|
||||
Err("different domain".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "float", &[]),
|
||||
Err("incorrect parameter number".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "float", &[v1]),
|
||||
Ok(Some(ctx.get_primitive(FLOAT_TYPE)))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "float", &[v2]),
|
||||
Err("different domain".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_methods() {
|
||||
let mut ctx = basic_ctx();
|
||||
|
||||
let v0 = ctx.add_variable(VarDef {
|
||||
name: "V0",
|
||||
bound: vec![],
|
||||
});
|
||||
let v0 = ctx.get_variable(v0);
|
||||
let v1 = ctx.add_variable(VarDef {
|
||||
name: "V1",
|
||||
bound: vec![ctx.get_primitive(INT32_TYPE), ctx.get_primitive(FLOAT_TYPE)],
|
||||
});
|
||||
let v1 = ctx.get_variable(v1);
|
||||
let v2 = ctx.add_variable(VarDef {
|
||||
name: "V2",
|
||||
bound: vec![ctx.get_primitive(INT32_TYPE), ctx.get_primitive(FLOAT_TYPE)],
|
||||
});
|
||||
let v2 = ctx.get_variable(v2);
|
||||
let v3 = ctx.add_variable(VarDef {
|
||||
name: "V3",
|
||||
bound: vec![
|
||||
ctx.get_primitive(BOOL_TYPE),
|
||||
ctx.get_primitive(INT32_TYPE),
|
||||
ctx.get_primitive(FLOAT_TYPE),
|
||||
],
|
||||
});
|
||||
let v3 = ctx.get_variable(v3);
|
||||
|
||||
let int32 = ctx.get_primitive(INT32_TYPE);
|
||||
let int64 = ctx.get_primitive(INT64_TYPE);
|
||||
let ctx = get_inference_context(ctx);
|
||||
|
||||
// simple cases
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(int32.clone()), "__add__", &[int32.clone()]),
|
||||
Ok(Some(int32.clone()))
|
||||
);
|
||||
|
||||
assert_ne!(
|
||||
resolve_call(&ctx, Some(int32.clone()), "__add__", &[int32.clone()]),
|
||||
Ok(Some(int64.clone()))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(int32), "__add__", &[int64]),
|
||||
Err("not equal".to_string())
|
||||
);
|
||||
|
||||
// with type variables
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v1.clone()]),
|
||||
Ok(Some(v1.clone()))
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(v0.clone()), "__add__", &[v2.clone()]),
|
||||
Err("unbounded type var".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v0]),
|
||||
Err("different domain".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v2]),
|
||||
Err("different domain".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v3.clone()]),
|
||||
Err("different domain".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(v3.clone()), "__add__", &[v1]),
|
||||
Err("no such function".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(v3.clone()), "__add__", &[v3]),
|
||||
Err("no such function".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multi_generic() {
|
||||
let mut ctx = basic_ctx();
|
||||
let v0 = ctx.add_variable(VarDef {
|
||||
name: "V0",
|
||||
bound: vec![],
|
||||
});
|
||||
let v0 = ctx.get_variable(v0);
|
||||
let v1 = ctx.add_variable(VarDef {
|
||||
name: "V1",
|
||||
bound: vec![],
|
||||
});
|
||||
let v1 = ctx.get_variable(v1);
|
||||
let v2 = ctx.add_variable(VarDef {
|
||||
name: "V2",
|
||||
bound: vec![],
|
||||
});
|
||||
let v2 = ctx.get_variable(v2);
|
||||
let v3 = ctx.add_variable(VarDef {
|
||||
name: "V3",
|
||||
bound: vec![],
|
||||
});
|
||||
let v3 = ctx.get_variable(v3);
|
||||
|
||||
ctx.add_fn(
|
||||
"foo",
|
||||
FnDef {
|
||||
args: vec![v0.clone(), v0.clone(), v1.clone()],
|
||||
result: Some(v0.clone()),
|
||||
},
|
||||
);
|
||||
|
||||
ctx.add_fn(
|
||||
"foo1",
|
||||
FnDef {
|
||||
args: vec![ParametricType(TUPLE_TYPE, vec![v0.clone(), v0.clone(), v1]).into()],
|
||||
result: Some(v0),
|
||||
},
|
||||
);
|
||||
let ctx = get_inference_context(ctx);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[v2.clone(), v2.clone(), v2.clone()]),
|
||||
Ok(Some(v2.clone()))
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[v2.clone(), v2.clone(), v3.clone()]),
|
||||
Ok(Some(v2.clone()))
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[v2.clone(), v3.clone(), v3.clone()]),
|
||||
Err("different variables".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(
|
||||
&ctx,
|
||||
None,
|
||||
"foo1",
|
||||
&[ParametricType(TUPLE_TYPE, vec![v2.clone(), v2.clone(), v2.clone()]).into()]
|
||||
),
|
||||
Ok(Some(v2.clone()))
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(
|
||||
&ctx,
|
||||
None,
|
||||
"foo1",
|
||||
&[ParametricType(TUPLE_TYPE, vec![v2.clone(), v2.clone(), v3.clone()]).into()]
|
||||
),
|
||||
Ok(Some(v2.clone()))
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(
|
||||
&ctx,
|
||||
None,
|
||||
"foo1",
|
||||
&[ParametricType(TUPLE_TYPE, vec![v2, v3.clone(), v3]).into()]
|
||||
),
|
||||
Err("different variables".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_class_generics() {
|
||||
let mut ctx = basic_ctx();
|
||||
|
||||
let list = ctx.get_parametric_def_mut(LIST_TYPE);
|
||||
let t = Rc::new(TypeVariable(list.params[0]));
|
||||
list.base.methods.insert(
|
||||
"head",
|
||||
FnDef {
|
||||
args: vec![],
|
||||
result: Some(t.clone()),
|
||||
},
|
||||
);
|
||||
list.base.methods.insert(
|
||||
"append",
|
||||
FnDef {
|
||||
args: vec![t],
|
||||
result: None,
|
||||
},
|
||||
);
|
||||
|
||||
let v0 = ctx.add_variable(VarDef {
|
||||
name: "V0",
|
||||
bound: vec![],
|
||||
});
|
||||
let v0 = ctx.get_variable(v0);
|
||||
let v1 = ctx.add_variable(VarDef {
|
||||
name: "V1",
|
||||
bound: vec![],
|
||||
});
|
||||
let v1 = ctx.get_variable(v1);
|
||||
let ctx = get_inference_context(ctx);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(
|
||||
&ctx,
|
||||
Some(ParametricType(LIST_TYPE, vec![v0.clone()]).into()),
|
||||
"head",
|
||||
&[]
|
||||
),
|
||||
Ok(Some(v0.clone()))
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(
|
||||
&ctx,
|
||||
Some(ParametricType(LIST_TYPE, vec![v0.clone()]).into()),
|
||||
"append",
|
||||
&[v0.clone()]
|
||||
),
|
||||
Ok(None)
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(
|
||||
&ctx,
|
||||
Some(ParametricType(LIST_TYPE, vec![v0]).into()),
|
||||
"append",
|
||||
&[v1]
|
||||
),
|
||||
Err("different variables".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_virtual_class() {
|
||||
let mut ctx = basic_ctx();
|
||||
|
||||
let foo = ctx.add_class(ClassDef {
|
||||
base: TypeDef {
|
||||
name: "Foo",
|
||||
methods: HashMap::new(),
|
||||
fields: HashMap::new(),
|
||||
},
|
||||
parents: vec![],
|
||||
});
|
||||
|
||||
let foo1 = ctx.add_class(ClassDef {
|
||||
base: TypeDef {
|
||||
name: "Foo1",
|
||||
methods: HashMap::new(),
|
||||
fields: HashMap::new(),
|
||||
},
|
||||
parents: vec![foo],
|
||||
});
|
||||
|
||||
let foo2 = ctx.add_class(ClassDef {
|
||||
base: TypeDef {
|
||||
name: "Foo2",
|
||||
methods: HashMap::new(),
|
||||
fields: HashMap::new(),
|
||||
},
|
||||
parents: vec![foo1],
|
||||
});
|
||||
|
||||
let bar = ctx.add_class(ClassDef {
|
||||
base: TypeDef {
|
||||
name: "bar",
|
||||
methods: HashMap::new(),
|
||||
fields: HashMap::new(),
|
||||
},
|
||||
parents: vec![],
|
||||
});
|
||||
|
||||
ctx.add_fn(
|
||||
"foo",
|
||||
FnDef {
|
||||
args: vec![VirtualClassType(foo).into()],
|
||||
result: None,
|
||||
},
|
||||
);
|
||||
ctx.add_fn(
|
||||
"foo1",
|
||||
FnDef {
|
||||
args: vec![VirtualClassType(foo1).into()],
|
||||
result: None,
|
||||
},
|
||||
);
|
||||
let ctx = get_inference_context(ctx);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[ClassType(foo).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[ClassType(foo1).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[ClassType(foo2).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[ClassType(bar).into()]),
|
||||
Err("not subtype".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo1", &[ClassType(foo1).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo1", &[ClassType(foo2).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo1", &[ClassType(foo).into()]),
|
||||
Err("not subtype".to_string())
|
||||
);
|
||||
|
||||
// virtual class substitution
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[VirtualClassType(foo).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[VirtualClassType(foo1).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[VirtualClassType(foo2).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[VirtualClassType(bar).into()]),
|
||||
Err("not subtype".to_string())
|
||||
);
|
||||
}
|
||||
}
|
@ -1,7 +1,17 @@
|
||||
#![warn(clippy::all)]
|
||||
#![allow(clippy::clone_double_ref)]
|
||||
|
||||
extern crate num_bigint;
|
||||
extern crate inkwell;
|
||||
extern crate rustpython_parser;
|
||||
|
||||
pub mod expression_inference;
|
||||
pub mod inference_core;
|
||||
mod magic_methods;
|
||||
pub mod primitives;
|
||||
pub mod typedef;
|
||||
pub mod context;
|
||||
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::path::Path;
|
||||
@ -224,7 +234,7 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
},
|
||||
ast::ExpressionType::Identifier { name } => {
|
||||
match self.namespace.get(name) {
|
||||
Some(value) => Ok(self.builder.build_load(*value, name).into()),
|
||||
Some(value) => Ok(self.builder.build_load(*value, name)),
|
||||
None => Err(self.compile_error(CompileErrorKind::UnboundIdentifier))
|
||||
}
|
||||
},
|
||||
@ -406,10 +416,10 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
_ => Err(self.compile_error(CompileErrorKind::Unsupported("unrecognized call")))
|
||||
}
|
||||
} else {
|
||||
return Err(self.compile_error(CompileErrorKind::Unsupported("function must be an identifier")))
|
||||
Err(self.compile_error(CompileErrorKind::Unsupported("function must be an identifier")))
|
||||
}
|
||||
},
|
||||
_ => return Err(self.compile_error(CompileErrorKind::Unsupported("unimplemented expression"))),
|
||||
_ => Err(self.compile_error(CompileErrorKind::Unsupported("unimplemented expression"))),
|
||||
}
|
||||
}
|
||||
|
||||
@ -515,7 +525,7 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
}
|
||||
},
|
||||
Return { value: None } => {
|
||||
if !return_type.is_none() {
|
||||
if return_type.is_some() {
|
||||
return Err(self.compile_error(CompileErrorKind::IncompatibleTypes));
|
||||
}
|
||||
self.builder.build_return(None);
|
||||
|
58
nac3core/src/magic_methods.rs
Normal file
58
nac3core/src/magic_methods.rs
Normal file
@ -0,0 +1,58 @@
|
||||
use rustpython_parser::ast::{Comparison, Operator, UnaryOperator};
|
||||
|
||||
pub fn binop_name(op: &Operator) -> &'static str {
|
||||
match op {
|
||||
Operator::Add => "__add__",
|
||||
Operator::Sub => "__sub__",
|
||||
Operator::Div => "__truediv__",
|
||||
Operator::Mod => "__mod__",
|
||||
Operator::Mult => "__mul__",
|
||||
Operator::Pow => "__pow__",
|
||||
Operator::BitOr => "__or__",
|
||||
Operator::BitXor => "__xor__",
|
||||
Operator::BitAnd => "__and__",
|
||||
Operator::LShift => "__lshift__",
|
||||
Operator::RShift => "__rshift__",
|
||||
Operator::FloorDiv => "__floordiv__",
|
||||
Operator::MatMult => "__matmul__",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn binop_assign_name(op: &Operator) -> &'static str {
|
||||
match op {
|
||||
Operator::Add => "__iadd__",
|
||||
Operator::Sub => "__isub__",
|
||||
Operator::Div => "__itruediv__",
|
||||
Operator::Mod => "__imod__",
|
||||
Operator::Mult => "__imul__",
|
||||
Operator::Pow => "__ipow__",
|
||||
Operator::BitOr => "__ior__",
|
||||
Operator::BitXor => "__ixor__",
|
||||
Operator::BitAnd => "__iand__",
|
||||
Operator::LShift => "__ilshift__",
|
||||
Operator::RShift => "__irshift__",
|
||||
Operator::FloorDiv => "__ifloordiv__",
|
||||
Operator::MatMult => "__imatmul__",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unaryop_name(op: &UnaryOperator) -> &'static str {
|
||||
match op {
|
||||
UnaryOperator::Pos => "__pos__",
|
||||
UnaryOperator::Neg => "__neg__",
|
||||
UnaryOperator::Not => "__not__",
|
||||
UnaryOperator::Inv => "__inv__",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn comparison_name(op: &Comparison) -> Option<&'static str> {
|
||||
match op {
|
||||
Comparison::Less => Some("__lt__"),
|
||||
Comparison::LessOrEqual => Some("__le__"),
|
||||
Comparison::Greater => Some("__gt__"),
|
||||
Comparison::GreaterOrEqual => Some("__ge__"),
|
||||
Comparison::Equal => Some("__eq__"),
|
||||
Comparison::NotEqual => Some("__ne__"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
184
nac3core/src/primitives.rs
Normal file
184
nac3core/src/primitives.rs
Normal file
@ -0,0 +1,184 @@
|
||||
use super::typedef::{TypeEnum::*, *};
|
||||
use crate::context::*;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub const TUPLE_TYPE: ParamId = ParamId(0);
|
||||
pub const LIST_TYPE: ParamId = ParamId(1);
|
||||
|
||||
pub const BOOL_TYPE: PrimitiveId = PrimitiveId(0);
|
||||
pub const INT32_TYPE: PrimitiveId = PrimitiveId(1);
|
||||
pub const INT64_TYPE: PrimitiveId = PrimitiveId(2);
|
||||
pub const FLOAT_TYPE: PrimitiveId = PrimitiveId(3);
|
||||
|
||||
fn impl_math(def: &mut TypeDef, ty: &Type) {
|
||||
let result = Some(ty.clone());
|
||||
let fun = FnDef {
|
||||
args: vec![ty.clone()],
|
||||
result: result.clone(),
|
||||
};
|
||||
def.methods.insert("__add__", fun.clone());
|
||||
def.methods.insert("__sub__", fun.clone());
|
||||
def.methods.insert("__mul__", fun.clone());
|
||||
def.methods.insert(
|
||||
"__neg__",
|
||||
FnDef {
|
||||
args: vec![],
|
||||
result,
|
||||
},
|
||||
);
|
||||
def.methods.insert(
|
||||
"__truediv__",
|
||||
FnDef {
|
||||
args: vec![ty.clone()],
|
||||
result: Some(PrimitiveType(FLOAT_TYPE).into()),
|
||||
},
|
||||
);
|
||||
def.methods.insert("__floordiv__", fun.clone());
|
||||
def.methods.insert("__mod__", fun.clone());
|
||||
def.methods.insert("__pow__", fun);
|
||||
}
|
||||
|
||||
fn impl_bits(def: &mut TypeDef, ty: &Type) {
|
||||
let result = Some(ty.clone());
|
||||
let fun = FnDef {
|
||||
args: vec![PrimitiveType(INT32_TYPE).into()],
|
||||
result,
|
||||
};
|
||||
|
||||
def.methods.insert("__lshift__", fun.clone());
|
||||
def.methods.insert("__rshift__", fun);
|
||||
def.methods.insert(
|
||||
"__xor__",
|
||||
FnDef {
|
||||
args: vec![ty.clone()],
|
||||
result: Some(ty.clone()),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn impl_eq(def: &mut TypeDef, ty: &Type) {
|
||||
let fun = FnDef {
|
||||
args: vec![ty.clone()],
|
||||
result: Some(PrimitiveType(BOOL_TYPE).into()),
|
||||
};
|
||||
|
||||
def.methods.insert("__eq__", fun.clone());
|
||||
def.methods.insert("__ne__", fun);
|
||||
}
|
||||
|
||||
fn impl_order(def: &mut TypeDef, ty: &Type) {
|
||||
let fun = FnDef {
|
||||
args: vec![ty.clone()],
|
||||
result: Some(PrimitiveType(BOOL_TYPE).into()),
|
||||
};
|
||||
|
||||
def.methods.insert("__lt__", fun.clone());
|
||||
def.methods.insert("__gt__", fun.clone());
|
||||
def.methods.insert("__le__", fun.clone());
|
||||
def.methods.insert("__ge__", fun);
|
||||
}
|
||||
|
||||
pub fn basic_ctx() -> TopLevelContext<'static> {
|
||||
let primitives = [
|
||||
TypeDef {
|
||||
name: "bool",
|
||||
fields: HashMap::new(),
|
||||
methods: HashMap::new(),
|
||||
},
|
||||
TypeDef {
|
||||
name: "int32",
|
||||
fields: HashMap::new(),
|
||||
methods: HashMap::new(),
|
||||
},
|
||||
TypeDef {
|
||||
name: "int64",
|
||||
fields: HashMap::new(),
|
||||
methods: HashMap::new(),
|
||||
},
|
||||
TypeDef {
|
||||
name: "float",
|
||||
fields: HashMap::new(),
|
||||
methods: HashMap::new(),
|
||||
},
|
||||
]
|
||||
.to_vec();
|
||||
let mut ctx = TopLevelContext::new(primitives);
|
||||
|
||||
let b = ctx.get_primitive(BOOL_TYPE);
|
||||
let b_def = ctx.get_primitive_def_mut(BOOL_TYPE);
|
||||
impl_eq(b_def, &b);
|
||||
let int32 = ctx.get_primitive(INT32_TYPE);
|
||||
let int32_def = ctx.get_primitive_def_mut(INT32_TYPE);
|
||||
impl_math(int32_def, &int32);
|
||||
impl_bits(int32_def, &int32);
|
||||
impl_order(int32_def, &int32);
|
||||
impl_eq(int32_def, &int32);
|
||||
let int64 = ctx.get_primitive(INT64_TYPE);
|
||||
let int64_def = ctx.get_primitive_def_mut(INT64_TYPE);
|
||||
impl_math(int64_def, &int64);
|
||||
impl_bits(int64_def, &int64);
|
||||
impl_order(int64_def, &int64);
|
||||
impl_eq(int64_def, &int64);
|
||||
let float = ctx.get_primitive(FLOAT_TYPE);
|
||||
let float_def = ctx.get_primitive_def_mut(FLOAT_TYPE);
|
||||
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 = 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,
|
||||
result: Some(PrimitiveType(FLOAT_TYPE).into()),
|
||||
},
|
||||
);
|
||||
|
||||
ctx
|
||||
}
|
60
nac3core/src/typedef.rs
Normal file
60
nac3core/src/typedef.rs
Normal file
@ -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<Rc<TypeEnum>>),
|
||||
TypeVariable(VariableId),
|
||||
}
|
||||
|
||||
pub type Type = Rc<TypeEnum>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FnDef {
|
||||
// we assume methods first argument to be SelfType,
|
||||
// so the first argument is not contained here
|
||||
pub args: Vec<Type>,
|
||||
pub result: Option<Type>,
|
||||
}
|
||||
|
||||
#[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<ClassId>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ParametricDef<'a> {
|
||||
pub base: TypeDef<'a>,
|
||||
pub params: Vec<VariableId>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct VarDef<'a> {
|
||||
pub name: &'a str,
|
||||
pub bound: Vec<Type>,
|
||||
}
|
@ -4,6 +4,6 @@ in
|
||||
pkgs.stdenv.mkDerivation {
|
||||
name = "nac3-env";
|
||||
buildInputs = with pkgs; [
|
||||
llvm_10 clang_10 cargo rustc libffi libxml2
|
||||
llvm_10 clang_10 cargo rustc libffi libxml2 clippy
|
||||
];
|
||||
}
|
||||
|
28
todo.txt
Normal file
28
todo.txt
Normal file
@ -0,0 +1,28 @@
|
||||
Errors:
|
||||
- Not supported
|
||||
- Only * is supported
|
||||
- Expected * in *, but got *
|
||||
- Divergent type in (construct), (location 1), (location 2)
|
||||
- Unknown field
|
||||
- Unbounded variable
|
||||
- Different variable
|
||||
- Different domain
|
||||
- * is not subclass of *
|
||||
- Type not equal
|
||||
- Incorrect number of parameters
|
||||
|
||||
GlobalContext:
|
||||
- Separate from typedefs
|
||||
- Interact with python intepreter to get data
|
||||
- Primitive Type Instance List
|
||||
- Symbol Table (readable, ever defined)
|
||||
- TypeVar definition stack
|
||||
- Provide subst, inv_subst, blablabla
|
||||
- Cache type var method lookup (dropped when related assumptions are changed)
|
||||
- Responsible for printing the error (lookup module/type info, handle line number offset)
|
||||
|
||||
Name Resolution:
|
||||
- Get class/methods, track module via `inspect.getmodule`
|
||||
- GlobalContext store function/class - module association, perform name
|
||||
resolution in the module when identifier is unbounded, and check its type
|
||||
|
Loading…
Reference in New Issue
Block a user