Adjustments to Primitive type

This commit is contained in:
Greg Shuflin 2021-10-25 19:42:49 -07:00
parent 0a2f06f598
commit 59956903f2
1 changed files with 24 additions and 50 deletions

View File

@ -78,6 +78,8 @@ fn paren_wrapped(terms: impl Iterator<Item=String>) -> String {
buf
}
/// Anything that can be stored in memory; that is, a function definition, or a fully-evaluated
/// program value.
#[derive(Debug)]
enum MemoryValue {
Function(FunctionDefinition),
@ -90,33 +92,6 @@ impl From<Primitive> for MemoryValue {
}
}
fn expr_to_repl(expr: &Expression) -> String {
match expr {
Expression::Literal(lit) => match lit {
Literal::Nat(n) => format!("{}", n),
Literal::Int(i) => format!("{}", i),
Literal::Float(f) => format!("{}", f),
Literal::Bool(b) => format!("{}", b),
Literal::StringLit(s) => format!("\"{}\"", s),
}
Expression::Tuple(terms) => paren_wrapped(terms.iter().map(|x| expr_to_repl(x))),
Expression::Assign { .. } => {
"".to_string() //TODO maybe the repl should say *something* here?
},
e => format!("Expression {:?} shouldn't be here", e),
}
}
impl Primitive {
fn to_repl(&self) -> String {
match self {
Primitive::Object { type_id, items, .. } => {
format!("{}{}", type_id.local_name(), paren_wrapped(items.iter().map(|item| item.to_repl())))
},
prim => expr_to_repl(&prim.to_expr()),
}
}
}
impl MemoryValue {
fn to_repl(&self) -> String {
@ -159,6 +134,23 @@ enum Primitive {
}
impl Primitive {
fn to_repl(&self) -> String {
match self {
Primitive::Object { type_id, items, .. } => {
format!("{}{}", type_id.local_name(), paren_wrapped(items.iter().map(|item| item.to_repl())))
},
Primitive::Literal(lit) => match lit {
Literal::Nat(n) => format!("{}", n),
Literal::Int(i) => format!("{}", i),
Literal::Float(f) => format!("{}", f),
Literal::Bool(b) => format!("{}", b),
Literal::StringLit(s) => format!("\"{}\"", s),
}
Primitive::Tuple(terms) => paren_wrapped(terms.iter().map(|x| x.to_repl())),
Primitive::Callable(..) => "<some-callable>".to_string(),
}
}
fn unit() -> Self {
Primitive::Tuple(vec![])
}
@ -170,24 +162,6 @@ impl From<Literal> for Primitive {
}
}
impl Primitive {
fn to_expr(&self) -> Expression {
match self {
Primitive::Tuple(items) => Expression::Tuple(items.iter().map(|item| item.to_expr()).collect()),
Primitive::Literal(lit) => Expression::Literal(lit.clone()),
Primitive::Callable(function) => Expression::Callable(function.clone()),
Primitive::Object { type_id, tag, items } if items.len() == 0 => {
Expression::Literal(Literal::Nat(420))
},
Primitive::Object { type_id, tag, items } => Expression::Call {
f: Box::new(Expression::Callable(Callable::DataConstructor { type_id: type_id.clone(), arity: items.len() as u32, tag: *tag as u32 })),
args: items.iter().map(|arg| arg.to_expr()).collect(),
},
}
}
}
impl<'a> State<'a> {
pub fn new() -> Self {
Self {
@ -334,10 +308,10 @@ impl<'a> State<'a> {
fn apply_builtin(&mut self, builtin: Builtin, args: Vec<Expression>) -> EvalResult<Primitive> {
use Builtin::*;
use Literal::*;
use Expression::Literal as Lit;
use Primitive::Literal as Lit;
let evaled_args: EvalResult<Vec<Expression>> =
args.into_iter().map(|arg| self.expression(arg).map(|prim| prim.to_expr())).collect();
let evaled_args: EvalResult<Vec<Primitive>> =
args.into_iter().map(|arg| self.expression(arg)).collect();
let evaled_args = evaled_args?;
Ok(match (builtin, evaled_args.as_slice()) {
@ -346,11 +320,11 @@ impl<'a> State<'a> {
}
/* builtin functions */
(IOPrint, &[ref anything]) => {
print!("{}", expr_to_repl(anything));
print!("{}", anything.to_repl());
Primitive::Tuple(vec![])
},
(IOPrintLn, &[ref anything]) => {
println!("{}", expr_to_repl(anything));
print!("{}", anything.to_repl());
Primitive::Tuple(vec![])
},
(IOGetLine, &[]) => {