WIP Commit for stackoverflow

This commit is contained in:
Aidan Hahn 2021-12-30 21:14:24 -08:00
parent a967ac5c3e
commit 4cadb1a7eb
No known key found for this signature in database
GPG key ID: 327711E983899316
3 changed files with 47 additions and 39 deletions

View file

@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#![feature(type_alias_impl_trait)]
use rustyline::error::ReadlineError; use rustyline::error::ReadlineError;
use rustyline::Editor; use rustyline::Editor;

View file

@ -15,6 +15,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#![feature(type_alias_impl_trait)]
mod segment; mod segment;
mod lex; mod lex;
mod func; mod func;

View file

@ -21,7 +21,7 @@ use std::collections::HashMap;
use std::env; use std::env;
use crate::segment::{Ctr, Ast, ast_to_string}; use crate::segment::{Ctr, Ast, ast_to_string};
use crate::eval::eval; use crate::eval::eval;
use crate::func::{Function, Operation, Args, FTable}; use crate::func::{Function, Operation, InternalOperation, Args, FTable};
/* Mapping between a string token and a tree of Segments /* Mapping between a string token and a tree of Segments
* The string token can be found in any Ctr::Symbol value * The string token can be found in any Ctr::Symbol value
* it is expected that the trees stored are already evaluated * it is expected that the trees stored are already evaluated
@ -45,53 +45,58 @@ pub fn get_export(env_cfg: bool) -> Function {
loose_syms: true, loose_syms: true,
eval_lazy: true, eval_lazy: true,
args: Args::Lazy(2), args: Args::Lazy(2),
function: Operation::Internal( function: Operation::Internal(get_export_callback(env_cfg)),
|a: Ast, b: Rc<RefCell<VTable>>, c: Rc<RefCell<FTable>>| -> Ctr { };
let inner = a.borrow_mut(); }
match &inner.car {
Ctr::Symbol(identifier) => {
match &inner.cdr {
Ctr::Seg(tree) => {
match eval(tree.clone(), b.clone(), c.clone(), false) {
Ok(seg) => {
match seg {
Ctr::Seg(val) => {
let val_tmp = val.borrow().clone();
define(b, identifier.to_string(), Rc::new(val_tmp.car));
if env_cfg {
match val_tmp.car {
Ctr::Symbol(s) => env::set_var(identifier, s),
Ctr::String(s) => env::set_var(identifier, s),
Ctr::Integer(i) => env::set_var(identifier, format!("{}", i)),
Ctr::Float(f) => env::set_var(identifier, format!("{}", f)),
Ctr::Bool(b) => env::set_var(identifier, format!("{}", b)),
Ctr::Seg(c) => env::set_var(identifier, ast_to_string(c)),
Ctr::None => ()
}
}
},
_ => eprintln!("impossible args to export") fn get_export_callback(env_cfg: bool) -> InternalOperation {
|a: Ast, b: Rc<RefCell<VTable>>, c: Rc<RefCell<FTable>>| -> Ctr {
let inner = a.borrow_mut();
match &inner.car {
Ctr::Symbol(identifier) => {
match &inner.cdr {
Ctr::Seg(tree) => {
match eval(tree.clone(), b.clone(), c.clone(), false) {
Ok(seg) => {
match seg {
Ctr::Seg(val) => {
let val_tmp = val.borrow().clone();
define(b, identifier.to_string(), Rc::new(val_tmp.car));
if env_cfg {
match val_tmp.car {
Ctr::Symbol(s) => env::set_var(identifier, s),
Ctr::String(s) => env::set_var(identifier, s),
Ctr::Integer(i) => env::set_var(identifier, format!("{}", i)),
Ctr::Float(f) => env::set_var(identifier, format!("{}", f)),
Ctr::Bool(b) => env::set_var(identifier, format!("{}", b)),
Ctr::Seg(c) => env::set_var(identifier, ast_to_string(c)),
Ctr::None => ()
}
} }
}, },
Err(e) => eprintln!("couldnt eval symbol: {}", e) _ => eprintln!("impossible args to export")
} }
}, },
Ctr::None => { Err(e) => eprintln!("couldnt eval symbol: {}", e)
(*b).borrow_mut().remove(identifier);
},
_ => eprintln!("args not in standard form")
} }
}, },
_ => eprintln!("first argument to export must be a symbol") Ctr::None => {
} (*b).borrow_mut().remove(identifier);
if env_cfg {
// TODO: unset variable
}
},
return Ctr::None; _ => eprintln!("args not in standard form")
} }
) },
};
_ => eprintln!("first argument to export must be a symbol")
}
return Ctr::None;
}
} }