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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
pub mod builtins;
pub mod bytecode;
pub mod error;
pub mod memory;
pub mod standard;
pub mod core_objects;
pub mod prelude;

#[macro_use]
extern crate lazy_static;

#[macro_use]
extern crate log;

use error::*;
use memory::*;
use mlrefcell::MLRefCell;
use once_cell::sync::OnceCell;
use stable_deref_trait::StableDeref;
use pretty_dtoa::FmtFloatConfig;
use std::any::Any;
use std::clone::Clone as RustClone;
use std::collections::HashSet;
use std::hash::Hash;
use std::hash::Hasher;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use std::path::PathBuf;
use std::fs;

use std::fmt;

/// Conditionally execute code depending on if an ObjectRef contains an inner object of a certain
/// type or not. This helps greatly improve readability in internal code
#[macro_export]
macro_rules! downcast {
    // TODO: add an optional 'mut' before the identifier, which will try_borrow_mut instead of
    // try_borrow
    (($id:ident : $type:ty = $val:expr) -> $main:block else $other:block) => {
        if let Some($id) = $val.as_any().downcast_ref::<ObjectCell<$type>>() {
            let $id = $id.try_borrow()?;
            $main
        } else {
            $other
        }
    };
    (($id:ident : $type:ty = $val:expr) -> $main:block) => {
        if let Some($id) = $val.as_any().downcast_ref::<ObjectCell<$type>>() {
            let $id = $id.try_borrow()?;
            $main
        }
    };
}

/// Dynamically dispatch to different code depending on the types of two variables. Useful for
/// defining readable and short internal code for operations
#[macro_export]
macro_rules! match_tech_types {
    (($a:expr, $b:expr) { $(($v1:ident : $t1:ty, $v2:ident : $t2:ty)=>$b1:block),* , _ => $b2:block } ) => {{
        let a_any__ = $a.as_any();
        let b_any__ = $b.as_any();
        match (a_any__.type_id(), b_any__.type_id()) {
            $( 
                (a__, b__)
                    if a__ == TypeId::of::<ObjectCell<$t1>>()
                    && b__ == TypeId::of::<ObjectCell<$t2>>() =>
                    {
                        let $v1 = a_any__
                            .downcast_ref::<ObjectCell<$t1>>()
                            .unwrap()
                            .try_borrow()?;
                        let $v2 = b_any__
                            .downcast_ref::<ObjectCell<$t2>>()
                            .unwrap()
                            .try_borrow()?;
                        $b1
                    }
            ),*
            _ => {
                $b2
            }
        }
    }};
}

pub static DEFAULT_FLOAT_FMT: FmtFloatConfig = FmtFloatConfig::default();

pub static PARSED_CLARGS: OnceCell<Vec<String>> = OnceCell::new();

/// The parent directory of the script that tc is being run in, or the parent directory from where
/// the user called the ``tech`` binary. Useful for locating the .tcmake folder
pub static INVOKE_ABSOLUTE_PARENT_DIR: OnceCell<PathBuf> = OnceCell::new();

/// The absolute path to the current running script. If not set, the script isn't
/// running from a file (this is a perfectly valid situation)
pub static CURR_SCRIPT_PATH: OnceCell<PathBuf> = OnceCell::new();

pub fn get_tcmake_dir() -> Option<PathBuf> {
    INVOKE_ABSOLUTE_PARENT_DIR.get().cloned().map(|mut path| {
        path.push(".tcmake");
        if !path.exists() {
            let _ = fs::create_dir(&path);
        }
        path
    })
}

/// The main object reference type, which can be passed around to represent
/// an object of any valid Object type
#[repr(transparent)]
#[derive(Debug)]
pub struct ObjectRef {
    inner: Box<dyn Object>,
}

