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
use crate::target::*;

use alloc::boxed::Box;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

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

impl<T: Target> Compile<T> for Literal {
    fn compile(self) -> Result<String, Error> {
        match self {
            Self::String(s) => Ok(T::push(T::string(T::quote(s)))),
            Self::Number(n) => Ok(T::push(T::number(n))),
            Self::ForeignFunction(f) => Ok(T::push(T::foreign_func(f))),
        }
    }
}

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

impl<T: Target> Compile<T> for FnCall {
    fn compile(self) -> Result<String, Error> {
        let FnCall(function, arguments) = self;
        let compiled_args = arguments
            .iter()
            .rev()
            .map(|arg| {
                let value = arg.clone();
                Compile::<T>::compile(value).unwrap()
            })
            .map(T::copy)
            .collect::<String>();

        if let Value::Name(Name::DotName(head, idents)) = (*function).clone() {
            let actual_idents = idents[..idents.len() - 1].to_vec();
            let Identifier(method_name) = &idents[idents.len() - 1];
            Ok(compiled_args
                + &Compile::<T>::compile(Name::DotName(head, actual_idents))?
                + &T::method_call(T::string(T::quote(method_name))))
        } else {
            Ok(compiled_args + &T::call(Compile::<T>::compile((*function).clone())?))
        }
    }
}

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

impl<T: Target> Compile<T> for Identifier {
    fn compile(self) -> Result<String, Error> {
        let Identifier(name) = self;
        Ok(T::push(T::string(T::quote(name))))
    }
}

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

impl<T: Target> Compile<T> for Function {
    fn compile(self) -> Result<String, Error> {
        let Function(parameters, body) = self;

        let stores = parameters
            .iter()
            .map(|s| T::store(Compile::<T>::compile((*s).clone()).unwrap()))
            .collect::<String>();

        Ok(T::push(T::func(stores + &Compile::<T>::compile(body)?)))
    }
}

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

impl<T: Target> Compile<T> for FunctionDef {
    fn compile(self) -> Result<String, Error> {
        let FunctionDef(name, function) = self;

        Ok(Compile::<T>::compile(Expr::Assignment(name, Value::Function(function)))?)
    }
}

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

impl<T: Target> Compile<T> for Value {
    fn compile(self) -> Result<String, Error> {
        match self {
            Self::Name(name) => match name {
                Name::Name(n) => Compile::<T>::compile(n).and_then(|n| Ok(T::load(n))),
                otherwise => Compile::<T>::compile(otherwise),
            },
            Self::Literal(l) => Compile::<T>::compile(l),
            Self::FnCall(f) => Compile::<T>::compile(f),
            Self::Function(f) => Compile::<T>::compile(f),
        }
    }
}

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

impl<T: Target> Compile<T> for Name {
    fn compile(self) -> Result<String, Error> {
        match self {
            Self::Name(n) => Compile::<T>::compile(n),
            Self::DotName(head, tail) => Ok(T::dotname((*head).clone(), tail)),
            Self::IndexName(head, tail) => Ok(T::indexname((*head).clone(), tail)),
        }
    }
}

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

impl<T: Target> Compile<T> for Expr {
    fn compile(self) -> Result<String, Error> {
        match self {
            Self::Assignment(name, value) => match name {
                Name::Name(n) => Ok(T::store(T::copy(Compile::<T>::compile(value)?) + &Compile::<T>::compile(n)?)),
                otherwise => Ok(T::assign(T::copy(Compile::<T>::compile(value)?) + &Compile::<T>::compile(otherwise)?)),
            },
            Self::WhileLoop(condition, body) => {
                Ok(T::while_loop(Compile::<T>::compile(condition)?, Compile::<T>::compile(body)?))
            }
            Self::IfThenElse(condition, then_body, else_body) => Ok(T::if_then_else(
                Compile::<T>::compile(condition)?,
                Compile::<T>::compile(then_body)?,
                Compile::<T>::compile(else_body)?,
            )),
            Self::FunctionDef(function_def) => Ok(Compile::<T>::compile(function_def)?),
            Self::StructDef(struct_def) => Ok(Compile::<T>::compile(struct_def)?),
            Self::Value(value) => Ok(Compile::<T>::compile(value)?),
        }
    }
}

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

impl<T: Target> Compile<T> for Suite {
    fn compile(self) -> Result<String, Error> {
        let Suite(exprs) = self;
        Ok(exprs
            .iter()
            .map(|c| Compile::<T>::compile(c.clone()).unwrap())
            .collect::<String>())
    }
}

#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
pub struct StructDef(pub Name, pub Vec<FunctionDef>);

impl<T: Target> Compile<T> for StructDef {
    fn compile(self) -> Result<String, Error> {
        let StructDef(name, function_defs) = self;
        let mut exprs = vec![Expr::Assignment(
            Name::Name(Identifier("self".to_string())),
            Value::FnCall(FnCall(
                Box::new(Value::Name(Name::Name(Identifier("dict".to_string())))),
                vec![],
            )),
        )];

        let self_function_defs = function_defs
            .iter()
            .map(|f| {
                let FunctionDef(name, fun) = f;
                let result_name;

                match name {
                    Name::DotName(_, other_names) => result_name = other_names.clone(),
                    Name::Name(ident) => {
                        result_name = vec![ident.clone()];
                    }
                    _ => {
                        // DO SOMETHING BAD HERE
                        result_name = vec![Identifier("BAD".to_string())];
                    }
                }

                FunctionDef(
                    Name::DotName(
                        Box::new(Value::Name(Name::Name(Identifier("self".to_string())))),
                        result_name,
                    ),
                    fun.clone(),
                )
            })
            .map(|f| Expr::FunctionDef(f.clone()))
            .collect::<Vec<Expr>>();

        exprs.extend(self_function_defs);
        exprs.push(Expr::Value(Value::Name(Name::Name(Identifier(
            "self".to_string(),
        )))));

        let body = Suite(exprs);
        let constructor: Function = Function(vec![], body);
        Ok(Compile::<T>::compile(Expr::Assignment(name, Value::Function(constructor)))?)
    }
}