2020-09-13 19:29:17 +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-20 22:22:18 +01:00
|
|
|
use crate::ast::Expr;
|
2020-09-13 19:29:17 +01:00
|
|
|
use crate::compiler::Compiler;
|
2020-09-19 01:23:33 +01:00
|
|
|
use crate::types::collections::core::Seq;
|
2020-09-20 22:22:18 +01:00
|
|
|
use crate::types::{ExprResult, Expression, List};
|
|
|
|
use crate::values::Value;
|
2020-09-13 19:29:17 +01:00
|
|
|
|
2020-09-20 22:22:18 +01:00
|
|
|
pub fn def<'ctx, 'val: 'ctx>(compiler: &'ctx mut Compiler<'val>, args: List) -> ExprResult<'val> {
|
2020-09-18 21:52:45 +01:00
|
|
|
// TODO: We need to support docstrings for def
|
2020-09-19 01:23:33 +01:00
|
|
|
if args.length() != 2 {
|
|
|
|
// TODO: Raise a meaningful error by including the location
|
|
|
|
panic!(format!(
|
|
|
|
"`def` expects 2 parameters, '{}' given.",
|
|
|
|
args.length()
|
|
|
|
));
|
|
|
|
}
|
2020-09-18 21:52:45 +01:00
|
|
|
|
2020-09-20 22:22:18 +01:00
|
|
|
let sym = match args.first() {
|
|
|
|
Some(e) => match e {
|
|
|
|
Expr::Sym(e) => e,
|
|
|
|
_ => return Err("First argument of 'def' has to be a symbol".to_string()),
|
|
|
|
},
|
|
|
|
_ => return Err("First argument of 'def' has to be a symbol".to_string()),
|
|
|
|
};
|
2020-09-18 21:52:45 +01:00
|
|
|
|
2020-09-20 22:22:18 +01:00
|
|
|
let value = match args.rest().first() {
|
|
|
|
Some(e) => {
|
|
|
|
let generated_code = e.code_gen(compiler);
|
|
|
|
Value::new(Some(sym.name.clone()), e, generated_code)
|
|
|
|
}
|
|
|
|
_ => return Err("Missing the second arugment for 'def'.".to_string()),
|
|
|
|
};
|
2020-09-18 21:52:45 +01:00
|
|
|
|
2020-09-20 22:22:18 +01:00
|
|
|
compiler
|
|
|
|
.current_ns()
|
|
|
|
.unwrap()
|
|
|
|
.define(sym.name.clone(), value, true)
|
2020-09-13 19:29:17 +01:00
|
|
|
}
|