From 75935db9e68fe289df14796b7f0e5af2494956d6 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Wed, 20 Oct 2021 00:18:44 -0700 Subject: [PATCH] Get rid of ReducedAST unit Treat it as the empty tuple instead --- schala-lang/language/src/eval/mod.rs | 11 +++++------ schala-lang/language/src/eval/test.rs | 2 +- schala-lang/language/src/reduced_ast/types.rs | 8 +++++++- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/schala-lang/language/src/eval/mod.rs b/schala-lang/language/src/eval/mod.rs index 8f238da..01d35b8 100644 --- a/schala-lang/language/src/eval/mod.rs +++ b/schala-lang/language/src/eval/mod.rs @@ -118,7 +118,7 @@ impl Expr { match self { ConditionalTargetSigilValue => replacement.clone(), - Unit | Lit(_) | Func(_) | Sym(_) | Constructor { .. } | + Lit(_) | Func(_) | Sym(_) | Constructor { .. } | CaseMatch { .. } | UnimplementedSigilValue | ReductionError(_) => self, Tuple(exprs) => Tuple(exprs.into_iter().map(|e| e.replace_conditional_target_sigil(replacement)).collect()), Call { f, args } => { @@ -186,7 +186,7 @@ impl<'a> State<'a> { for stmt in stmts { ret = self.statement(stmt)?; } - Ok(ret.unwrap_or(Node::Expr(Expr::Unit))) + Ok(ret.unwrap_or(Node::Expr(Expr::unit()))) } fn expression(&mut self, node: Node) -> EvalResult { @@ -210,7 +210,6 @@ impl<'a> State<'a> { }, Conditional { box cond, then_clause, else_clause } => self.conditional(cond, then_clause, else_clause), Assign { box val, box expr } => self.assign_expression(val, expr), - Unit => Ok(Node::Expr(Unit)), CaseMatch { box cond, alternatives } => self.case_match_expression(cond, alternatives), ConditionalTargetSigilValue => Ok(Node::Expr(ConditionalTargetSigilValue)), UnimplementedSigilValue => Err("Sigil value eval not implemented".to_string()), @@ -331,11 +330,11 @@ impl<'a> State<'a> { /* builtin functions */ (IOPrint, &[ref anything]) => { print!("{}", anything.to_repl()); - Expr::Unit.to_node() + Expr::unit().to_node() }, (IOPrintLn, &[ref anything]) => { println!("{}", anything.to_repl()); - Expr::Unit.to_node() + Expr::unit().to_node() }, (IOGetLine, &[]) => { let mut buf = String::new(); @@ -370,7 +369,7 @@ impl<'a> State<'a> { } let val = self.expression(Node::Expr(expr))?; self.values.insert(name, ValueEntry::Binding { constant: false, val }); - Ok(Node::Expr(Expr::Unit)) + Ok(Node::Expr(Expr::unit())) } fn guard_passes(&mut self, guard: &Option, cond: &Node) -> EvalResult { diff --git a/schala-lang/language/src/eval/test.rs b/schala-lang/language/src/eval/test.rs index 41bfa18..185a5f4 100644 --- a/schala-lang/language/src/eval/test.rs +++ b/schala-lang/language/src/eval/test.rs @@ -30,7 +30,7 @@ macro_rules! test_in_fresh_env { #[test] fn test_basic_eval() { test_in_fresh_env!("1 + 2", "3"); - test_in_fresh_env!("let mut a = 1; a = 2", "Unit"); + test_in_fresh_env!("let mut a = 1; a = 2", "()"); /* test_in_fresh_env!("let mut a = 1; a = 2; a", "2"); test_in_fresh_env!(r#"("a", 1 + 2)"#, r#"("a", 3)"#); diff --git a/schala-lang/language/src/reduced_ast/types.rs b/schala-lang/language/src/reduced_ast/types.rs index 3743afd..d6aef6e 100644 --- a/schala-lang/language/src/reduced_ast/types.rs +++ b/schala-lang/language/src/reduced_ast/types.rs @@ -23,7 +23,6 @@ pub enum Stmt { #[derive(Debug, Clone)] pub enum Expr { - Unit, Lit(Lit), Sym(Rc), //a Sym is anything that can be looked up by name at runtime - i.e. a function or variable address Tuple(Vec), @@ -56,6 +55,13 @@ pub enum Expr { ReductionError(String), } +impl Expr { + // The unit value is an empty tuple + pub fn unit() -> Expr { + Expr::Tuple(vec![]) + } +} + pub type BoundVars = Vec>>; //remember that order matters here #[derive(Debug, Clone)]