Last active
August 29, 2015 14:05
-
-
Save sinelaw/3f3b468298b597c77300 to your computer and use it in GitHub Desktop.
drafty draft
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Test where | |
import Data.Either(isLeft, lefts, isRight) | |
data Type = Unknown | JNumber | JString | JRegex | JArray Type | JObject [(String, Type)] | JFunc [Type] Type | |
data Expr = LitNumber Double | LitString String | LitRegex String | LitArray [Expr] | LitObject [(String, Expr)] | |
| Var String -- TODO: perhaps there should only be Property (and for function scopes use a hidden base obj?) | |
| Plus Expr Expr | Minus Expr Expr | |
| Assign Expr Expr | |
-- | Property Expr String | |
| Index Expr Expr | |
deriving (Show, Eq) | |
data TypeError = TypeError String Expr [TypeError] | |
inferType :: Expr -> Either TypeError Type | |
inferType (LitNumber x) = Right JNumber | |
inferType (LitString x) = Right JString | |
inferType (LitRegex x) = Right JRegex | |
inferType (LitArray []) = Right $ JArray Unknown | |
inferType ar@(LitArray (x:xs)) | |
| areSameType = fmap JArray headType | |
| otherwise = Left $ TypeError "array elements are of inconsistent type" ar [] | |
where headType = inferType x | |
areSameType = all (headType ==) $ map inferType xs | |
inferType obj@(LitObject xs) = if (any isLeft propTypes) | |
then Left $ TypeError "properties are badly typed" obj (lefts propTypes) | |
else Right $ JObject (propNamedTypes | |
where propNamedTypes = map (\name expr -> (name, inferType expr)) xs | |
propTypes = map snd propNamedTypes | |
inferType expr = Left "not implemented" expr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment