Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enum.tag, and fixes #134 #141

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Kit/Ast/ExprType.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ data ExprType a b
| Yield a
| Tokens Str
| Defined (Identifier b)
| TagExpr a
deriving (Eq, Generic, Show)

instance (Hashable a, Hashable b) => Hashable (ExprType a b)
Expand Down Expand Up @@ -162,6 +163,7 @@ exprDiscriminant et = case et of
Defined _ -> 53
ArrayWrite _ _ _ -> 54
FieldWrite _ _ _ -> 55
TagExpr _ -> 56
x -> throwk
$ InternalError ("Expression has no discriminant: " ++ show x) Nothing

Expand Down Expand Up @@ -203,6 +205,7 @@ exprChildren et = case et of
Yield x -> [x]
ArrayWrite x y z -> [x, y, z]
FieldWrite x s y -> [x, y]
TagExpr x -> [x]
_ -> []

exprMapReduce :: (a -> c) -> (c -> d -> d) -> (a -> ExprType a b) -> d -> a -> d
Expand Down
3 changes: 3 additions & 0 deletions src/Kit/Compiler/Ir/ExprToIr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,9 @@ typedToIr ctx ictx mod e@(TypedExpr { tExpr = et, tPos = pos, inferredType = t }
[IrIdentifier ([], x), IrType f]
( VarArgListCopy x) -> return $ IrIdentifier ([], x)
InlineCExpr s t -> return $ IrInlineC s
(TagExpr x) -> do
x <- r x
return $ IrField x discriminantFieldName
t -> do
throwk $ InternalError
("Unexpected expression in typed AST:\n\n" ++ show t)
Expand Down
3 changes: 3 additions & 0 deletions src/Kit/Compiler/Typers/ConvertExpr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ convertExpr ctx tctx mod params e = do
return $ m (Defined id) TypeBool
VarArgListCopy s -> do
return $ m (VarArgListCopy s) TypeVaList
TagExpr x -> do
x <- r x
return $ m (TagExpr x) (TypeInt 0)
_ -> throwk $ InternalError
("Can't convert expression: " ++ show (expr e))
(Just pos')
4 changes: 4 additions & 0 deletions src/Kit/Compiler/Typers/TypeExpression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ typeExpr ctx tctx mod ex@(TypedExpr { tExpr = et, tPos = pos }) = do
(tPos ex)
return ex

(TagExpr x) -> do
x <- r x
return $ makeExprTyped (TagExpr x) (inferredType ex) pos

_ -> return $ ex

t' <- follow ctx tctx $ inferredType result
Expand Down
53 changes: 28 additions & 25 deletions src/Kit/Compiler/Typers/TypeExpression/TypeField.hs
Original file line number Diff line number Diff line change
Expand Up @@ -229,33 +229,36 @@ typeField (TyperUtils { _r = r, _tryRewrite = tryRewrite, _resolve = resolve, _t
pos

Enum { enumVariants = variants } -> do
case
find (((==) fieldName) . tpName . variantName)
variants
of
Just v -> do
resolve $ TypeEq
(TypeEnumVariant tp
(tpName $ variantName v)
params
)
(inferredType ex)
"Struct field access must match the field's type"
(tPos r1)
return $ r1
{ inferredType = (TypeEnumVariant
tp
if fieldName == "tag"
then return $ makeExprTyped (TagExpr r1) (TypeInt 0) pos
else
case
find (((==) fieldName) . tpName . variantName)
variants
of
Just v -> do
resolve $ TypeEq
(TypeEnumVariant tp
(tpName $ variantName v)
params
)
}
Nothing -> throwk $ TypingError
( "Enum "
++ (s_unpack $ showTypePath tp)
++ " doesn't have a variant called "
++ s_unpack fieldName
)
pos
)
(inferredType ex)
"Enum field access must match the field's type"
(tPos r1)
return $ r1
{ inferredType = (TypeEnumVariant
tp
(tpName $ variantName v)
params
)
}
Nothing -> throwk $ TypingError
( "Enum "
++ (s_unpack $ showTypePath tp)
++ " doesn't have a variant called "
++ s_unpack fieldName
)
pos

Abstract { abstractUnderlyingType = u } ->
-- forward to parent
Expand Down
9 changes: 7 additions & 2 deletions tests/functional/enums.kit
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ function main() {

// simple enum equality
if (a != b) {
printf("hello!\n");
printf("simple enums: equal\n");
}

// TODO: complex enum equality (not yet implemented)
if (c.tag == Apple2(1).tag) {
printf("complex enums: tags should be equal\n");
}
if (c.tag != Banana2(1).tag) {
printf("complex enums: tags shouldn't be equal\n");
}

// simple enum methods + match
a.print();
Expand Down
4 changes: 3 additions & 1 deletion tests/functional/enums.stdout
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
hello
hello!
simple enums: equal
complex enums: tags should be equal
complex enums: tags shouldn't be equal
apple
banana
Apple2: 1
Expand Down
13 changes: 13 additions & 0 deletions tests/functional/issues/issue134.kit
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
struct MyStruct[A] {
var value: A;
}

enum MyEnum[A] {
MyVariant(a: MyStruct[A]);
}

function main() {
var e: MyEnum[Int];
// var s: MyStruct[Int];
puts("hi");
}
1 change: 1 addition & 0 deletions tests/functional/issues/issue134.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hi