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
use derive_more::From;
use crate::code_point::CodePoint;
use crate::structure::rlt::function::BodiedFunction;
use crate::structure::rlt::new_types::*;
use crate::structure::rlt::types::TypedParameter;
use crate::structure::Located;

#[derive(Debug, Clone, PartialEq)]
pub struct Struct {
    pub keyword: Keyword,
    pub id: TypeName,
    pub parameters: Option<Enclosed<Box<[TypedParameter]>>>,
    pub body: Option<Enclosed<Box<[BodiedFunction]>>>,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Enum {
    Stack {
        keyword: Keyword,
        id: TypeName,
        contents: Option<Enclosed<Box<[TypeName]>>>,
    },
    Heap {
        keyword: Keyword,
        id: TypeName,
        contents: Option<Enclosed<Box<[TypeName]>>>,
    },
}

#[derive(Debug, Clone, PartialEq, From)]
pub enum TopLevelNode {
    Enum(Enum),
    Struct(Struct),
    BodiedFunction(BodiedFunction),
}

impl Located for Struct {
    fn location(&self) -> CodePoint {
        self.keyword.location()
    }
}

impl Located for Enum {
    fn location(&self) -> CodePoint {
        match self {
            Enum::Stack { keyword, .. } => keyword.location(),
            Enum::Heap { keyword, .. } => keyword.location(),
        }
    }
}

impl Located for TopLevelNode {
    fn location(&self) -> CodePoint {
        match self {
            TopLevelNode::Enum(x) => x.location(),
            TopLevelNode::Struct(x) => x.location(),
            TopLevelNode::BodiedFunction(x) => x.location(),
        }
    }
}

impl Enum {
    pub fn id(&self) -> &TypeName {
        match self {
            Enum::Stack { id, .. } => id,
            Enum::Heap { id, .. } => id,
        }
    }
}