Skip to content

Instantly share code, notes, and snippets.

@EliasC
Created June 18, 2015 07:50
Show Gist options
  • Save EliasC/2dbebda951e302a9f18f to your computer and use it in GitHub Desktop.
Save EliasC/2dbebda951e302a9f18f to your computer and use it in GitHub Desktop.
diff --git a/src/ir/Identifiers.hs b/src/ir/Identifiers.hs
index 0c4f9e5..515604f 100644
--- a/src/ir/Identifiers.hs
+++ b/src/ir/Identifiers.hs
@@ -17,6 +17,9 @@ type QName = [Name]
thisName :: Name
thisName = Name "this"
+mainName :: Name
+mainName = Name "main"
+
-- | The supported (infix) operators
data Op = AND | OR | NOT | LT | GT | LTE | GTE | EQ | NEQ | PLUS | MINUS | TIMES | DIV | MOD deriving(Read, Eq)
instance Show Op where
diff --git a/src/types/Typechecker/Environment.hs b/src/types/Typechecker/Environment.hs
index f2ababf..cff730b 100644
--- a/src/types/Typechecker/Environment.hs
+++ b/src/types/Typechecker/Environment.hs
@@ -15,9 +15,8 @@ module Typechecker.Environment(Environment,
methodLookup,
fieldLookup,
varLookup,
+ varHasType,
isLocal,
- isInMain,
- isMainMethod,
typeVarLookup,
extendEnvironment,
addTypeParameters,
@@ -135,19 +134,14 @@ varLookup x env = case lookup x (locals env) of
Nothing -> lookup x (globals env)
result -> result
+varHasType :: Name -> Type -> Environment -> Bool
+varHasType x ty env = case varLookup x env of
+ Nothing -> False
+ Just ty' -> subtypeOf ty ty'
+
isLocal :: Name -> Environment -> Bool
isLocal x env = isJust $ lookup x (locals env)
-isInMain :: MonadReader Environment m => m Bool
-isInMain = asks (varLookup thisName) >>= return . is_main
- where
- is_main :: Maybe Type -> Bool
- is_main Nothing = False
- is_main (Just t) = isMainType t
-
-isMainMethod :: MonadReader Environment m => MethodDecl -> m Bool
-isMainMethod method = isInMain >>= return . (&& (mname method == Name "main"))
-
typeVarLookup :: Type -> Environment -> Maybe Type
typeVarLookup ty env
| isTypeVar ty = lookup ty (bindings env)
diff --git a/src/types/Typechecker/Typechecker.hs b/src/types/Typechecker/Typechecker.hs
index 0cd36d9..f4f4457 100644
--- a/src/types/Typechecker/Typechecker.hs
+++ b/src/types/Typechecker/Typechecker.hs
@@ -232,7 +232,8 @@ instance Checkable MethodDecl where
-- E |- def mname(x1 : t1, .., xn : tn) : mtype mbody
typecheck m@(Method {mtype, mparams, mbody, mname}) =
do ty <- checkType mtype
- isMainMethod m >>= \b -> when b checkMainParams
+ inMainClass <- asks $ varHasType thisName mainType
+ when (inMainClass && mname == mainName) checkMainParams
eMparams <- mapM typecheckParam mparams
eBody <- local (addParams eMparams) $
if isVoidType ty
@@ -333,7 +334,7 @@ instance Checkable Expr where
tcError $ "Cannot call method on expression '" ++
(show $ ppExpr target) ++
"' of type '" ++ show targetType ++ "'"
- when (isMainType targetType && name == Name "main") $ tcError "Cannot call the main method"
+ when (isMainType targetType && name == mainName) $ tcError "Cannot call the main method"
when (name == Name "init") $ tcError "Constructor method 'init' can only be called during object creation"
lookupResult <- asks $ methodLookup targetType name
mdecl <-
diff --git a/src/types/Types.hs b/src/types/Types.hs
index a4a6bc5..c943ea9 100644
--- a/src/types/Types.hs
+++ b/src/types/Types.hs
@@ -4,7 +4,7 @@ module Types(Type, arrowType, isArrowType, futureType, isFutureType,
parType, isParType, streamType, isStreamType, arrayType, isArrayType,
refTypeWithParams, passiveRefTypeWithParams, activeRefTypeWithParams,
refType, isRefType, passiveRefType, activeRefType,
- isActiveRefType, isPassiveRefType, isMainType,
+ isActiveRefType, isPassiveRefType, mainType, isMainType,
makeActive, makePassive, typeVar, isTypeVar, replaceTypeVars,
voidType, isVoidType, nullType, isNullType,
boolType, isBoolType, intType, isIntType,
@@ -139,8 +139,8 @@ makeActive ty = ty
isActiveRefType (RefType (RefTypeInfo {activity = Active})) = True
isActiveRefType _ = False
-isMainType (RefType (RefTypeInfo {refId = "Main"})) = True
-isMainType _ = False
+mainType = activeRefType "Main"
+isMainType = (== mainType)
typeVar = TypeVar
isTypeVar (TypeVar _) = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment