Adjustments to Primitive type

This commit is contained in:
Greg Shuflin 2021-10-25 19:42:49 -07:00
parent 0a2f06f598
commit 59956903f2

View File

@ -78,6 +78,8 @@ fn paren_wrapped(terms: impl Iterator<Item=String>) -> String {
buf buf
} }
/// Anything that can be stored in memory; that is, a function definition, or a fully-evaluated
/// program value.
#[derive(Debug)] #[derive(Debug)]
enum MemoryValue { enum MemoryValue {
Function(FunctionDefinition), 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 { impl MemoryValue {
fn to_repl(&self) -> String { fn to_repl(&self) -> String {
@ -159,6 +134,23 @@ enum Primitive {
} }
impl 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 { fn unit() -> Self {
Primitive::Tuple(vec![]) 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> { impl<'a> State<'a> {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
@ -334,10 +308,10 @@ impl<'a> State<'a> {
fn apply_builtin(&mut self, builtin: Builtin, args: Vec<Expression>) -> EvalResult<Primitive> { fn apply_builtin(&mut self, builtin: Builtin, args: Vec<Expression>) -> EvalResult<Primitive> {
use Builtin::*; use Builtin::*;
use Literal::*; use Literal::*;
use Expression::Literal as Lit; use Primitive::Literal as Lit;
let evaled_args: EvalResult<Vec<Expression>> = let evaled_args: EvalResult<Vec<Primitive>> =
args.into_iter().map(|arg| self.expression(arg).map(|prim| prim.to_expr())).collect(); args.into_iter().map(|arg| self.expression(arg)).collect();
let evaled_args = evaled_args?; let evaled_args = evaled_args?;
Ok(match (builtin, evaled_args.as_slice()) { Ok(match (builtin, evaled_args.as_slice()) {
@ -346,11 +320,11 @@ impl<'a> State<'a> {
} }
/* builtin functions */ /* builtin functions */
(IOPrint, &[ref anything]) => { (IOPrint, &[ref anything]) => {
print!("{}", expr_to_repl(anything)); print!("{}", anything.to_repl());
Primitive::Tuple(vec![]) Primitive::Tuple(vec![])
}, },
(IOPrintLn, &[ref anything]) => { (IOPrintLn, &[ref anything]) => {
println!("{}", expr_to_repl(anything)); print!("{}", anything.to_repl());
Primitive::Tuple(vec![]) Primitive::Tuple(vec![])
}, },
(IOGetLine, &[]) => { (IOGetLine, &[]) => {