WIP commit to re-add and refactor config, repl, and library code

Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
Ava Hahn 2023-02-27 17:30:49 -08:00
parent 93a1e06a53
commit ae365ad63c
Signed by untrusted user who does not match committer: affine
GPG key ID: 3A4645B8CF806069
10 changed files with 758 additions and 67 deletions

147
src/bin/main.rs Normal file
View file

@ -0,0 +1,147 @@
/* relish: versatile lisp shell
* Copyright (C) 2021 Aidan Hahn
*
* 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 3 of the License, or
* (at your option) any later version.
*
* 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/>.
*/
use dirs::home_dir;
use relish::ast::{ast_to_string, eval, func_call, lex, new_ast, Ctr, FTable, VTable};
use relish::aux::configure;
use rustyline::error::ReadlineError;
use rustyline::Editor;
use std::cell::RefCell;
use std::env;
use std::rc::Rc;
fn main() {
let mut rl = Editor::<()>::new();
const HIST_FILE: &str = "/.relish_hist";
const CONFIG_FILE_DEFAULT: &str = "/.relishrc";
let mut hist: String = "".to_owned();
let mut cfg: String = "".to_owned();
if let Some(home) = home_dir() {
if let Some(h) = home.to_str() {
hist = h.to_owned() + HIST_FILE;
cfg = h.to_owned() + CONFIG_FILE_DEFAULT;
}
}
if hist != "" {
// ignore result. it loads or it doesnt.
let _ = rl.load_history(&hist);
}
let vt = Rc::new(RefCell::new(VTable::new()));
let conf_file;
let ft;
match env::var("RELISH_CFG_FILE") {
Ok(s) => {
conf_file = s
},
Err(e) => {
eprintln!("{}", e);
conf_file = cfg;
},
}
match configure(conf_file, vt.clone()) {
Ok(f) => ft = f,
Err(e) => {
ft = Rc::new(RefCell::new(FTable::new()));
eprintln!("{}", e);
},
}
loop {
let readline: Result<String, ReadlineError>;
// Rust is pain
let tmp_ft_clone = ft.clone();
// this is not okay
let t_ft_c_b = tmp_ft_clone.borrow();
let pfunc = t_ft_c_b.get("CFG_RELISH_PROMPT");
if let Some(fnc) = pfunc {
match func_call(
fnc.clone(),
new_ast(Ctr::None, Ctr::None),
vt.clone(),
ft.clone(),
) {
Err(s) => {
eprintln!("Couldnt generate prompt: {}", s);
readline = rl.readline("");
}
Ok(c) => match c {
Ctr::Symbol(s) => readline = rl.readline(&s.to_owned()),
Ctr::String(s) => readline = rl.readline(&s),
Ctr::Integer(i) => readline = rl.readline(&format!("{}", i)),
Ctr::Float(f) => readline = rl.readline(&format!("{}", f)),
Ctr::Bool(b) => readline = rl.readline(&format!("{}", b)),
Ctr::Seg(c) => readline = rl.readline(&ast_to_string(c.clone())),
Ctr::None => readline = rl.readline(""),
},
}
} else {
readline = rl.readline("");
}
match readline {
Ok(line) => {
rl.add_history_entry(line.as_str());
let mut l = line.as_str().to_owned();
if !l.starts_with("(") {
l = "(".to_owned() + &l;
}
if !l.ends_with(")") {
l = l + ")";
}
match lex(l) {
Ok(a) => match eval(a.clone(), vt.clone(), ft.clone(), false) {
Ok(a) => match a {
Ctr::Symbol(s) => println!("{}", s),
Ctr::String(s) => println!("{}", s),
Ctr::Integer(i) => println!("{}", i),
Ctr::Float(f) => println!("{}", f),
Ctr::Bool(b) => println!("{}", b),
Ctr::Seg(c) => println!("{}", ast_to_string(c.clone())),
Ctr::None => (),
},
Err(s) => {
println!("{}", s);
}
},
Err(s) => {
println!("{}", s);
}
}
}
Err(ReadlineError::Interrupted) => break,
Err(ReadlineError::Eof) => return,
Err(err) => {
eprintln!("Prompt error: {:?}", err);
break;
}
}
}
if hist != "" {
rl.save_history(&hist).unwrap();
}
}

93
src/config.rs Normal file
View file

@ -0,0 +1,93 @@
/* relish: versatile lisp shell
* Copyright (C) 2021 Aidan Hahn
*
* 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 3 of the License, or
* (at your option) any later version.
*
* 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/>.
*/
use crate::eval::eval;
use crate::func::{func_declare, Args, FTable, Function, Operation};
use crate::lex::lex;
use crate::segment::{Ast, Ctr};
use crate::stl::get_stdlib;
use crate::vars::{define, VTable};
use std::cell::RefCell;
use std::fs;
use std::io::{self, Write};
use std::rc::Rc;
fn prompt_default_callback(_: Ast, _: Rc<RefCell<VTable>>, _: Rc<RefCell<FTable>>) -> Ctr {
return Ctr::String("λ ".to_string());
}
pub fn configure(filename: String, vars: Rc<RefCell<VTable>>) -> Result<Rc<RefCell<FTable>>, String> {
let funcs;
define(
vars.clone(),
String::from("CFG_RELISH_POSIX"),
Rc::new(Ctr::String(String::from("0"))),
);
define(
vars.clone(),
String::from("CFG_RELISH_ENV"),
Rc::new(Ctr::String(String::from("1"))),
);
match get_stdlib(vars.clone()) {
Ok(f) => funcs = f,
Err(s) => {
funcs = Rc::new(RefCell::new(FTable::new()));
println!("Couldnt get stdlib: {}", s)
},
}
match func_declare(
funcs.clone(),
Rc::new(RefCell::new(Function {
name: String::from("CFG_RELISH_PROMPT"),
loose_syms: false,
eval_lazy: false,
args: Args::Lazy(0),
function: Operation::Internal(Box::new(prompt_default_callback)),
})),
) {
Some(e) => return Err(e),
None => {},
}
match fs::read_to_string(filename.clone()) {
Err(s) => {
return Err(format!("Couldnt open configuration file: {}", s));
}
Ok(raw_config) => {
let mut l = raw_config;
l = "(".to_owned() + &l + ")";
match lex(l) {
Err(s) => {
return Err(format!("Error in configuration: {}", s));
}
Ok(config) => {
if let Err(errst) = eval(config, vars, funcs.clone(), false) {
return Err(format!("Error loading {}: {}", filename.clone(), errst));
}
}
}
},
}
return Ok(funcs);
}

40
src/control.rs Normal file
View file

@ -0,0 +1,40 @@
/* relish: versatile lisp shell
* Copyright (C) 2021 Aidan Hahn
*
* 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 3 of the License, or
* (at your option) any later version.
*
* 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/>.
*/
use crate::append::get_append;
use crate::func::{func_declare, FTable, Ast};
use crate::segment::Ctr;
use crate::str::{get_concat, get_echo};
use crate::vars::{get_export, VTable};
use std::cell::RefCell;
use std::rc::Rc;
pub fn get_if() -> Function {
return Function {
name: String::from("if"),
loose_syms: false,
eval_lazy: true,
args: Args::Lazy(-1),
function: Operation::Internal(
Box::new(|args: Ast, vars: Rc<RefCell<VTable>>, funcs: Rc<RefCell<FTable>>| -> Ctr {
// Either 2 long or 3 long.
// arg 1 must eval to a bool
// then eval arg 2 or 3
})
),
};
}

View file

