Compare commits

...

38 Commits

Author SHA1 Message Date
pca006132 65d9b620fe Merge remote-tracking branch 'origin/master' 2021-01-27 16:00:47 +08:00
pca006132 075245f4c2 get list of unknowns 2021-01-27 16:00:06 +08:00
pca006132 0e9dea0e65 updated todo 2021-01-25 17:09:17 +08:00
pca006132 7d06c903e3 added get_expression_unknowns 2021-01-15 15:19:41 +08:00
pca006132 5fce6cf069 updated lifetime 2021-01-15 14:37:55 +08:00
pca006132 d466b7bc2b simplified lifetime 2021-01-11 11:12:37 +08:00
pca006132 b6e4e68587 started adding error types
* converted to string to avoid breaking interface, would be fixed later
* tests would not check error messages now, as messages are not
  finalized
2021-01-11 10:40:32 +08:00
pca006132 779288d685 added todo 2021-01-08 17:16:25 +08:00
pca006132 3ecf57a588 fixed bugs 2021-01-08 16:54:34 +08:00
pca006132 ebe1027ffa added resolve signature 2021-01-08 16:24:25 +08:00
pca006132 7c6349520c started getting signatures 2021-01-08 14:52:48 +08:00
pca006132 04f121403a moved type check code to submodule 2021-01-08 12:58:33 +08:00
pca006132 b51168a5ab added statement tests 2021-01-05 13:35:44 +08:00
pca006132 007843c1ef added aug assign for primitives 2021-01-05 13:21:39 +08:00
pca006132 ff41cdb000 implemented statement check 2021-01-05 12:17:45 +08:00
pca006132 e1efb47ad2 statement inference: assignment 2021-01-04 17:07:14 +08:00
pca006132 4902f9f645 added expression_inference tests 2021-01-04 14:55:11 +08:00
pca006132 24d512c30f added expression_inference 2021-01-04 14:54:57 +08:00
pca006132 5b5e9ce04f added magic methods 2021-01-04 14:53:52 +08:00
pca006132 6824b9c4d3 added inference_core tests 2021-01-04 14:53:33 +08:00
pca006132 7c9e80cc1b added inference core 2021-01-04 14:52:50 +08:00
pca006132 bde52f7ad3 added primitives 2021-01-04 14:52:00 +08:00
pca006132 04e9f9c077 added inference context 2021-01-04 14:51:43 +08:00
pca006132 7523b43a08 added top level context 2021-01-04 14:51:25 +08:00
pca006132 7eb40e25dd added typedef 2021-01-04 14:49:48 +08:00
pca006132 96317686af enabled clippy 2021-01-04 14:49:14 +08:00
Sebastien Bourdeauducq dd29ae251a nac3embedded: compilation demo 2020-12-19 16:23:12 +08:00
Sebastien Bourdeauducq 75bd9b9a15 nac3embedded: ast storage 2020-12-19 15:29:39 +08:00
Sebastien Bourdeauducq aa8b4e2305 nac3embedded: parse 2020-12-19 00:52:06 +08:00
Sebastien Bourdeauducq b79d8ad640 nac3embedded: basic source extraction 2020-12-18 23:45:09 +08:00
Sebastien Bourdeauducq 703059adab nac3standalone: renames 2020-12-18 22:35:59 +08:00
Sebastien Bourdeauducq 947a528656 libnac3 -> nac3core 2020-12-18 10:18:21 +08:00
Sebastien Bourdeauducq 8558d41cd8 pyo3 demo 2020-12-18 10:09:35 +08:00
Sebastien Bourdeauducq 06d2fbd25c split off standalone compiler 2020-12-17 22:20:30 +08:00
Sebastien Bourdeauducq c54e5994d7 update dependencies 2020-12-17 21:50:47 +08:00
Sebastien Bourdeauducq d7ded27def add shell.nix 2020-11-10 20:39:25 +08:00
Sebastien Bourdeauducq d538988e5a use clang for demo 2020-11-10 20:39:17 +08:00
Sebastien Bourdeauducq f0d331c35a update all dependencies, switch to LLVM 10 2020-11-10 20:34:26 +08:00
27 changed files with 3964 additions and 346 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
__pycache__
/target

628
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,6 @@
[package]
name = "nac3"
version = "0.1.0"
authors = ["Sebastien Bourdeauducq <sb@m-labs.hk>"]
edition = "2018"
[dependencies]
num-bigint = "0.2"
num-traits = "0.2"
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "llvm8-0" }
rustpython-parser = { git = "https://github.com/RustPython/RustPython", branch = "master" }
[workspace]
members = [
"nac3core",
"nac3standalone",
"nac3embedded",
]

16
nac3core/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "nac3core"
version = "0.1.0"
authors = ["M-Labs"]
edition = "2018"
[dependencies]
num-bigint = "0.3"
num-traits = "0.2"
thiserror = "1.0"
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm10-0"] }
rustpython-parser = { git = "https://github.com/RustPython/RustPython", branch = "master" }
[dev-dependencies]
indoc = "1.0"

View File

@ -1,16 +1,20 @@
#![warn(clippy::all)]
#![allow(clippy::clone_double_ref)]
extern crate num_bigint;
extern crate inkwell;
extern crate rustpython_parser;
pub mod type_check;
use std::error::Error;
use std::fmt;
use std::path::Path;
use std::collections::HashMap;
use std::fs;
use num_traits::cast::ToPrimitive;
use rustpython_parser::{ast, parser};
use rustpython_parser::ast;
use inkwell::OptimizationLevel;
use inkwell::builder::Builder;
@ -58,7 +62,7 @@ impl fmt::Display for CompileErrorKind {
}
#[derive(Debug)]
struct CompileError {
pub struct CompileError {
location: ast::Location,
kind: CompileErrorKind,
}
@ -73,7 +77,7 @@ impl Error for CompileError {}
type CompileResult<T> = Result<T, CompileError>;
struct CodeGen<'ctx> {
pub struct CodeGen<'ctx> {
context: &'ctx Context,
module: Module<'ctx>,
pass_manager: passes::PassManager<values::FunctionValue<'ctx>>,
@ -84,7 +88,7 @@ struct CodeGen<'ctx> {
}
impl<'ctx> CodeGen<'ctx> {
fn new(context: &'ctx Context) -> CodeGen<'ctx> {
pub fn new(context: &'ctx Context) -> CodeGen<'ctx> {
let module = context.create_module("kernel");
let pass_manager = passes::PassManager::create(&module);
@ -538,7 +542,7 @@ impl<'ctx> CodeGen<'ctx> {
Ok(())
}
fn compile_toplevel(&mut self, statement: &ast::Statement) -> CompileResult<()> {
pub fn compile_toplevel(&mut self, statement: &ast::Statement) -> CompileResult<()> {
self.set_source_location(statement.location);
if let ast::StatementType::FunctionDef {
is_async,
@ -556,11 +560,11 @@ impl<'ctx> CodeGen<'ctx> {
}
}
fn print_ir(&self) {
pub fn print_ir(&self) {
self.module.print_to_stderr();
}
fn output(&self) {
pub fn output(&self, filename: &str) {
//let triple = TargetTriple::create("riscv32-none-linux-gnu");
let triple = TargetMachine::get_default_triple();
let target = Target::from_triple(&triple)
@ -578,29 +582,7 @@ impl<'ctx> CodeGen<'ctx> {
.expect("couldn't create target machine");
target_machine
.write_to_file(&self.module, FileType::Object, Path::new("test.o"))
.write_to_file(&self.module, FileType::Object, Path::new(filename))
.expect("couldn't write module to file");
}
}
fn main() {
Target::initialize_all(&InitializationConfig::default());
let program = match fs::read_to_string("test.py") {
Ok(program) => program,
Err(err) => { println!("Cannot open input file: {}", err); return; }
};
let ast = match parser::parse_program(&program) {
Ok(ast) => ast,
Err(err) => { println!("Parse error: {}", err); return; }
};
let context = Context::create();
let mut codegen = CodeGen::new(&context);
match codegen.compile_toplevel(&ast.statements[0]) {
Ok(_) => (),
Err(err) => { println!("Compilation error: {}", err); return; }
}
codegen.print_ir();
codegen.output();
}

View File

@ -0,0 +1,228 @@
use super::super::typedef::*;
use super::TopLevelContext;
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 mapping.
sym_table: HashMap<&'a str, Type>,
/// resolution function reference, that may resolve unbounded identifiers to some type
resolution_fn: Box<dyn FnMut(&str) -> Result<Type, String>>,
/// stack
stack: ContextStack<'a>,
/// return type
result: Option<Type>,
}
// 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(),
},
result: None,
}
}
/// 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, Type)>, 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();
let ty = self.sym_table.remove(name).unwrap();
poped_names.push((name, ty));
} 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) = self.sym_table.get_mut(name) {
if t == &ty {
Ok(ty)
} else {
Err("different types".into())
}
} else {
self.stack.sym_def.push((name, self.stack.level));
self.sym_table.insert(name, ty.clone());
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) = self.sym_table.get(name) {
Ok(t.clone())
} 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));
}
pub fn set_result(&mut self, result: Option<Type>) {
self.result = result;
}
}
// 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)
}
pub fn get_result(&self) -> Option<Type> {
self.result.clone()
}
}
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<'a>(&'a self, ctx: &'a InferenceContext) -> Option<&'a 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,
}
}
}

View File

@ -0,0 +1,4 @@
mod inference_context;
mod top_level_context;
pub use inference_context::InferenceContext;
pub use top_level_context::TopLevelContext;

View File

@ -0,0 +1,138 @@
use super::super::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 name visibility
// possibly by passing a function from outside to tell what names are allowed, and what are
// not...
self.sym_table.get(name).cloned()
}
}

View File

@ -0,0 +1,967 @@
use super::context::InferenceContext;
use super::inference_core::resolve_call;
use super::magic_methods::*;
use super::primitives::*;
use super::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<'a>(
ctx: &mut InferenceContext<'a>,
expr: &'a 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<'a>(
ctx: &mut InferenceContext<'a>,
elements: &'a [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<'a>(
ctx: &mut InferenceContext<'a>,
elements: &'a [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<'a>(
ctx: &mut InferenceContext<'a>,
op: &Operator,
left: &'a Expression,
right: &'a 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<'a>(
ctx: &mut InferenceContext<'a>,
op: &UnaryOperator,
obj: &'a 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<'a>(
ctx: &mut InferenceContext<'a>,
vals: &'a [Expression],
ops: &'a [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<'a>(
ctx: &mut InferenceContext<'a>,
args: &'a [Expression],
function: &'a 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<'a>(
ctx: &mut InferenceContext<'a>,
a: &'a Expression,
b: &'a 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<'a>(
ctx: &mut InferenceContext<'a>,
test: &'a Expression,
body: &'a Expression,
orelse: &'a 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())
}
}
pub fn infer_simple_binding<'a>(
ctx: &mut InferenceContext<'a>,
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<'a>(
ctx: &mut InferenceContext<'a>,
element: &'a Expression,
comprehension: &'a 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::{
super::{context::*, 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("different types".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())
);
}
}

View File

@ -0,0 +1,617 @@
use super::context::InferenceContext;
use super::typedef::{TypeEnum::*, *};
use std::collections::HashMap;
use thiserror::Error;
#[derive(Error, Debug)]
enum SubstError {
#[error("different type variables after substitution")]
DifferentSubstVar(VariableId, VariableId),
#[error("cannot substitute unbounded type variable into bounded one")]
UnboundedTypeVar(VariableId, VariableId),
#[error("incompatible bound for type variables")]
IncompatibleBound(VariableId, VariableId),
#[error("only subtype of virtual class can be substituted into virtual class type")]
NotVirtualClassSubtype(Type, ClassId),
#[error("different types")]
DifferentTypes(Type, Type),
}
fn find_subst(
ctx: &InferenceContext,
valuation: &Option<(VariableId, Type)>,
sub: &mut HashMap<VariableId, Type>,
mut a: Type,
mut b: Type,
) -> Result<(), SubstError> {
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(SubstError::DifferentSubstVar(*id_a, *id_b))
};
}
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(SubstError::UnboundedTypeVar(*id_a, *id_b));
} else {
let diff: Vec<_> = v_a
.bound
.iter()
.filter(|x| !v_b.bound.contains(x))
.collect();
if !diff.is_empty() {
return Err(SubstError::IncompatibleBound(*id_a, *id_b));
}
}
}
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(SubstError::DifferentTypes(a.clone(), b.clone()))
}
}
(_, 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(SubstError::DifferentTypes(a.clone(), b.clone()))
}
}
(_, 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(SubstError::NotVirtualClassSubtype(a.clone(), *id_b));
}
};
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(SubstError::NotVirtualClassSubtype(a.clone(), *id_b))
}
(ParametricType(id_a, param_a), ParametricType(id_b, param_b)) => {
if id_a != id_b || param_a.len() != param_b.len() {
Err(SubstError::DifferentTypes(a.clone(), b.clone()))
} 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(SubstError::DifferentTypes(a.clone(), b.clone()))
}
}
}
}
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()).map_err(|v| v.to_string())?;
}
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::{
super::{context::*, primitives::*},
*,
};
use std::matches;
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!(matches!(
resolve_call(&ctx, None, "float", &[ctx.get_primitive(BOOL_TYPE)]),
Err(..)
));
assert!(matches!(
resolve_call(&ctx, None, "float", &[]),
Err(..)
));
assert_eq!(
resolve_call(&ctx, None, "float", &[v1]),
Ok(Some(ctx.get_primitive(FLOAT_TYPE)))
);
assert!(matches!(
resolve_call(&ctx, None, "float", &[v2]),
Err(..)
));
}
#[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!(matches!(
resolve_call(&ctx, Some(int32), "__add__", &[int64]),
Err(..)
));
// with type variables
assert_eq!(
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v1.clone()]),
Ok(Some(v1.clone()))
);
assert!(matches!(
resolve_call(&ctx, Some(v0.clone()), "__add__", &[v2.clone()]),
Err(..)
));
assert!(matches!(
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v0]),
Err(..)
));
assert!(matches!(
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v2]),
Err(..)
));
assert!(matches!(
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v3.clone()]),
Err(..)
));
assert!(matches!(
resolve_call(&ctx, Some(v3.clone()), "__add__", &[v1]),
Err(..)
));
assert!(matches!(
resolve_call(&ctx, Some(v3.clone()), "__add__", &[v3]),
Err(..)
));
}
#[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!(matches!(
resolve_call(&ctx, None, "foo", &[v2.clone(), v3.clone(), v3.clone()]),
Err(..)
));
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!(matches!(
resolve_call(
&ctx,
None,
"foo1",
&[ParametricType(TUPLE_TYPE, vec![v2, v3.clone(), v3]).into()]
),
Err(..)
));
}
#[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!(matches!(
resolve_call(
&ctx,
Some(ParametricType(LIST_TYPE, vec![v0]).into()),
"append",
&[v1]
),
Err(..)
));
}
#[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!(matches!(
resolve_call(&ctx, None, "foo", &[ClassType(bar).into()]),
Err(..)
));
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!(matches!(
resolve_call(&ctx, None, "foo1", &[ClassType(foo).into()]),
Err(..)
));
// 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!(matches!(
resolve_call(&ctx, None, "foo", &[VirtualClassType(bar).into()]),
Err(..)
));
}
}

