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
extern crate hlvm_runtime;

#[macro_use]
pub use hlvm_runtime::literals::*;
pub use hlvm_runtime::value::Value;
pub use hlvm_runtime::object::Object;
use hlvm_runtime::stack::StackFrame;
use hlvm_runtime::object::Instruction::*;


pub type ForeignFunction = fn(Value) -> Value;


#[derive(Clone, Debug)]
pub struct Fun {
    body: Vec<Value>
}

impl Fun {
    pub fn new() -> Self {
        Self {
            body: vec![]
        }
    }

    pub fn return_self(&self) -> Self {self.clone()}

    pub fn define(instruction: Value) -> Self {
        Self {
            body: vec![instruction]
        }
    }

    pub fn add_data(&mut self, data: Value) -> Self {
        self.body.push(data);
        self.return_self()
    }

    pub fn add_fun(&mut self, fun: Self) -> Self {
        self.add_data(fun.as_value());
        self.return_self()
    }

    pub fn add_foreign_fun(&mut self, fun: ForeignFunction) -> Self {
        self.add_fun(
            Fun::new()
                .add_data(foreign_function(fun))
                .call_foreign_function()
        );
        self.return_self()
    }

    pub fn add_str(&mut self, s: &str) -> Self {
        self.add_data(string(s));
        self.return_self()
    }

    pub fn add_num(&mut self, n: &str) -> Self {
        self.add_data(num(n));
        self.return_self()
    }

    pub fn add_list(&mut self, l: &[Value]) -> Self {
        self.add_data(list(l));
        self.return_self()
    }

    pub fn add_obj(&mut self) -> Self {
        self.add_data(empty_obj());
        self.return_self()
    }

    pub fn call(&mut self, fun: Self) -> Self {
        self.add_data(fun.as_value());
        self.add_data(ins(Call));
        self.return_self()
    }

    pub fn call_from_stack(&mut self) -> Self {
        self.add_data(ins(Call));
        self.return_self()
    }

    pub fn call_foreign_function(&mut self) -> Self {
        self.add_data(ins(Execute));
        self.return_self()
    }

    pub fn store(&mut self, name: &str) -> Self {
        self.body.push(string(name));
        self.add_data(ins(Store));
        self.return_self()
    }

    pub fn get_parameter(&mut self, name: &str) -> Self {self.store(name)}
    pub fn store_variable(&mut self, name: &str) -> Self {self.store(name)}


    pub fn load(&mut self, name: &str) -> Self {
        self.body.push(string(name));
        self.add_data(ins(Load));
        self.return_self()
    }
    
    pub fn load_variable(&mut self, name: &str) -> Self {
        self.load(name)
    }

    pub fn disassemble(&mut self) -> Self {
        self.add_data(self.as_value());
        self.return_self()
    }

    pub fn as_value(&self) -> Value {
        let mut body = list(&[]);
        for expr in self.body.clone() {
            body.list_push(expr);
        }

        body
    }

    pub fn run(&mut self) {
        StackFrame::from_instructions(
            self.as_value()
        ).run();
    }

    pub fn debug(&mut self) {
        println!("===| DEBUG |===> main {}\n===[ START ]===>", self.as_value());
        StackFrame::from_instructions(
            self.as_value()
        ).run();
    }

    // pub fn print(&mut self) -> Self {self.call(Fun::define(ins(Print))); self.return_self()}
    // pub fn println(&mut self) -> Self {self.call(Fun::define(ins(Println))); self.return_self()}
    // pub fn add(&mut self) -> Self {self.call(Fun::define(ins(Add))); self.return_self()}
    // pub fn mul(&mut self) -> Self {self.call(Fun::define(ins(Mul))); self.return_self()}
    // pub fn sub(&mut self) -> Self {self.call(Fun::define(ins(Sub))); self.return_self()}
    // pub fn div(&mut self) -> Self {self.call(Fun::define(ins(Div))); self.return_self()}
    // pub fn not(&mut self) -> Self {self.call(Fun::define(ins(Not))); self.return_self()}
    // pub fn greater(&mut self) -> Self {self.call(Fun::define(ins(Greater))); self.return_self()}
    // pub fn less(&mut self) -> Self {self.call(Fun::define(ins(Less))); self.return_self()}
    // pub fn append_list(&mut self) -> Self {self.call(Fun::define(ins(Append))); self.return_self()}
    // pub fn pop_list(&mut self) -> Self {self.call(Fun::define(ins(Pop))); self.return_self()}

    pub fn print(&mut self) -> Self {
        self.add_data(ins(Print));
        self.return_self()
    }
    pub fn println(&mut self) -> Self {
        self.add_data(ins(Println));
        self.return_self()
    }

    pub fn add(&mut self) -> Self {
        self.add_data(ins(Add));
        self.return_self()
    }
    pub fn mul(&mut self) -> Self {
        self.add_data(ins(Mul));
        self.return_self()
    }
    pub fn sub(&mut self) -> Self {
        self.add_data(ins(Sub));
        self.return_self()
    }
    pub fn div(&mut self) -> Self {
        self.add_data(ins(Div));
        self.return_self()
    }
    pub fn not(&mut self) -> Self {
        self.add_data(ins(Not));
        self.return_self()
    }

    pub fn greater(&mut self) -> Self {
        self.add_data(ins(Greater));
        self.return_self()
    }
    pub fn less(&mut self) -> Self {
        self.add_data(ins(Less));
        self.return_self()
    }

    pub fn append_list(&mut self) -> Self {
        self.add_data(ins(Append));
        self.return_self()
    }
    pub fn pop_list(&mut self) -> Self {
        self.add_data(ins(Pop));
        self.return_self()
    }

    pub fn eq(&mut self) -> Self {
        self.add_data(ins(Equal));
        self.return_self()
    }

    pub fn index(&mut self) -> Self {
        self.add_data(ins(Index));
        self.return_self()
    }


    pub fn get_attr(&mut self) -> Self {
        self.add_data(ins(GetAttr));
        self.return_self()
    }
    pub fn set_attr(&mut self) -> Self {
        self.add_data(ins(SetAttr));
        self.return_self()
    }


    pub fn while_function(&mut self) -> Self {
        self.add_data(ins(While));
        self.return_self()
    }

    pub fn if_function(&mut self) -> Self {
        self.add_data(ins(If));
        self.return_self()
    }

    // pub fn if_function(&mut self) -> Self {self.call(Fun::define(ins(If))); self.return_self()}
    // pub fn if_function(&mut self) -> Self {self.call(Fun::define(ins(If))); self.return_self()}

    // pub fn if_function(&mut self) -> Self {
    //     let if_fun = Fun::new()
    //         .get_parameter("c")
    //         .get_parameter("a")
    //         .get_parameter("b")

    //         .load("c").store("d")
    //         .add_fun(
    //             Fun::new()
    //                 .load("a")
    //                 .add_num("0").store("d")
    //         )
    //         .add_fun(
    //             Fun::new()
    //                 .load("d")
    //         ).while_function()

    //         .load("c").store("d")
    //         .add_fun(
    //             Fun::new()
    //                 .load("b")
    //                 .add_num("1").store("d")
    //         )
    //         .add_fun(
    //             Fun::new()
    //                 .load("d")
    //                 .not()
    //         ).while_function()
    //         ;

    //     self.add_fun(if_fun);
    //     self.call_from_stack();
    //     self.return_self()
    // }
}