Changeset 27
- Timestamp:
- 02/14/05 07:42:04 (4 years ago)
- svk:copy_cache_prev:
- 1041
- Location:
- src
- Files:
-
- 8 modified
Legend:
- Unmodified
- Added
- Removed
-
src/AST.hs
r26 r27 303 303 extract other = other 304 304 305 cxtOfSigil '$' = "Scalar" 306 cxtOfSigil '@' = "Array" 307 cxtOfSigil '%' = "Hash" 308 cxtOfSigil '&' = "Code" 309 305 310 cxtOf '*' '$' = "List" 306 311 cxtOf '*' '@' = "List" -
src/Bind.hs
r25 r27 60 60 doBindArray _ (xs, -1) (p, '@') = return (((p, emptyArrayExp):xs), -1) 61 61 doBindArray _ (xs, -1) (p, '$') = fail $ "Slurpy array followed by slurpy scalar: " ++ show p 62 doBindArray v (xs, n) (p, '@') = return (((p, doSlice v [n.. ]):xs), -1)62 doBindArray v (xs, n) (p, '@') = return (((p, doSlice v [n..99]):xs), -1) 63 63 doBindArray v (xs, n) (p, '$') = return (((p, doIndex v n):xs), n+1) 64 64 -
src/Eval.hs
r26 r27 20 20 import Prim 21 21 import Context 22 import Monad 22 import Monads 23 import Pretty 23 24 24 25 emptyEnv :: (MonadIO m) => m Env … … 36 37 } 37 38 39 -- Evaluation --------------------------------------------------------------- 40 41 debug :: (Pretty a) => String -> a -> Eval () 42 debug str a = do 43 liftIO $ putStrLn ("*** " ++ str ++ ": " ++ pretty a) 44 38 45 evaluate :: Exp -> Eval Val 39 46 evaluate exp = do 47 debug "Evaluating" exp 40 48 val <- local (\e -> e { envBody = exp }) reduce 41 49 return $ case val of … … 52 60 local (\e -> e { envContext = cxt }) $ evalEnv exp 53 61 54 -- addSym :: Pad -> Eval () 55 addSym syms f = local doAddSyms f 56 where 57 doAddSyms env@Env{ envPad = pad } = env{ envPad = syms++pad } 58 59 -- OK... Now let's implement the hideously clever autothreading algorithm. 60 -- First pass - thread thru all() and none() 61 -- Second pass - thread thru any() and one() 62 63 chainFun :: Params -> Exp -> Params -> Exp -> [Val] -> Eval Val 64 chainFun p1 f1 p2 f2 (v1:v2:vs) = do 65 val <- applyFun (chainArgs p1 [v1, v2]) f1 66 case val of 67 VBool False -> return val 68 _ -> applyFun (chainArgs p2 (v2:vs)) f2 69 where 70 chainArgs prms vals = map chainArg (prms `zip` vals) 71 chainArg (p, v) = ApplyArg (paramName p) v False 72 73 applyFun :: [ApplyArg] -> Exp -> Eval Val 74 applyFun bound (Prim f) 75 = f [ argValue arg | arg <- bound, (argName arg !! 1) /= '_' ] 76 applyFun bound body = do 77 -- pushPad formal 78 evalEnv body 79 where 80 formal = filter (not . null . symName) $ map argNameValue bound 81 argNameValue (ApplyArg name val _) = Symbol SMy name val 82 83 apply :: VSub -> [Exp] -> [Exp] -> Eval Exp 84 apply sub invs args = do 85 env <- ask 86 doApply env sub invs args 87 88 doApply :: Env -> VSub -> [Exp] -> [Exp] -> Eval Exp 89 doApply env@Env{ envClasses = cls } Sub{ subParams = prms, subFun = fun } invs args = 90 case bindParams prms invs args of 91 Left errMsg -> retVal $ VError errMsg (Val VUndef) 92 Right bindings -> retVal $ VUndef -- XXX -- juncApply eval (reverse . fst $ foldl doBind ([],env) bindings) 93 where 94 eval bound = applyFun bound fun 95 {- XXX 96 doBind :: ([ApplyArg], Env) -> (Param, Exp) -> ([ApplyArg], Env) 97 doBind (bs, env) (prm@Param{ paramName = name }, exp) = do 98 (val, coll) <- expToVal prm exp 99 (((ApplyArg name val coll): bs), env `addSym` [Symbol SMy name val]) 100 -} 101 expToVal Param{ isSlurpy = slurpy, paramContext = cxt } exp = do 102 val <- evalEnvWithContext cxt exp 103 return (val, (slurpy || isCollapsed cxt)) 104 isCollapsed cxt 105 | isaType cls "Bool" cxt = True 106 | isaType cls "Junction" cxt = True 107 | isaType cls cxt "Any" = True 108 | otherwise = False 109 110 toGlobal name 111 | (sigil, identifier) <- break (\x -> isAlpha x || x == '_') name 112 , last sigil /= '*' 113 = sigil ++ ('*':identifier) 114 | otherwise = name 115 116 retVal :: Val -> Eval Exp 117 retVal val = return $ Val val 118 119 isGlobalExp (Syn name _) = name `elem` (words ":= ::=") 120 isGlobalExp _ = False 121 122 findSym :: String -> Pad -> Maybe Val 123 findSym name pad 124 | Just s <- find ((== name) . symName) pad 125 = Just $ symValue s 126 | otherwise 127 = Nothing 62 -- Reduction --------------------------------------------------------------- 128 63 129 64 reduce :: Eval Exp … … 132 67 doReduce env body 133 68 69 retVal :: Val -> Eval Exp 70 retVal val = return $ Val val 71 72 reduceStatements [] = retVal VUndef 73 reduceStatements [exp] = do 74 val <- evalEnv exp 75 retVal val 76 reduceStatements (exp:rest) 77 | Syn name [Var var _, exp'] <- exp 78 , name == ":=" || name == "::=" 79 = do 80 val <- evalEnvWithContext (cxtOfSigil (head var)) exp 81 case val of 82 VError _ _ -> retVal val 83 _ -> enterLex [Symbol SMy var val] $ reduceStatements rest 84 | otherwise 85 = do { evalEnvWithContext "Any" exp; reduceStatements rest } 86 87 doReduce :: Env -> Exp -> Eval Exp 88 89 -- Reduction for variables 134 90 doReduce Env{ envPad = pad } exp@(Var var _) 135 91 | Just val <- findSym var pad … … 140 96 = retVal $ VError ("Undefined variable " ++ var) exp 141 97 98 -- Reduction for syntactic constructs 142 99 doReduce env@Env{ envContext = cxt } exp@(Syn name exps) = case name of 143 100 ";" -> do 144 let (lead, final) = buildStatements exps 145 vals <- mapM (evalEnvWithContext "Any") lead 146 -- collect IO values from vals? 147 retVal =<< evalEnv final 101 let (global, local) = partition isGlobalExp exps 102 reduceStatements (global ++ local) 148 103 ":=" -> do 149 104 let [Var var _, exp] = exps 150 105 val <- evalEnv exp 151 -- addSym [Symbol SMy var val] -- XXX scope152 106 retVal val 153 107 "::=" -> do -- XXX wrong 154 108 let [Var var _, exp] = exps 155 109 val <- evalEnv exp 156 -- addSym [Symbol SMy var val] -- XXX scope 157 retVal VUndef 110 retVal VUndef -- XXX wrong 158 111 "=>" -> do 159 112 let [keyExp, valExp] = exps … … 186 139 = Nothing 187 140 doSlice _ _ _ = Nothing 188 buildStatements exps189 | ((Syn name' exps'):rest) <- exps190 , name' == ";"191 = buildStatements (exps' ++ rest)192 | (global, local) <- partition isGlobalExp exps193 , stmts <- global ++ local194 = (init stmts, last stmts)195 141 196 142 doReduce env@Env{ envClasses = cls, envContext = cxt, envPad = pad } exp@(App name invs args) = do … … 250 196 doReduce _ other = return other 251 197 198 -- OK... Now let's implement the hideously clever autothreading algorithm. 199 -- First pass - thread thru all() and none() 200 -- Second pass - thread thru any() and one() 201 202 chainFun :: Params -> Exp -> Params -> Exp -> [Val] -> Eval Val 203 chainFun p1 f1 p2 f2 (v1:v2:vs) = do 204 val <- applyFun (chainArgs p1 [v1, v2]) f1 205 case val of 206 VBool False -> return val 207 _ -> applyFun (chainArgs p2 (v2:vs)) f2 208 where 209 chainArgs prms vals = map chainArg (prms `zip` vals) 210 chainArg (p, v) = ApplyArg (paramName p) v False 211 212 applyFun :: [ApplyArg] -> Exp -> Eval Val 213 applyFun bound (Prim f) 214 = f [ argValue arg | arg <- bound, (argName arg !! 1) /= '_' ] 215 applyFun bound body = do 216 -- XXX - resetT here 217 enterLex formal $ evalEnv body 218 where 219 formal = filter (not . null . symName) $ map argNameValue bound 220 argNameValue (ApplyArg name val _) = Symbol SMy name val 221 222 apply :: VSub -> [Exp] -> [Exp] -> Eval Exp 223 apply sub invs args = do 224 env <- ask 225 doApply env sub invs args 226 227 -- XXX - faking application of lexical contexts 228 -- XXX - what about defaulting that depends on a junction? 229 doApply :: Env -> VSub -> [Exp] -> [Exp] -> Eval Exp 230 doApply env@Env{ envClasses = cls } Sub{ subParams = prms, subFun = fun } invs args = 231 case bindParams prms invs args of 232 Left errMsg -> retVal $ VError errMsg (Val VUndef) 233 Right bindings -> do 234 bound <- doBind bindings 235 retVal =<< juncApply (`applyFun` fun) bound 236 -- juncApply eval (reverse . fst $ foldl doBind ([],env) bindings) 237 where 238 doBind :: [(Param, Exp)] -> Eval [ApplyArg] 239 doBind [] = return [] 240 doBind ((prm, exp):rest) = do 241 (val, coll) <- expToVal prm exp 242 let name = paramName prm 243 arg = ApplyArg name val coll 244 restArgs <- enterLex [Symbol SMy name val] $ do 245 doBind rest 246 return (arg:restArgs) 247 expToVal Param{ isSlurpy = slurpy, paramContext = cxt } exp = do 248 val <- evalEnvWithContext cxt exp 249 return (val, (slurpy || isCollapsed cxt)) 250 isCollapsed cxt 251 | isaType cls "Bool" cxt = True 252 | isaType cls "Junction" cxt = True 253 | isaType cls cxt "Any" = True 254 | otherwise = False 255 256 toGlobal name 257 | (sigil, identifier) <- break (\x -> isAlpha x || x == '_') name 258 , last sigil /= '*' 259 = sigil ++ ('*':identifier) 260 | otherwise = name 261 262 isGlobalExp (Syn name _) = name `elem` (words ":= ::=") 263 isGlobalExp _ = False 264 265 findSym :: String -> Pad -> Maybe Val 266 findSym name pad 267 | Just s <- find ((== name) . symName) pad 268 = Just $ symValue s 269 | otherwise 270 = Nothing 271 252 272 arityMatch sub@Sub{ subAssoc = assoc, subParams = prms } args 253 273 | assoc == "list" = Just sub -
src/Junc.hs
r25 r27 45 45 dups = mkSet (ds ++ [ v | (v:_:_) <- group $ sort (vs ++ ds) ]) 46 46 47 juncApply :: ([ApplyArg] -> Eval Val) -> [ApplyArg] -> Eval Val 47 48 juncApply f args 48 49 | this@(_, (pivot:_)) <- break isTotalJunc args 49 50 , VJunc (Junc j dups vals) <- argValue pivot 50 = VJunc $ Junc j dups $ appSet this vals 51 = do 52 vals' <- appSet this vals 53 return $ VJunc (Junc j dups vals') 51 54 | this@(_, (pivot:_)) <- break isPartialJunc args 52 55 , VJunc (Junc j dups vals) <- argValue pivot 53 = VJunc $ mergeJunc j (appList this dups) (appList this vals) 56 = do 57 dups' <- appList this dups 58 vals' <- appList this vals 59 return $ VJunc (mergeJunc j dups' vals') 54 60 | (val:_) <- [ val | (ApplyArg _ val@(VError _ _) _) <- args ] 55 = val61 = return val 56 62 | otherwise 57 63 = f args 58 64 where 59 appSet x y = mkSet $appList x y60 appList (before, (ApplyArg name _ coll):after) vs 61 = map(\v -> juncApply f (before ++ ((ApplyArg name v coll):after))) $ setToList vs65 appSet x y = return . mkSet =<< appList x y 66 appList (before, (ApplyArg name _ coll):after) vs = do 67 mapM (\v -> juncApply f (before ++ ((ApplyArg name v coll):after))) $ setToList vs 62 68 63 69 isTotalJunc arg -
src/Main.hs
r26 r27 59 59 eval str = doEval str [] 60 60 61 doEval str args = undefined 62 doRun str args = undefined 61 doEval str args = do 62 env <- emptyEnv 63 let env' = runRule env id ruleProgram str 64 rv <- (`runReaderT` env') $ do 65 (`runContT` return) $ evaluate (envBody env') 66 putStrLn $ pretty rv 63 67 64 {- XXX -65 doEval str args = do66 runRule emptyEnv (putStrLn . pretty . evaluate emptyEnv) ruleProgram str67 -}68 69 {- XXX -70 68 doRun str args = do 71 runRule emptyEnv (putStr . concatMap vCast . vCast . evaluate emptyEnv) ruleProgram str 72 -} 69 env <- emptyEnv 70 let env' = runRule env id ruleProgram str 71 rv <- (`runReaderT` runRule env id ruleProgram str) $ do 72 (`runContT` return) $ evaluate (envBody env') 73 putStr . concatMap vCast . vCast $ rv -
src/Monads.hs
r26 r27 14 14 import AST 15 15 16 enterLex :: Pad -> Eval a -> Eval a 17 enterLex pad = local (\e -> e { envPad = (pad ++ envPad e) }) 16 18 17 19 {- … … 102 104 103 105 -- enter a lexical context 104 enterLex str = local (\e -> e { envPad = (str:envPad e) })105 106 106 107 dumpLex :: String -> Eval () -
src/Parser.hs
r26 r27 253 253 , parseApply 254 254 , parseParens parseOp 255 , parseEof256 255 ] 257 256 <?> "term" 258 257 258 {- 259 259 parseEof = do 260 260 eof 261 261 pos <- getPosition 262 262 return $ NonTerm pos 263 -} 263 264 264 265 parseLitTerm = choice -
src/Pretty.hs
r26 r27 24 24 -- Execution of - aborted due to compilation errors. 25 25 26 instance Pretty VStr 27 26 28 instance Pretty Exp where 27 29 pretty (Val (VError msg (NonTerm pos))) = "Syntax error at " ++ (show pos) ++ msg 30 pretty (Val v) = pretty v 31 pretty (Syn x vs) = "{ Syn " ++ pretty x ++ " | " ++ joinList "; " (map pretty vs) ++ " }" 28 32 pretty x = show x 29 33 … … 48 52 pretty (VRat x) = show $ (fromIntegral $ numerator x) / (fromIntegral $ denominator x) 49 53 pretty (VComplex x) = show x 50 pretty (VRef (VList x)) = "[" ++ joinList ", " (map pretty x) ++ "]" 54 pretty (VRef (VList x)) 55 | (v:_:_:_:_:_:_:_:_:_:_) <- x 56 = "[" ++ pretty v ++ ", ...]" 57 | otherwise = "[" ++ joinList ", " (map pretty x) ++ "]" 51 58 pretty (VRef x) = "\\(" ++ pretty x ++ ")" 52 pretty (VList x) = "(" ++ joinList ", " (map pretty x) ++ ")" 59 pretty (VList x) 60 | (v:_:_:_:_:_:_:_:_:_:_) <- x 61 = "(" ++ pretty v ++ ", ...)" 62 | otherwise = "(" ++ joinList ", " (map pretty x) ++ ")" 53 63 pretty (VSub x) = "sub {...}" 54 64 pretty (VBlock x) = "{...}"