@ -19,31 +19,36 @@ use crate::segment::{Ctr, Seg, Type};
use crate::eval::eval;
use crate::sym::{SymTable, Symbol, ValueType, Args, UserFn};
use std::env;
use std::rc::Rc;
/*
// the stdlib var export function with env_sync on
static LIB_STORE_ENV: Symbol = Symbol {
name: String::from("export"),
args: Args::Lazy(2),
value: ValueType::Internal(Box::new( |ast: &Seg| -> Ctr {
_store_callback(ast, true)
},
)),
has_undefined_symbols: false,
};
fn store_stdlib(env: bool, syms: &mut SymTable) -> Result<(), String> {
syms.insert("def".to_string(), Symbol {
name: String::from("export"),
args: Args::Lazy(2),
conditional_branches: false,
value: ValueType::Internal(Rc::new( move |ast: &Seg, syms: &mut SymTable| -> Ctr {
_store_callback(ast, syms, env)
},
)),
});
// the stdlib var export function with env_sync off
pub static LIB_STORE_NO_ENV: Symbol = Symbol {
name: String::from("export"),
args: Args::Lazy(2),
value: ValueType::Internal(Box::new( |ast: &Seg| -> Ctr {
_store_callback(ast, false)
},
)),
has_undefined_symbols: false,
};*/
syms.insert("append".to_string(), Symbol {
name: String::from("append"),
args: Args::Infinite,
conditional_branches: false,
value: ValueType::Internal(Rc::new(_append_callback)),
});
Ok(())
}
fn _append_callback (_ast: &Seg, _syms: &mut SymTable) -> Ctr {
// if car is a list, append cdr
// otherwise create a list out of all arguments
todo!()
}
// TODO : declare function if arg list is long enough
fn _store_callback (ast: &Seg, syms: &mut SymTable, env_cfg: bool) -> Ctr {
let is_var = ast.len() == 2;
if let Ctr::Symbol(ref identifier) = *ast.car {

85
src/str.rs Normal file
View file

@ -0,0 +1,85 @@
/* relish: versatile lisp shell
* Copyright (C) 2021 Aidan Hahn
*
* 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 3 of the License, or
* (at your option) any later version.
*
* 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/>.
*/
use crate::func::{Args, FTable, Function, Operation};
use crate::segment::{ast_as_string, circuit, Ast, Ctr};
use crate::vars::VTable;
use std::cell::RefCell;
use std::rc::Rc;
// Current primitive is to use a get_NNNNN function defined for each library function which returns a not-previously-owned
// copy of the Function struct.
pub fn get_echo() -> Function {
return Function {
name: String::from("echo"),
loose_syms: false,
eval_lazy: false,
args: Args::Lazy(-1),
function: Operation::Internal(Box::new(
|a: Ast, _b: Rc<RefCell<VTable>>, _c: Rc<RefCell<FTable>>| -> Ctr {
let mut string = String::from("");
if !circuit(a, &mut |arg: &Ctr| {
match arg {
// should be a thing here
Ctr::Symbol(_) => return false,
Ctr::String(s) => string.push_str(&s),
Ctr::Integer(i) => string.push_str(&i.to_string()),
Ctr::Float(f) => string.push_str(&f.to_string()),
Ctr::Bool(b) => string.push_str(&b.to_string()),
Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()),
Ctr::None => (),
}
println!("{}", string);
return true;
}) {
eprintln!("circuit loop in echo should not have returned false")
}
return Ctr::None;
},
)),
};
}
pub fn get_concat() -> Function {
return Function {
name: String::from("concat"),
loose_syms: false,
eval_lazy: false,
args: Args::Lazy(-1),
function: Operation::Internal(Box::new(
|a: Ast, _b: Rc<RefCell<VTable>>, _c: Rc<RefCell<FTable>>| -> Ctr {
let mut string = String::from("");
if !circuit(a, &mut |arg: &Ctr| {
match arg {
// should be a thing here
Ctr::Symbol(_) => return false,
Ctr::String(s) => string.push_str(&s),
Ctr::Integer(i) => string.push_str(&i.to_string()),
Ctr::Float(f) => string.push_str(&f.to_string()),
Ctr::Bool(b) => string.push_str(&b.to_string()),
Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()),
Ctr::None => (),
}
return true;
}) {
eprintln!("circuit loop in concat should not have returned false")
}
return Ctr::String(string);
},
)),
};
}

View file

@ -18,7 +18,7 @@
use crate::eval::eval;
use crate::segment::{Seg, Ctr, Type};
use std::collections::HashMap;
use std::rc::Rc;
pub struct SymTable(HashMap<String, Symbol>);
#[derive(Debug, Clone)]
@ -39,7 +39,7 @@ pub struct UserFn {
*/
#[derive(Clone)]
pub enum ValueType {
Internal(Box<fn(&Seg, &mut SymTable) -> Ctr>),
Internal(Rc<dyn Fn(&Seg, &mut SymTable) -> Ctr>),
FuncForm(UserFn),
VarForm(Box<Ctr>)
}