View 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,
}
}

View File

@ -0,0 +1,9 @@
pub mod context;
pub mod expression_inference;
pub mod inference_core;
mod magic_methods;
pub mod primitives;
pub mod statement_check;
pub mod typedef;
pub mod signature;

View File

@ -0,0 +1,201 @@
use super::context::*;
use super::typedef::{TypeEnum::*, *};
use std::collections::HashMap;
pub const PRIMITIVES: [&str; 6] = ["int32", "int64", "float", "bool", "list", "tuple"];
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("__iadd__", fun.clone());
def.methods.insert("__sub__", fun.clone());
def.methods.insert("__isub__", fun.clone());
def.methods.insert("__mul__", fun.clone());
def.methods.insert("__imul__", 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()),
},
);
if ty.as_ref() == &PrimitiveType(FLOAT_TYPE) {
def.methods.insert(
"__itruediv__",
FnDef {
args: vec![ty.clone()],
result: Some(PrimitiveType(FLOAT_TYPE).into()),
},
);
}
def.methods.insert("__floordiv__", fun.clone());
def.methods.insert("__ifloordiv__", fun.clone());
def.methods.insert("__mod__", fun.clone());
def.methods.insert("__imod__", fun.clone());
def.methods.insert("__pow__", fun.clone());
def.methods.insert("__ipow__", 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
}

View File

@ -0,0 +1,503 @@
/// obtain class and function signature from AST
use super::context::TopLevelContext;
use super::primitives::*;
use super::typedef::*;
use rustpython_parser::ast::{
ComprehensionKind, ExpressionType, Statement, StatementType, StringGroup,
};
use std::collections::HashMap;
// TODO: fix condition checking, return error message instead of panic...
fn typename_from_expr<'a>(typenames: &mut Vec<&'a str>, expr: &'a ExpressionType) {
match expr {
ExpressionType::Identifier { name } => typenames.push(&name),
ExpressionType::String { value } => match value {
StringGroup::Constant { value } => typenames.push(&value),
_ => unimplemented!(),
},
ExpressionType::Subscript { a, b } => {
typename_from_expr(typenames, &b.node);
typename_from_expr(typenames, &a.node)
}
_ => unimplemented!(),
}
}
fn typename_from_fn<'a>(typenames: &mut Vec<&'a str>, fun: &'a StatementType) {
match fun {
StatementType::FunctionDef { args, returns, .. } => {
for arg in args.args.iter() {
if let Some(ann) = &arg.annotation {
typename_from_expr(typenames, &ann.node);
}
}
if let Some(returns) = &returns {
typename_from_expr(typenames, &returns.node);
}
}
_ => unreachable!(),
}
}
fn name_from_expr<'a>(expr: &'a ExpressionType) -> &'a str {
match &expr {
ExpressionType::Identifier { name } => &name,
ExpressionType::String { value } => match value {
StringGroup::Constant { value } => &value,
_ => unimplemented!(),
},
_ => unimplemented!(),
}
}
fn type_from_expr<'a>(ctx: &'a TopLevelContext, expr: &'a ExpressionType) -> Result<Type, String> {
match expr {
ExpressionType::Identifier { name } => {
ctx.get_type(name).ok_or_else(|| "no such type".into())
}
ExpressionType::String { value } => match value {
StringGroup::Constant { value } => {
ctx.get_type(&value).ok_or_else(|| "no such type".into())
}
_ => unimplemented!(),
},
ExpressionType::Subscript { a, b } => {
if let ExpressionType::Identifier { name } = &a.node {
match name.as_str() {
"list" => {
let ty = type_from_expr(ctx, &b.node)?;
Ok(TypeEnum::ParametricType(LIST_TYPE, vec![ty]).into())
}
"tuple" => {
if let ExpressionType::Tuple { elements } = &b.node {
let ty_list: Result<Vec<_>, _> = elements
.iter()
.map(|v| type_from_expr(ctx, &v.node))
.collect();
Ok(TypeEnum::ParametricType(TUPLE_TYPE, ty_list?).into())
} else {
Err("unsupported format".into())
}
}
_ => Err("no such parameterized type".into()),
}
} else {
// we require a to be an identifier, for a[b]
Err("unsupported format".into())
}
}
_ => Err("unsupported format".into()),
}
}
pub fn get_typenames<'a>(stmts: &'a [Statement]) -> (Vec<&'a str>, Vec<&'a str>) {
let mut classes = Vec::new();
let mut typenames = Vec::new();
for stmt in stmts.iter() {
match &stmt.node {
StatementType::ClassDef {
name, body, bases, ..
} => {
// check if class is not duplicated...
// and annotations
classes.push(&name[..]);
for base in bases.iter() {
let name = name_from_expr(&base.node);
typenames.push(name);
}
// may check if fields/functions are not duplicated
for stmt in body.iter() {
match &stmt.node {
StatementType::AnnAssign { annotation, .. } => {
typename_from_expr(&mut typenames, &annotation.node)
}
StatementType::FunctionDef { .. } => {
typename_from_fn(&mut typenames, &stmt.node);
}
_ => unimplemented!(),
}
}
}
StatementType::FunctionDef { .. } => {
// may check annotations
typename_from_fn(&mut typenames, &stmt.node);
}
_ => (),
}
}
let mut unknowns = Vec::new();
for n in typenames {
if !PRIMITIVES.contains(&n) && !classes.contains(&n) && !unknowns.contains(&n) {
unknowns.push(n);
}
}
(classes, unknowns)
}
fn resolve_function<'a>(
ctx: &'a TopLevelContext,
fun: &'a StatementType,
method: bool,
) -> Result<FnDef, String> {
if let StatementType::FunctionDef { args, returns, .. } = &fun {
let args = if method {
args.args[1..].iter()
} else {
args.args.iter()
};
let args: Result<Vec<_>, _> = args
.map(|arg| type_from_expr(ctx, &arg.annotation.as_ref().unwrap().node))
.collect();
let args = args?;
let result = match returns {
Some(v) => Some(type_from_expr(ctx, &v.node)?),
None => None,
};
Ok(FnDef { args, result })
} else {
unreachable!()
}
}
fn get_expr_unknowns<'a>(
defined: &mut Vec<&'a str>,
unknowns: &mut Vec<&'a str>,
expr: &'a ExpressionType,
) {
match expr {
ExpressionType::BoolOp { values, .. } => {
for v in values.iter() {
get_expr_unknowns(defined, unknowns, &v.node)
}
}
ExpressionType::Binop { a, b, .. } => {
get_expr_unknowns(defined, unknowns, &a.node);
get_expr_unknowns(defined, unknowns, &b.node);
}
ExpressionType::Subscript { a, b } => {
get_expr_unknowns(defined, unknowns, &a.node);
get_expr_unknowns(defined, unknowns, &b.node);
}
ExpressionType::Unop { a, .. } => {
get_expr_unknowns(defined, unknowns, &a.node);
}
ExpressionType::Compare { vals, .. } => {
for v in vals.iter() {
get_expr_unknowns(defined, unknowns, &v.node)
}
}
ExpressionType::Attribute { value, .. } => {
get_expr_unknowns(defined, unknowns, &value.node);
}
ExpressionType::Call { function, args, .. } => {
get_expr_unknowns(defined, unknowns, &function.node);
for v in args.iter() {
get_expr_unknowns(defined, unknowns, &v.node)
}
}
ExpressionType::List { elements } => {
for v in elements.iter() {
get_expr_unknowns(defined, unknowns, &v.node)
}
}
ExpressionType::Tuple { elements } => {
for v in elements.iter() {
get_expr_unknowns(defined, unknowns, &v.node)
}
}
ExpressionType::Comprehension { kind, generators } => {
if generators.len() != 1 {
unimplemented!()
}
let g = &generators[0];
get_expr_unknowns(defined, unknowns, &g.iter.node);
let mut scoped = defined.clone();
get_expr_unknowns(defined, &mut scoped, &g.target.node);
for if_expr in g.ifs.iter() {
get_expr_unknowns(&mut scoped, unknowns, &if_expr.node);
}
match kind.as_ref() {
ComprehensionKind::List { element } => {
get_expr_unknowns(&mut scoped, unknowns, &element.node);
}
_ => unimplemented!(),
}
}
ExpressionType::Slice { elements } => {
for v in elements.iter() {
get_expr_unknowns(defined, unknowns, &v.node);
}
}
ExpressionType::Identifier { name } => {
if !defined.contains(&name.as_str()) && !unknowns.contains(&name.as_str()) {
unknowns.push(name);
}
}
ExpressionType::IfExpression { test, body, orelse } => {
get_expr_unknowns(defined, unknowns, &test.node);
get_expr_unknowns(defined, unknowns, &body.node);
get_expr_unknowns(defined, unknowns, &orelse.node);
}
_ => (),
};
}
struct ExprPattern<'a>(&'a ExpressionType, Vec<usize>, bool);
impl<'a> ExprPattern<'a> {
fn new(expr: &'a ExpressionType) -> ExprPattern {
let mut pattern = ExprPattern(expr, Vec::new(), true);
pattern.find_leaf();
pattern
}
fn pointed(&mut self) -> &'a ExpressionType {
let mut current = self.0;
for v in self.1.iter() {
if let ExpressionType::Tuple { elements } = current {
current = &elements[*v].node
} else {
unreachable!()
}
}
current
}
fn find_leaf(&mut self) {
let mut current = self.pointed();
while let ExpressionType::Tuple { elements } = current {
if elements.is_empty() {
break;
}
current = &elements[0].node;
self.1.push(0);
}
}
fn inc(&mut self) -> bool {
loop {
if self.1.is_empty() {
return false;
}
let ind = self.1.pop().unwrap() + 1;
let parent = self.pointed();
if let ExpressionType::Tuple { elements } = parent {
if ind < elements.len() {
self.1.push(ind);
self.find_leaf();
return true;
}
} else {
unreachable!()
}
}
}
}
impl<'a> Iterator for ExprPattern<'a> {
type Item = &'a ExpressionType;
fn next(&mut self) -> Option<Self::Item> {
if self.2 {
self.2 = false;
Some(self.pointed())
} else if self.inc() {
Some(self.pointed())
} else {
None
}
}
}
fn get_stmt_unknowns<'a>(
defined: &mut Vec<&'a str>,
unknowns: &mut Vec<&'a str>,
stmts: &'a [Statement],
) {
for stmt in stmts.iter() {
match &stmt.node {
StatementType::Return { value } => {
if let Some(value) = value {
get_expr_unknowns(defined, unknowns, &value.node);
}
}
StatementType::Assign { targets, value } => {
get_expr_unknowns(defined, unknowns, &value.node);
for target in targets.iter() {
for node in ExprPattern::new(&target.node).into_iter() {
if let ExpressionType::Identifier { name } = node {
let name = name.as_str();
if !defined.contains(&name) {
defined.push(name);
}
} else {
get_expr_unknowns(defined, unknowns, node);
}
}
}
}
StatementType::AugAssign { target, value, .. } => {
get_expr_unknowns(defined, unknowns, &target.node);
get_expr_unknowns(defined, unknowns, &value.node);
}
StatementType::AnnAssign { target, value, .. } => {
get_expr_unknowns(defined, unknowns, &target.node);
if let Some(value) = value {
get_expr_unknowns(defined, unknowns, &value.node);
}
}
StatementType::Expression { expression } => {
get_expr_unknowns(defined, unknowns, &expression.node);
}
StatementType::Global { names } => {
for name in names.iter() {
let name = name.as_str();
if !unknowns.contains(&name) {
unknowns.push(name);
}
}
}
StatementType::If { test, body, orelse }
| StatementType::While { test, body, orelse } => {
get_expr_unknowns(defined, unknowns, &test.node);
// we are not very strict at this point...
// some identifiers not treated as unknowns may not be resolved
// but should be checked during type inference
get_stmt_unknowns(defined, unknowns, body.as_slice());
if let Some(orelse) = orelse {
get_stmt_unknowns(defined, unknowns, orelse.as_slice());
}
}
StatementType::For { is_async, target, iter, body, orelse } => {
if *is_async {
unimplemented!()
}
get_expr_unknowns(defined, unknowns, &iter.node);
for node in ExprPattern::new(&target.node).into_iter() {
if let ExpressionType::Identifier { name } = node {
let name = name.as_str();
if !defined.contains(&name) {
defined.push(name);
}
} else {
get_expr_unknowns(defined, unknowns, node);
}
}
get_stmt_unknowns(defined, unknowns, body.as_slice());
if let Some(orelse) = orelse {
get_stmt_unknowns(defined, unknowns, orelse.as_slice());
}
}
_ => (),
}
}
}
pub fn resolve_signatures<'a>(ctx: &mut TopLevelContext<'a>, stmts: &'a [Statement]) {
for stmt in stmts.iter() {
match &stmt.node {
StatementType::ClassDef {
name, bases, body, ..
} => {
let mut parents = Vec::new();
for base in bases.iter() {
let name = name_from_expr(&base.node);
let c = ctx.get_type(name).unwrap();
let id = if let TypeEnum::ClassType(id) = c.as_ref() {
*id
} else {
unreachable!()
};
parents.push(id);
}
let mut fields = HashMap::new();
let mut functions = HashMap::new();
for stmt in body.iter() {
match &stmt.node {
StatementType::AnnAssign {
target, annotation, ..
} => {
let name = name_from_expr(&target.node);
let ty = type_from_expr(ctx, &annotation.node).unwrap();
fields.insert(name, ty);
}
StatementType::FunctionDef { name, .. } => {
functions.insert(
&name[..],
resolve_function(ctx, &stmt.node, true).unwrap(),
);
}
_ => unimplemented!(),
}
}
let class = ctx.get_type(name).unwrap();
let class = if let TypeEnum::ClassType(id) = class.as_ref() {
ctx.get_class_def_mut(*id)
} else {
unreachable!()
};
class.parents.extend_from_slice(&parents);
class.base.fields.clone_from(&fields);
class.base.methods.clone_from(&functions);
}
StatementType::FunctionDef { name, .. } => {
ctx.add_fn(&name[..], resolve_function(ctx, &stmt.node, false).unwrap());
}
_ => unimplemented!(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use rustpython_parser::parser::{parse_program, parse_statement};
#[test]
fn test_get_classes() {
let ast = parse_program(indoc! {"
class Foo:
a: int32
b: Test
def test(self, a: int32) -> Test2:
return self.b
class Bar(Foo, 'FooBar'):
def test2(self, a: list[Foo]) -> Test2:
return self.b
def test3(self, a: list[FooBar2]) -> Test2:
return self.b
" })
.unwrap();
let (mut classes, mut unknowns) = get_typenames(&ast.statements);
let classes_count = classes.len();
let unknowns_count = unknowns.len();
classes.sort();
unknowns.sort();
assert_eq!(classes.len(), classes_count);
assert_eq!(unknowns.len(), unknowns_count);
assert_eq!(&classes, &["Bar", "Foo"]);
assert_eq!(&unknowns, &["FooBar", "FooBar2", "Test", "Test2"]);
}
#[test]
fn test_assignment() {
let ast = parse_statement(indoc! {"
((a, b), c[i]) = core.foo(x, get_y())
" })
.unwrap();
let mut defined = Vec::new();
let mut unknowns = Vec::new();
get_stmt_unknowns(&mut defined, &mut unknowns, ast.as_slice());
defined.sort();
unknowns.sort();
assert_eq!(defined.as_slice(), &["a", "b"]);
assert_eq!(unknowns.as_slice(), &["c", "core", "get_y", "i", "x"]);
}
}

View File

@ -0,0 +1,572 @@
use super::context::InferenceContext;
use super::expression_inference::{infer_expr, infer_simple_binding};
use super::inference_core::resolve_call;
use super::magic_methods::binop_assign_name;
use super::primitives::*;
use super::typedef::{Type, TypeEnum::*};
use rustpython_parser::ast::*;
pub fn check_stmts<'b: 'a, 'a>(
ctx: &mut InferenceContext<'a>,
stmts: &'b [Statement],
) -> Result<bool, String> {
for stmt in stmts.iter() {
match &stmt.node {
StatementType::Assign { targets, value } => {
check_assign(ctx, targets.as_slice(), &value)?;
}
StatementType::AugAssign { target, op, value } => {
check_aug_assign(ctx, &target, op, &value)?;
}
StatementType::If { test, body, orelse } => {
if check_if(ctx, test, body.as_slice(), orelse)? {
return Ok(true);
}
}
StatementType::While { test, body, orelse } => {
check_while_stmt(ctx, test, body.as_slice(), orelse)?;
}
StatementType::For {
is_async,
target,
iter,
body,
orelse,
} => {
if *is_async {
return Err("async for is not supported".to_string());
}
check_for_stmt(ctx, target, iter, body.as_slice(), orelse)?;
}
StatementType::Return { value } => {
let result = ctx.get_result();
let t = if let Some(value) = value {
infer_expr(ctx, value)?
} else {
None
};
return if t == result {
Ok(true)
} else {
Err("return type mismatch".to_string())
};
}
StatementType::Continue | StatementType::Break => {
continue;
}
_ => return Err("not supported".to_string()),
}
}
Ok(false)
}
fn get_target_type<'b: 'a, 'a>(
ctx: &mut InferenceContext<'a>,
target: &'b Expression,
) -> Result<Type, String> {
match &target.node {
ExpressionType::Subscript { a, b } => {
let int32 = ctx.get_primitive(INT32_TYPE);
if infer_expr(ctx, &a)? == Some(int32) {
let b = get_target_type(ctx, &b)?;
if let ParametricType(LIST_TYPE, t) = b.as_ref() {
Ok(t[0].clone())
} else {
Err("subscript is only supported for list".to_string())
}
} else {
Err("subscript must be int32".to_string())
}
}
ExpressionType::Attribute { value, name } => {
let t = get_target_type(ctx, &value)?;
let base = t.get_base(ctx).ok_or_else(|| "no attributes".to_string())?;
Ok(base
.fields
.get(name.as_str())
.ok_or_else(|| "no such attribute")?
.clone())
}
ExpressionType::Identifier { name } => Ok(ctx.resolve(name.as_str())?),
_ => Err("not supported".to_string()),
}
}
fn check_stmt_binding<'b: 'a, 'a>(
ctx: &mut InferenceContext<'a>,
target: &'b Expression,
ty: Type,
) -> Result<(), String> {
match &target.node {
ExpressionType::Identifier { name } => {
if name.as_str() == "_" {
Ok(())
} else {
match ctx.resolve(name.as_str()) {
Ok(t) if t == ty => Ok(()),
Err(_) => {
ctx.assign(name.as_str(), ty).unwrap();
Ok(())
}
_ => Err("conflicting type".into()),
}
}
}
ExpressionType::Tuple { elements } => {
if let ParametricType(TUPLE_TYPE, ls) = ty.as_ref() {
if ls.len() != elements.len() {
return Err("incorrect pattern length".into());
}
for (x, y) in elements.iter().zip(ls.iter()) {
check_stmt_binding(ctx, x, y.clone())?;
}
Ok(())
} else {
Err("pattern matching supports tuple only".into())
}
}
_ => {
let t = get_target_type(ctx, target)?;
if ty == t {
Ok(())
} else {
Err("type mismatch".into())
}
}
}
}
fn check_assign<'b: 'a, 'a>(
ctx: &mut InferenceContext<'a>,
targets: &'b [Expression],
value: &'b Expression,
) -> Result<(), String> {
let ty = infer_expr(ctx, value)?.ok_or_else(|| "no value".to_string())?;
for t in targets.iter() {
check_stmt_binding(ctx, t, ty.clone())?;
}
Ok(())
}
fn check_aug_assign<'b: 'a, 'a>(
ctx: &mut InferenceContext<'a>,
target: &'b Expression,
op: &'b Operator,
value: &'b Expression,
) -> Result<(), String> {
let left = infer_expr(ctx, target)?.ok_or_else(|| "no value".to_string())?;
let right = infer_expr(ctx, value)?.ok_or_else(|| "no value".to_string())?;
let fun = binop_assign_name(op);
resolve_call(ctx, Some(left), fun, &[right])?;
Ok(())
}
fn check_if<'b: 'a, 'a>(
ctx: &mut InferenceContext<'a>,
test: &'b Expression,
body: &'b [Statement],
orelse: &'b Option<Suite>,
) -> Result<bool, String> {
let boolean = ctx.get_primitive(BOOL_TYPE);
let t = infer_expr(ctx, test)?;
if t == Some(boolean) {
let (names, result) = ctx.with_scope(|ctx| check_stmts(ctx, body));
let returned = result?;
if let Some(orelse) = orelse {
let (names2, result) = ctx.with_scope(|ctx| check_stmts(ctx, orelse.as_slice()));
let returned = returned && result?;
for (name, ty) in names.iter() {
for (name2, ty2) in names2.iter() {
if *name == *name2 && ty == ty2 {
ctx.assign(name, ty.clone()).unwrap();
}
}
}
Ok(returned)
} else {
Ok(false)
}
} else {
Err("condition should be bool".to_string())
}
}
fn check_while_stmt<'b: 'a, 'a>(
ctx: &mut InferenceContext<'a>,
test: &'b Expression,
body: &'b [Statement],
orelse: &'b Option<Suite>,
) -> Result<bool, String> {
let boolean = ctx.get_primitive(BOOL_TYPE);
let t = infer_expr(ctx, test)?;
if t == Some(boolean) {
// to check what variables are defined, we would have to do a graph analysis...
// not implemented now
let (_, result) = ctx.with_scope(|ctx| check_stmts(ctx, body));
result?;
if let Some(orelse) = orelse {
let (_, result) = ctx.with_scope(|ctx| check_stmts(ctx, orelse.as_slice()));
result?;
}
// to check whether the loop returned on every possible path, we need to analyse the graph,
// not implemented now
Ok(false)
} else {
Err("condition should be bool".to_string())
}
}
fn check_for_stmt<'b: 'a, 'a>(
ctx: &mut InferenceContext<'a>,
target: &'b Expression,
iter: &'b Expression,
body: &'b [Statement],
orelse: &'b Option<Suite>,
) -> Result<bool, String> {
let ty = infer_expr(ctx, iter)?.ok_or_else(|| "no value".to_string())?;
if let ParametricType(LIST_TYPE, ls) = ty.as_ref() {
let (_, result) = ctx.with_scope(|ctx| {
infer_simple_binding(ctx, target, ls[0].clone())?;
check_stmts(ctx, body)
});
result?;
if let Some(orelse) = orelse {
let (_, result) = ctx.with_scope(|ctx| check_stmts(ctx, orelse.as_slice()));
result?;
}
// to check whether the loop returned on every possible path, we need to analyse the graph,
// not implemented now
Ok(false)
} else {
Err("only list can be iterated over".to_string())
}
}
#[cfg(test)]
mod test {
use super::{super::context::*, *};
use indoc::indoc;
use rustpython_parser::parser::parse_program;
fn get_inference_context(ctx: TopLevelContext) -> InferenceContext {
InferenceContext::new(ctx, Box::new(|_| Err("unbounded identifier".into())))
}
#[test]
fn test_assign() {
let ctx = basic_ctx();
let mut ctx = get_inference_context(ctx);
let ast = parse_program(indoc! {"
a = 1
b = a * 2
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(Ok(false), check_stmts(ctx, ast.statements.as_slice()));
});
let ast = parse_program(indoc! {"
a = 1
b = b * 2
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(
Err("unbounded identifier".to_string()),
check_stmts(ctx, ast.statements.as_slice())
);
});
let ast = parse_program(indoc! {"
b = a = 1
b = b * 2
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(Ok(false), check_stmts(ctx, ast.statements.as_slice()));
});
let ast = parse_program(indoc! {"
b = a = 1
b = [a]
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(
Err("conflicting type".to_string()),
check_stmts(ctx, ast.statements.as_slice())
);
});
}
#[test]
fn test_if() {
let ctx = basic_ctx();
let mut ctx = get_inference_context(ctx);
let ast = parse_program(indoc! {"
a = 1
b = a * 2
if b > a:
c = 1
else:
c = 0
d = c
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(Ok(false), check_stmts(ctx, ast.statements.as_slice()));
});
let ast = parse_program(indoc! {"
a = 1
b = a * 2
if b > a:
c = 1
else:
d = 0
d = c
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(
Err("unbounded identifier".to_string()),
check_stmts(ctx, ast.statements.as_slice())
);
});
let ast = parse_program(indoc! {"
a = 1
b = a * 2
if b > a:
c = 1
d = c
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(
Err("unbounded identifier".to_string()),
check_stmts(ctx, ast.statements.as_slice())
);
});
let ast = parse_program(indoc! {"
a = 1
b = a * 2
if a:
b = 0
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(
Err("condition should be bool".to_string()),
check_stmts(ctx, ast.statements.as_slice())
);
});
let ast = parse_program(indoc! {"
a = 1
b = a * 2
if b > a:
c = 1
c = [1]
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(Ok(false), check_stmts(ctx, ast.statements.as_slice()));
});
let ast = parse_program(indoc! {"
a = 1
b = a * 2
if b > a:
c = 1
else:
c = 0
c = [1]
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(
Err("conflicting type".to_string()),
check_stmts(ctx, ast.statements.as_slice())
);
});
}
#[test]
fn test_while() {
let ctx = basic_ctx();
let mut ctx = get_inference_context(ctx);
let ast = parse_program(indoc! {"
a = 1
b = 1
while a < 10:
a += 1
b *= a
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(Ok(false), check_stmts(ctx, ast.statements.as_slice()));
});
let ast = parse_program(indoc! {"
a = 1
b = 1
while a < 10:
a += 1
b *= a
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(Ok(false), check_stmts(ctx, ast.statements.as_slice()));
});
let ast = parse_program(indoc! {"
a = 1
b = 1
while a < 10:
a += 1
b *= a
else:
a += 1
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(Ok(false), check_stmts(ctx, ast.statements.as_slice()));
});
let ast = parse_program(indoc! {"
a = 1
b = 1
while a:
a += 1
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(
Err("condition should be bool".to_string()),
check_stmts(ctx, ast.statements.as_slice())
);
});
let ast = parse_program(indoc! {"
a = 1
b = 1
while a < 10:
a += 1
c = a*2
else:
c = a*2
b = c
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(
Err("unbounded identifier".to_string()),
check_stmts(ctx, ast.statements.as_slice())
);
});
}
#[test]
fn test_for() {
let ctx = basic_ctx();
let mut ctx = get_inference_context(ctx);
let ast = parse_program(indoc! {"
b = 1
for a in [0, 1, 2, 3, 4, 5]:
b *= a
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(Ok(false), check_stmts(ctx, ast.statements.as_slice()));
});
let ast = parse_program(indoc! {"
b = 1
for a, a1 in [(0, 1), (2, 3), (4, 5)]:
b *= a
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(Ok(false), check_stmts(ctx, ast.statements.as_slice()));
});
}
#[test]
fn test_return() {
let ctx = basic_ctx();
let mut ctx = get_inference_context(ctx);
let ast = parse_program(indoc! {"
b = 1
return
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(Ok(true), check_stmts(ctx, ast.statements.as_slice()));
});
let ast = parse_program(indoc! {"
b = 1
if b > 0:
return
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(Ok(false), check_stmts(ctx, ast.statements.as_slice()));
});
let ast = parse_program(indoc! {"
b = 1
if b > 0:
return
else:
return
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(Ok(true), check_stmts(ctx, ast.statements.as_slice()));
});
let ast = parse_program(indoc! {"
b = 1
while b > 0:
return
else:
return
" })
.unwrap();
ctx.with_scope(|ctx| {
// with sophisticated analysis, this one should be Ok(true)
// but with our simple implementation, this is Ok(false)
// as we don't analyse the control flow
assert_eq!(Ok(false), check_stmts(ctx, ast.statements.as_slice()));
});
ctx.set_result(Some(ctx.get_primitive(INT32_TYPE)));
let ast = parse_program(indoc! {"
b = 1
return 1
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(Ok(true), check_stmts(ctx, ast.statements.as_slice()));
});
ctx.set_result(Some(ctx.get_primitive(INT32_TYPE)));
let ast = parse_program(indoc! {"
b = 1
return [1]
" })
.unwrap();
ctx.with_scope(|ctx| {
assert_eq!(
Err("return type mismatch".to_string()),
check_stmts(ctx, ast.statements.as_slice())
);
});
}
}

View 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>,
}

15
nac3embedded/Cargo.toml Normal file
View File

@ -0,0 +1,15 @@
[package]
name = "nac3embedded"
version = "0.1.0"
authors = ["M-Labs"]
edition = "2018"
[lib]
name = "nac3embedded"
crate-type = ["cdylib"]
[dependencies]
pyo3 = { version = "0.12.4", features = ["extension-module"] }
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm10-0"] }
rustpython-parser = { git = "https://github.com/RustPython/RustPython", branch = "master" }
nac3core = { path = "../nac3core" }

11
nac3embedded/demo.py Normal file
View File

@ -0,0 +1,11 @@
from language import *
class Demo:
@kernel
def run(self: bool) -> bool:
return False
if __name__ == "__main__":
Demo().run()

19
nac3embedded/language.py Normal file
View File

@ -0,0 +1,19 @@
from functools import wraps
import nac3embedded
__all__ = ["kernel", "portable"]
def kernel(function):
@wraps(function)
def run_on_core(self, *args, **kwargs):
nac3 = nac3embedded.NAC3()
nac3.register_host_object(self)
nac3.compile_method(self, function.__name__)
return run_on_core
def portable(function):
return fn

View File

@ -0,0 +1 @@
../target/release/libnac3embedded.so

116
nac3embedded/src/lib.rs Normal file
View File

@ -0,0 +1,116 @@
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use pyo3::prelude::*;
use pyo3::exceptions;
use rustpython_parser::{ast, parser};
use inkwell::context::Context;
use inkwell::targets::*;
use nac3core::CodeGen;
fn runs_on_core(decorator_list: &[ast::Expression]) -> bool {
for decorator in decorator_list.iter() {
if let ast::ExpressionType::Identifier { name } = &decorator.node {
if name == "kernel" || name == "portable" {
return true
}
}
}
false
}
#[pyclass(name=NAC3)]
struct Nac3 {
type_definitions: HashMap<i64, ast::Program>,
host_objects: HashMap<i64, i64>,
}
#[pymethods]
impl Nac3 {
#[new]
fn new() -> Self {
Nac3 {
type_definitions: HashMap::new(),
host_objects: HashMap::new(),
}
}
fn register_host_object(&mut self, obj: PyObject) -> PyResult<()> {
Python::with_gil(|py| -> PyResult<()> {
let obj: &PyAny = obj.extract(py)?;
let obj_type = obj.get_type();
let builtins = PyModule::import(py, "builtins")?;
let type_id = builtins.call1("id", (obj_type, ))?.extract()?;
let entry = self.type_definitions.entry(type_id);
if let Entry::Vacant(entry) = entry {
let source = PyModule::import(py, "inspect")?.call1("getsource", (obj_type, ))?;
let ast = parser::parse_program(source.extract()?).map_err(|e|
exceptions::PySyntaxError::new_err(format!("failed to parse host object source: {}", e)))?;
entry.insert(ast);
// TODO: examine AST and recursively register dependencies
};
let obj_id = builtins.call1("id", (obj, ))?.extract()?;
match self.host_objects.entry(obj_id) {
Entry::Vacant(entry) => entry.insert(type_id),
Entry::Occupied(_) => return Err(
exceptions::PyValueError::new_err("host object registered twice")),
};
// TODO: collect other information about host object, e.g. value of fields
Ok(())
})
}
fn compile_method(&self, obj: PyObject, name: String) -> PyResult<()> {
Python::with_gil(|py| -> PyResult<()> {
let obj: &PyAny = obj.extract(py)?;
let builtins = PyModule::import(py, "builtins")?;
let obj_id = builtins.call1("id", (obj, ))?.extract()?;
let type_id = self.host_objects.get(&obj_id).ok_or_else(||
exceptions::PyKeyError::new_err("type of host object not found"))?;
let ast = self.type_definitions.get(&type_id).ok_or_else(||
exceptions::PyKeyError::new_err("type definition not found"))?;
if let ast::StatementType::ClassDef {
name: _,
body,
bases: _,
keywords: _,
decorator_list: _ } = &ast.statements[0].node {
for statement in body.iter() {
if let ast::StatementType::FunctionDef {
is_async: _,
name: funcdef_name,
args: _,
body: _,
decorator_list,
returns: _ } = &statement.node {
if runs_on_core(decorator_list) && funcdef_name == &name {
let context = Context::create();
let mut codegen = CodeGen::new(&context);
codegen.compile_toplevel(&body[0]).map_err(|e|
exceptions::PyRuntimeError::new_err(format!("compilation failed: {}", e)))?;
codegen.print_ir();
}
}
}
} else {
return Err(exceptions::PyValueError::new_err("expected ClassDef for type definition"));
}
Ok(())
})
}
}
#[pymodule]
fn nac3embedded(_py: Python, m: &PyModule) -> PyResult<()> {
Target::initialize_all(&InitializationConfig::default());
m.add_class::<Nac3>()?;
Ok(())
}

10
nac3standalone/Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "nac3standalone"
version = "0.1.0"
authors = ["M-Labs"]
edition = "2018"
[dependencies]
inkwell = { git = "https://github.com/TheDan64/inkwell", branch = "master", features = ["llvm10-0"] }
rustpython-parser = { git = "https://github.com/RustPython/RustPython", branch = "master" }
nac3core = { path = "../nac3core" }

View File

@ -1,4 +1,4 @@
// gcc -Wall -o demo demo.c test.o
// clang -Wall -o demo demo.c mandelbrot.o
#include <stdio.h>
#include <string.h>

View File

@ -0,0 +1,30 @@
use std::fs;
use inkwell::context::Context;
use inkwell::targets::*;
use rustpython_parser::parser;
use nac3core::CodeGen;
fn main() {
Target::initialize_all(&InitializationConfig::default());
let program = match fs::read_to_string("mandelbrot.py") {
Ok(program) => program,
Err(err) => { println!("Cannot open input file: {}", err); return; }
};
let ast = match parser::parse_program(&program) {
Ok(ast) => ast,
Err(err) => { println!("Parse error: {}", err); return; }
};
let context = Context::create();
let mut codegen = CodeGen::new(&context);
match codegen.compile_toplevel(&ast.statements[0]) {
Ok(_) => (),
Err(err) => { println!("Compilation error: {}", err); return; }
}
codegen.print_ir();
codegen.output("mandelbrot.o");
}

9
shell.nix Normal file
View File

@ -0,0 +1,9 @@
let
pkgs = import <nixpkgs> { };
in
pkgs.stdenv.mkDerivation {
name = "nac3-env";
buildInputs = with pkgs; [
llvm_10 clang_10 cargo rustc libffi libxml2 clippy
];
}

34
todo.txt Normal file
View File

@ -0,0 +1,34 @@
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
Symbol Resolution:
- Add all files with annotated class/functions.
- Find class references, load them all in TopLevelContext.
- Find unbounded identifiers in the functions.
- If it is a function/class name, record its object ID.
- Otherwise, load its value. (check to see if specified with `global`)
(Function implemented in python, with rust binding to add value to global
variable dictionary)
Global variable dictionary:
- Primitives, including integers, floats, bools, etc.
- Primitive lists.
- Numpy multi-dimensional array, with value + dimension vectors.
- Reference array, with integer index referring to other things.
- Symbol table: python id -> reference id.
TopLevelContext/InferenceContext:
- Restrict visibility by user defined function.