From bd3cf3c3f12cd74aa0e02e3cad717aaf1473290e Mon Sep 17 00:00:00 2001 From: Samuel Hammersberg Date: Mon, 27 Mar 2023 13:40:18 +0200 Subject: [PATCH] Fixed simple pattern matching. --- Justfile | 2 +- src/Codegen/Codegen.hs | 9 ++++++++- src/Monomorphizer/MonomorphizerIr.hs | 12 ++++++------ test_program.crf | 5 ++--- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Justfile b/Justfile index 2e2f216..d804399 100644 --- a/Justfile +++ b/Justfile @@ -15,4 +15,4 @@ test: # compile a specific file run FILE: - cabal run language {{FILE}} + cabal run language -- -d {{FILE}} diff --git a/src/Codegen/Codegen.hs b/src/Codegen/Codegen.hs index ea187fc..041671d 100644 --- a/src/Codegen/Codegen.hs +++ b/src/Codegen/Codegen.hs @@ -300,8 +300,10 @@ defaultStart :: [LLVMIr] defaultStart = [ UnsafeRaw "target triple = \"x86_64-pc-linux-gnu\"\n" , UnsafeRaw "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n" - , UnsafeRaw "@.str = private unnamed_addr constant [3 x i8] c\"%x\n\", align 1\n" + , UnsafeRaw "@.str = private unnamed_addr constant [3 x i8] c\"%i\n\", align 1\n" + , UnsafeRaw "@.non_exhaustive_patterns = private unnamed_addr constant [41 x i8] c\"Non-exhaustive patterns in case at %i:%i\n\"" , UnsafeRaw "declare i32 @printf(ptr noalias nocapture, ...)\n" + , UnsafeRaw "declare i32 @exit(i32)\n" ] compileExp :: ExpT -> CompilerState () @@ -330,6 +332,11 @@ emitECased t e cases = do stackPtr <- getNewVar emit $ SetVariable stackPtr (Alloca ty) mapM_ (emitCases rt ty label stackPtr vs) cs + emit . UnsafeRaw $ "call i32 (ptr, ...) @printf(ptr noundef @.non_exhaustive_patterns, i64 noundef 6, i64 noundef 6)\n" + emit . UnsafeRaw $ "call i32 @exit(i32 1)\n" + emit . UnsafeRaw $ "unreachable\n" + increaseVarCount >> increaseVarCount >> increaseVarCount + emit $ Br label emit $ Label label res <- getNewVar emit $ SetVariable res (Load ty Ptr stackPtr) diff --git a/src/Monomorphizer/MonomorphizerIr.hs b/src/Monomorphizer/MonomorphizerIr.hs index 76fefbf..e0e7383 100644 --- a/src/Monomorphizer/MonomorphizerIr.hs +++ b/src/Monomorphizer/MonomorphizerIr.hs @@ -1,8 +1,7 @@ -module Monomorphizer.MonomorphizerIr (module Monomorphizer.MonomorphizerIr, module RE, module GA) where +module Monomorphizer.MonomorphizerIr (module Monomorphizer.MonomorphizerIr, module GA) where -import Grammar.Abs (Ident (..), UIdent) -import Grammar.Abs qualified as GA (Ident (..)) -import TypeChecker.TypeCheckerIr qualified as RE +import Grammar.Abs (Ident (..)) +import qualified Grammar.Abs as GA (Ident (..)) type Id = (Ident, Type) @@ -27,7 +26,8 @@ data Exp | ECase ExpT [Branch] deriving (Show, Ord, Eq) -data Pattern = PVar Id | PLit (Lit, Type) | PInj Ident [Pattern] | PCatch | PEnum Ident +data Pattern = PVar Id | PLit (Lit, Type) | PInj Ident [Pattern] + | PCatch | PEnum Ident deriving (Eq, Ord, Show) data Branch = Branch (Pattern, Type) ExpT @@ -48,4 +48,4 @@ data Type = TLit Ident | TFun Type Type flattenType :: Type -> [Type] flattenType (TFun t1 t2) = t1 : flattenType t2 -flattenType x = [x] +flattenType x = [x] diff --git a/test_program.crf b/test_program.crf index bd3538d..72593d2 100644 --- a/test_program.crf +++ b/test_program.crf @@ -1,7 +1,7 @@ data Maybe () where { Nothing : Maybe () Just : Int -> Maybe () - }; +}; -- fmap : (Int -> Int) -> Maybe () -> Maybe () ; -- fmap f ma = case ma of { @@ -9,10 +9,9 @@ data Maybe () where { -- Just a => Just (f a) ; -- }; -main = case (Just 5) of { +main = case (Just 10) of { Just a => a ; Nothing => 1 ; - _ => 66 ; }; -- pure : Int -> Maybe () ;