From 3bca82a8c89cd44d14dbc72ca8f701ddd7fe35de Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 10 Oct 2019 10:34:54 -0700 Subject: [PATCH] Still more refactoring work --- schala-lang/language/src/ast.rs | 6 ++--- schala-lang/language/src/parsing.rs | 12 +++++----- schala-lang/language/src/scope_resolution.rs | 24 -------------------- schala-lang/language/src/typechecking.rs | 8 +++---- 4 files changed, 13 insertions(+), 37 deletions(-) diff --git a/schala-lang/language/src/ast.rs b/schala-lang/language/src/ast.rs index 22b7bba..c362442 100644 --- a/schala-lang/language/src/ast.rs +++ b/schala-lang/language/src/ast.rs @@ -190,7 +190,7 @@ pub enum ExpressionKind { }, IfExpression { discriminator: Option>, - body: IfExpressionBody, + body: Box, }, WhileExpression { condition: Option>, @@ -242,8 +242,8 @@ pub struct ConditionArm { #[derive(Debug, PartialEq, Clone)] pub enum Condition { Pattern(Pattern), - TruncatedOp(BinOp, Box), - Expression(Box), + TruncatedOp(BinOp, Expression), + Expression(Expression), Else, } diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index c7a0435..e9264b3 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -818,7 +818,7 @@ impl Parser { } else { None }; - let body = if_expr_body()?; + let body = Box::new(self.if_expr_body()?); Ok(Expression::new(self.id_store.fresh(), ExpressionKind::IfExpression { discriminator, body })) } @@ -881,10 +881,10 @@ impl Parser { } #[recursive_descent_method] - fn cond_arm(&mut self) -> ParseResult { + fn cond_arm(&mut self) -> ParseResult { let (condition, guard) = if let Keyword(Kw::Else) = self.token_handler.peek_kind() { self.token_handler.next(); - (Condition:Else, None) + (Condition::Else, None) } else { let condition = self.condition()?; let guard = self.guard()?; @@ -902,9 +902,9 @@ impl Parser { self.token_handler.next(); Condition::Pattern(self.pattern()?) }, - Operator(ref op) if !PrefixOp::is_prefix(&*op) => { - let op = self.token_handler.next(); - let expr = Box::new(self.expression()?); + tok if BinOp::from_sigil_token(&tok).is_some() => { + let op = BinOp::from_sigil_token(&self.token_handler.next().kind).unwrap(); + let expr = self.expression()?; Condition::TruncatedOp(op, expr) }, _ => { diff --git a/schala-lang/language/src/scope_resolution.rs b/schala-lang/language/src/scope_resolution.rs index 7e05c03..8e912f0 100644 --- a/schala-lang/language/src/scope_resolution.rs +++ b/schala-lang/language/src/scope_resolution.rs @@ -120,30 +120,6 @@ impl<'a> ScopeResolver<'a> { } } }, - IfExpression { ref body, ref discriminator } => { - match &**discriminator { - Discriminator::Simple(expr) | Discriminator::BinOp(expr, _) => self.expr(expr)? - }; - - match &**body { - IfExpressionBody::SimplePatternMatch(ref pat, ref alt1, ref alt2) => { - self.pattern(pat)?; - self.block(alt1)?; - if let Some(alt) = alt2 { - self.block(alt)?; - } - }, - IfExpressionBody::GuardList(guardarms) => { - for arm in guardarms.iter() { - if let Guard::Pat(ref pat) = arm.guard { - self.pattern(pat)?; - } - self.block(&arm.body)?; - } - } - _ => () - } - }, _ => () }; Ok(()) diff --git a/schala-lang/language/src/typechecking.rs b/schala-lang/language/src/typechecking.rs index b25d036..e256224 100644 --- a/schala-lang/language/src/typechecking.rs +++ b/schala-lang/language/src/typechecking.rs @@ -318,7 +318,7 @@ impl<'a> TypeContext<'a> { StringLiteral(_) => ty!(StringT), PrefixExp(op, expr) => self.prefix(op, expr)?, BinExp(op, lhs, rhs) => self.binexp(op, lhs, rhs)?, - IfExpression { discriminator, body } => self.if_expr(discriminator, body)?, + IfExpression { discriminator, body } => self.if_expr(&*discriminator, &**body)?, Value(val) => self.handle_value(val)?, Call { box ref f, arguments } => self.call(f, arguments)?, Lambda { params, type_anno, body } => self.lambda(params, type_anno, body)?, @@ -348,10 +348,10 @@ impl<'a> TypeContext<'a> { self.handle_apply(tf, vec![t_lhs, t_rhs]) } - fn if_expr(&mut self, discriminator: &Discriminator, body: &IfExpressionBody) -> InferResult { - use self::Discriminator::*; use self::IfExpressionBody::*; + fn if_expr(&mut self, discriminator: Option<&Expression>, body: &IfExpressionBody) -> InferResult { + use self::IfExpressionBody::*; match (discriminator, body) { - (Simple(expr), SimpleConditional(then_clause, else_clause)) => self.handle_simple_if(expr, then_clause, else_clause), + (Some(expr), SimpleConditional{ then_case, else_case }) => self.handle_simple_if(expr, then_case, else_case), _ => TypeError::new(format!("Complex conditionals not supported")) } }