Get rid of ReducedAST unit

Treat it as the empty tuple instead
This commit is contained in:
Greg Shuflin 2021-10-20 00:18:44 -07:00
parent 2f669b77fd
commit 75935db9e6
3 changed files with 13 additions and 8 deletions

View File

@ -118,7 +118,7 @@ impl Expr {
match self { match self {
ConditionalTargetSigilValue => replacement.clone(), ConditionalTargetSigilValue => replacement.clone(),
Unit | Lit(_) | Func(_) | Sym(_) | Constructor { .. } | Lit(_) | Func(_) | Sym(_) | Constructor { .. } |
CaseMatch { .. } | UnimplementedSigilValue | ReductionError(_) => self, CaseMatch { .. } | UnimplementedSigilValue | ReductionError(_) => self,
Tuple(exprs) => Tuple(exprs.into_iter().map(|e| e.replace_conditional_target_sigil(replacement)).collect()), Tuple(exprs) => Tuple(exprs.into_iter().map(|e| e.replace_conditional_target_sigil(replacement)).collect()),
Call { f, args } => { Call { f, args } => {
@ -186,7 +186,7 @@ impl<'a> State<'a> {
for stmt in stmts { for stmt in stmts {
ret = self.statement(stmt)?; 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<Node> { fn expression(&mut self, node: Node) -> EvalResult<Node> {
@ -210,7 +210,6 @@ impl<'a> State<'a> {
}, },
Conditional { box cond, then_clause, else_clause } => self.conditional(cond, then_clause, else_clause), Conditional { box cond, then_clause, else_clause } => self.conditional(cond, then_clause, else_clause),
Assign { box val, box expr } => self.assign_expression(val, expr), 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), CaseMatch { box cond, alternatives } => self.case_match_expression(cond, alternatives),
ConditionalTargetSigilValue => Ok(Node::Expr(ConditionalTargetSigilValue)), ConditionalTargetSigilValue => Ok(Node::Expr(ConditionalTargetSigilValue)),
UnimplementedSigilValue => Err("Sigil value eval not implemented".to_string()), UnimplementedSigilValue => Err("Sigil value eval not implemented".to_string()),
@ -331,11 +330,11 @@ impl<'a> State<'a> {
/* builtin functions */ /* builtin functions */
(IOPrint, &[ref anything]) => { (IOPrint, &[ref anything]) => {
print!("{}", anything.to_repl()); print!("{}", anything.to_repl());
Expr::Unit.to_node() Expr::unit().to_node()
}, },
(IOPrintLn, &[ref anything]) => { (IOPrintLn, &[ref anything]) => {
println!("{}", anything.to_repl()); println!("{}", anything.to_repl());
Expr::Unit.to_node() Expr::unit().to_node()
}, },
(IOGetLine, &[]) => { (IOGetLine, &[]) => {
let mut buf = String::new(); let mut buf = String::new();
@ -370,7 +369,7 @@ impl<'a> State<'a> {
} }
let val = self.expression(Node::Expr(expr))?; let val = self.expression(Node::Expr(expr))?;
self.values.insert(name, ValueEntry::Binding { constant: false, val }); 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<Expr>, cond: &Node) -> EvalResult<bool> { fn guard_passes(&mut self, guard: &Option<Expr>, cond: &Node) -> EvalResult<bool> {

View File

@ -30,7 +30,7 @@ macro_rules! test_in_fresh_env {
#[test] #[test]
fn test_basic_eval() { fn test_basic_eval() {
test_in_fresh_env!("1 + 2", "3"); 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!("let mut a = 1; a = 2; a", "2");
test_in_fresh_env!(r#"("a", 1 + 2)"#, r#"("a", 3)"#); test_in_fresh_env!(r#"("a", 1 + 2)"#, r#"("a", 3)"#);

View File

@ -23,7 +23,6 @@ pub enum Stmt {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Expr { pub enum Expr {
Unit,
Lit(Lit), Lit(Lit),
Sym(Rc<String>), //a Sym is anything that can be looked up by name at runtime - i.e. a function or variable address Sym(Rc<String>), //a Sym is anything that can be looked up by name at runtime - i.e. a function or variable address
Tuple(Vec<Expr>), Tuple(Vec<Expr>),
@ -56,6 +55,13 @@ pub enum Expr {
ReductionError(String), ReductionError(String),
} }
impl Expr {
// The unit value is an empty tuple
pub fn unit() -> Expr {
Expr::Tuple(vec![])
}
}
pub type BoundVars = Vec<Option<Rc<String>>>; //remember that order matters here pub type BoundVars = Vec<Option<Rc<String>>>; //remember that order matters here
#[derive(Debug, Clone)] #[derive(Debug, Clone)]