Changeset 26
- Timestamp:
- 02/14/05 06:02:18 (4 years ago)
- svk:copy_cache_prev:
- 1041
- Location:
- src
- Files:
-
- 9 modified
Legend:
- Unmodified
- Added
- Removed
-
src/AST.hs
r25 r26 18 18 type Ident = String 19 19 20 class Contextn where20 class Value n where 21 21 vCast :: Val -> n 22 22 vCast (VRef v) = vCast v … … 31 31 fmapVal f = castV . f . vCast 32 32 33 instance Context(Val, Val) where33 instance Value (Val, Val) where 34 34 castV (x, y) = VPair x y 35 35 vCast (VPair x y) = (x, y) … … 39 39 other -> error $ "cannot cast into (Val, Val): " ++ (show v) 40 40 41 instance ContextVHash where41 instance Value VHash where 42 42 castV = VHash 43 43 vCast x = MkHash $ listToFM (map vCast $ vCast x) 44 44 45 instance ContextVSub where45 instance Value VSub where 46 46 castV = VSub 47 47 doCast (VSub b) = b 48 48 49 instance ContextVBool where49 instance Value VBool where 50 50 castV = VBool 51 51 doCast (VJunc j) = juncToBool j … … 70 70 = (1 ==) . length . filter vCast $ setToList vs 71 71 72 instance ContextVInt where72 instance Value VInt where 73 73 castV = VInt 74 74 doCast (VInt i) = i … … 78 78 doCast x = round (vCast x :: VNum) 79 79 80 instance ContextVRat where80 instance Value VRat where 81 81 castV = VRat 82 82 doCast (VInt i) = i % 1 … … 84 84 doCast x = approxRational (vCast x :: VNum) 1 85 85 86 instance ContextVNum where86 instance Value VNum where 87 87 castV = VNum 88 88 doCast VUndef = 0 … … 97 97 doCast x = error $ "cannot cast: " ++ (show x) 98 98 99 instance ContextVComplex where99 instance Value VComplex where 100 100 castV = VComplex 101 101 doCast x = (vCast x :: VNum) :+ 0 102 102 103 instance ContextVStr where103 instance Value VStr where 104 104 castV = VStr 105 105 vCast VUndef = "" … … 121 121 str = show x 122 122 123 instance ContextVArray where123 instance Value VArray where 124 124 castV = VArray 125 125 vCast x = MkArray (vCast x) 126 126 127 127 {- 128 instance ContextVJunc where128 instance Value VJunc where 129 129 castV = JAny . castV 130 130 vCast x = JAny $ mkSet (vCast x) 131 131 -} 132 132 133 instance ContextVList where133 instance Value VList where 134 134 castV = VList 135 135 vCast (VList l) = l … … 139 139 vCast v = [v] 140 140 141 instance Context(Maybe a) where141 instance Value (Maybe a) where 142 142 vCast VUndef = Nothing 143 143 vCast _ = Just undefined 144 144 145 instance ContextInt where doCast = intCast146 instance ContextWord where doCast = intCast147 instance ContextWord8 where doCast = intCast148 instance Context[Word8] where doCast = map (toEnum . ord) . vCast145 instance Value Int where doCast = intCast 146 instance Value Word where doCast = intCast 147 instance Value Word8 where doCast = intCast 148 instance Value [Word8] where doCast = map (toEnum . ord) . vCast 149 149 150 150 type VScalar = Val 151 151 -- type VJunc = Set Val 152 152 153 instance ContextVScalar where153 instance Value VScalar where 154 154 vCast = id 155 155 castV = id … … 239 239 , subName :: String 240 240 , subType :: SubType 241 , subPad :: Symbols241 , subPad :: Pad 242 242 , subAssoc :: String 243 243 , subParams :: Params … … 257 257 -} 258 258 259 instance Ord ([Val] -> StateEnv Val) where260 compare _ _ = LT261 259 instance (Ord a) => Ord (Set a) where 262 260 compare x y = compare (setToList x) (setToList y) … … 272 270 | Syn String [Exp] 273 271 | Sym Scope Var 274 | Prim ([Val] -> StateEnvVal)272 | Prim ([Val] -> Eval Val) 275 273 | Val Val 276 274 | Var Var SourcePos … … 326 324 defaultScalarParam = buildParam "" "*" "$_" (Val VUndef) 327 325 328 -- The eval monad!329 type StateEnv a = State Env a330 331 326 data Env = Env { envContext :: Cxt 332 , envPad :: Symbols327 , envPad :: Pad 333 328 , envClasses :: ClassTree 334 329 , envEval :: Exp -> Eval Val … … 339 334 } deriving (Show, Eq) 340 335 341 type Symbols= [Symbol]336 type Pad = [Symbol] 342 337 data Symbol = Symbol { symScope :: Scope 343 338 , symName :: String -
src/Eval.hs
r25 r26 22 22 import Monad 23 23 24 emptyEnv = Env { envContext = "List" 25 , envPad = [initSyms] 26 , envClasses = initTree 27 , envEval = evaluate 28 } 29 30 addSym :: Symbols -> StateEnv () 31 addSym syms = modify doAddSyms 32 where 33 doAddSyms env@Env{ envPad = (pad:outer) } = env{ envPad = ((syms++pad):outer) } 34 35 pushPad :: Symbols -> StateEnv () 36 pushPad syms = modify (\env -> env{ envPad = tail $ envPad env }) 37 38 popPad :: StateEnv () 39 popPad = modify (\env -> env{ envPad = tail $ envPad env }) 40 41 evaluate :: Exp -> StateEnv Val 24 emptyEnv :: (MonadIO m) => m Env 25 emptyEnv = do 26 uniq <- liftIO newUnique 27 return $ Env 28 { envContext = "List" 29 , envPad = initSyms 30 , envClasses = initTree 31 , envEval = evaluate 32 , envCC = return 33 , envDepth = 0 34 , envID = uniq 35 , envBody = Val VUndef 36 } 37 38 evaluate :: Exp -> Eval Val 42 39 evaluate exp = do 43 val <- reduce exp40 val <- local (\e -> e { envBody = exp }) reduce 44 41 return $ case val of 45 42 Val v -> v 46 43 otherwise -> VError "Invalid expression" exp 47 44 45 evalEnv :: Exp -> Eval Val 46 evalEnv exp = do 47 evl <- asks envEval 48 evl exp 49 50 evalEnvWithContext :: Cxt -> Exp -> Eval Val 51 evalEnvWithContext cxt exp = do 52 local (\e -> e { envContext = cxt }) $ evalEnv exp 53 54 -- addSym :: Pad -> Eval () 55 addSym syms f = local doAddSyms f 56 where 57 doAddSyms env@Env{ envPad = pad } = env{ envPad = syms++pad } 58 48 59 -- OK... Now let's implement the hideously clever autothreading algorithm. 49 60 -- First pass - thread thru all() and none() 50 61 -- Second pass - thread thru any() and one() 51 62 52 chainFun :: Params -> Exp -> Params -> Exp -> [Val] -> StateEnvVal63 chainFun :: Params -> Exp -> Params -> Exp -> [Val] -> Eval Val 53 64 chainFun p1 f1 p2 f2 (v1:v2:vs) = do 54 65 val <- applyFun (chainArgs p1 [v1, v2]) f1 … … 60 71 chainArg (p, v) = ApplyArg (paramName p) v False 61 72 62 applyFun :: [ApplyArg] -> Exp -> StateEnvVal73 applyFun :: [ApplyArg] -> Exp -> Eval Val 63 74 applyFun bound (Prim f) 64 75 = f [ argValue arg | arg <- bound, (argName arg !! 1) /= '_' ] 65 76 applyFun bound body = do 66 pushPad formal 67 exp <- reduce body 68 return $ case exp of 69 Val val -> val 70 otherwise -> VError "Invalid expression" exp 77 -- pushPad formal 78 evalEnv body 71 79 where 72 80 formal = filter (not . null . symName) $ map argNameValue bound 73 81 argNameValue (ApplyArg name val _) = Symbol SMy name val 74 82 75 apply :: VSub -> [Exp] -> [Exp] -> StateEnvExp83 apply :: VSub -> [Exp] -> [Exp] -> Eval Exp 76 84 apply sub invs args = do 77 env <- get85 env <- ask 78 86 doApply env sub invs args 79 87 80 doApply :: Env -> VSub -> [Exp] -> [Exp] -> StateEnvExp88 doApply :: Env -> VSub -> [Exp] -> [Exp] -> Eval Exp 81 89 doApply env@Env{ envClasses = cls } Sub{ subParams = prms, subFun = fun } invs args = 82 90 case bindParams prms invs args of … … 100 108 | otherwise = False 101 109 102 evalEnv exp = do103 evl <- gets envEval104 evl exp105 106 evalEnvWithContext newCxt exp = do107 Env{ envContext = cxt, envEval = evl } <- get108 modify (\env -> env{ envContext = newCxt })109 val <- evl exp110 modify (\env -> env{ envContext = cxt })111 return val112 113 110 toGlobal name 114 111 | (sigil, identifier) <- break (\x -> isAlpha x || x == '_') name … … 117 114 | otherwise = name 118 115 119 retVal :: Val -> StateEnvExp116 retVal :: Val -> Eval Exp 120 117 retVal val = return $ Val val 121 118 … … 123 120 isGlobalExp _ = False 124 121 125 findSym :: String -> [Symbols]-> Maybe Val122 findSym :: String -> Pad -> Maybe Val 126 123 findSym name pad 127 | Just s <- find ((== name) . symName) (concat pad)124 | Just s <- find ((== name) . symName) pad 128 125 = Just $ symValue s 129 126 | otherwise 130 127 = Nothing 131 128 132 reduce :: E xp -> StateEnvExp133 reduce exp= do134 env <- get135 doReduce env exp129 reduce :: Eval Exp 130 reduce = do 131 env@Env{ envBody = body } <- ask 132 doReduce env body 136 133 137 134 doReduce Env{ envPad = pad } exp@(Var var _) … … 152 149 let [Var var _, exp] = exps 153 150 val <- evalEnv exp 154 addSym [Symbol SMy var val] -- XXX scope151 -- addSym [Symbol SMy var val] -- XXX scope 155 152 retVal val 156 153 "::=" -> do -- XXX wrong 157 154 let [Var var _, exp] = exps 158 155 val <- evalEnv exp 159 addSym [Symbol SMy var val] -- XXX scope156 -- addSym [Symbol SMy var val] -- XXX scope 160 157 retVal VUndef 161 158 "=>" -> do … … 234 231 , fromJust fun 235 232 ) 236 | ((Symbol _ n val), order) <- concatpad `zip` [0..]233 | ((Symbol _ n val), order) <- pad `zip` [0..] 237 234 , let sub@(Sub{ subType = subT, subReturns = ret, subParams = prms }) = vCast val 238 235 , (n ==) `any` [name, toGlobal name] … … 250 247 deltaFromScalar x = deltaType cls x "Scalar" 251 248 252 doReduce _ (Parens exp) = reduceexp249 doReduce env (Parens exp) = doReduce env exp 253 250 doReduce _ other = return other 254 251 -
src/Internals.hs
r25 r26 77 77 instance Eq (a -> b) where 78 78 _ == _ = False 79 instance Ord (a -> b) where 80 compare _ _ = LT -
src/Lexer.hs
r25 r26 15 15 import qualified Text.ParserCombinators.Parsec.Token as P 16 16 17 type Pad = [Symbols] 18 type RuleParser a = GenParser Char Pad a 17 type RuleParser a = GenParser Char Env a 19 18 20 19 perl6Def = javaStyle -
src/Main.hs
r25 r26 53 53 54 54 doParse = parse 55 parse str = runRule emptyEnv (putStrLn . pretty) ruleProgram str 55 parse str = do 56 env <- emptyEnv 57 runRule env (putStrLn . pretty) ruleProgram str 56 58 57 59 eval str = doEval str [] -
src/Monads.hs
r23 r26 164 164 | otherwise = do 165 165 env <- caller n 166 shiftT $ \r -> return $ VErr $ ErrRet ( (==) (envID env) . envID) v166 shiftT $ \r -> return $ VErr $ ErrRet (return . (==) (envID env) . envID) v 167 167 168 168 returnScope = callerReturn 0 . VStr 169 169 170 170 data VErr = ErrStr String 171 | ErrRet (Env -> Bool) Val171 | ErrRet (Env -> Eval Bool) Val 172 172 deriving (Typeable, Show, Eq) 173 173 -
src/Parser.hs
r25 r26 17 17 -- Lexical units -------------------------------------------------- 18 18 19 ruleProgram :: RuleParser E xp19 ruleProgram :: RuleParser Env 20 20 ruleProgram = rule "program" $ do 21 whiteSpace 21 22 many (symbol ";") 22 23 rv <- option [] ruleStatementList 23 24 eof 24 retSyn ";" rv 25 env <- getState 26 return $ env { envBody = (Syn ";" rv) } 25 27 26 28 ruleBlock :: RuleParser Exp 27 29 ruleBlock = rule "block" $ braces $ do 30 whiteSpace 28 31 many (symbol ";") 29 32 rv <- option [] ruleStatementList … … 63 66 return (scope, multi, name) 64 67 pos <- getPosition 65 cxt <- option "Any" $ preSpace (ruleBareTrait "returns")68 cxt <- option "Any" $ ruleBareTrait "returns" 66 69 formal <- option Nothing $ return . Just =<< parens ruleSubParameters 67 70 body <- ruleBlock … … 85 88 ruleSubName = rule "subroutine name" $ do 86 89 star <- option "" $ string "*" 87 fixity <- option "prefix:" $ choice (map string$ words fixities)90 fixity <- option "prefix:" $ choice (map (try . string) $ words fixities) 88 91 c <- wordAlpha 89 92 cs <- many wordAny … … 352 355 parseProgram = do { whiteSpace ; x <- parseOp ; eof ; return x } 353 356 354 runRule :: Env -> (E xp -> a) -> RuleParser Exp-> String -> a355 runRule env f p str = f $ case ( runParser ruleProgram (envPad env)"" str ) of356 Left err -> Val $ VError (showErr err) (NonTerm $ errorPos err)357 Right ast -> ast357 runRule :: Env -> (Env -> a) -> RuleParser Env -> String -> a 358 runRule env f p str = f $ case ( runParser ruleProgram env "" str ) of 359 Left err -> env { envBody = Val $ VError (showErr err) (NonTerm $ errorPos err) } 360 Right env' -> env' 358 361 359 362 showErr err = -
src/Pretty.hs
r25 r26 27 27 pretty (Val (VError msg (NonTerm pos))) = "Syntax error at " ++ (show pos) ++ msg 28 28 pretty x = show x 29 30 instance Pretty Env where 31 pretty x = "{ " ++ (pretty $ envBody x) ++ " }" 29 32 30 33 instance Pretty Val where -
src/Prim.hs
r25 r26 27 27 op0 s = \x -> VError ("unimplemented listOp: " ++ s) (Val $ VList x) 28 28 29 op1 :: Ident -> (forall a. Context a => a) -> StateEnvVal29 op1 :: Ident -> (forall a. Value a => a) -> Eval Val 30 30 op1 "!" = return . fmapVal not 31 31 op1 "+" = return . op1Numeric id … … 50 50 op1 s = return . (\x -> VError ("unimplemented unaryOp: " ++ s) (Val x)) 51 51 52 opEval :: String -> StateEnvVal52 opEval :: String -> Eval Val 53 53 opEval str = do 54 pad <- gets envPad55 let rv = ( runParser ruleProgram pad"" str )54 env <- ask 55 let rv = ( runParser ruleProgram env "" str ) 56 56 return $ VUndef 57 57 {- 58 58 case rv of 59 59 Left err -> return $ VError (showErr err) (NonTerm $ errorPos err) 60 Right exp -> gets evl >>= (($) exp)60 Right exp -> asks evl >>= (($) exp) 61 61 -} 62 62 … … 200 200 , subFun = (Prim f) 201 201 } 202 f :: [Val] -> StateEnvVal202 f :: [Val] -> Eval Val 203 203 f = case arity of 204 204 0 -> \(x:_) -> return $ op0 sym (vCast x)
