From 59956903f25e06a7a2291361fbc1aab89fb3bc0d Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Mon, 25 Oct 2021 19:42:49 -0700 Subject: [PATCH] Adjustments to Primitive type --- .../language/src/tree_walk_eval/mod.rs | 74 ++++++------------- 1 file changed, 24 insertions(+), 50 deletions(-) diff --git a/schala-lang/language/src/tree_walk_eval/mod.rs b/schala-lang/language/src/tree_walk_eval/mod.rs index cb8c899..29a3fde 100644 --- a/schala-lang/language/src/tree_walk_eval/mod.rs +++ b/schala-lang/language/src/tree_walk_eval/mod.rs @@ -78,6 +78,8 @@ fn paren_wrapped(terms: impl Iterator) -> 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 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(..) => "".to_string(), + } + } + fn unit() -> Self { Primitive::Tuple(vec![]) } @@ -170,24 +162,6 @@ impl From 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) -> EvalResult { use Builtin::*; use Literal::*; - use Expression::Literal as Lit; + use Primitive::Literal as Lit; - let evaled_args: EvalResult> = - args.into_iter().map(|arg| self.expression(arg).map(|prim| prim.to_expr())).collect(); + let evaled_args: EvalResult> = + 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, &[]) => {