impl Deref for ObjectRef {
    type Target = Box<dyn Object>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for ObjectRef {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

unsafe impl StableDeref for ObjectRef {}

/// An object reference that's guaranteed to have a valid hash (that doesn't throw errors)
///
/// Construct a `HashableObjectRef` using the `hashable` method of `ObjectRef`
///
/// Unlike ObjectRef, HashableObjectRef implements Hash
#[derive(Debug)]
pub struct HashableObjectRef {
    inner: Box<dyn Object>,
}

impl Deref for HashableObjectRef {
    type Target = Box<dyn Object>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for HashableObjectRef {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl Hash for HashableObjectRef {
    fn hash<H: Hasher>(&self, state: &mut H) {
        state.write_u64(self.technetium_hash().unwrap());
    }
}

impl PartialEq for HashableObjectRef {
    fn eq(&self, other: &HashableObjectRef) -> bool {
        match self.technetium_eq(other.opaque_clone()) {
            Some(val) => val,
            None => self.ref_eq(other.opaque_clone()),
        }
    }
}

impl Eq for HashableObjectRef {}

impl Clone for HashableObjectRef {
    fn clone(&self) -> Self {
        self.inner.opaque_clone().hashable().unwrap()
    }
}

impl HashableObjectRef {
    pub fn downgrade(self) -> ObjectRef {
        ObjectRef {inner: self.inner}
    }
}

impl ObjectRef {
    pub fn new_from_cell<T>(obj: ObjectCell<T>) -> Self
    where
        ObjectCell<T>: Object,
    {
        ObjectRef {
            inner: Box::new(obj),
        }
    }

    pub fn new<T>(inner: T) -> Self
    where
        ObjectCell<T>: Object,
    {
        ObjectRef {
            inner: Box::new(ObjectCell::new(inner)),
        }
    }

    /// Create a HashableObjectRef, by checking if it has a valid hash
    pub fn hashable(&self) -> Option<HashableObjectRef> {
        if self.technetium_hash().is_none() {
            None
        } else {
            let new = self.opaque_clone();
            Some(HashableObjectRef { inner: new.inner })
        }
    }
}

impl Clone for ObjectRef {
    fn clone(&self) -> Self {
        self.inner.opaque_clone()
    }
}

/// The universal container object for implementers of the Object trait.
/// Anything that implements Object should be of the form ObjectCell<T>.
/// ObjectCell combines interior mutability and shared ownership, so it
/// is the primary container used for objects in technetium
#[repr(transparent)]
#[derive(Debug)]
pub struct ObjectCell<T>
where
    ObjectCell<T>: Object,
{
    inner: Rc<MLRefCell<T>>,
}

impl<T> Clone for ObjectCell<T>
where
    ObjectCell<T>: Object,
{
    fn clone(&self) -> Self {
        ObjectCell {
            inner: Rc::clone(&self.inner),
        }
    }
}

impl<T> ObjectCell<T>
where
    ObjectCell<T>: Object,
{
    pub fn new(val: T) -> Self {
        ObjectCell {
            inner: Rc::new(MLRefCell::new(val)),
        }
    }
    /// Lock the ``MLRefCell`` inside the cell, effectively making the value immutable, giving a
    /// runtime error for any future mutation of the object. This is useful if the value will need
    /// to be used as the key in a HashMap, or in a HashSet
    pub fn lock(&self) {
        let _ = self.inner.lock();
    }
}

impl<T> Deref for ObjectCell<T>
where
    ObjectCell<T>: Object,
{
    type Target = Rc<MLRefCell<T>>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl<T> DerefMut for ObjectCell<T>
where
    ObjectCell<T>: Object,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

unsafe impl<T> StableDeref for ObjectCell<T> where ObjectCell<T>: Object {}

pub trait ToAny {
    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;
}

impl<T: Object> ToAny for T {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

pub trait OpaqueClone {
    fn opaque_clone(&self) -> ObjectRef;
}

impl<T> OpaqueClone for ObjectCell<T>
where
    ObjectCell<T>: Object,
{
    fn opaque_clone(&self) -> ObjectRef {
        let self_copy = ObjectCell {
            inner: Rc::clone(&self.inner),
        };
        ObjectRef {
            inner: Box::new(self_copy),
        }
    }
}

pub trait RawPointer {
    fn raw_pointer(&self) -> *const ();
}

impl<T> RawPointer for ObjectCell<T>
where
    ObjectCell<T>: Object,
{
    fn raw_pointer(&self) -> *const () {
        self.as_ptr() as *const ()
    }
}

pub trait LockImmutable {
    fn lock_immutable(&self);
}

impl<T> LockImmutable for ObjectCell<T>
where
    ObjectCell<T>: Object,
{
    fn lock_immutable(&self) {
        self.lock();
    }
}

pub struct RuntimeContext<'a> {
    pub memory: &'a mut MemoryManager,
}

/// The primary trait for objects in technetium.
///
/// Types that implement ``Object``
/// should be of the form ``ObjectCell<T>`` for some T. This will give all of the
/// requirement subtraits for free.
pub trait Object: Any + ToAny + OpaqueClone + RawPointer + LockImmutable {
    /// Create a deep clone of an object. This is primarily used in the ``clone``
    /// function in technetium
    fn technetium_clone(&self, _context: &mut RuntimeContext<'_>) -> RuntimeResult<ObjectRef> {
        Err(RuntimeError::type_error(format!(
            "{} can not be cloned",
            self.technetium_type_name()
        )))
    }
    
    /// Hash an object. This is not required, so the default implementation always
    /// returns ``None``. 
    ///
    /// In implementing a hash, it's important that ``x == y``
    /// implies that ``x.technetium_hash() == y.technetium_hash()`` to avoid logic
    /// errors.
    ///
    /// Note that ObjectRef does not implement ``Hash`` in Rust, but HashableObjectRef
    /// does. See the docs for [HashableObjectRef](struct.HashableObjectRef.html) for
    /// more information
    fn technetium_hash(&self) -> Option<u64> {
        None
    }
    
    /// A type name for an object.
    ///
    /// Conventions are that type names are all lowercase, and use parentheses to denote
    /// "sub-types" (for example: "iterator(list)")
    ///
    /// This function should not fail, so should return a set value.
    fn technetium_type_name(&self) -> String;
    
    /// Convert an object to a String.
    fn to_string(&self) -> RuntimeResult<String> {
        // TODO fix: This should be passed some way to ask "Am I the recursive call of myself" (i.e.
        // should I just return [...] for lists). This might be able to be attached to a
        // RuntimeContext<'_>
        Ok(format!("<{}>", self.technetium_type_name()))
    }
    
    /// Get an attribute of an object
    fn get_attr(&self, _attr: String, _context: &mut RuntimeContext<'_>) -> RuntimeResult<ObjectRef> {
        Err(RuntimeError::attribute_error(format!(
            "{} has no attributes",
            self.technetium_type_name()
        )))
    }
    
    /// Set an attribute of an object
    fn set_attr(&self, _attr: String, _val: ObjectRef, _context: &mut RuntimeContext<'_>) -> RuntimeResult<()> {
        Err(RuntimeError::attribute_error(format!(
            "Cannot set attributes of {}",
            self.technetium_type_name()
        )))
    }
    
    /// Call a given method of an object
    fn call_method(&self, method: &str, _args: &[ObjectRef], _context: &mut RuntimeContext<'_>) -> RuntimeResult<ObjectRef> {
        Err(RuntimeError::attribute_error(format!(
            "Cannot call method {} of {}",
            method,
            self.technetium_type_name()
        )))
    }
    
    /// Call a given object as a function.
    fn call(&self, _args: &[ObjectRef], _context: &mut RuntimeContext<'_>) -> RuntimeResult<ObjectRef> {
        Err(RuntimeError::type_error(format!(
            "Object of type {} is not callable",
            self.technetium_type_name()
        )))
    }
    
    /// Create an iterator over an object. This is used for initializing
    /// ``for`` loops.
    fn make_iter(&self, _context: &mut RuntimeContext<'_>) -> RuntimeResult<ObjectRef> {
        Err(RuntimeError::type_error(format!(
            "Object of type {} cannot be made into an iterator",
            self.technetium_type_name()
        )))
    }
    
    /// Take from this object, assuming it is an iterator. This is used for
    /// stepping through ``for`` loops.
    fn take_iter(&self, _context: &mut RuntimeContext<'_>) -> RuntimeResult<Option<ObjectRef>> {
        Err(RuntimeError::type_error(format!(
            "Object of type {} cannot be iterated",
            self.technetium_type_name()
        )))
    }
    
    /// Determine whether an object is "truthy" (whether it should be treated
    /// as true when used as a boolean)
    fn truthy(&self) -> bool {
        true
    }
    
    /// Equal-as-value (like == in Python, or .equals() in Java)
    /// Should return None if ``self`` cannot reasonably determine if ``other`` is equal or not.
    /// Typical implementations will usually only return Some(value) if they can definitively answer,
    /// And will quickly answer None otherwise.
    /// This allows equality to be reflexive by calling ``other.technetium_eq`` in case the answer is None.
    fn technetium_eq(&self, _other: ObjectRef) -> Option<bool> {
        None
    }
    
    /// Equal-as-reference (like ``is`` in Python, or == in Java)
    ///
    /// This is treated as a fallback in the ``Eq`` implementation
    /// for ``ObjectRef``, primarily for sets and dictionaries
    fn ref_eq(&self, other: ObjectRef) -> bool {
        self.raw_pointer() == other.raw_pointer()
    }
}

impl fmt::Debug for dyn Object {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            fmt,
            "{}",
            self.to_string().unwrap_or_else(|_| "Object".to_string())
        )
    }
}

impl PartialEq for ObjectRef {
    fn eq(&self, other: &ObjectRef) -> bool {
        match self.technetium_eq(ObjectRef::clone(other)) {
            Some(val) => val,
            None => {
                match other.technetium_eq(ObjectRef::clone(self)) {
                    Some(val) => val,
                    None => {
                        self.ref_eq(ObjectRef::clone(other))
                    }
                }
            },
        }
    }
}

impl Eq for ObjectRef {}