big temp status
Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
parent
45453f819f
commit
5261efbc65
12 changed files with 960 additions and 224 deletions
40
src/control.rs
Normal file
40
src/control.rs
Normal 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
|
||||
})
|
||||
),
|
||||
};
|
||||
}
|
||||
24
src/lex.rs
24
src/lex.rs
|
|
@ -15,7 +15,7 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use crate::segment::{list_append, new_ast, Ast, Ctr};
|
||||
use crate::segment::{list_append, Ctr, Seg};
|
||||
|
||||
const UNMATCHED_STR_DELIM: &str = "Unmatched string delimiter in input";
|
||||
const UNMATCHED_LIST_DELIM: &str = "Unmatched list delimiter in input";
|
||||
|
|
@ -23,7 +23,7 @@ const UNMATCHED_LIST_DELIM: &str = "Unmatched list delimiter in input";
|
|||
/* takes a line of user input
|
||||
* returns an unsimplified tree of tokens.
|
||||
*/
|
||||
pub fn lex(document: String) -> Result<Ast, String> {
|
||||
pub fn lex<'a>(document: String) -> Result<Box<Seg<'a>>, String> {
|
||||
if !document.is_ascii() {
|
||||
return Err("document may only contain ascii characters".to_string());
|
||||
}
|
||||
|
|
@ -42,7 +42,7 @@ pub fn lex(document: String) -> Result<Ast, String> {
|
|||
* Returns Ok(Rc<Seg>) if lexing passes
|
||||
* Returns Err(String) if an error occurs
|
||||
*/
|
||||
fn process(document: String) -> Result<Ast, String> {
|
||||
fn process<'a>(document: &'a String) -> Result<Box<Seg<'a>>, String> {
|
||||
let doc_len = document.len();
|
||||
|
||||
if doc_len == 0 {
|
||||
|
|
@ -120,7 +120,7 @@ fn process(document: String) -> Result<Ast, String> {
|
|||
return Err("list started in middle of another token".to_string());
|
||||
}
|
||||
|
||||
ref_stack.push(new_ast(Ctr::None, Ctr::None));
|
||||
ref_stack.push(Box::new(Seg::new()));
|
||||
|
||||
delim_stack.push(')');
|
||||
}
|
||||
|
|
@ -152,13 +152,12 @@ fn process(document: String) -> Result<Ast, String> {
|
|||
return Err("Empty token".to_string());
|
||||
}
|
||||
|
||||
let mut current_seg_ref = ref_stack.pop().unwrap();
|
||||
let mut current_seg = ref_stack.pop();
|
||||
let mut obj;
|
||||
if is_str {
|
||||
obj = Ctr::String(token);
|
||||
is_str = false;
|
||||
token = String::new();
|
||||
list_append(current_seg_ref.clone(), obj);
|
||||
} else if token.len() > 0 {
|
||||
if token == "true" {
|
||||
obj = Ctr::Bool(true);
|
||||
|
|
@ -175,22 +174,23 @@ fn process(document: String) -> Result<Ast, String> {
|
|||
}
|
||||
|
||||
token = String::new();
|
||||
list_append(current_seg_ref.clone(), obj);
|
||||
}
|
||||
|
||||
list_append(current_seg, obj);
|
||||
|
||||
if alloc_list {
|
||||
// return if we have finished the document
|
||||
if ref_stack.len() == 0 {
|
||||
return Ok(current_seg_ref);
|
||||
return Ok(current_seg);
|
||||
}
|
||||
|
||||
// shortening this will lead to naught but pain
|
||||
obj = Ctr::Seg(current_seg_ref.clone());
|
||||
current_seg_ref = ref_stack.pop().unwrap();
|
||||
list_append(current_seg_ref.clone(), obj);
|
||||
obj = Ctr::Seg(current_seg.into_raw());
|
||||
current_seg = ref_stack.pop();
|
||||
list_append(current_seg, obj);
|
||||
}
|
||||
|
||||
ref_stack.push(current_seg_ref);
|
||||
ref_stack.push(current_seg);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#![feature(derive_default_enum)]
|
||||
|
||||
mod append;
|
||||
mod config;
|
||||
mod eval;
|
||||
|
|
@ -31,7 +33,7 @@ pub mod ast {
|
|||
func_call, func_declare, Args, ExternalOperation, FTable, Function, Operation,
|
||||
};
|
||||
pub use crate::lex::lex;
|
||||
pub use crate::segment::{ast_to_string, new_ast, Ast, Ctr, Seg, Type};
|
||||
pub use crate::segment::{Ctr, Seg, Type};
|
||||
pub use crate::vars::{define, VTable};
|
||||
}
|
||||
|
||||
|
|
|
|||
221
src/segment.rs
221
src/segment.rs
|
|
@ -14,22 +14,19 @@
|
|||
* 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 std::fmt;
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
// Recursive data type for a tree of Segments
|
||||
pub type Ast = Rc<RefCell<Seg>>;
|
||||
|
||||
// Container
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Ctr {
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub enum Ctr <'a> {
|
||||
Symbol(String),
|
||||
String(String),
|
||||
Integer(i128),
|
||||
Float(f64),
|
||||
Bool(bool),
|
||||
Seg(Ast),
|
||||
Seg(Seg<'a>),
|
||||
#[default]
|
||||
None,
|
||||
}
|
||||
|
||||
|
|
@ -48,23 +45,21 @@ pub enum Type {
|
|||
/* Segment
|
||||
* Holds two Containers.
|
||||
* Basic building block for more complex data structures.
|
||||
* I was going to call it Cell and then I learned about
|
||||
* how important RefCells were in Rust
|
||||
*/
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Seg {
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Seg <'a> {
|
||||
/* "Contents of Address Register"
|
||||
* Historical way of referring to the first value in a cell.
|
||||
*/
|
||||
pub car: Ctr,
|
||||
pub car: &mut Ctr<'a>,
|
||||
|
||||
/* "Contents of Decrement Register"
|
||||
* Historical way of referring to the second value in a cell.
|
||||
*/
|
||||
pub cdr: Ctr,
|
||||
pub cdr: &mut Ctr<'a>,
|
||||
}
|
||||
|
||||
impl Ctr {
|
||||
impl Ctr<'_> {
|
||||
pub fn to_type(&self) -> Type {
|
||||
match self {
|
||||
Ctr::Symbol(_s) => Type::Symbol,
|
||||
|
|
@ -76,10 +71,65 @@ impl Ctr {
|
|||
Ctr::None => Type::None,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn seg_to_string(s: &Seg, parens: bool) -> String {
|
||||
let mut string = String::new();
|
||||
match s.car {
|
||||
Ctr::None => string.push_str("<nil>"),
|
||||
_ => string.push_str(s.car),
|
||||
}
|
||||
string.push(' ');
|
||||
match s.cdr {
|
||||
Ctr::Seg(inner) => string.push_str(seg_to_string(inner, false)),
|
||||
Ctr::None => {},
|
||||
_ => string.push_str(s.cdr),
|
||||
}
|
||||
|
||||
if parens {
|
||||
String::from("(" + string + ")")
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Ctr <'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Ctr::Symbol(s) => write!(f, "{}", s),
|
||||
Ctr::String(s) => write!(f, "\'{}\'", s),
|
||||
Ctr::Integer(s) => write!(f, "{}", s),
|
||||
Ctr::Float(s) => write!(f, "{}", s),
|
||||
Ctr::Bool(s) => {
|
||||
if s {
|
||||
write!(f, "T")
|
||||
} else {
|
||||
write!(f, "F")
|
||||
}
|
||||
},
|
||||
Ctr::Seg(s) => write!(f, "{}", s),
|
||||
Ctr::None => Ok(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Seg<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", seg_to_string(self, true))
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Seg<'_> {
|
||||
fn next(&self) -> Option<&Seg> {
|
||||
if let Ctr::Seg(s) = self.cdr {
|
||||
Ok(s)
|
||||
} else {
|
||||
None()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Type {
|
||||
pub fn to_str(&self) -> String {
|
||||
pub fn to_string(&self) -> String {
|
||||
let ret: &str;
|
||||
match self {
|
||||
Type::Symbol => ret = "symbol",
|
||||
|
|
@ -95,84 +145,16 @@ impl Type {
|
|||
}
|
||||
}
|
||||
|
||||
/* Prints a Syntax Tree as a string
|
||||
*/
|
||||
pub fn ast_as_string(c: Ast, with_parens: bool) -> String {
|
||||
let mut string = String::new();
|
||||
let mut prn_space = true;
|
||||
let seg = c.borrow();
|
||||
match &seg.car {
|
||||
Ctr::Symbol(s) => string.push_str(&s),
|
||||
Ctr::String(s) => {
|
||||
string.push('\'');
|
||||
string.push_str(&s);
|
||||
string.push('\'');
|
||||
}
|
||||
Ctr::Integer(i) => string = string + &i.to_string(),
|
||||
Ctr::Float(f) => string = string + &f.to_string(),
|
||||
Ctr::Bool(b) => string = string + &b.to_string(),
|
||||
Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), true).as_str()),
|
||||
Ctr::None => prn_space = false,
|
||||
}
|
||||
|
||||
if prn_space {
|
||||
string.push(' ');
|
||||
}
|
||||
|
||||
match &seg.cdr {
|
||||
Ctr::Symbol(s) => string.push_str(&s),
|
||||
Ctr::String(s) => {
|
||||
string.push('\'');
|
||||
string.push_str(&s);
|
||||
string.push('\'');
|
||||
}
|
||||
Ctr::Integer(i) => string = string + &i.to_string(),
|
||||
Ctr::Float(f) => string = string + &f.to_string(),
|
||||
Ctr::Bool(b) => string = string + &b.to_string(),
|
||||
Ctr::Seg(c) => string.push_str(ast_as_string(c.clone(), false).as_str()),
|
||||
Ctr::None => {
|
||||
if prn_space {
|
||||
string.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: maybe a better way to do this
|
||||
if with_parens {
|
||||
let mut extra = String::from("(");
|
||||
extra.push_str(&string);
|
||||
extra.push(')');
|
||||
string = extra
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
pub fn ast_to_string(c: Ast) -> String {
|
||||
ast_as_string(c.clone(), true)
|
||||
}
|
||||
|
||||
/* NOTE: "Standard form" is used here to refer to a list of segments
|
||||
* that resembles a typical linked list. This means that Car may hold whatever,
|
||||
* but Cdr must either be Seg or None.
|
||||
*/
|
||||
|
||||
/* Initializes a new ast node with segment car and cdr passed in
|
||||
*/
|
||||
pub fn new_ast(car: Ctr, cdr: Ctr) -> Ast {
|
||||
Rc::new(RefCell::new(Seg { car: car, cdr: cdr }))
|
||||
}
|
||||
|
||||
/* applies a function across a list in standard form
|
||||
* function must take a Ctr and return a bool
|
||||
* short circuits on the first false returned.
|
||||
* also returns false on a non standard form list
|
||||
*/
|
||||
pub fn circuit<F: FnMut(&Ctr) -> bool>(tree: Ast, func: &mut F) -> bool {
|
||||
let inner = tree.borrow();
|
||||
if func(&inner.car) {
|
||||
match &inner.cdr {
|
||||
pub fn circuit<F: Fn(&Ctr)>(list: &Seg, func: &mut F) -> bool {
|
||||
if func(&list.car) {
|
||||
match list.cdr {
|
||||
Ctr::None => true,
|
||||
Ctr::Seg(c) => circuit(c.clone(), func),
|
||||
Ctr::Seg(l) => circuit(l, func),
|
||||
_ => false,
|
||||
}
|
||||
} else {
|
||||
|
|
@ -183,55 +165,46 @@ pub fn circuit<F: FnMut(&Ctr) -> bool>(tree: Ast, func: &mut F) -> bool {
|
|||
/* recurs over ast assumed to be list in standard form
|
||||
* returns length
|
||||
*/
|
||||
pub fn list_len(tree: Ast) -> u128 {
|
||||
match &tree.borrow().cdr {
|
||||
Ctr::Seg(c) => list_len(c.clone()) + 1,
|
||||
_ => 1,
|
||||
}
|
||||
pub fn list_len(list: &Seg) -> u128 {
|
||||
let mut len = 0;
|
||||
circuit(list, &circuit(&mut |c: &Ctr| -> bool { len += 1; true }))
|
||||
}
|
||||
|
||||
/* recurs over tree assumed to be list in standard form
|
||||
* returns clone of ctr at index provided
|
||||
*
|
||||
* TODO: return result (or option?)
|
||||
*/
|
||||
pub fn list_idx(tree: Ast, idx: u128) -> Ctr {
|
||||
let inner = tree.borrow();
|
||||
pub fn list_idx<'a>(list: &Seg, idx: u128) -> Ctr<'a> {
|
||||
if idx > 0 {
|
||||
match &inner.cdr {
|
||||
Ctr::None => Ctr::None,
|
||||
Ctr::Seg(c) => list_idx(c.clone(), idx - 1),
|
||||
_ => {
|
||||
if idx == 1 {
|
||||
inner.cdr.clone()
|
||||
} else {
|
||||
Ctr::None
|
||||
}
|
||||
}
|
||||
if let Ctr::Seg(s) = list.car {
|
||||
list_idx(s, idx - 1)
|
||||
} else if idx == 1 {
|
||||
list.cdr
|
||||
} else {
|
||||
Ctr::None
|
||||
}
|
||||
} else {
|
||||
match inner.car {
|
||||
Ctr::None => Ctr::None,
|
||||
_ => inner.car.clone(),
|
||||
}
|
||||
list.car
|
||||
}
|
||||
}
|
||||
|
||||
/* recurs over tree assumed to be list in standard form
|
||||
* appends object to end of list
|
||||
*
|
||||
* TODO: return result
|
||||
*/
|
||||
pub fn list_append(tree: Ast, obj: Ctr) {
|
||||
let mut inner = tree.borrow_mut();
|
||||
match &inner.car {
|
||||
Ctr::None => {
|
||||
inner.car = obj;
|
||||
}
|
||||
_ => match &inner.cdr {
|
||||
Ctr::None => {
|
||||
inner.cdr = Ctr::Seg(new_ast(obj, Ctr::None));
|
||||
}
|
||||
Ctr::Seg(tr) => {
|
||||
list_append(tr.clone(), obj);
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
pub fn list_append<'a>(list: &Seg, obj: Ctr) {
|
||||
if let Ctr::None = list.car {
|
||||
list.car = obj;
|
||||
return
|
||||
}
|
||||
|
||||
if let Ctr::Seg(s) = list.cdr {
|
||||
list_append(s, obj)
|
||||
}
|
||||
|
||||
if let Ctr::None = list.cdr {
|
||||
list.cdr = Ctr::Seg(&Seg{car:obj, cdr:Ctr::None})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ use crate::append::get_append;
|
|||
use crate::func::{func_declare, FTable};
|
||||
use crate::segment::Ctr;
|
||||
use crate::str::{get_concat, get_echo};
|
||||
use crate::control::{get_if};
|
||||
use crate::vars::{get_export, VTable};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
|
@ -53,5 +54,9 @@ pub fn get_stdlib(conf: Rc<RefCell<VTable>>) -> Result<Rc<RefCell<FTable>>, Stri
|
|||
return Err(s);
|
||||
}
|
||||
|
||||
if let Some(s) = func_declare(ft.clone(), Rc::new(RefCell::new(get_if()))) {
|
||||
return Err(s);
|
||||
}
|
||||
|
||||
return Ok(ft);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue