1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::ops::Deref;

use crate::check::constrain::unify::finished::Finished;
use crate::check::name::Name;
use crate::check::name::string_name::StringName;
use crate::common::position::Position;
use crate::parse::ast::AST;
use crate::parse::ast::node_op::NodeOp;

pub mod node;

#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub struct ASTTy {
    pub pos: Position,
    pub node: NodeTy,
    pub ty: Option<Name>,
}

impl From<(&Box<AST>, &Finished)> for ASTTy {
    fn from((ast, finished): (&Box<AST>, &Finished)) -> Self {
        ASTTy::from((ast.deref(), finished))
    }
}

impl From<(Box<AST>, &Finished)> for ASTTy {
    fn from((ast, finished): (Box<AST>, &Finished)) -> Self {
        ASTTy::from((ast.deref(), finished))
    }
}

impl From<(&AST, &Finished)> for ASTTy {
    fn from((ast, finished): (&AST, &Finished)) -> Self {
        let node = NodeTy::from((&ast.node, finished));
        ASTTy { node, ty: finished.pos_to_name.get(&ast.pos).cloned(), pos: ast.pos }
    }
}

impl From<&AST> for ASTTy {
    fn from(ast: &AST) -> Self {
        let node = NodeTy::from((&ast.node, &Finished::default()));
        ASTTy { node, ty: None, pos: ast.pos }
    }
}

impl From<&Box<AST>> for ASTTy {
    fn from(ast: &Box<AST>) -> Self {
        let node = NodeTy::from((&ast.node, &Finished::default()));
        ASTTy { node, ty: None, pos: ast.pos }
    }
}

impl ASTTy {
    pub fn with_ty(self, ty: &Name) -> ASTTy {
        trace!("Annotated AST at {} with '{}'", self.pos, ty);
        ASTTy { ty: Some(ty.clone()), ..self }
    }
}

type OptASTTy = Option<Box<ASTTy>>;
type OptName = Option<Name>;

#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub enum NodeTy {
    Import { from: Option<Box<ASTTy>>, import: Vec<ASTTy>, alias: Vec<ASTTy> },
    Class { ty: StringName, args: Vec<ASTTy>, parents: Vec<ASTTy>, body: OptASTTy },
    Parent { ty: StringName, args: Vec<ASTTy> },
    Reassign { left: Box<ASTTy>, right: Box<ASTTy>, op: NodeOp },
    VariableDef { mutable: bool, var: Box<ASTTy>, ty: OptName, expr: OptASTTy, forward: Vec<ASTTy> },
    FunDef { pure: bool, id: Box<ASTTy>, args: Vec<ASTTy>, ret: OptName, raises: Vec<ASTTy>, body: OptASTTy },
    AnonFun { args: Vec<ASTTy>, body: Box<ASTTy> },
    Raises { expr_or_stmt: Box<ASTTy>, errors: Vec<ASTTy> },
    Raise { error: Box<ASTTy> },
    Handle { expr_or_stmt: Box<ASTTy>, cases: Vec<ASTTy> },
    With { resource: Box<ASTTy>, alias: Option<(Box<ASTTy>, bool, OptName)>, expr: Box<ASTTy> },
    FunctionCall { name: StringName, args: Vec<ASTTy> },
    PropertyCall { instance: Box<ASTTy>, property: Box<ASTTy> },
    Id { lit: String },
    ExpressionType { expr: Box<ASTTy>, mutable: bool, ty: OptName },
    TypeDef { ty: StringName, isa: OptName, body: OptASTTy },
    TypeAlias { ty: StringName, isa: Name, conditions: Vec<ASTTy> },
    Condition { cond: Box<ASTTy>, el: OptASTTy },
    FunArg { vararg: bool, mutable: bool, var: Box<ASTTy>, ty: OptName, default: OptASTTy },
    Dict { elements: Vec<(ASTTy, ASTTy)> },
    DictBuilder { from: Box<ASTTy>, to: Box<ASTTy>, conditions: Vec<ASTTy> },
    Set { elements: Vec<ASTTy> },
    SetBuilder { item: Box<ASTTy>, conditions: Vec<ASTTy> },
    List { elements: Vec<ASTTy> },
    ListBuilder { item: Box<ASTTy>, conditions: Vec<ASTTy> },
    Tuple { elements: Vec<ASTTy> },
    Range { from: Box<ASTTy>, to: Box<ASTTy>, inclusive: bool, step: OptASTTy },
    Slice { from: Box<ASTTy>, to: Box<ASTTy>, inclusive: bool, step: OptASTTy },
    Index { item: Box<ASTTy>, range: Box<ASTTy> },
    Block { statements: Vec<ASTTy> },
    Real { lit: String },
    Int { lit: String },
    ENum { num: String, exp: String },
    Str { lit: String, expressions: Vec<ASTTy> },
    DocStr { lit: String },
    Bool { lit: bool },
    Add { left: Box<ASTTy>, right: Box<ASTTy> },
    AddU { expr: Box<ASTTy> },
    Sub { left: Box<ASTTy>, right: Box<ASTTy> },
    SubU { expr: Box<ASTTy> },
    Mul { left: Box<ASTTy>, right: Box<ASTTy> },
    Div { left: Box<ASTTy>, right: Box<ASTTy> },
    FDiv { left: Box<ASTTy>, right: Box<ASTTy> },
    Mod { left: Box<ASTTy>, right: Box<ASTTy> },
    Pow { left: Box<ASTTy>, right: Box<ASTTy> },
    Sqrt { expr: Box<ASTTy> },
    BAnd { left: Box<ASTTy>, right: Box<ASTTy> },
    BOr { left: Box<ASTTy>, right: Box<ASTTy> },
    BXOr { left: Box<ASTTy>, right: Box<ASTTy> },
    BOneCmpl { expr: Box<ASTTy> },
    BLShift { left: Box<ASTTy>, right: Box<ASTTy> },
    BRShift { left: Box<ASTTy>, right: Box<ASTTy> },
    Le { left: Box<ASTTy>, right: Box<ASTTy> },
    Ge { left: Box<ASTTy>, right: Box<ASTTy> },
    Leq { left: Box<ASTTy>, right: Box<ASTTy> },
    Geq { left: Box<ASTTy>, right: Box<ASTTy> },
    Is { left: Box<ASTTy>, right: Box<ASTTy> },
    IsN { left: Box<ASTTy>, right: Box<ASTTy> },
    Eq { left: Box<ASTTy>, right: Box<ASTTy> },
    Neq { left: Box<ASTTy>, right: Box<ASTTy> },
    IsA { left: Box<ASTTy>, right: Box<ASTTy> },
    IsNA { left: Box<ASTTy>, right: Box<ASTTy> },
    Not { expr: Box<ASTTy> },
    And { left: Box<ASTTy>, right: Box<ASTTy> },
    Or { left: Box<ASTTy>, right: Box<ASTTy> },
    IfElse { cond: Box<ASTTy>, then: Box<ASTTy>, el: OptASTTy },
    Match { cond: Box<ASTTy>, cases: Vec<ASTTy> },
    Case { cond: Box<ASTTy>, body: Box<ASTTy> },
    For { expr: Box<ASTTy>, col: Box<ASTTy>, body: Box<ASTTy> },
    In { left: Box<ASTTy>, right: Box<ASTTy> },
    While { cond: Box<ASTTy>, body: Box<ASTTy> },
    Break,
    Continue,
    Return { expr: Box<ASTTy> },
    ReturnEmpty,
    Underscore,
    Undefined,
    Pass,
    Question { left: Box<ASTTy>, right: Box<ASTTy> },
    QuestionOp { expr: Box<ASTTy> },
    Empty,
}

#[cfg(test)]
mod test {
    use crate::{AST, ASTTy};
    use crate::check::name::Name;
    use crate::common::position::Position;
    use crate::parse::ast::Node;

    #[test]
    fn to_ty() {
        let node = Node::Pass;
        let ast = AST::new(Position::invisible(), node.clone());
        let ast_ty = ASTTy::from(&ast).with_ty(&Name::from("Dummy"));

        assert_eq!(ast_ty.ty, Some(Name::from("Dummy")));
    }
}