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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use super::TypeInfo;

use std::{
    collections::HashMap,
    rc::Rc,
    cell::RefCell,
    fmt,
};

pub type LocalId = usize;
pub type DataId  = usize;

#[derive(Clone, Debug)]
pub enum Literal {
    Number(f64),
    String(String),
    Boolean(bool),
    Nil,
}

// When depth is None, we're dealing with a global.
#[derive(Clone, Debug, PartialEq)]
pub struct Binding {
    pub name: String,
    pub depth: Option<usize>,
    pub function_depth: usize,
}

impl Binding {
    // Define to be resolved later
    pub fn define_local(name: &str) -> Self {
        Binding {
            name: name.to_string(),
            depth: Some(0),
            function_depth: 0
        }
    }

    pub fn global(name: &str) -> Self {
        Binding {
            name: name.to_string(),
            depth: None,
            function_depth: 0
        }
    }

    pub fn local(name: &str, depth: usize, function_depth: usize) -> Self {
        Binding {
            name: name.to_string(),
            depth: Some(depth),
            function_depth: function_depth
        }
    }

    pub fn resolve(&mut self, depth: usize, function_depth: usize) {
        self.depth = Some(depth);
        self.function_depth = function_depth
    }

    #[inline]
    pub fn is_upvalue(&self) -> bool {
        self.depth
            .map(|d| d > self.function_depth)
            .unwrap_or(false)
    }

    pub fn upvalue_depth(&self) -> Option<usize> {
        self.depth.and_then(|d|
            if self.is_upvalue() {
                Some(d - self.function_depth)
            } else {
                None
            })
    }

    pub fn name(&self) -> &str {
        &self.name
    }
}

#[derive(Clone, Debug)]
pub enum BinaryOp {
    Add,
    Sub,
    Mul,
    Div,
    Rem,
    Equal,
    NEqual,
    GtEqual,
    LtEqual,
    Index,
    Gt,
    Lt,
    And,
    Or,
    Pow,
}

#[derive(Clone, Debug)]
pub enum UnaryOp {
    Neg,
    Not,
}

#[derive(Clone, Debug)]
pub struct IrFunctionBody {
    pub params: Vec<Binding>,
    pub method: bool,
    pub inner: Vec<ExprNode>, // the actual function body
}

#[derive(Clone, Debug)]
pub struct IrFunction {
    pub var: Binding,
    pub body: Rc<RefCell<IrFunctionBody>>, // A Literal/Constant
}

#[derive(Clone, Debug)]
pub struct Call {
    pub callee: Node<Expr>,
    pub args: Vec<Node<Expr>>,
}

#[derive(Clone)]
pub struct Node<T> {
    inner: Box<T>,
    type_info: TypeInfo,
}

impl<T> Node<T> {
    pub fn new(inner: T, type_info: TypeInfo) -> Self {
        Node {
            inner: Box::new(inner),
            type_info
        }
    }

    pub fn inner(&self) -> &T {
        &self.inner
    }

    pub fn inner_mut(&mut self) -> &mut T {
        &mut self.inner
    }

    pub fn type_info(&self) -> &TypeInfo {
        &self.type_info
    }
}

impl<T: fmt::Debug> fmt::Debug for Node<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:#?}", self.inner)
    }
}

pub type ExprNode = Node<Expr>;

// NOTE: LocalId removed for now, as it wasn't used in the compiler


#[derive(Clone, Debug)]
pub enum Expr {
    Data(DataId),

    Literal(Literal),

    Bind(Binding, ExprNode), // @zesterer: like `with`
    BindGlobal(Binding, ExprNode),

    Var(Binding), // access binding

    Mutate(ExprNode, ExprNode),
    Binary(ExprNode, BinaryOp, ExprNode),
    Call(Call),
    Function(IrFunction),
    AnonFunction(IrFunction), // variable here will be unique id
    Unary(UnaryOp, ExprNode),
    Return(Option<ExprNode>),

    Not(ExprNode),
    Neg(ExprNode),

    If(ExprNode, ExprNode, Option<ExprNode>),
    While(ExprNode, ExprNode),

    List(Vec<ExprNode>),
    Dict(Vec<ExprNode>, Vec<ExprNode>), // They need to be the same size, funny enough
    SetElement(ExprNode, ExprNode, ExprNode),

    Block(Vec<ExprNode>),

    Break,
    Pop,
}

impl Expr {
    pub fn node(self, type_info: TypeInfo) -> ExprNode {
        Node::new(self, type_info)
    }
}

#[derive(Debug)]
pub struct Program {
    data: HashMap<DataId, ExprNode>,
    entry: Option<DataId>
}

impl Program {
    pub fn empty() -> Self {
        Program {
            data: HashMap::new(),
            entry: None,
        }
    }

    pub fn with_entry(entry: DataId) -> Self {
        Program {
            data: HashMap::new(),
            entry: Some(entry)
        }
    }

    pub fn insert(&mut self, id: DataId, atom: ExprNode) {
        self.data.insert(id, atom);
    }
}