2020-09-09 21:40:31 +01:00
|
|
|
/** Serene --- Yet an other Lisp
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-09-11 20:55:26 +01:00
|
|
|
use crate::compiler::Compiler;
|
2020-09-18 21:52:45 +01:00
|
|
|
use crate::types::collections;
|
|
|
|
use crate::types::{ExprResult, Expression, Number, Symbol};
|
2020-06-13 17:55:27 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, Clone)]
|
|
|
|
pub enum Expr {
|
2020-06-20 22:14:48 +01:00
|
|
|
Sym(Symbol),
|
2020-06-13 17:55:27 +01:00
|
|
|
Str(String),
|
|
|
|
Num(Number),
|
|
|
|
Comment,
|
|
|
|
Error(String),
|
2020-09-19 00:28:40 +01:00
|
|
|
Cons(Box<collections::List>),
|
2020-06-14 21:02:31 +01:00
|
|
|
Nil,
|
|
|
|
NoMatch,
|
|
|
|
}
|
|
|
|
|
2020-09-13 19:29:17 +01:00
|
|
|
impl Expr {
|
2020-09-19 00:28:40 +01:00
|
|
|
pub fn make_list(elements: &[Expr]) -> Expr {
|
|
|
|
Expr::Cons(Box::new(collections::List::new(elements)))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list_to_cons(l: collections::List) -> Expr {
|
|
|
|
Expr::Cons(Box::new(l))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn make_empty_list() -> collections::List {
|
|
|
|
collections::List::new_empty()
|
2020-06-20 15:20:05 +01:00
|
|
|
}
|
2020-06-20 16:25:05 +01:00
|
|
|
|
|
|
|
pub fn make_symbol(v: String) -> Expr {
|
2020-06-20 22:14:48 +01:00
|
|
|
Expr::Sym(Symbol::new(v))
|
2020-06-20 16:25:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn make_string(v: String) -> Expr {
|
|
|
|
Expr::Str(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn make_number(n: Number) -> Expr {
|
|
|
|
Expr::Num(n)
|
|
|
|
}
|
2020-06-20 15:20:05 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 12:34:08 +01:00
|
|
|
impl Expression for Expr {
|
2020-06-20 15:20:05 +01:00
|
|
|
fn eval() {}
|
2020-09-20 22:22:18 +01:00
|
|
|
fn code_gen<'ctx, 'val: 'ctx>(&self, compiler: &'ctx mut Compiler<'val>) -> ExprResult<'val> {
|
2020-07-06 14:18:29 +01:00
|
|
|
match self {
|
2020-09-11 20:55:26 +01:00
|
|
|
Expr::Sym(s) => s.code_gen(compiler),
|
2020-09-18 21:52:45 +01:00
|
|
|
Expr::Cons(s) => s.code_gen(compiler),
|
2020-07-06 14:18:29 +01:00
|
|
|
_ => Err("NotImplemented".to_string()),
|
|
|
|
}
|
|
|
|
}
|
2020-06-13 17:55:27 +01:00
|
|
|
}
|