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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright 2019 Jeremy Wall
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::rc::Rc;

mod cache;
mod debug;
mod display;
pub mod environment;
#[macro_use]
mod error;
mod convert;
pub mod pointer;
mod runtime;
pub mod scope;
pub mod translate;
mod vm;

pub use environment::Environment;
pub use error::Error;
pub use vm::VM;

use crate::ast::{CastType, Position};
use pointer::OpPointer;
use scope::Stack;

#[derive(Debug, PartialEq, Clone)]
pub enum Primitive {
    // Primitive Types
    Int(i64),
    Float(f64),
    Str(String),
    Bool(bool),
    Empty,
}

use Primitive::{Bool, Empty, Float, Int, Str};

impl Value {
    fn type_name(&self) -> &'static str {
        match self {
            P(Int(_)) => "Int",
            P(Float(_)) => "Float",
            P(Str(_)) => "String",
            P(Bool(_)) => "Bool",
            P(Empty) => "NULL",
            C(List(_, _)) => "List",
            C(Tuple(_, _)) => "Tuple",
            F(_) => "Func",
            M(_) => "Func",
            T(_) => "Expression",
            S(_) => "Symbol",
        }
    }
}

#[derive(Debug, PartialEq, Clone)]
pub enum Composite {
    List(Vec<Rc<Value>>, Vec<Position>),
    Tuple(Vec<(String, Rc<Value>)>, Vec<(Position, Position)>),
}

use Composite::{List, Tuple};

impl From<&Composite> for String {
    fn from(c: &Composite) -> Self {
        let mut buf = String::new();
        match c {
            &List(ref elems, _) => {
                buf.push_str("[");
                for e in elems.iter() {
                    let val: String = e.as_ref().into();
                    buf.push_str(&val);
                    buf.push_str(",");
                }
                buf.push_str("]");
            }
            &Tuple(ref flds, _) => {
                buf.push_str("{");
                for &(ref k, ref v) in flds.iter() {
                    buf.push_str(&k);
                    buf.push_str(" = ");
                    let val: String = v.as_ref().into();
                    buf.push_str(&val);
                    buf.push_str(",");
                }
                buf.push_str("}");
            }
        }
        buf
    }
}

#[derive(Debug, PartialEq, Clone)]
pub struct Func {
    ptr: OpPointer,
    bindings: Vec<String>,
    snapshot: Stack,
}

#[derive(Debug, PartialEq, Clone)]
pub struct Module {
    ptr: OpPointer,
    result_ptr: Option<usize>,
    flds: Vec<(String, Rc<Value>)>,
    flds_pos_list: Vec<(Position, Position)>,
    pkg_ptr: Option<OpPointer>,
}

#[derive(Clone)]
pub enum Value {
    // Binding names.
    S(String),
    // Primitive Types
    P(Primitive),
    // Composite Types.
    C(Composite),
    // Program Pointer
    T(usize),
    // Function
    F(Func),
    // Module
    M(Module),
}

impl From<&Value> for String {
    fn from(v: &Value) -> Self {
        match v {
            &S(ref s) => s.clone(),
            &P(ref p) => p.into(),
            &C(ref c) => c.into(),
            &T(_) => "<Thunk>".to_owned(),
            &F(_) => "<Func>".to_owned(),
            &M(_) => "<Module>".to_owned(),
        }
    }
}

use Value::{C, F, M, P, S, T};

#[derive(Debug, PartialEq, Clone)]
pub enum Hook {
    Map,
    Include,
    Filter,
    Reduce,
    Import,
    Out,
    Assert,
    Convert,
    Regex,
    Range,
    Trace(Position),
}

#[derive(Debug, PartialEq, Clone)]
pub enum Op {
    // Stack and Name manipulation.
    Bind,     // Bind a Val to a name in the heap
    BindOver, // Overwrite a value in the heap
    Pop,      // Pop a Value off the value stack and discard it.
    NewScope(i32),
    // Math ops
    Add,
    Sub,
    Div,
    Mul,
    Mod,
    // Comparison Ops
    Equal,
    Gt,
    Lt,
    GtEq,
    LtEq,
    // Not,
    Not,
    // Primitive Types ops
    Val(Primitive),
    // Primitive casts
    Cast(CastType),
    // A bareword for use in bindings or lookups
    Sym(String),
    // Reference a binding on the heap
    DeRef(String),
    // Complex Type ops
    InitTuple,
    Field,
    InitList,
    Element,
    // Copy Operation
    Cp,
    // Control Flow
    Bang,
    Jump(i32),
    JumpIfTrue(i32),
    JumpIfFalse(i32),
    SelectJump(i32),
    And(i32),
    Or(i32),
    // Spacer operation, Does nothing.
    Index,     // indexing operation
    SafeIndex, // Safe indexing operation. Does Null Coelescing
    Exist,
    Noop,
    // Pending Computation
    InitThunk(i32), // Basically just used for module return expressions
    Module(i32),
    Func(i32),
    Return,
    // Calls
    FCall,
    // TypeSystem
    Typ,
    // Runtime hooks
    Runtime(Hook),
    Render,
    // The self lookup for tuples.
    PushSelf,
    PopSelf,
}

