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
use crate::shell::Shell;

#[derive(Debug)]
pub enum Error {}

pub trait Execute {
    fn execute(&self, _: &mut Shell) -> Result<(), Error> {
        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub enum Literal {
    String(String),
    Number(f64),
}

impl Execute for Literal {
    fn execute(&self, shell: &mut Shell) -> Result<(), Error> {
        shell.machine.push(match self {
            Self::String(s) => xmachine::Value::string(s),
            Self::Number(n) => xmachine::Value::number(n.clone()),
        });
        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct FnCall(pub Box<Value>, pub Vec<Value>);

impl Execute for FnCall {
    fn execute(&self, shell: &mut Shell) -> Result<(), Error> {
        let FnCall(function, mut arguments) = self.clone();
        arguments.reverse();
        for arg in arguments {
            arg.execute(shell)?;
        }
        function.execute(shell)?;

        if let Value::Builtin(_) = (*function).clone() {
        } else {
            shell.machine.call();
        }

        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct Identifier(pub String);

impl Execute for Identifier {
    fn execute(&self, shell: &mut Shell) -> Result<(), Error> {
        let Identifier(name) = self;
        shell.machine.push(xmachine::Value::string(name));
        shell.machine.load();
        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub enum Builtin {
    List,
    ChangeDir,
    Move,
    Clear,
    Remove,
    MakeDir,
    MakeFile,
    ShellOut,
    WorkingDir,
    Exit,
}

impl Execute for Builtin {
    fn execute(&self, shell: &mut Shell) -> Result<(), Error> {
        match self {
            Self::Clear => {
                shell.clear();
            }
            Self::List => {
                let arg = shell.machine.pop().map(|v| (*v).clone().to_string());
                shell.ls(arg);
            }
            Self::ChangeDir => {
                let arg = shell.machine.get_arg::<String>();
                shell.cd(&arg);
            }
            Self::Move => {
                let old = shell.machine.get_arg::<String>();
                let new = shell.machine.get_arg::<String>();
                shell.mv(&old, &new);
            }
            Self::Remove => {
                let path = shell.machine.get_arg::<String>();
                shell.rm(&path);
            }
            Self::MakeDir => {
                let path = shell.machine.get_arg::<String>();
                shell.mkdir(&path);
            }
            Self::MakeFile => {
                let path = shell.machine.get_arg::<String>();
                shell.mkf(&path);
            }
            Self::ShellOut => {
                let arg = shell.machine.get_arg::<String>();
                shell.sh(&arg);
            }
            Self::WorkingDir => {
                shell.wd();
            }
            Self::Exit => shell.exit(),
        };

        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub enum Value {
    Name(Name),
    Literal(Literal),
    FnCall(FnCall),
    Builtin(Builtin),
    Function(Function),
}

impl Execute for Value {
    fn execute(&self, shell: &mut Shell) -> Result<(), Error> {
        match self {
            Self::Name(name) => name.execute(shell)?,
            Self::Literal(literal) => literal.execute(shell)?,
            Self::FnCall(call) => call.execute(shell)?,
            Self::Builtin(call) => call.execute(shell)?,
            Self::Function(func) => func.execute(shell)?,
        };
        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub enum Name {
    Name(Identifier),
    IndexName(Box<Value>, Vec<Value>),
    DotName(Box<Value>, Vec<Identifier>),
}

impl Execute for Name {
    fn execute(&self, shell: &mut Shell) -> Result<(), Error> {
        match self {
            Self::Name(name) => name.execute(shell)?,
            Self::DotName(head, identifiers) => {
                head.execute(shell)?;
                for ident in identifiers {
                    let Identifier(name) = ident;
                    shell.machine.push(xmachine::Value::string(name));
                    shell.machine.index();
                }
            }
            Self::IndexName(head, values) => {
                head.execute(shell)?;
                for value in values {
                    value.execute(shell)?;
                    shell.machine.index();
                }
            }
        };
        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub enum Expr {
    Assignment(Name, Value),
    WhileLoop(Value, Suite),
    IfThenElse(Value, Suite, Suite),
    FunctionDef(FunctionDef),
    Value(Value),
}

impl Execute for Expr {
    fn execute(&self, shell: &mut Shell) -> Result<(), Error> {
        match self {
            Self::Assignment(name, value) => match name {
                Name::Name(ident) => {
                    let Identifier(store) = ident;
                    value.execute(shell)?;
                    shell.machine.push(xmachine::Value::string(store));
                    shell.machine.store();
                }
                dotname => {
                    value.execute(shell)?;
                    dotname.execute(shell)?;
                    shell.machine.assign();
                }
            },
            Self::WhileLoop(value, body) => {
                let ret_val = |shell: &mut Shell| match shell.machine.pop() {
                    Some(v) => bool::from((*v).clone()),
                    _ => false,
                };

                value.execute(shell)?;
                while ret_val(shell) {
                    body.execute(shell)?;
                    value.execute(shell)?;
                }
            }
            Self::IfThenElse(value, then_body, else_body) => {
                let ret_val = |shell: &mut Shell| match shell.machine.pop() {
                    Some(v) => bool::from((*v).clone()),
                    _ => false,
                };

                value.execute(shell)?;
                if ret_val(shell) {
                    then_body.execute(shell)?;
                } else {
                    else_body.execute(shell)?;
                }
            }
            Self::FunctionDef(func_def) => func_def.execute(shell)?,
            Self::Value(v) => v.execute(shell)?,
        };
        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct Suite(pub Vec<Expr>);

impl Execute for Suite {
    fn execute(&self, shell: &mut Shell) -> Result<(), Error> {
        let Suite(exprs) = self;
        for expr in exprs {
            expr.execute(shell)?;
        }

        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct FunctionDef(pub Name, pub Function);

impl Execute for FunctionDef {
    fn execute(&self, shell: &mut Shell) -> Result<(), Error> {
        let FunctionDef(name, func) = self;

        Expr::Assignment(name.clone(), Value::Function(func.clone())).execute(shell)?;

        Ok(())
    }
}

#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct Function(pub Vec<Identifier>, pub Suite);

impl Execute for Function {
    fn execute(&self, shell: &mut Shell) -> Result<(), Error> {
        let Function(args, suite) = self.clone();
        shell.machine.push(xmachine::Value::function(
            move |m| {
                let shell = &mut Shell::new();
                shell.machine.stack = m.stack.clone();
                shell.machine.registers = m.registers.clone();
                for arg in args.clone() {
                    let Identifier(store) = arg;
                    shell.machine.push(xmachine::Value::string(store));
                    shell.machine.store();
                }
                match suite.execute(shell) {
                    _ => {}
                };
                m.stack = shell.machine.stack.clone();
            },
            &shell.machine,
        ));

        Ok(())
    }
}