diff --git a/schala-lang/language/src/ast/visitor.rs b/schala-lang/language/src/ast/visitor.rs index 9a1558d..9936cf4 100644 --- a/schala-lang/language/src/ast/visitor.rs +++ b/schala-lang/language/src/ast/visitor.rs @@ -21,7 +21,7 @@ pub trait ASTVisitor: Sized { fn named_struct(&mut self, _name: &QualifiedName, _fields: &Vec<(Rc, Expression)>) {} fn call(&mut self, _f: &Expression, _arguments: &Vec) {} fn index(&mut self, _indexee: &Expression, _indexers: &Vec) {} - fn if_expression(&mut self, _discrim: &Discriminator, _body: &IfExpressionBody) {} + fn if_expression(&mut self, _discrim: Option<&Expression>, _body: &IfExpressionBody) {} fn while_expression(&mut self, _condition: Option<&Expression>, _body: &Block) {} fn for_expression(&mut self, _enumerators: &Vec, _body: &ForBody) {} fn lambda(&mut self, _params: &Vec, _type_anno: Option<&TypeIdentifier>, _body: &Block) {} diff --git a/schala-lang/language/src/ast/walker.rs b/schala-lang/language/src/ast/walker.rs index ea8782b..d1462c0 100644 --- a/schala-lang/language/src/ast/walker.rs +++ b/schala-lang/language/src/ast/walker.rs @@ -2,7 +2,7 @@ use std::rc::Rc; use crate::ast::*; use crate::ast::visitor::ASTVisitor; -use std::ops::Deref; +use crate::util::deref_optional_box; pub fn ast(v: &mut V, ast: &AST) { v.ast(ast); @@ -143,8 +143,8 @@ fn expression_kind(v: &mut V, expression_kind: &ExpressionKind) { v.index(indexee, indexers); index(v, indexee, indexers); }, - IfExpression { discriminator, body } => v.if_expression(discriminator, body), - WhileExpression { condition, body } => v.while_expression(condition.as_ref().map(|b: &Box| Deref::deref(b)), body), + IfExpression { discriminator, body } => v.if_expression(deref_optional_box(discriminator), body), + WhileExpression { condition, body } => v.while_expression(deref_optional_box(condition), body), ForExpression { enumerators, body } => v.for_expression(enumerators, body), Lambda { params , type_anno, body } => { v.lambda(params, type_anno.as_ref(), body); diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index e9264b3..d5dc93f 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -825,9 +825,9 @@ impl Parser { #[recursive_descent_method] fn if_expr_body(&mut self) -> ParseResult { match self.token_handler.peek_kind() { - Keyword(Kw::Then) => self.simple_conditional()?, - Keyword(Kw::Is) => self.simple_pattern_match()? , - _ => self.cond_block()? + Keyword(Kw::Then) => self.simple_conditional(), + Keyword(Kw::Is) => self.simple_pattern_match(), + _ => self.cond_block(), } } diff --git a/schala-lang/language/src/typechecking.rs b/schala-lang/language/src/typechecking.rs index e256224..a2ccdbd 100644 --- a/schala-lang/language/src/typechecking.rs +++ b/schala-lang/language/src/typechecking.rs @@ -5,6 +5,7 @@ use ena::unify::{UnifyKey, InPlaceUnificationTable, UnificationTable, EqUnifyVal use crate::ast::*; use crate::util::ScopeStack; +use crate::util::deref_optional_box; #[derive(Debug, Clone, PartialEq)] @@ -318,7 +319,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(deref_optional_box(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)?, diff --git a/schala-lang/language/src/util.rs b/schala-lang/language/src/util.rs index 73add43..7e830ee 100644 --- a/schala-lang/language/src/util.rs +++ b/schala-lang/language/src/util.rs @@ -1,6 +1,11 @@ use std::collections::HashMap; use std::hash::Hash; use std::cmp::Eq; +use std::ops::Deref; + +pub fn deref_optional_box(x: &Option>) -> Option<&T> { + x.as_ref().map(|b: &Box| Deref::deref(b)) +} #[derive(Default, Debug)] pub struct ScopeStack<'a, T: 'a, V: 'a> where T: Hash + Eq {