Skip to content

Commit

Permalink
AST - Draft let-else impl
Browse files Browse the repository at this point in the history
  • Loading branch information
thepowersgang committed Dec 3, 2023
1 parent 3b6ea38 commit 12d304a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/ast/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,14 @@ NODE(ExprNode_Flow, {

NODE(ExprNode_LetBinding, {
os << "let " << m_pat << ": " << m_type;
if(m_value)
if(m_value) {
os << " = " << *m_value;
if( m_else ) {
os << " else " << *m_else;
}
}
},{
return NEWNODE(ExprNode_LetBinding, m_pat.clone(), m_type.clone(), OPT_CLONE(m_value));
return NEWNODE(ExprNode_LetBinding, m_pat.clone(), m_type.clone(), OPT_CLONE(m_value), OPT_CLONE(m_else));
})

NODE(ExprNode_Assign, {
Expand Down Expand Up @@ -664,6 +668,7 @@ NV(ExprNode_LetBinding,
{
// TODO: Handle recurse into Let pattern?
visit(node.m_value);
visit(node.m_else);
})
NV(ExprNode_Assign,
{
Expand Down
10 changes: 6 additions & 4 deletions src/ast/expr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,13 @@ struct ExprNode_LetBinding:
Pattern m_pat;
TypeRef m_type;
ExprNodeP m_value;
ExprNodeP m_else;

ExprNode_LetBinding(Pattern pat, TypeRef type, ExprNodeP value):
m_pat( move(pat) ),
m_type( move(type) ),
m_value( move(value) )
ExprNode_LetBinding(Pattern pat, TypeRef type, ExprNodeP value, ExprNodeP else_arm={})
: m_pat( move(pat) )
, m_type( move(type) )
, m_value( move(value) )
, m_else( move(else_arm) )
{
}

Expand Down
3 changes: 3 additions & 0 deletions src/hir/from_ast_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ struct LowerHIR_ExprNode_Visitor:
}
}
virtual void visit(::AST::ExprNode_LetBinding& v) override {
if( v.m_else ) {
TODO(v.span(), "Handle let-else in HIR expand, or elsewhere?");
}
m_rv.reset( new ::HIR::ExprNode_Let( v.span(),
LowerHIR_Pattern( v.m_pat ),
LowerHIR_Type( v.m_type ),
Expand Down
7 changes: 6 additions & 1 deletion src/parse/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,13 +657,18 @@ ExprNodeP Parse_Stmt_Let(TokenStream& lex)
GET_TOK(tok, lex);
}
ExprNodeP val;
ExprNodeP else_arm;
if( tok.type() == TOK_EQUAL ) {
val = Parse_Expr0(lex);
if( lex.lookahead(0) == TOK_RWORD_ELSE ) {
GET_TOK(tok, lex);
else_arm = Parse_ExprBlockNode(lex);
}
}
else {
PUTBACK(tok, lex);
}
return NEWNODE( AST::ExprNode_LetBinding, ::std::move(pat), mv$(type), ::std::move(val) );
return NEWNODE( AST::ExprNode_LetBinding, ::std::move(pat), mv$(type), ::std::move(val), ::std::move(else_arm) );
}

::std::vector<ExprNodeP> Parse_ParenList(TokenStream& lex)
Expand Down

0 comments on commit 12d304a

Please sign in to comment.