use super::ir::Val;

impl PartialEq for Value {
    fn eq(&self, other: &Value) -> bool {
        match (self, other) {
            (P(left), P(right)) => left == right,
            (C(List(left, _)), C(List(right, _))) => left == right,
            (C(Tuple(left, _)), C(Tuple(right, _))) => {
                if left.len() != right.len() {
                    return false;
                }
                for (ref lk, ref lv) in left.iter() {
                    let mut found = false;
                    for (ref rk, ref rv) in right.iter() {
                        if lk == rk {
                            found = true;
                            if lv != rv {
                                return false;
                            }
                        }
                    }
                    if !found {
                        return false;
                    }
                }
                true
            }
            (F(left), F(right)) => left == right,
            (M(left), M(right)) => left == right,
            (T(_), T(_)) | (S(_), S(_)) => false,
            (_, _) => false,
        }
    }
}

// TODO(jwall): Move all of this into the convert module.
impl From<Rc<Value>> for Val {
    fn from(val: Rc<Value>) -> Val {
        val.as_ref().into()
    }
}

impl From<Value> for Val {
    fn from(val: Value) -> Val {
        (&val).into()
    }
}

impl From<&Value> for Val {
    fn from(val: &Value) -> Val {
        match val {
            P(Int(i)) => Val::Int(*i),
            P(Float(f)) => Val::Float(*f),
            P(Str(s)) => Val::Str(s.clone()),
            P(Bool(b)) => Val::Boolean(*b),
            C(Tuple(fs, _)) => {
                let mut flds = Vec::new();
                for &(ref k, ref v) in fs.iter() {
                    let v = v.clone();
                    flds.push((k.clone(), Rc::new(v.into())));
                }
                Val::Tuple(flds)
            }
            C(List(elems, _)) => {
                let mut els = Vec::new();
                for e in elems.iter() {
                    let e = e.clone();
                    els.push(Rc::new(e.into()));
                }
                Val::List(els)
            }
            S(_) | F(_) | M(_) | T(_) | P(Empty) => Val::Empty,
        }
    }
}

impl From<Rc<Val>> for Value {
    fn from(val: Rc<Val>) -> Self {
        val.as_ref().into()
    }
}

impl From<Val> for Value {
    fn from(val: Val) -> Self {
        (&val).into()
    }
}

impl From<&Val> for Value {
    fn from(val: &Val) -> Self {
        match val {
            Val::Int(i) => P(Int(*i)),
            Val::Float(f) => P(Float(*f)),
            Val::Boolean(b) => P(Bool(*b)),
            Val::Str(s) => P(Str(s.clone())),
            Val::Empty => P(Empty),
            Val::List(els) => {
                let mut lst = Vec::new();
                let mut positions = Vec::new();
                for e in els.iter() {
                    let e = e.clone();
                    lst.push(Rc::new(e.into()));
                    positions.push(Position::new(0, 0, 0));
                }
                // TODO(jwall): This should have a set of
                // Positions of the same length.
                C(List(lst, positions))
            }
            Val::Tuple(flds) => {
                let mut field_list = Vec::new();
                let mut positions = Vec::new();
                for &(ref key, ref val) in flds.iter() {
                    let val = val.clone();
                    field_list.push((key.clone(), Rc::new(val.into())));
                    positions.push((Position::new(0, 0, 0), Position::new(0, 0, 0)));
                }
                C(Tuple(field_list, positions))
            }
            Val::Env(flds) => {
                let mut field_list = Vec::new();
                let mut positions = Vec::new();
                for &(ref key, ref val) in flds.iter() {
                    field_list.push((key.clone(), Rc::new(P(Str(val.clone())))));
                    positions.push((Position::new(0, 0, 0), Position::new(0, 0, 0)));
                }
                C(Tuple(field_list, positions))
            }
        }
    }
}