Skip to content

Commit 4bf943a

Browse files
KikoEliasC
Kiko
authored andcommitted
fix array access (#832)
* fix array access * fix array error message * fix error test * remove unnecessary condition * else-if expression
1 parent 7d98adc commit 4bf943a

File tree

3 files changed

+37
-32
lines changed

3 files changed

+37
-32
lines changed
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
active class Main
2+
def main(): unit
3+
val i = 4
4+
i(0) = 3
5+
end
6+
end
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Expected an array or a function call but found expression of type 'int'

src/types/Typechecker/Typechecker.hs

+30-32
Original file line numberDiff line numberDiff line change
@@ -807,39 +807,37 @@ instance Checkable Expr where
807807
doTypecheck ArrayAccess{emeta
808808
,target = VarAccess{emeta, qname}
809809
,index = head args}
810-
else do
811-
let typeParams = getTypeParameters ty
812-
argTypes = getArgTypes ty
813-
resultType = getResultType ty
814-
actualLength = length args
815-
expectedLength = length argTypes
816-
defName = qname'{qnlocal = Name $ "_" ++ show qname ++ show (expectedLength - actualLength)}
817-
818-
calledName <-
819-
if (actualLength == expectedLength)
820-
then return qname'
810+
else if (isArrowType ty) then do
811+
let typeParams = getTypeParameters ty
812+
argTypes = getArgTypes ty
813+
resultType = getResultType ty
814+
actualLength = length args
815+
expectedLength = length argTypes
816+
defName = qname'{qnlocal = Name $ "_" ++ show qname ++ show (expectedLength - actualLength)}
817+
calledName <-
818+
if (actualLength == expectedLength)
819+
then return qname'
820+
else do
821+
result2 <- findVar defName
822+
case result2 of
823+
Just (qname2, ty2) -> return defName
824+
Nothing -> tcError $ WrongNumberOfFunctionArgumentsError
825+
qname (length argTypes) (length args)
826+
(eArgs, returnType, typeArgs) <-
827+
if null typeArguments
828+
then inferenceCall fcall typeParams (take actualLength argTypes) resultType
821829
else do
822-
result2 <- findVar defName
823-
case result2 of
824-
Just (qname2, ty2) -> return defName
825-
Nothing -> tcError $ WrongNumberOfFunctionArgumentsError
826-
qname (length argTypes) (length args)
827-
828-
unless (isArrowType ty) $
829-
tcError $ NonFunctionTypeError ty
830-
831-
(eArgs, returnType, typeArgs) <-
832-
if null typeArguments
833-
then inferenceCall fcall typeParams (take actualLength argTypes) resultType
834-
else do
835-
unless (length typeArguments == length typeParams) $
836-
tcError $ WrongNumberOfFunctionTypeArgumentsError qname
837-
(length typeParams) (length typeArguments)
838-
typecheckCall fcall typeParams (take actualLength argTypes) resultType
839-
return $ setArrowType ty $
840-
setType returnType fcall {args = eArgs,
841-
qname = calledName,
842-
typeArguments = typeArgs}
830+
unless (length typeArguments == length typeParams) $
831+
tcError $ WrongNumberOfFunctionTypeArgumentsError qname
832+
(length typeParams) (length typeArguments)
833+
typecheckCall fcall typeParams (take actualLength argTypes) resultType
834+
return $ setArrowType ty $
835+
setType returnType fcall {args = eArgs,
836+
qname = calledName,
837+
typeArguments = typeArgs}
838+
else do
839+
tcError $
840+
ExpectingOtherTypeError "an array or a function call" ty
843841

844842
--- |- t1 .. |- tn
845843
-- E, x1 : t1, .., xn : tn |- body : t

0 commit comments

Comments
 (0)