Fixed simple pattern matching.
This commit is contained in:
parent
582747a997
commit
bd3cf3c3f1
4 changed files with 17 additions and 11 deletions
2
Justfile
2
Justfile
|
|
@ -15,4 +15,4 @@ test:
|
|||
|
||||
# compile a specific file
|
||||
run FILE:
|
||||
cabal run language {{FILE}}
|
||||
cabal run language -- -d {{FILE}}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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 () ;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue