forked from M-Labs/nac3
Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
95789ec303 | |||
03712e3762 | |||
b98117a049 | |||
7027135d6b | |||
a439ce61f7 | |||
1cc4c3f8d4 | |||
7b1ea58bc0 | |||
06af1623a8 | |||
1990486cc2 | |||
a9827da70b | |||
94ea2c7a9d | |||
422b92f686 | |||
0200ea1458 | |||
75183c39fd | |||
87dd0ee3cb | |||
929b7e1d92 | |||
7f09596bcb | |||
81f4be60c7 | |||
fd3e1d4923 | |||
fa02dc8271 | |||
0bca238642 |
377
nac3core/src/expression.rs
Normal file
377
nac3core/src/expression.rs
Normal file
@ -0,0 +1,377 @@
|
||||
use crate::inference::resolve_call;
|
||||
use crate::operators::*;
|
||||
use crate::primitives::*;
|
||||
use crate::typedef::{GlobalContext, Type, Type::*};
|
||||
use rustpython_parser::ast::{
|
||||
Comparison, Comprehension, ComprehensionKind, Expression, ExpressionType, Operator,
|
||||
UnaryOperator,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryInto;
|
||||
use std::rc::Rc;
|
||||
|
||||
type SymTable<'a> = HashMap<&'a str, Rc<Type>>;
|
||||
type ParserResult = Result<Option<Rc<Type>>, String>;
|
||||
|
||||
pub fn parse_expr(ctx: &GlobalContext, sym_table: &SymTable, expr: &Expression) -> ParserResult {
|
||||
match &expr.node {
|
||||
ExpressionType::Number { value } => parse_constant(ctx, sym_table, value),
|
||||
ExpressionType::Identifier { name } => parse_identifier(ctx, sym_table, name),
|
||||
ExpressionType::List { elements } => parse_list(ctx, sym_table, elements),
|
||||
ExpressionType::Tuple { elements } => parse_tuple(ctx, sym_table, elements),
|
||||
ExpressionType::Attribute { value, name } => parse_attribute(ctx, sym_table, value, name),
|
||||
ExpressionType::BoolOp { values, .. } => parse_bool_ops(ctx, sym_table, values),
|
||||
ExpressionType::Binop { a, b, op } => parse_bin_ops(ctx, sym_table, op, a, b),
|
||||
ExpressionType::Unop { op, a } => parse_unary_ops(ctx, sym_table, op, a),
|
||||
ExpressionType::Compare { vals, ops } => parse_compare(ctx, sym_table, vals, ops),
|
||||
ExpressionType::Call {
|
||||
args,
|
||||
function,
|
||||
keywords,
|
||||
} => {
|
||||
if keywords.len() > 0 {
|
||||
Err("keyword is not supported".into())
|
||||
} else {
|
||||
parse_call(ctx, sym_table, &args, &function)
|
||||
}
|
||||
}
|
||||
ExpressionType::Subscript { a, b } => parse_subscript(ctx, sym_table, a, b),
|
||||
ExpressionType::IfExpression { test, body, orelse } => {
|
||||
parse_if_expr(ctx, sym_table, &test, &body, orelse)
|
||||
}
|
||||
ExpressionType::Comprehension { kind, generators } => match kind.as_ref() {
|
||||
ComprehensionKind::List { element } => {
|
||||
if generators.len() == 1 {
|
||||
parse_list_comprehension(ctx, sym_table, 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(PrimitiveType(BOOL_TYPE).into())),
|
||||
_ => Err("not supported".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_constant(
|
||||
_: &GlobalContext,
|
||||
_: &SymTable,
|
||||
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(PrimitiveType(INT32_TYPE).into()))
|
||||
} else {
|
||||
let int64: Result<i64, _> = value.try_into();
|
||||
if int64.is_ok() {
|
||||
Ok(Some(PrimitiveType(INT64_TYPE).into()))
|
||||
} else {
|
||||
Err("integer out of range".into())
|
||||
}
|
||||
}
|
||||
}
|
||||
Number::Float { .. } => Ok(Some(PrimitiveType(FLOAT_TYPE).into())),
|
||||
_ => Err("not supported".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_identifier(_: &GlobalContext, sym_table: &SymTable, name: &str) -> ParserResult {
|
||||
match sym_table.get(name) {
|
||||
Some(v) => Ok(Some(v.clone())),
|
||||
None => Err("unbounded variable".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_list(ctx: &GlobalContext, sym_table: &SymTable, elements: &[Expression]) -> ParserResult {
|
||||
if elements.len() == 0 {
|
||||
return Ok(Some(ParametricType(LIST_TYPE, vec![BotType.into()]).into()));
|
||||
}
|
||||
|
||||
let mut types = elements.iter().map(|v| parse_expr(&ctx, sym_table, 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 parse_tuple(ctx: &GlobalContext, sym_table: &SymTable, elements: &[Expression]) -> ParserResult {
|
||||
let types: Result<Option<Vec<_>>, String> = elements
|
||||
.iter()
|
||||
.map(|v| parse_expr(&ctx, sym_table, v))
|
||||
.collect();
|
||||
if let Some(t) = types? {
|
||||
Ok(Some(ParametricType(TUPLE_TYPE, t).into()))
|
||||
} else {
|
||||
Err("tuple elements must have some type".into())
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_attribute(
|
||||
ctx: &GlobalContext,
|
||||
sym_table: &SymTable,
|
||||
value: &Expression,
|
||||
name: &String,
|
||||
) -> ParserResult {
|
||||
let value = parse_expr(ctx, sym_table, value)?.ok_or("no value".to_string())?;
|
||||
if let TypeVariable(id) = value.as_ref() {
|
||||
let v = ctx.get_variable(*id);
|
||||
if v.bound.len() == 0 {
|
||||
return Err("no fields on unbounded type variable".into());
|
||||
}
|
||||
let ty = v.bound[0]
|
||||
.get_base(ctx)
|
||||
.and_then(|v| v.fields.get(name.as_str()));
|
||||
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.as_str()));
|
||||
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.as_str()) {
|
||||
Some(t) => Ok(Some(t.clone())),
|
||||
None => Err("no such field".into()),
|
||||
},
|
||||
None => Err("this object has no fields".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_bool_ops(
|
||||
ctx: &GlobalContext,
|
||||
sym_table: &SymTable,
|
||||
values: &[Expression],
|
||||
) -> ParserResult {
|
||||
assert_eq!(values.len(), 2);
|
||||
let left = parse_expr(ctx, sym_table, &values[0])?.ok_or("no value".to_string())?;
|
||||
let right = parse_expr(ctx, sym_table, &values[1])?.ok_or("no value".to_string())?;
|
||||
|
||||
let b = PrimitiveType(BOOL_TYPE);
|
||||
if left.as_ref() == &b && right.as_ref() == &b {
|
||||
Ok(Some(b.into()))
|
||||
} else {
|
||||
Err("bool operands must be bool".into())
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_bin_ops(
|
||||
ctx: &GlobalContext,
|
||||
sym_table: &SymTable,
|
||||
op: &Operator,
|
||||
left: &Expression,
|
||||
right: &Expression,
|
||||
) -> ParserResult {
|
||||
let left = parse_expr(ctx, sym_table, left)?.ok_or("no value".to_string())?;
|
||||
let right = parse_expr(ctx, sym_table, right)?.ok_or("no value".to_string())?;
|
||||
let fun = binop_name(op);
|
||||
resolve_call(ctx, Some(left), fun, &[right])
|
||||
}
|
||||
|
||||
fn parse_unary_ops(
|
||||
ctx: &GlobalContext,
|
||||
sym_table: &SymTable,
|
||||
op: &UnaryOperator,
|
||||
obj: &Expression,
|
||||
) -> ParserResult {
|
||||
let ty = parse_expr(ctx, sym_table, obj)?.ok_or("no value".to_string())?;
|
||||
if let UnaryOperator::Not = op {
|
||||
if ty.as_ref() == &PrimitiveType(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 parse_compare(
|
||||
ctx: &GlobalContext,
|
||||
sym_table: &SymTable,
|
||||
vals: &[Expression],
|
||||
ops: &[Comparison],
|
||||
) -> ParserResult {
|
||||
let types: Result<Option<Vec<_>>, _> =
|
||||
vals.iter().map(|v| parse_expr(ctx, sym_table, v)).collect();
|
||||
let types = types?;
|
||||
if types.is_none() {
|
||||
return Err("comparison operands must have type".into());
|
||||
}
|
||||
let types = types.unwrap();
|
||||
let boolean = PrimitiveType(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("unsupported comparison".to_string())?;
|
||||
let ty = resolve_call(ctx, Some(a.clone()), fun, &[b.clone()])?;
|
||||
if ty.is_none() || ty.unwrap().as_ref() != &boolean {
|
||||
return Err("comparison result must be boolean".into());
|
||||
}
|
||||
}
|
||||
Ok(Some(boolean.into()))
|
||||
}
|
||||
|
||||
fn parse_call(
|
||||
ctx: &GlobalContext,
|
||||
sym_table: &SymTable,
|
||||
args: &[Expression],
|
||||
function: &Expression,
|
||||
) -> ParserResult {
|
||||
let types: Result<Option<Vec<_>>, _> =
|
||||
args.iter().map(|v| parse_expr(ctx, sym_table, 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(parse_expr(ctx, sym_table, &value)?.ok_or("no value".to_string())?),
|
||||
name,
|
||||
),
|
||||
_ => return Err("not supported".into()),
|
||||
};
|
||||
resolve_call(ctx, obj, fun.as_str(), &types.unwrap())
|
||||
}
|
||||
|
||||
fn parse_subscript(
|
||||
ctx: &GlobalContext,
|
||||
sym_table: &SymTable,
|
||||
a: &Expression,
|
||||
b: &Expression,
|
||||
) -> ParserResult {
|
||||
let a = parse_expr(ctx, sym_table, a)?.ok_or("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 types: Result<Option<Vec<_>>, _> = elements
|
||||
.iter()
|
||||
.map(|v| parse_expr(ctx, sym_table, v))
|
||||
.collect();
|
||||
let types = types?.ok_or("slice must have type".to_string())?;
|
||||
let int32 = PrimitiveType(INT32_TYPE);
|
||||
if types.iter().all(|v| v.as_ref() == &int32) {
|
||||
Ok(Some(a))
|
||||
} else {
|
||||
Err("slice must be int32 type".into())
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let b = parse_expr(ctx, sym_table, b)?.ok_or("no value".to_string())?;
|
||||
if b.as_ref() == &PrimitiveType(INT32_TYPE) {
|
||||
Ok(Some(t))
|
||||
} else {
|
||||
Err("index must be either slice or int32".into())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_if_expr(
|
||||
ctx: &GlobalContext,
|
||||
sym_table: &SymTable,
|
||||
test: &Expression,
|
||||
body: &Expression,
|
||||
orelse: &Expression,
|
||||
) -> ParserResult {
|
||||
let test = parse_expr(ctx, sym_table, test)?.ok_or("no value".to_string())?;
|
||||
if test.as_ref() != &PrimitiveType(BOOL_TYPE) {
|
||||
return Err("test should be bool".into());
|
||||
}
|
||||
|
||||
let body = parse_expr(ctx, sym_table, body)?.ok_or("no value".to_string())?;
|
||||
let orelse = parse_expr(ctx, sym_table, orelse)?.ok_or("no value".to_string())?;
|
||||
if body.as_ref() == orelse.as_ref() {
|
||||
Ok(Some(body))
|
||||
} else {
|
||||
Err("divergent type".into())
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_simple_binding<'a: 'b, 'b>(
|
||||
sym_table: &mut SymTable<'b>,
|
||||
name: &'a Expression,
|
||||
ty: Rc<Type>,
|
||||
) -> Result<(), String> {
|
||||
match &name.node {
|
||||
ExpressionType::Identifier { name } => {
|
||||
if name == "_" {
|
||||
Ok(())
|
||||
} else if sym_table.get(name.as_str()).is_some() {
|
||||
Err("duplicated naming".into())
|
||||
} else {
|
||||
sym_table.insert(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()) {
|
||||
parse_simple_binding(sym_table, a, b.clone())?;
|
||||
}
|
||||
Ok(())
|
||||
} else {
|
||||
Err("different length".into())
|
||||
}
|
||||
} else {
|
||||
Err("not supported".into())
|
||||
}
|
||||
}
|
||||
_ => Err("not supported".into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_list_comprehension(
|
||||
ctx: &GlobalContext,
|
||||
sym_table: &SymTable,
|
||||
element: &Expression,
|
||||
comprehension: &Comprehension,
|
||||
) -> ParserResult {
|
||||
if comprehension.is_async {
|
||||
return Err("async is not supported".into());
|
||||
}
|
||||
|
||||
// TODO: it may be more efficient to use multi-level table
|
||||
// but it would better done in a whole program level
|
||||
let iter = parse_expr(ctx, sym_table, &comprehension.iter)?.ok_or("no value".to_string())?;
|
||||
if let ParametricType(LIST_TYPE, ls) = iter.as_ref() {
|
||||
let mut local_sym = sym_table.clone();
|
||||
parse_simple_binding(&mut local_sym, &comprehension.target, ls[0].clone())?;
|
||||
|
||||
let boolean = PrimitiveType(BOOL_TYPE);
|
||||
for test in comprehension.ifs.iter() {
|
||||
let result =
|
||||
parse_expr(ctx, &local_sym, test)?.ok_or("no value in test".to_string())?;
|
||||
if result.as_ref() != &boolean {
|
||||
return Err("test must be bool".into());
|
||||
}
|
||||
}
|
||||
parse_expr(ctx, &local_sym, element)
|
||||
} else {
|
||||
Err("iteration is supported for list only".into())
|
||||
}
|
||||
}
|
591
nac3core/src/inference.rs
Normal file
591
nac3core/src/inference.rs
Normal file
@ -0,0 +1,591 @@
|
||||
use super::typedef::{Type::*, *};
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
fn find_subst(
|
||||
ctx: &GlobalContext,
|
||||
valuation: &Option<(VariableId, Rc<Type>)>,
|
||||
sub: &mut HashMap<VariableId, Rc<Type>>,
|
||||
mut a: Rc<Type>,
|
||||
mut b: Rc<Type>,
|
||||
) -> Result<(), String> {
|
||||
// TODO: fix error messages later
|
||||
if let TypeVariable(id) = a.as_ref() {
|
||||
if let Some((assumption_id, t)) = valuation {
|
||||
if assumption_id == id {
|
||||
a = t.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut substituted = false;
|
||||
if let TypeVariable(id) = b.as_ref() {
|
||||
if let Some(c) = sub.get(&id) {
|
||||
b = c.clone();
|
||||
substituted = true;
|
||||
}
|
||||
}
|
||||
|
||||
match (a.as_ref(), b.as_ref()) {
|
||||
(BotType, _) => Ok(()),
|
||||
(TypeVariable(id_a), TypeVariable(id_b)) => {
|
||||
if substituted {
|
||||
return if id_a == id_b {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("different variables".to_string())
|
||||
};
|
||||
}
|
||||
let v_a = ctx.get_variable(*id_a);
|
||||
let v_b = ctx.get_variable(*id_b);
|
||||
if v_b.bound.len() > 0 {
|
||||
if v_a.bound.len() == 0 {
|
||||
return Err("unbounded a".to_string());
|
||||
} else {
|
||||
let diff: Vec<_> = v_a
|
||||
.bound
|
||||
.iter()
|
||||
.filter(|x| !v_b.bound.contains(x))
|
||||
.collect();
|
||||
if diff.len() > 0 {
|
||||
return Err("different domain".to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
sub.insert(*id_b, a.clone().into());
|
||||
Ok(())
|
||||
}
|
||||
(TypeVariable(id_a), _) => {
|
||||
let v_a = ctx.get_variable(*id_a);
|
||||
if v_a.bound.len() == 1 && v_a.bound[0].as_ref() == b.as_ref() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("different domain".to_string())
|
||||
}
|
||||
}
|
||||
(_, TypeVariable(id_b)) => {
|
||||
let v_b = ctx.get_variable(*id_b);
|
||||
if v_b.bound.len() == 0 || v_b.bound.contains(&a) {
|
||||
sub.insert(*id_b, a.clone().into());
|
||||
Ok(())
|
||||
} else {
|
||||
Err("different domain".to_string())
|
||||
}
|
||||
}
|
||||
(_, VirtualClassType(id_b)) => {
|
||||
let mut parents;
|
||||
match a.as_ref() {
|
||||
ClassType(id_a) => {
|
||||
parents = [*id_a].to_vec();
|
||||
}
|
||||
VirtualClassType(id_a) => {
|
||||
parents = [*id_a].to_vec();
|
||||
}
|
||||
_ => {
|
||||
return Err("cannot substitute non-class type into virtual class".to_string());
|
||||
}
|
||||
};
|
||||
while !parents.is_empty() {
|
||||
if *id_b == parents[0] {
|
||||
return Ok(());
|
||||
}
|
||||
let c = ctx.get_class(parents.remove(0));
|
||||
parents.extend_from_slice(&c.parents);
|
||||
}
|
||||
Err("not subtype".to_string())
|
||||
}
|
||||
(ParametricType(id_a, param_a), ParametricType(id_b, param_b)) => {
|
||||
if id_a != id_b || param_a.len() != param_b.len() {
|
||||
Err("different parametric types".to_string())
|
||||
} else {
|
||||
for (x, y) in param_a.iter().zip(param_b.iter()) {
|
||||
find_subst(ctx, valuation, sub, x.clone(), y.clone())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
(_, _) => {
|
||||
if a == b {
|
||||
Ok(())
|
||||
} else {
|
||||
Err("not equal".to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_call_rec(
|
||||
ctx: &GlobalContext,
|
||||
valuation: &Option<(VariableId, Rc<Type>)>,
|
||||
obj: Option<Rc<Type>>,
|
||||
func: &str,
|
||||
args: &[Rc<Type>],
|
||||
) -> Result<Option<Rc<Type>>, String> {
|
||||
let mut subst = obj
|
||||
.as_ref()
|
||||
.map(|v| v.get_subst(ctx))
|
||||
.unwrap_or(HashMap::new());
|
||||
|
||||
let fun = match &obj {
|
||||
Some(obj) => {
|
||||
let base = match obj.as_ref() {
|
||||
TypeVariable(id) => {
|
||||
let v = ctx.get_variable(*id);
|
||||
if v.bound.len() == 0 {
|
||||
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().into())]))
|
||||
});
|
||||
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(*id),
|
||||
ClassType(id) | VirtualClassType(id) => &ctx.get_class(*id).base,
|
||||
ParametricType(id, _) => &ctx.get_parametric(*id).base,
|
||||
_ => return Err("not supported".to_string()),
|
||||
};
|
||||
base.methods.get(func)
|
||||
}
|
||||
None => ctx.get_fn(func),
|
||||
}
|
||||
.ok_or("no such function".to_string())?;
|
||||
|
||||
if args.len() != fun.args.len() {
|
||||
return Err("incorrect parameter number".to_string());
|
||||
}
|
||||
for (a, b) in args.iter().zip(fun.args.iter()) {
|
||||
find_subst(ctx, valuation, &mut subst, a.clone(), b.clone())?;
|
||||
}
|
||||
let result = fun.result.as_ref().map(|v| v.subst(&subst));
|
||||
Ok(result.map(|result| {
|
||||
if let SelfType = result {
|
||||
obj.unwrap()
|
||||
} else {
|
||||
result.into()
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn resolve_call(
|
||||
ctx: &GlobalContext,
|
||||
obj: Option<Rc<Type>>,
|
||||
func: &str,
|
||||
args: &[Rc<Type>],
|
||||
) -> Result<Option<Rc<Type>>, String> {
|
||||
resolve_call_rec(ctx, &None, obj, func, args)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::primitives::*;
|
||||
|
||||
#[test]
|
||||
fn test_simple_generic() {
|
||||
let mut ctx = basic_ctx();
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "int32", &[PrimitiveType(FLOAT_TYPE).into()]),
|
||||
Ok(Some(PrimitiveType(INT32_TYPE).into()))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "int32", &[PrimitiveType(INT32_TYPE).into()],),
|
||||
Ok(Some(PrimitiveType(INT32_TYPE).into()))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "float", &[PrimitiveType(INT32_TYPE).into()]),
|
||||
Ok(Some(PrimitiveType(FLOAT_TYPE).into()))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "float", &[PrimitiveType(BOOL_TYPE).into()]),
|
||||
Err("different domain".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "float", &[]),
|
||||
Err("incorrect parameter number".to_string())
|
||||
);
|
||||
|
||||
let v1 = ctx.add_variable(VarDef {
|
||||
name: "V1",
|
||||
bound: vec![
|
||||
PrimitiveType(INT32_TYPE).into(),
|
||||
PrimitiveType(FLOAT_TYPE).into(),
|
||||
],
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "float", &[TypeVariable(v1).into()]),
|
||||
Ok(Some(PrimitiveType(FLOAT_TYPE).into()))
|
||||
);
|
||||
|
||||
let v2 = ctx.add_variable(VarDef {
|
||||
name: "V2",
|
||||
bound: vec![
|
||||
PrimitiveType(BOOL_TYPE).into(),
|
||||
PrimitiveType(INT32_TYPE).into(),
|
||||
PrimitiveType(FLOAT_TYPE).into(),
|
||||
],
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "float", &[TypeVariable(v2).into()]),
|
||||
Err("different domain".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_methods() {
|
||||
let mut ctx = basic_ctx();
|
||||
|
||||
let v0 = Rc::new(TypeVariable(ctx.add_variable(VarDef {
|
||||
name: "V0",
|
||||
bound: vec![],
|
||||
})));
|
||||
let v1 = Rc::new(TypeVariable(ctx.add_variable(VarDef {
|
||||
name: "V1",
|
||||
bound: vec![
|
||||
PrimitiveType(INT32_TYPE).into(),
|
||||
PrimitiveType(FLOAT_TYPE).into(),
|
||||
],
|
||||
})));
|
||||
let v2 = Rc::new(TypeVariable(ctx.add_variable(VarDef {
|
||||
name: "V2",
|
||||
bound: vec![
|
||||
PrimitiveType(INT32_TYPE).into(),
|
||||
PrimitiveType(FLOAT_TYPE).into(),
|
||||
],
|
||||
})));
|
||||
let v3 = Rc::new(TypeVariable(ctx.add_variable(VarDef {
|
||||
name: "V3",
|
||||
bound: vec![
|
||||
PrimitiveType(BOOL_TYPE).into(),
|
||||
PrimitiveType(INT32_TYPE).into(),
|
||||
PrimitiveType(FLOAT_TYPE).into(),
|
||||
],
|
||||
})));
|
||||
|
||||
let int32 = Rc::new(PrimitiveType(INT32_TYPE));
|
||||
let int64 = Rc::new(PrimitiveType(INT64_TYPE));
|
||||
|
||||
// simple cases
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(int32.clone()), "__add__", &[int32.clone()]),
|
||||
Ok(Some(int32.clone()))
|
||||
);
|
||||
|
||||
assert_ne!(
|
||||
resolve_call(&ctx, Some(int32.clone()), "__add__", &[int32.clone()]),
|
||||
Ok(Some(int64.clone()))
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(int32.clone()), "__add__", &[int64.clone()]),
|
||||
Err("not equal".to_string())
|
||||
);
|
||||
|
||||
// with type variables
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v1.clone()]),
|
||||
Ok(Some(v1.clone()))
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(v0.clone()), "__add__", &[v2.clone()]),
|
||||
Err("unbounded type var".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v0.clone()]),
|
||||
Err("different domain".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v2.clone()]),
|
||||
Err("different domain".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(v1.clone()), "__add__", &[v3.clone()]),
|
||||
Err("different domain".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(v3.clone()), "__add__", &[v1.clone()]),
|
||||
Err("no such function".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, Some(v3.clone()), "__add__", &[v3.clone()]),
|
||||
Err("no such function".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multi_generic() {
|
||||
let mut ctx = basic_ctx();
|
||||
let v0 = Rc::new(TypeVariable(ctx.add_variable(VarDef {
|
||||
name: "V0",
|
||||
bound: vec![],
|
||||
})));
|
||||
let v1 = Rc::new(TypeVariable(ctx.add_variable(VarDef {
|
||||
name: "V1",
|
||||
bound: vec![],
|
||||
})));
|
||||
let v2 = Rc::new(TypeVariable(ctx.add_variable(VarDef {
|
||||
name: "V2",
|
||||
bound: vec![],
|
||||
})));
|
||||
let v3 = Rc::new(TypeVariable(ctx.add_variable(VarDef {
|
||||
name: "V3",
|
||||
bound: vec![],
|
||||
})));
|
||||
|
||||
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.clone()]).into(),
|
||||
],
|
||||
result: Some(v0.clone()),
|
||||
},
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[v2.clone(), v2.clone(), v2.clone()]),
|
||||
Ok(Some(v2.clone()))
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[v2.clone(), v2.clone(), v3.clone()]),
|
||||
Ok(Some(v2.clone()))
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[v2.clone(), v3.clone(), v3.clone()]),
|
||||
Err("different variables".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(
|
||||
&ctx,
|
||||
None,
|
||||
"foo1",
|
||||
&[ParametricType(TUPLE_TYPE, vec![v2.clone(), v2.clone(), v2.clone()]).into()]
|
||||
),
|
||||
Ok(Some(v2.clone()))
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(
|
||||
&ctx,
|
||||
None,
|
||||
"foo1",
|
||||
&[ParametricType(TUPLE_TYPE, vec![v2.clone(), v2.clone(), v3.clone()]).into()]
|
||||
),
|
||||
Ok(Some(v2.clone()))
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(
|
||||
&ctx,
|
||||
None,
|
||||
"foo1",
|
||||
&[ParametricType(TUPLE_TYPE, vec![v2.clone(), v3.clone(), v3.clone()]).into()]
|
||||
),
|
||||
Err("different variables".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_class_generics() {
|
||||
let mut ctx = basic_ctx();
|
||||
|
||||
let list = ctx.get_parametric_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.clone()],
|
||||
result: None,
|
||||
},
|
||||
);
|
||||
|
||||
let v0 = Rc::new(TypeVariable(ctx.add_variable(VarDef {
|
||||
name: "V0",
|
||||
bound: vec![],
|
||||
})));
|
||||
let v1 = Rc::new(TypeVariable(ctx.add_variable(VarDef {
|
||||
name: "V1",
|
||||
bound: vec![],
|
||||
})));
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(
|
||||
&ctx,
|
||||
Some(ParametricType(LIST_TYPE, vec![v0.clone()]).into()),
|
||||
"head",
|
||||
&[]
|
||||
),
|
||||
Ok(Some(v0.clone()))
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(
|
||||
&ctx,
|
||||
Some(ParametricType(LIST_TYPE, vec![v0.clone()]).into()),
|
||||
"append",
|
||||
&[v0.clone()]
|
||||
),
|
||||
Ok(None)
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(
|
||||
&ctx,
|
||||
Some(ParametricType(LIST_TYPE, vec![v0.clone()]).into()),
|
||||
"append",
|
||||
&[v1.clone()]
|
||||
),
|
||||
Err("different variables".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_virtual_class() {
|
||||
let mut ctx = basic_ctx();
|
||||
|
||||
let foo = ctx.add_class(ClassDef {
|
||||
base: TypeDef {
|
||||
name: "Foo",
|
||||
methods: HashMap::new(),
|
||||
fields: HashMap::new(),
|
||||
},
|
||||
parents: vec![],
|
||||
});
|
||||
|
||||
let foo1 = ctx.add_class(ClassDef {
|
||||
base: TypeDef {
|
||||
name: "Foo1",
|
||||
methods: HashMap::new(),
|
||||
fields: HashMap::new(),
|
||||
},
|
||||
parents: vec![foo],
|
||||
});
|
||||
|
||||
let foo2 = ctx.add_class(ClassDef {
|
||||
base: TypeDef {
|
||||
name: "Foo2",
|
||||
methods: HashMap::new(),
|
||||
fields: HashMap::new(),
|
||||
},
|
||||
parents: vec![foo1],
|
||||
});
|
||||
|
||||
let bar = ctx.add_class(ClassDef {
|
||||
base: TypeDef {
|
||||
name: "bar",
|
||||
methods: HashMap::new(),
|
||||
fields: HashMap::new(),
|
||||
},
|
||||
parents: vec![],
|
||||
});
|
||||
|
||||
ctx.add_fn(
|
||||
"foo",
|
||||
FnDef {
|
||||
args: vec![VirtualClassType(foo).into()],
|
||||
result: None,
|
||||
},
|
||||
);
|
||||
ctx.add_fn(
|
||||
"foo1",
|
||||
FnDef {
|
||||
args: vec![VirtualClassType(foo1).into()],
|
||||
result: None,
|
||||
},
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[ClassType(foo).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[ClassType(foo1).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[ClassType(foo2).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[ClassType(bar).into()]),
|
||||
Err("not subtype".to_string())
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo1", &[ClassType(foo1).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo1", &[ClassType(foo2).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo1", &[ClassType(foo).into()]),
|
||||
Err("not subtype".to_string())
|
||||
);
|
||||
|
||||
// virtual class substitution
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[VirtualClassType(foo).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[VirtualClassType(foo1).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[VirtualClassType(foo2).into()]),
|
||||
Ok(None)
|
||||
);
|
||||
assert_eq!(
|
||||
resolve_call(&ctx, None, "foo", &[VirtualClassType(bar).into()]),
|
||||
Err("not subtype".to_string())
|
||||
);
|
||||
}
|
||||
}
|
@ -1,28 +1,33 @@
|
||||
extern crate num_bigint;
|
||||
extern crate inkwell;
|
||||
extern crate num_bigint;
|
||||
extern crate rustpython_parser;
|
||||
|
||||
pub mod expression;
|
||||
pub mod inference;
|
||||
mod operators;
|
||||
pub mod primitives;
|
||||
pub mod typedef;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::path::Path;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use num_traits::cast::ToPrimitive;
|
||||
|
||||
use rustpython_parser::ast;
|
||||
|
||||
use inkwell::OptimizationLevel;
|
||||
use inkwell::basic_block;
|
||||
use inkwell::builder::Builder;
|
||||
use inkwell::context::Context;
|
||||
use inkwell::module::Module;
|
||||
use inkwell::passes;
|
||||
use inkwell::targets::*;
|
||||
use inkwell::types;
|
||||
use inkwell::types::BasicType;
|
||||
use inkwell::values;
|
||||
use inkwell::{IntPredicate, FloatPredicate};
|
||||
use inkwell::basic_block;
|
||||
use inkwell::passes;
|
||||
|
||||
use inkwell::OptimizationLevel;
|
||||
use inkwell::{FloatPredicate, IntPredicate};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum CompileErrorKind {
|
||||
@ -32,26 +37,25 @@ enum CompileErrorKind {
|
||||
IncompatibleTypes,
|
||||
UnboundIdentifier,
|
||||
BreakOutsideLoop,
|
||||
Internal(&'static str)
|
||||
Internal(&'static str),
|
||||
}
|
||||
|
||||
impl fmt::Display for CompileErrorKind {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
CompileErrorKind::Unsupported(feature)
|
||||
=> write!(f, "The following Python feature is not supported by NAC3: {}", feature),
|
||||
CompileErrorKind::MissingTypeAnnotation
|
||||
=> write!(f, "Missing type annotation"),
|
||||
CompileErrorKind::UnknownTypeAnnotation
|
||||
=> write!(f, "Unknown type annotation"),
|
||||
CompileErrorKind::IncompatibleTypes
|
||||
=> write!(f, "Incompatible types"),
|
||||
CompileErrorKind::UnboundIdentifier
|
||||
=> write!(f, "Unbound identifier"),
|
||||
CompileErrorKind::BreakOutsideLoop
|
||||
=> write!(f, "Break outside loop"),
|
||||
CompileErrorKind::Internal(details)
|
||||
=> write!(f, "Internal compiler error: {}", details),
|
||||
CompileErrorKind::Unsupported(feature) => write!(
|
||||
f,
|
||||
"The following Python feature is not supported by NAC3: {}",
|
||||
feature
|
||||
),
|
||||
CompileErrorKind::MissingTypeAnnotation => write!(f, "Missing type annotation"),
|
||||
CompileErrorKind::UnknownTypeAnnotation => write!(f, "Unknown type annotation"),
|
||||
CompileErrorKind::IncompatibleTypes => write!(f, "Incompatible types"),
|
||||
CompileErrorKind::UnboundIdentifier => write!(f, "Unbound identifier"),
|
||||
CompileErrorKind::BreakOutsideLoop => write!(f, "Break outside loop"),
|
||||
CompileErrorKind::Internal(details) => {
|
||||
write!(f, "Internal compiler error: {}", details)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -102,7 +106,9 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
module.add_function("output", fn_type, None);
|
||||
|
||||
CodeGen {
|
||||
context, module, pass_manager,
|
||||
context,
|
||||
module,
|
||||
pass_manager,
|
||||
builder: context.create_builder(),
|
||||
current_source_location: ast::Location::default(),
|
||||
namespace: HashMap::new(),
|
||||
@ -117,7 +123,7 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
fn compile_error(&self, kind: CompileErrorKind) -> CompileError {
|
||||
CompileError {
|
||||
location: self.current_source_location,
|
||||
kind
|
||||
kind,
|
||||
}
|
||||
}
|
||||
|
||||
@ -128,7 +134,7 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
"int64" => Ok(self.context.i64_type().into()),
|
||||
"float32" => Ok(self.context.f32_type().into()),
|
||||
"float64" => Ok(self.context.f64_type().into()),
|
||||
_ => Err(self.compile_error(CompileErrorKind::UnknownTypeAnnotation))
|
||||
_ => Err(self.compile_error(CompileErrorKind::UnknownTypeAnnotation)),
|
||||
}
|
||||
}
|
||||
|
||||
@ -142,37 +148,53 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
is_async: bool,
|
||||
) -> CompileResult<values::FunctionValue<'ctx>> {
|
||||
if is_async {
|
||||
return Err(self.compile_error(CompileErrorKind::Unsupported("async functions")))
|
||||
return Err(self.compile_error(CompileErrorKind::Unsupported("async functions")));
|
||||
}
|
||||
for decorator in decorator_list.iter() {
|
||||
self.set_source_location(decorator.location);
|
||||
if let ast::ExpressionType::Identifier { name } = &decorator.node {
|
||||
if name != "kernel" && name != "portable" {
|
||||
return Err(self.compile_error(CompileErrorKind::Unsupported("custom decorators")))
|
||||
return Err(
|
||||
self.compile_error(CompileErrorKind::Unsupported("custom decorators"))
|
||||
);
|
||||
}
|
||||
} else {
|
||||
return Err(self.compile_error(CompileErrorKind::Unsupported("decorator must be an identifier")))
|
||||
return Err(self.compile_error(CompileErrorKind::Unsupported(
|
||||
"decorator must be an identifier",
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
let args_type = args.args.iter().map(|val| {
|
||||
self.set_source_location(val.location);
|
||||
if let Some(annotation) = &val.annotation {
|
||||
if let ast::ExpressionType::Identifier { name } = &annotation.node {
|
||||
Ok(self.get_basic_type(&name)?)
|
||||
let args_type = args
|
||||
.args
|
||||
.iter()
|
||||
.map(|val| {
|
||||
self.set_source_location(val.location);
|
||||
if let Some(annotation) = &val.annotation {
|
||||
if let ast::ExpressionType::Identifier { name } = &annotation.node {
|
||||
Ok(self.get_basic_type(&name)?)
|
||||
} else {
|
||||
Err(self.compile_error(CompileErrorKind::Unsupported(
|
||||
"type annotation must be an identifier",
|
||||
)))
|
||||
}
|
||||
} else {
|
||||
Err(self.compile_error(CompileErrorKind::Unsupported("type annotation must be an identifier")))
|
||||
Err(self.compile_error(CompileErrorKind::MissingTypeAnnotation))
|
||||
}
|
||||
} else {
|
||||
Err(self.compile_error(CompileErrorKind::MissingTypeAnnotation))
|
||||
}
|
||||
}).collect::<CompileResult<Vec<types::BasicTypeEnum>>>()?;
|
||||
})
|
||||
.collect::<CompileResult<Vec<types::BasicTypeEnum>>>()?;
|
||||
let return_type = if let Some(returns) = returns {
|
||||
self.set_source_location(returns.location);
|
||||
if let ast::ExpressionType::Identifier { name } = &returns.node {
|
||||
if name == "None" { None } else { Some(self.get_basic_type(name)?) }
|
||||
if name == "None" {
|
||||
None
|
||||
} else {
|
||||
Some(self.get_basic_type(name)?)
|
||||
}
|
||||
} else {
|
||||
return Err(self.compile_error(CompileErrorKind::Unsupported("type annotation must be an identifier")))
|
||||
return Err(self.compile_error(CompileErrorKind::Unsupported(
|
||||
"type annotation must be an identifier",
|
||||
)));
|
||||
}
|
||||
} else {
|
||||
None
|
||||
@ -180,7 +202,7 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
|
||||
let fn_type = match return_type {
|
||||
Some(ty) => ty.fn_type(&args_type, false),
|
||||
None => self.context.void_type().fn_type(&args_type, false)
|
||||
None => self.context.void_type().fn_type(&args_type, false),
|
||||
};
|
||||
|
||||
let function = self.module.add_function(name, fn_type, None);
|
||||
@ -201,57 +223,74 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
|
||||
fn compile_expression(
|
||||
&mut self,
|
||||
expression: &ast::Expression
|
||||
expression: &ast::Expression,
|
||||
) -> CompileResult<values::BasicValueEnum<'ctx>> {
|
||||
self.set_source_location(expression.location);
|
||||
|
||||
match &expression.node {
|
||||
ast::ExpressionType::True => Ok(self.context.bool_type().const_int(1, false).into()),
|
||||
ast::ExpressionType::False => Ok(self.context.bool_type().const_int(0, false).into()),
|
||||
ast::ExpressionType::Number { value: ast::Number::Integer { value } } => {
|
||||
ast::ExpressionType::Number {
|
||||
value: ast::Number::Integer { value },
|
||||
} => {
|
||||
let mut bits = value.bits();
|
||||
if value.sign() == num_bigint::Sign::Minus {
|
||||
bits += 1;
|
||||
}
|
||||
match bits {
|
||||
0..=32 => Ok(self.context.i32_type().const_int(value.to_i32().unwrap() as _, true).into()),
|
||||
33..=64 => Ok(self.context.i64_type().const_int(value.to_i64().unwrap() as _, true).into()),
|
||||
_ => Err(self.compile_error(CompileErrorKind::Unsupported("integers larger than 64 bits")))
|
||||
}
|
||||
},
|
||||
ast::ExpressionType::Number { value: ast::Number::Float { value } } => {
|
||||
Ok(self.context.f64_type().const_float(*value).into())
|
||||
},
|
||||
ast::ExpressionType::Identifier { name } => {
|
||||
match self.namespace.get(name) {
|
||||
Some(value) => Ok(self.builder.build_load(*value, name).into()),
|
||||
None => Err(self.compile_error(CompileErrorKind::UnboundIdentifier))
|
||||
0..=32 => Ok(self
|
||||
.context
|
||||
.i32_type()
|
||||
.const_int(value.to_i32().unwrap() as _, true)
|
||||
.into()),
|
||||
33..=64 => Ok(self
|
||||
.context
|
||||
.i64_type()
|
||||
.const_int(value.to_i64().unwrap() as _, true)
|
||||
.into()),
|
||||
_ => Err(self.compile_error(CompileErrorKind::Unsupported(
|
||||
"integers larger than 64 bits",
|
||||
))),
|
||||
}
|
||||
}
|
||||
ast::ExpressionType::Number {
|
||||
value: ast::Number::Float { value },
|
||||
} => Ok(self.context.f64_type().const_float(*value).into()),
|
||||
ast::ExpressionType::Identifier { name } => match self.namespace.get(name) {
|
||||
Some(value) => Ok(self.builder.build_load(*value, name).into()),
|
||||
None => Err(self.compile_error(CompileErrorKind::UnboundIdentifier)),
|
||||
},
|
||||
ast::ExpressionType::Unop { op, a } => {
|
||||
let a = self.compile_expression(&a)?;
|
||||
match (op, a) {
|
||||
(ast::UnaryOperator::Pos, values::BasicValueEnum::IntValue(a))
|
||||
=> Ok(a.into()),
|
||||
(ast::UnaryOperator::Pos, values::BasicValueEnum::FloatValue(a))
|
||||
=> Ok(a.into()),
|
||||
(ast::UnaryOperator::Neg, values::BasicValueEnum::IntValue(a))
|
||||
=> Ok(self.builder.build_int_neg(a, "tmpneg").into()),
|
||||
(ast::UnaryOperator::Neg, values::BasicValueEnum::FloatValue(a))
|
||||
=> Ok(self.builder.build_float_neg(a, "tmpneg").into()),
|
||||
(ast::UnaryOperator::Inv, values::BasicValueEnum::IntValue(a))
|
||||
=> Ok(self.builder.build_not(a, "tmpnot").into()),
|
||||
(ast::UnaryOperator::Pos, values::BasicValueEnum::IntValue(a)) => Ok(a.into()),
|
||||
(ast::UnaryOperator::Pos, values::BasicValueEnum::FloatValue(a)) => {
|
||||
Ok(a.into())
|
||||
}
|
||||
(ast::UnaryOperator::Neg, values::BasicValueEnum::IntValue(a)) => {
|
||||
Ok(self.builder.build_int_neg(a, "tmpneg").into())
|
||||
}
|
||||
(ast::UnaryOperator::Neg, values::BasicValueEnum::FloatValue(a)) => {
|
||||
Ok(self.builder.build_float_neg(a, "tmpneg").into())
|
||||
}
|
||||
(ast::UnaryOperator::Inv, values::BasicValueEnum::IntValue(a)) => {
|
||||
Ok(self.builder.build_not(a, "tmpnot").into())
|
||||
}
|
||||
(ast::UnaryOperator::Not, values::BasicValueEnum::IntValue(a)) => {
|
||||
// boolean "not"
|
||||
if a.get_type().get_bit_width() != 1 {
|
||||
Err(self.compile_error(CompileErrorKind::Unsupported("unimplemented unary operation")))
|
||||
Err(self.compile_error(CompileErrorKind::Unsupported(
|
||||
"unimplemented unary operation",
|
||||
)))
|
||||
} else {
|
||||
Ok(self.builder.build_not(a, "tmpnot").into())
|
||||
}
|
||||
},
|
||||
_ => Err(self.compile_error(CompileErrorKind::Unsupported("unimplemented unary operation"))),
|
||||
}
|
||||
_ => Err(self.compile_error(CompileErrorKind::Unsupported(
|
||||
"unimplemented unary operation",
|
||||
))),
|
||||
}
|
||||
},
|
||||
}
|
||||
ast::ExpressionType::Binop { a, op, b } => {
|
||||
let a = self.compile_expression(&a)?;
|
||||
let b = self.compile_expression(&b)?;
|
||||
@ -260,27 +299,53 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
}
|
||||
use ast::Operator::*;
|
||||
match (op, a, b) {
|
||||
(Add, values::BasicValueEnum::IntValue(a), values::BasicValueEnum::IntValue(b))
|
||||
=> Ok(self.builder.build_int_add(a, b, "tmpadd").into()),
|
||||
(Sub, values::BasicValueEnum::IntValue(a), values::BasicValueEnum::IntValue(b))
|
||||
=> Ok(self.builder.build_int_sub(a, b, "tmpsub").into()),
|
||||
(Mult, values::BasicValueEnum::IntValue(a), values::BasicValueEnum::IntValue(b))
|
||||
=> Ok(self.builder.build_int_mul(a, b, "tmpmul").into()),
|
||||
(
|
||||
Add,
|
||||
values::BasicValueEnum::IntValue(a),
|
||||
values::BasicValueEnum::IntValue(b),
|
||||
) => Ok(self.builder.build_int_add(a, b, "tmpadd").into()),
|
||||
(
|
||||
Sub,
|
||||
values::BasicValueEnum::IntValue(a),
|
||||
values::BasicValueEnum::IntValue(b),
|
||||
) => Ok(self.builder.build_int_sub(a, b, "tmpsub").into()),
|
||||
(
|
||||
Mult,
|
||||
values::BasicValueEnum::IntValue(a),
|
||||
values::BasicValueEnum::IntValue(b),
|
||||
) => Ok(self.builder.build_int_mul(a, b, "tmpmul").into()),
|
||||
|
||||
(Add, values::BasicValueEnum::FloatValue(a), values::BasicValueEnum::FloatValue(b))
|
||||
=> Ok(self.builder.build_float_add(a, b, "tmpadd").into()),
|
||||
(Sub, values::BasicValueEnum::FloatValue(a), values::BasicValueEnum::FloatValue(b))
|
||||
=> Ok(self.builder.build_float_sub(a, b, "tmpsub").into()),
|
||||
(Mult, values::BasicValueEnum::FloatValue(a), values::BasicValueEnum::FloatValue(b))
|
||||
=> Ok(self.builder.build_float_mul(a, b, "tmpmul").into()),
|
||||
(
|
||||
Add,
|
||||
values::BasicValueEnum::FloatValue(a),
|
||||
values::BasicValueEnum::FloatValue(b),
|
||||
) => Ok(self.builder.build_float_add(a, b, "tmpadd").into()),
|
||||
(
|
||||
Sub,
|
||||
values::BasicValueEnum::FloatValue(a),
|
||||
values::BasicValueEnum::FloatValue(b),
|
||||
) => Ok(self.builder.build_float_sub(a, b, "tmpsub").into()),
|
||||
(
|
||||
Mult,
|
||||
values::BasicValueEnum::FloatValue(a),
|
||||
values::BasicValueEnum::FloatValue(b),
|
||||
) => Ok(self.builder.build_float_mul(a, b, "tmpmul").into()),
|
||||
|
||||
(Div, values::BasicValueEnum::FloatValue(a), values::BasicValueEnum::FloatValue(b))
|
||||
=> Ok(self.builder.build_float_div(a, b, "tmpdiv").into()),
|
||||
(FloorDiv, values::BasicValueEnum::IntValue(a), values::BasicValueEnum::IntValue(b))
|
||||
=> Ok(self.builder.build_int_signed_div(a, b, "tmpdiv").into()),
|
||||
_ => Err(self.compile_error(CompileErrorKind::Unsupported("unimplemented binary operation"))),
|
||||
(
|
||||
Div,
|
||||
values::BasicValueEnum::FloatValue(a),
|
||||
values::BasicValueEnum::FloatValue(b),
|
||||
) => Ok(self.builder.build_float_div(a, b, "tmpdiv").into()),
|
||||
(
|
||||
FloorDiv,
|
||||
values::BasicValueEnum::IntValue(a),
|
||||
values::BasicValueEnum::IntValue(b),
|
||||
) => Ok(self.builder.build_int_signed_div(a, b, "tmpdiv").into()),
|
||||
_ => Err(self.compile_error(CompileErrorKind::Unsupported(
|
||||
"unimplemented binary operation",
|
||||
))),
|
||||
}
|
||||
},
|
||||
}
|
||||
ast::ExpressionType::Compare { vals, ops } => {
|
||||
let mut vals = vals.iter();
|
||||
let mut ops = ops.iter();
|
||||
@ -293,43 +358,93 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
if a.get_type() != b.get_type() {
|
||||
return Err(self.compile_error(CompileErrorKind::IncompatibleTypes));
|
||||
}
|
||||
let this_result = match (a, b) {
|
||||
(values::BasicValueEnum::IntValue(a), values::BasicValueEnum::IntValue(b)) => {
|
||||
match op {
|
||||
ast::Comparison::Equal
|
||||
=> self.builder.build_int_compare(IntPredicate::EQ, a, b, "tmpeq"),
|
||||
ast::Comparison::NotEqual
|
||||
=> self.builder.build_int_compare(IntPredicate::NE, a, b, "tmpne"),
|
||||
ast::Comparison::Less
|
||||
=> self.builder.build_int_compare(IntPredicate::SLT, a, b, "tmpslt"),
|
||||
ast::Comparison::LessOrEqual
|
||||
=> self.builder.build_int_compare(IntPredicate::SLE, a, b, "tmpsle"),
|
||||
ast::Comparison::Greater
|
||||
=> self.builder.build_int_compare(IntPredicate::SGT, a, b, "tmpsgt"),
|
||||
ast::Comparison::GreaterOrEqual
|
||||
=> self.builder.build_int_compare(IntPredicate::SGE, a, b, "tmpsge"),
|
||||
_ => return Err(self.compile_error(CompileErrorKind::Unsupported("special comparison"))),
|
||||
let this_result =
|
||||
match (a, b) {
|
||||
(
|
||||
values::BasicValueEnum::IntValue(a),
|
||||
values::BasicValueEnum::IntValue(b),
|
||||
) => {
|
||||
match op {
|
||||
ast::Comparison::Equal => self.builder.build_int_compare(
|
||||
IntPredicate::EQ,
|
||||
a,
|
||||
b,
|
||||
"tmpeq",
|
||||
),
|
||||
ast::Comparison::NotEqual => self
|
||||
.builder
|
||||
.build_int_compare(IntPredicate::NE, a, b, "tmpne"),
|
||||
ast::Comparison::Less => self.builder.build_int_compare(
|
||||
IntPredicate::SLT,
|
||||
a,
|
||||
b,
|
||||
"tmpslt",
|
||||
),
|
||||
ast::Comparison::LessOrEqual => self
|
||||
.builder
|
||||
.build_int_compare(IntPredicate::SLE, a, b, "tmpsle"),
|
||||
ast::Comparison::Greater => self.builder.build_int_compare(
|
||||
IntPredicate::SGT,
|
||||
a,
|
||||
b,
|
||||
"tmpsgt",
|
||||
),
|
||||
ast::Comparison::GreaterOrEqual => self
|
||||
.builder
|
||||
.build_int_compare(IntPredicate::SGE, a, b, "tmpsge"),
|
||||
_ => {
|
||||
return Err(self.compile_error(
|
||||
CompileErrorKind::Unsupported("special comparison"),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
(values::BasicValueEnum::FloatValue(a), values::BasicValueEnum::FloatValue(b)) => {
|
||||
match op {
|
||||
ast::Comparison::Equal
|
||||
=> self.builder.build_float_compare(FloatPredicate::OEQ, a, b, "tmpoeq"),
|
||||
ast::Comparison::NotEqual
|
||||
=> self.builder.build_float_compare(FloatPredicate::UNE, a, b, "tmpune"),
|
||||
ast::Comparison::Less
|
||||
=> self.builder.build_float_compare(FloatPredicate::OLT, a, b, "tmpolt"),
|
||||
ast::Comparison::LessOrEqual
|
||||
=> self.builder.build_float_compare(FloatPredicate::OLE, a, b, "tmpole"),
|
||||
ast::Comparison::Greater
|
||||
=> self.builder.build_float_compare(FloatPredicate::OGT, a, b, "tmpogt"),
|
||||
ast::Comparison::GreaterOrEqual
|
||||
=> self.builder.build_float_compare(FloatPredicate::OGE, a, b, "tmpoge"),
|
||||
_ => return Err(self.compile_error(CompileErrorKind::Unsupported("special comparison"))),
|
||||
(
|
||||
values::BasicValueEnum::FloatValue(a),
|
||||
values::BasicValueEnum::FloatValue(b),
|
||||
) => match op {
|
||||
ast::Comparison::Equal => self.builder.build_float_compare(
|
||||
FloatPredicate::OEQ,
|
||||
a,
|
||||
b,
|
||||
"tmpoeq",
|
||||
),
|
||||
ast::Comparison::NotEqual => self.builder.build_float_compare(
|
||||
FloatPredicate::UNE,
|
||||
a,
|
||||
b,
|
||||
"tmpune",
|
||||
),
|
||||
ast::Comparison::Less => self.builder.build_float_compare(
|
||||
FloatPredicate::OLT,
|
||||
a,
|
||||
b,
|
||||
"tmpolt",
|
||||
),
|
||||
ast::Comparison::LessOrEqual => self
|
||||
.builder
|
||||
.build_float_compare(FloatPredicate::OLE, a, b, "tmpole"),
|
||||
ast::Comparison::Greater => self.builder.build_float_compare(
|
||||
FloatPredicate::OGT,
|
||||
a,
|
||||
b,
|
||||
"tmpogt",
|
||||
),
|
||||
ast::Comparison::GreaterOrEqual => self
|
||||
.builder
|
||||
.build_float_compare(FloatPredicate::OGE, a, b, "tmpoge"),
|
||||
_ => {
|
||||
return Err(self.compile_error(
|
||||
CompileErrorKind::Unsupported("special comparison"),
|
||||
))
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
return Err(self.compile_error(CompileErrorKind::Unsupported(
|
||||
"comparison of non-numerical types",
|
||||
)))
|
||||
}
|
||||
},
|
||||
_ => return Err(self.compile_error(CompileErrorKind::Unsupported("comparison of non-numerical types"))),
|
||||
};
|
||||
};
|
||||
match result {
|
||||
Some(last) => {
|
||||
result = Some(self.builder.build_and(last, this_result, "tmpand"));
|
||||
@ -340,15 +455,23 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
}
|
||||
a = b;
|
||||
} else {
|
||||
return Ok(result.unwrap().into())
|
||||
return Ok(result.unwrap().into());
|
||||
}
|
||||
}
|
||||
},
|
||||
ast::ExpressionType::Call { function, args, keywords } => {
|
||||
}
|
||||
ast::ExpressionType::Call {
|
||||
function,
|
||||
args,
|
||||
keywords,
|
||||
} => {
|
||||
if !keywords.is_empty() {
|
||||
return Err(self.compile_error(CompileErrorKind::Unsupported("keyword arguments")))
|
||||
return Err(
|
||||
self.compile_error(CompileErrorKind::Unsupported("keyword arguments"))
|
||||
);
|
||||
}
|
||||
let args = args.iter().map(|val| self.compile_expression(val))
|
||||
let args = args
|
||||
.iter()
|
||||
.map(|val| self.compile_expression(val))
|
||||
.collect::<CompileResult<Vec<values::BasicValueEnum>>>()?;
|
||||
self.set_source_location(expression.location);
|
||||
if let ast::ExpressionType::Identifier { name } = &function.node {
|
||||
@ -356,67 +479,99 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
("int32", values::BasicValueEnum::IntValue(a)) => {
|
||||
let nbits = a.get_type().get_bit_width();
|
||||
if nbits < 32 {
|
||||
Ok(self.builder.build_int_s_extend(a, self.context.i32_type(), "tmpsext").into())
|
||||
Ok(self
|
||||
.builder
|
||||
.build_int_s_extend(a, self.context.i32_type(), "tmpsext")
|
||||
.into())
|
||||
} else if nbits > 32 {
|
||||
Ok(self.builder.build_int_truncate(a, self.context.i32_type(), "tmptrunc").into())
|
||||
Ok(self
|
||||
.builder
|
||||
.build_int_truncate(a, self.context.i32_type(), "tmptrunc")
|
||||
.into())
|
||||
} else {
|
||||
Ok(a.into())
|
||||
}
|
||||
},
|
||||
}
|
||||
("int64", values::BasicValueEnum::IntValue(a)) => {
|
||||
let nbits = a.get_type().get_bit_width();
|
||||
if nbits < 64 {
|
||||
Ok(self.builder.build_int_s_extend(a, self.context.i64_type(), "tmpsext").into())
|
||||
Ok(self
|
||||
.builder
|
||||
.build_int_s_extend(a, self.context.i64_type(), "tmpsext")
|
||||
.into())
|
||||
} else {
|
||||
Ok(a.into())
|
||||
}
|
||||
},
|
||||
("int32", values::BasicValueEnum::FloatValue(a)) => {
|
||||
Ok(self.builder.build_float_to_signed_int(a, self.context.i32_type(), "tmpfptosi").into())
|
||||
},
|
||||
("int64", values::BasicValueEnum::FloatValue(a)) => {
|
||||
Ok(self.builder.build_float_to_signed_int(a, self.context.i64_type(), "tmpfptosi").into())
|
||||
},
|
||||
("float32", values::BasicValueEnum::IntValue(a)) => {
|
||||
Ok(self.builder.build_signed_int_to_float(a, self.context.f32_type(), "tmpsitofp").into())
|
||||
},
|
||||
("float64", values::BasicValueEnum::IntValue(a)) => {
|
||||
Ok(self.builder.build_signed_int_to_float(a, self.context.f64_type(), "tmpsitofp").into())
|
||||
},
|
||||
}
|
||||
("int32", values::BasicValueEnum::FloatValue(a)) => Ok(self
|
||||
.builder
|
||||
.build_float_to_signed_int(a, self.context.i32_type(), "tmpfptosi")
|
||||
.into()),
|
||||
("int64", values::BasicValueEnum::FloatValue(a)) => Ok(self
|
||||
.builder
|
||||
.build_float_to_signed_int(a, self.context.i64_type(), "tmpfptosi")
|
||||
.into()),
|
||||
("float32", values::BasicValueEnum::IntValue(a)) => Ok(self
|
||||
.builder
|
||||
.build_signed_int_to_float(a, self.context.f32_type(), "tmpsitofp")
|
||||
.into()),
|
||||
("float64", values::BasicValueEnum::IntValue(a)) => Ok(self
|
||||
.builder
|
||||
.build_signed_int_to_float(a, self.context.f64_type(), "tmpsitofp")
|
||||
.into()),
|
||||
("float32", values::BasicValueEnum::FloatValue(a)) => {
|
||||
if a.get_type() == self.context.f64_type() {
|
||||
Ok(self.builder.build_float_trunc(a, self.context.f32_type(), "tmptrunc").into())
|
||||
Ok(self
|
||||
.builder
|
||||
.build_float_trunc(a, self.context.f32_type(), "tmptrunc")
|
||||
.into())
|
||||
} else {
|
||||
Ok(a.into())
|
||||
}
|
||||
},
|
||||
}
|
||||
("float64", values::BasicValueEnum::FloatValue(a)) => {
|
||||
if a.get_type() == self.context.f32_type() {
|
||||
Ok(self.builder.build_float_ext(a, self.context.f64_type(), "tmpext").into())
|
||||
Ok(self
|
||||
.builder
|
||||
.build_float_ext(a, self.context.f64_type(), "tmpext")
|
||||
.into())
|
||||
} else {
|
||||
Ok(a.into())
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
("output", values::BasicValueEnum::IntValue(a)) => {
|
||||
let fn_value = self.module.get_function("output").unwrap();
|
||||
Ok(self.builder.build_call(fn_value, &[a.into()], "call")
|
||||
.try_as_basic_value().left().unwrap())
|
||||
},
|
||||
_ => Err(self.compile_error(CompileErrorKind::Unsupported("unrecognized call")))
|
||||
Ok(self
|
||||
.builder
|
||||
.build_call(fn_value, &[a.into()], "call")
|
||||
.try_as_basic_value()
|
||||
.left()
|
||||
.unwrap())
|
||||
}
|
||||
_ => {
|
||||
Err(self
|
||||
.compile_error(CompileErrorKind::Unsupported("unrecognized call")))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(self.compile_error(CompileErrorKind::Unsupported("function must be an identifier")))
|
||||
return Err(self.compile_error(CompileErrorKind::Unsupported(
|
||||
"function must be an identifier",
|
||||
)));
|
||||
}
|
||||
},
|
||||
_ => return Err(self.compile_error(CompileErrorKind::Unsupported("unimplemented expression"))),
|
||||
}
|
||||
_ => {
|
||||
return Err(
|
||||
self.compile_error(CompileErrorKind::Unsupported("unimplemented expression"))
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn compile_statement(
|
||||
&mut self,
|
||||
statement: &ast::Statement,
|
||||
return_type: Option<types::BasicTypeEnum>
|
||||
return_type: Option<types::BasicTypeEnum>,
|
||||
) -> CompileResult<()> {
|
||||
self.set_source_location(statement.location);
|
||||
|
||||
@ -428,29 +583,43 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
self.set_source_location(target.location);
|
||||
if let ast::ExpressionType::Identifier { name } = &target.node {
|
||||
let builder = &self.builder;
|
||||
let target = self.namespace.entry(name.clone()).or_insert_with(
|
||||
|| builder.build_alloca(value.get_type(), name));
|
||||
if target.get_type() != value.get_type().ptr_type(inkwell::AddressSpace::Generic) {
|
||||
let target = self
|
||||
.namespace
|
||||
.entry(name.clone())
|
||||
.or_insert_with(|| builder.build_alloca(value.get_type(), name));
|
||||
if target.get_type()
|
||||
!= value.get_type().ptr_type(inkwell::AddressSpace::Generic)
|
||||
{
|
||||
return Err(self.compile_error(CompileErrorKind::IncompatibleTypes));
|
||||
}
|
||||
builder.build_store(*target, value);
|
||||
} else {
|
||||
return Err(self.compile_error(CompileErrorKind::Unsupported("assignment target must be an identifier")))
|
||||
return Err(self.compile_error(CompileErrorKind::Unsupported(
|
||||
"assignment target must be an identifier",
|
||||
)));
|
||||
}
|
||||
}
|
||||
},
|
||||
Expression { expression } => { self.compile_expression(expression)?; },
|
||||
}
|
||||
Expression { expression } => {
|
||||
self.compile_expression(expression)?;
|
||||
}
|
||||
If { test, body, orelse } => {
|
||||
let test = self.compile_expression(test)?;
|
||||
if test.get_type() != self.context.bool_type().into() {
|
||||
return Err(self.compile_error(CompileErrorKind::IncompatibleTypes));
|
||||
}
|
||||
|
||||
let parent = self.builder.get_insert_block().unwrap().get_parent().unwrap();
|
||||
let parent = self
|
||||
.builder
|
||||
.get_insert_block()
|
||||
.unwrap()
|
||||
.get_parent()
|
||||
.unwrap();
|
||||
let then_bb = self.context.append_basic_block(parent, "then");
|
||||
let else_bb = self.context.append_basic_block(parent, "else");
|
||||
let cont_bb = self.context.append_basic_block(parent, "ifcont");
|
||||
self.builder.build_conditional_branch(test.into_int_value(), then_bb, else_bb);
|
||||
self.builder
|
||||
.build_conditional_branch(test.into_int_value(), then_bb, else_bb);
|
||||
|
||||
self.builder.position_at_end(then_bb);
|
||||
self.compile_suite(body, return_type)?;
|
||||
@ -462,9 +631,14 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
}
|
||||
self.builder.build_unconditional_branch(cont_bb);
|
||||
self.builder.position_at_end(cont_bb);
|
||||
},
|
||||
}
|
||||
While { test, body, orelse } => {
|
||||
let parent = self.builder.get_insert_block().unwrap().get_parent().unwrap();
|
||||
let parent = self
|
||||
.builder
|
||||
.get_insert_block()
|
||||
.unwrap()
|
||||
.get_parent()
|
||||
.unwrap();
|
||||
let test_bb = self.context.append_basic_block(parent, "test");
|
||||
self.builder.build_unconditional_branch(test_bb);
|
||||
self.builder.position_at_end(test_bb);
|
||||
@ -476,7 +650,8 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
let then_bb = self.context.append_basic_block(parent, "then");
|
||||
let else_bb = self.context.append_basic_block(parent, "else");
|
||||
let cont_bb = self.context.append_basic_block(parent, "ifcont");
|
||||
self.builder.build_conditional_branch(test.into_int_value(), then_bb, else_bb);
|
||||
self.builder
|
||||
.build_conditional_branch(test.into_int_value(), then_bb, else_bb);
|
||||
|
||||
self.break_bb = Some(cont_bb);
|
||||
|
||||
@ -492,11 +667,16 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
self.builder.position_at_end(cont_bb);
|
||||
|
||||
self.break_bb = None;
|
||||
},
|
||||
}
|
||||
Break => {
|
||||
if let Some(bb) = self.break_bb {
|
||||
self.builder.build_unconditional_branch(bb);
|
||||
let parent = self.builder.get_insert_block().unwrap().get_parent().unwrap();
|
||||
let parent = self
|
||||
.builder
|
||||
.get_insert_block()
|
||||
.unwrap()
|
||||
.get_parent()
|
||||
.unwrap();
|
||||
let unreachable_bb = self.context.append_basic_block(parent, "unreachable");
|
||||
self.builder.position_at_end(unreachable_bb);
|
||||
} else {
|
||||
@ -513,13 +693,13 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
} else {
|
||||
return Err(self.compile_error(CompileErrorKind::IncompatibleTypes));
|
||||
}
|
||||
},
|
||||
}
|
||||
Return { value: None } => {
|
||||
if !return_type.is_none() {
|
||||
return Err(self.compile_error(CompileErrorKind::IncompatibleTypes));
|
||||
}
|
||||
self.builder.build_return(None);
|
||||
},
|
||||
}
|
||||
Pass => (),
|
||||
_ => return Err(self.compile_error(CompileErrorKind::Unsupported("special statement"))),
|
||||
}
|
||||
@ -529,7 +709,7 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
fn compile_suite(
|
||||
&mut self,
|
||||
suite: &ast::Suite,
|
||||
return_type: Option<types::BasicTypeEnum>
|
||||
return_type: Option<types::BasicTypeEnum>,
|
||||
) -> CompileResult<()> {
|
||||
for statement in suite.iter() {
|
||||
self.compile_statement(statement, return_type)?;
|
||||
@ -540,18 +720,22 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
pub fn compile_toplevel(&mut self, statement: &ast::Statement) -> CompileResult<()> {
|
||||
self.set_source_location(statement.location);
|
||||
if let ast::StatementType::FunctionDef {
|
||||
is_async,
|
||||
name,
|
||||
args,
|
||||
body,
|
||||
decorator_list,
|
||||
returns,
|
||||
} = &statement.node {
|
||||
let function = self.compile_function_def(name, args, body, decorator_list, returns, *is_async)?;
|
||||
is_async,
|
||||
name,
|
||||
args,
|
||||
body,
|
||||
decorator_list,
|
||||
returns,
|
||||
} = &statement.node
|
||||
{
|
||||
let function =
|
||||
self.compile_function_def(name, args, body, decorator_list, returns, *is_async)?;
|
||||
self.pass_manager.run_on(&function);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(self.compile_error(CompileErrorKind::Internal("top-level is not a function definition")))
|
||||
Err(self.compile_error(CompileErrorKind::Internal(
|
||||
"top-level is not a function definition",
|
||||
)))
|
||||
}
|
||||
}
|
||||
|
||||
@ -562,8 +746,8 @@ impl<'ctx> CodeGen<'ctx> {
|
||||
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)
|
||||
.expect("couldn't create target from target triple");
|
||||
let target =
|
||||
Target::from_triple(&triple).expect("couldn't create target from target triple");
|
||||
|
||||
let target_machine = target
|
||||
.create_target_machine(
|
||||
|
58
nac3core/src/operators.rs
Normal file
58
nac3core/src/operators.rs
Normal file
@ -0,0 +1,58 @@
|
||||
use rustpython_parser::ast::{Comparison, Operator, UnaryOperator};
|
||||
|
||||
pub fn binop_name(op: &Operator) -> &'static str {
|
||||
match op {
|
||||
Operator::Add => "__add__",
|
||||
Operator::Sub => "__sub__",
|
||||
Operator::Div => "__truediv__",
|
||||
Operator::Mod => "__mod__",
|
||||
Operator::Mult => "__mul__",
|
||||
Operator::Pow => "__pow__",
|
||||
Operator::BitOr => "__or__",
|
||||
Operator::BitXor => "__xor__",
|
||||
Operator::BitAnd => "__and__",
|
||||
Operator::LShift => "__lshift__",
|
||||
Operator::RShift => "__rshift__",
|
||||
Operator::FloorDiv => "__floordiv__",
|
||||
Operator::MatMult => "__matmul__",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn binop_assign_name(op: &Operator) -> &'static str {
|
||||
match op {
|
||||
Operator::Add => "__iadd__",
|
||||
Operator::Sub => "__isub__",
|
||||
Operator::Div => "__itruediv__",
|
||||
Operator::Mod => "__imod__",
|
||||
Operator::Mult => "__imul__",
|
||||
Operator::Pow => "__ipow__",
|
||||
Operator::BitOr => "__ior__",
|
||||
Operator::BitXor => "__ixor__",
|
||||
Operator::BitAnd => "__iand__",
|
||||
Operator::LShift => "__ilshift__",
|
||||
Operator::RShift => "__irshift__",
|
||||
Operator::FloorDiv => "__ifloordiv__",
|
||||
Operator::MatMult => "__imatmul__",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unaryop_name(op: &UnaryOperator) -> &'static str {
|
||||
match op {
|
||||
UnaryOperator::Pos => "__pos__",
|
||||
UnaryOperator::Neg => "__neg__",
|
||||
UnaryOperator::Not => "__not__",
|
||||
UnaryOperator::Inv => "__inv__",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn comparison_name(op: &Comparison) -> Option<&'static str> {
|
||||
match op {
|
||||
Comparison::Less => Some("__lt__"),
|
||||
Comparison::LessOrEqual => Some("__le__"),
|
||||
Comparison::Greater => Some("__gt__"),
|
||||
Comparison::GreaterOrEqual => Some("__ge__"),
|
||||
Comparison::Equal => Some("__eq__"),
|
||||
Comparison::NotEqual => Some("__ne__"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
178
nac3core/src/primitives.rs
Normal file
178
nac3core/src/primitives.rs
Normal file
@ -0,0 +1,178 @@
|
||||
use super::typedef::{Type::*, *};
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
|
||||
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: &Rc<Type>) {
|
||||
let result = Some(ty.clone());
|
||||
let fun = FnDef {
|
||||
args: vec![ty.clone()],
|
||||
result,
|
||||
};
|
||||
def.methods.insert("__add__", fun.clone());
|
||||
def.methods.insert("__sub__", fun.clone());
|
||||
def.methods.insert("__mul__", fun.clone());
|
||||
def.methods.insert("__neg__", fun.clone());
|
||||
def.methods.insert(
|
||||
"__truediv__",
|
||||
FnDef {
|
||||
args: vec![ty.clone()],
|
||||
result: Some(PrimitiveType(FLOAT_TYPE).into()),
|
||||
},
|
||||
);
|
||||
def.methods.insert("__floordiv__", fun.clone());
|
||||
def.methods.insert("__mod__", fun.clone());
|
||||
def.methods.insert("__pow__", fun.clone());
|
||||
}
|
||||
|
||||
fn impl_bits(def: &mut TypeDef, ty: &Rc<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.clone());
|
||||
def.methods.insert(
|
||||
"__xor__",
|
||||
FnDef {
|
||||
args: vec![ty.clone()],
|
||||
result: Some(ty.clone()),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn impl_eq(def: &mut TypeDef, ty: &Rc<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.clone());
|
||||
}
|
||||
|
||||
fn impl_order(def: &mut TypeDef, ty: &Rc<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.clone());
|
||||
}
|
||||
|
||||
pub fn basic_ctx() -> GlobalContext<'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 = GlobalContext::new(primitives);
|
||||
|
||||
let b_def = ctx.get_primitive_mut(BOOL_TYPE);
|
||||
let b = PrimitiveType(BOOL_TYPE).into();
|
||||
impl_eq(b_def, &b);
|
||||
let int32_def = ctx.get_primitive_mut(INT32_TYPE);
|
||||
let int32 = PrimitiveType(INT32_TYPE).into();
|
||||
impl_math(int32_def, &int32);
|
||||
impl_bits(int32_def, &int32);
|
||||
impl_order(int32_def, &int32);
|
||||
impl_eq(int32_def, &int32);
|
||||
let int64_def = ctx.get_primitive_mut(INT64_TYPE);
|
||||
let int64 = PrimitiveType(INT64_TYPE).into();
|
||||
impl_math(int64_def, &int64);
|
||||
impl_bits(int64_def, &int64);
|
||||
impl_order(int64_def, &int64);
|
||||
impl_eq(int64_def, &int64);
|
||||
let float_def = ctx.get_primitive_mut(FLOAT_TYPE);
|
||||
let float = PrimitiveType(FLOAT_TYPE).into();
|
||||
impl_math(float_def, &float);
|
||||
impl_order(float_def, &float);
|
||||
impl_eq(float_def, &float);
|
||||
|
||||
let t = ctx.add_variable_private(VarDef {
|
||||
name: "T",
|
||||
bound: vec![],
|
||||
});
|
||||
|
||||
ctx.add_parametric(ParametricDef {
|
||||
base: TypeDef {
|
||||
name: "tuple",
|
||||
fields: HashMap::new(),
|
||||
methods: HashMap::new(),
|
||||
},
|
||||
// we have nothing for tuple, so no param def
|
||||
params: vec![],
|
||||
});
|
||||
|
||||
ctx.add_parametric(ParametricDef {
|
||||
base: TypeDef {
|
||||
name: "list",
|
||||
fields: HashMap::new(),
|
||||
methods: HashMap::new(),
|
||||
},
|
||||
params: vec![t],
|
||||
});
|
||||
|
||||
let i = ctx.add_variable_private(VarDef {
|
||||
name: "I",
|
||||
bound: vec![
|
||||
PrimitiveType(INT32_TYPE).into(),
|
||||
PrimitiveType(INT64_TYPE).into(),
|
||||
PrimitiveType(FLOAT_TYPE).into(),
|
||||
],
|
||||
});
|
||||
let args = vec![TypeVariable(i).into()];
|
||||
ctx.add_fn(
|
||||
"int32",
|
||||
FnDef {
|
||||
args: args.clone(),
|
||||
result: Some(PrimitiveType(INT32_TYPE).into()),
|
||||
},
|
||||
);
|
||||
ctx.add_fn(
|
||||
"int64",
|
||||
FnDef {
|
||||
args: args.clone(),
|
||||
result: Some(PrimitiveType(INT64_TYPE).into()),
|
||||
},
|
||||
);
|
||||
ctx.add_fn(
|
||||
"float",
|
||||
FnDef {
|
||||
args: args.clone(),
|
||||
result: Some(PrimitiveType(FLOAT_TYPE).into()),
|
||||
},
|
||||
);
|
||||
|
||||
ctx
|
||||
}
|
223
nac3core/src/typedef.rs
Normal file
223
nac3core/src/typedef.rs
Normal file
@ -0,0 +1,223 @@
|
||||
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 Type {
|
||||
BotType,
|
||||
SelfType,
|
||||
PrimitiveType(PrimitiveId),
|
||||
ClassType(ClassId),
|
||||
VirtualClassType(ClassId),
|
||||
ParametricType(ParamId, Vec<Rc<Type>>),
|
||||
TypeVariable(VariableId),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FnDef {
|
||||
// we assume methods first argument to be SelfType,
|
||||
// so the first argument is not contained here
|
||||
pub args: Vec<Rc<Type>>,
|
||||
pub result: Option<Rc<Type>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TypeDef<'a> {
|
||||
pub name: &'a str,
|
||||
pub fields: HashMap<&'a str, Rc<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<Rc<Type>>,
|
||||
}
|
||||
|
||||
pub struct GlobalContext<'a> {
|
||||
primitive_defs: Vec<TypeDef<'a>>,
|
||||
class_defs: Vec<ClassDef<'a>>,
|
||||
parametric_defs: Vec<ParametricDef<'a>>,
|
||||
var_defs: Vec<VarDef<'a>>,
|
||||
sym_table: HashMap<&'a str, Type>,
|
||||
fn_table: HashMap<&'a str, FnDef>,
|
||||
}
|
||||
|
||||
impl<'a> GlobalContext<'a> {
|
||||
pub fn new(primitives: Vec<TypeDef<'a>>) -> GlobalContext {
|
||||
let mut sym_table = HashMap::new();
|
||||
for (i, t) in primitives.iter().enumerate() {
|
||||
sym_table.insert(t.name, Type::PrimitiveType(PrimitiveId(i)));
|
||||
}
|
||||
return GlobalContext {
|
||||
primitive_defs: primitives,
|
||||
class_defs: Vec::new(),
|
||||
parametric_defs: Vec::new(),
|
||||
var_defs: Vec::new(),
|
||||
fn_table: HashMap::new(),
|
||||
sym_table,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn add_class(&mut self, def: ClassDef<'a>) -> ClassId {
|
||||
self.sym_table.insert(
|
||||
def.base.name,
|
||||
Type::ClassType(ClassId(self.class_defs.len())),
|
||||
);
|
||||
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(Type::TypeVariable(v)))
|
||||
.collect();
|
||||
self.sym_table.insert(
|
||||
def.base.name,
|
||||
Type::ParametricType(ParamId(self.parametric_defs.len()), params),
|
||||
);
|
||||
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,
|
||||
Type::TypeVariable(VariableId(self.var_defs.len())),
|
||||
);
|
||||
self.add_variable_private(def)
|
||||
}
|
||||
|
||||
pub fn add_variable_private(&mut self, def: VarDef<'a>) -> VariableId {
|
||||
self.var_defs.push(def);
|
||||
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(&self, name: &str) -> Option<&FnDef> {
|
||||
self.fn_table.get(name)
|
||||
}
|
||||
|
||||
pub fn get_primitive_mut(&mut self, id: PrimitiveId) -> &mut TypeDef<'a> {
|
||||
self.primitive_defs.get_mut(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_primitive(&self, id: PrimitiveId) -> &TypeDef {
|
||||
self.primitive_defs.get(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_class_mut(&mut self, id: ClassId) -> &mut ClassDef<'a> {
|
||||
self.class_defs.get_mut(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_class(&self, id: ClassId) -> &ClassDef {
|
||||
self.class_defs.get(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_parametric_mut(&mut self, id: ParamId) -> &mut ParametricDef<'a> {
|
||||
self.parametric_defs.get_mut(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_parametric(&self, id: ParamId) -> &ParametricDef {
|
||||
self.parametric_defs.get(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_variable_mut(&mut self, id: VariableId) -> &mut VarDef<'a> {
|
||||
self.var_defs.get_mut(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_variable(&self, id: VariableId) -> &VarDef {
|
||||
self.var_defs.get(id.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get_type(&self, name: &str) -> Option<Type> {
|
||||
// TODO: change this to handle import
|
||||
self.sym_table.get(name).map(|v| v.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl Type {
|
||||
pub fn subst(&self, map: &HashMap<VariableId, Rc<Type>>) -> Type {
|
||||
match self {
|
||||
Type::TypeVariable(id) => map.get(id).map(|v| v.as_ref()).unwrap_or(self).clone(),
|
||||
Type::ParametricType(id, params) => Type::ParametricType(
|
||||
*id,
|
||||
params
|
||||
.iter()
|
||||
.map(|v| v.as_ref().subst(map).into())
|
||||
.collect(),
|
||||
),
|
||||
_ => self.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inv_subst(&self, map: &[(Rc<Type>, Rc<Type>)]) -> Rc<Type> {
|
||||
for (from, to) in map.iter() {
|
||||
if self == from.as_ref() {
|
||||
return to.clone();
|
||||
}
|
||||
}
|
||||
match self {
|
||||
Type::ParametricType(id, params) => Type::ParametricType(
|
||||
*id,
|
||||
params
|
||||
.iter()
|
||||
.map(|v| v.as_ref().inv_subst(map).into())
|
||||
.collect(),
|
||||
),
|
||||
_ => self.clone(),
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
pub fn get_subst(&self, ctx: &GlobalContext) -> HashMap<VariableId, Rc<Type>> {
|
||||
match self {
|
||||
Type::ParametricType(id, params) => {
|
||||
let vars = &ctx.get_parametric(*id).params;
|
||||
vars.iter()
|
||||
.zip(params)
|
||||
.map(|(v, p)| (*v, p.as_ref().clone().into()))
|
||||
.collect()
|
||||
}
|
||||
// if this proves to be slow, we can use option type
|
||||
_ => HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_base<'b: 'a, 'a>(&'a self, ctx: &'b GlobalContext) -> Option<&'b TypeDef> {
|
||||
match self {
|
||||
Type::PrimitiveType(id) => Some(ctx.get_primitive(*id)),
|
||||
Type::ClassType(id) | Type::VirtualClassType(id) => Some(&ctx.get_class(*id).base),
|
||||
Type::ParametricType(id, _) => Some(&ctx.get_parametric(*id).base),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user