Skip to content

Commit

Permalink
Merge branch 'main' into 103-parse-expr
Browse files Browse the repository at this point in the history
  • Loading branch information
oriollinan authored Jan 8, 2025
2 parents 7f433ec + 560a9bc commit 939c009
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/Ast/Parser/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ parseCall = do
parseIf :: PU.Parser AT.Expr
parseIf = do
srcLoc <- PU.parseSrcLoc
cond <- PU.symbol "if" *> parseExpr
cond <- PU.symbol "if" *> PU.lexeme parseExpr
then' <- parseBlock
else' <- M.optional $ PU.symbol "else" *> parseBlock
return $ AT.If {AT.ifLoc = srcLoc, AT.ifCond = cond, AT.ifThen = then', AT.ifElse = else'}
Expand Down
14 changes: 11 additions & 3 deletions lib/Codegen/Codegen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,25 @@ generateIf (AT.If _ cond then_ else_) = mdo

thenBlock <- IRM.block `IRM.named` U.stringToByteString "if.then"
thenValue <- generateExpr then_
I.br mergeBB

case TD.typeOf thenValue of
T.VoidType -> I.br mergeBB
_ -> pure ()

elseBlock <- IRM.block `IRM.named` U.stringToByteString "if.else"
elseValue <- case else_ of
Just e -> generateExpr e
Nothing -> pure $ AST.ConstantOperand $ C.Undef T.void
I.br mergeBB

case TD.typeOf elseValue of
T.VoidType -> I.br mergeBB
_ -> pure ()

mergeBB <- IRM.block `IRM.named` U.stringToByteString "if.merge"

I.phi [(thenValue, thenBlock), (elseValue, elseBlock)]
case TD.typeOf elseValue of
T.VoidType -> I.phi [(thenValue, elseBlock)]
_ -> I.phi [(thenValue, thenBlock), (elseValue, elseBlock)]
generateIf expr =
E.throwError $ CodegenError (U.getLoc expr) $ UnsupportedDefinition expr

Expand Down

0 comments on commit 939c009

Please # to comment.