jweinst1
4/18/2017 - 6:33 AM

noodle language added bools as 1 or 0

noodle language added bools as 1 or 0



#[derive(Debug)]
enum Noodle {
    Int(i32),
    Plus(Box<Noodle>, Box<Noodle>),
    Sub(Box<Noodle>, Box<Noodle>),
    Mul(Box<Noodle>, Box<Noodle>),
    Div(Box<Noodle>, Box<Noodle>),
    Rem(Box<Noodle>, Box<Noodle>),
    Eq(Box<Noodle>, Box<Noodle>)
    
}

impl Noodle {
    fn call(self) -> Noodle {
        match self {
            Noodle::Int(num) => Noodle::Int(num),
            Noodle::Plus(l, r) => Noodle::Int(l.call().int() + r.call().int()),
            Noodle::Sub(l, r) => Noodle::Int(l.call().int() - r.call().int()),
            Noodle::Mul(l, r) => Noodle::Int(l.call().int() * r.call().int()),
            Noodle::Div(l, r) => Noodle::Int(l.call().int() / r.call().int()),
            Noodle::Rem(l, r) => Noodle::Int(l.call().int() % r.call().int()),
            Noodle::Eq(l, r) => if l.call().int() == r.call().int() {Noodle::Int(1)} else {Noodle::Int(0)}
        }
    }
    
    fn int(self) -> i32 {
        match self {
            Noodle::Int(num) => num,
            _ => self.call().int()
        }
    }
    
}

use std::collections::HashMap;

pub struct Env {
    vars: HashMap<String, Noodle>,
    parent:Vec<Env>
}

impl Env {

    pub fn new() -> Env {
        Env{vars:HashMap::new(), parent:Vec::with_capacity(1)}
    }
    
    pub fn create_child(self) -> Env {
        let mut newchild = Env::new();
        newchild.parent.push(self);
        newchild
    }
    
    pub fn has_parent(&self) -> bool {
        !self.parent.is_empty()
    }
    
    pub fn get(&self, key:String) -> Noodle {
        match self.vars.get(&key) {
            Some(i) => i,
            None => return if self.has_parent() {self.parent[0].get(key)} else {Noodle::Int(0)}
        }
    }
    
    pub fn set(&mut self, key:String, val:Noodle) {
        self.vars.insert(key, val);
    }
    
    pub fn has(&self, key:String) -> bool {
        self.vars.contains_key(&key)
    }
    
    pub fn del(&mut self, key:String) {
        self.vars.remove(&key);
    }
}

fn main() {

   // (8 - 6) * 6 = 12
}