|
| 1 | +use std::convert::TryFrom; |
| 2 | +use std::fmt; |
| 3 | +use std::fmt::{Display, Error, Formatter}; |
| 4 | +use std::hash::Hash; |
| 5 | +use std::ops::Deref; |
| 6 | + |
| 7 | +use itertools::{EitherOrBoth, Itertools}; |
| 8 | + |
| 9 | +use crate::check::constrain::constraint::expected::Expect::*; |
| 10 | +use crate::check::context::clss; |
| 11 | +use crate::check::context::clss::{BOOL_PRIMITIVE, FLOAT_PRIMITIVE, INT_PRIMITIVE, STRING_PRIMITIVE}; |
| 12 | +use crate::check::context::name::{DirectName, NameUnion}; |
| 13 | +use crate::check::result::{TypeErr, TypeResult}; |
| 14 | +use crate::common::delimit::comma_delm; |
| 15 | +use crate::common::position::Position; |
| 16 | +use crate::parse::ast::{Node, AST}; |
| 17 | + |
| 18 | +#[derive(Clone, Debug, PartialEq, Eq, Hash)] |
| 19 | +pub struct Expected { |
| 20 | + pub pos: Position, |
| 21 | + pub expect: Expect |
| 22 | +} |
| 23 | + |
| 24 | +impl Expected { |
| 25 | + pub fn new(pos: &Position, expect: &Expect) -> Expected { |
| 26 | + Expected { pos: pos.clone(), expect: expect.clone() } |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl TryFrom<&AST> for Expected { |
| 31 | + type Error = Vec<TypeErr>; |
| 32 | + |
| 33 | + /// Creates Expected from AST. |
| 34 | + /// |
| 35 | + /// If primitive or Constructor, constructs Type. |
| 36 | + fn try_from(ast: &AST) -> TypeResult<Expected> { |
| 37 | + let ast = match &ast.node { |
| 38 | + Node::Block { statements } | Node::Script { statements } => |
| 39 | + statements.last().unwrap_or(ast), |
| 40 | + _ => ast |
| 41 | + }; |
| 42 | + |
| 43 | + let expect = match &ast.node { |
| 44 | + Node::Int { .. } | Node::ENum { .. } => Type { name: NameUnion::from(INT_PRIMITIVE) }, |
| 45 | + Node::Real { .. } => Type { name: NameUnion::from(FLOAT_PRIMITIVE) }, |
| 46 | + Node::Bool { .. } => Type { name: NameUnion::from(BOOL_PRIMITIVE) }, |
| 47 | + Node::Str { .. } => Type { name: NameUnion::from(STRING_PRIMITIVE) }, |
| 48 | + Node::Undefined => Nullable, |
| 49 | + Node::Underscore => ExpressionAny, |
| 50 | + _ => Expression { ast: ast.clone() } |
| 51 | + }; |
| 52 | + |
| 53 | + Ok(Expected::new(&ast.pos, &expect)) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl TryFrom<&Box<AST>> for Expected { |
| 58 | + type Error = Vec<TypeErr>; |
| 59 | + |
| 60 | + fn try_from(ast: &Box<AST>) -> TypeResult<Expected> { Expected::try_from(ast.deref()) } |
| 61 | +} |
| 62 | + |
| 63 | +#[derive(Clone, Debug, PartialEq, Eq, Hash)] |
| 64 | +pub enum Expect { |
| 65 | + Nullable, |
| 66 | + Expression { ast: AST }, |
| 67 | + ExpressionAny, |
| 68 | + Collection { ty: Box<Expected> }, |
| 69 | + Raises { name: NameUnion }, |
| 70 | + Function { name: DirectName, args: Vec<Expected> }, |
| 71 | + Field { name: String }, |
| 72 | + Access { entity: Box<Expected>, name: Box<Expected> }, |
| 73 | + Type { name: NameUnion } |
| 74 | +} |
| 75 | + |
| 76 | +impl Display for Expected { |
| 77 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { write!(f, "{}", self.expect) } |
| 78 | +} |
| 79 | + |
| 80 | +impl Expected { |
| 81 | + pub fn is_expr(&self) -> bool { |
| 82 | + if let Expression { .. } = self.expect { |
| 83 | + true |
| 84 | + } else { |
| 85 | + false |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl Display for Expect { |
| 91 | + fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 92 | + write!(f, "{}", match &self { |
| 93 | + Nullable => String::from("None"), |
| 94 | + ExpressionAny => String::from("Any"), |
| 95 | + Expression { ast } => format!("{}", ast.node), |
| 96 | + Collection { ty } => format!("Collection[{}]", ty.expect), |
| 97 | + Raises { name: ty } => format!("Raises[{}]", ty), |
| 98 | + Access { entity, name } => format!("{}.{}", entity.expect, name.expect), |
| 99 | + Function { name, args } => format!("{}({})", name, comma_delm(args)), |
| 100 | + Field { name } => name.clone(), |
| 101 | + Type { name: ty } => format!("{}", ty) |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +impl Expect { |
| 107 | + pub fn structurally_eq(&self, other: &Self) -> bool { |
| 108 | + match (self, other) { |
| 109 | + (Collection { ty: l }, Collection { ty: r }) => l.expect.structurally_eq(&r.expect), |
| 110 | + (Field { name: l }, Field { name: r }) => l == r, |
| 111 | + (Raises { name: l }, Raises { name: r }) | (Type { name: l }, Type { name: r }) => |
| 112 | + l == r, |
| 113 | + (Access { entity: le, name: ln }, Access { entity: re, name: rn }) => |
| 114 | + le == re && ln == rn, |
| 115 | + (Function { name: l, args: la }, Function { name: r, args: ra }) => |
| 116 | + l == r |
| 117 | + && la.iter().zip_longest(ra.iter()).all(|pair| { |
| 118 | + if let EitherOrBoth::Both(left, right) = pair { |
| 119 | + left.expect.structurally_eq(&right.expect) |
| 120 | + } else { |
| 121 | + false |
| 122 | + } |
| 123 | + }), |
| 124 | + |
| 125 | + (Expression { ast: l }, Expression { ast: r }) => l.equal_structure(r), |
| 126 | + |
| 127 | + (ExpressionAny, ExpressionAny) | (Nullable, Nullable) => true, |
| 128 | + |
| 129 | + (Type { name: ty, .. }, Expression { ast: AST { node: Node::Str { .. }, .. } }) |
| 130 | + | (Expression { ast: AST { node: Node::Str { .. }, .. } }, Type { name: ty, .. }) |
| 131 | + if ty == &NameUnion::from(clss::STRING_PRIMITIVE) => |
| 132 | + true, |
| 133 | + (Type { name: ty, .. }, Expression { ast: AST { node: Node::Real { .. }, .. } }) |
| 134 | + | (Expression { ast: AST { node: Node::Real { .. }, .. } }, Type { name: ty, .. }) |
| 135 | + if ty == &NameUnion::from(clss::FLOAT_PRIMITIVE) => |
| 136 | + true, |
| 137 | + (Type { name: ty, .. }, Expression { ast: AST { node: Node::Int { .. }, .. } }) |
| 138 | + | (Expression { ast: AST { node: Node::Int { .. }, .. } }, Type { name: ty, .. }) |
| 139 | + if ty == &NameUnion::from(clss::INT_PRIMITIVE) => |
| 140 | + true, |
| 141 | + |
| 142 | + _ => false |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments