packages feed

ewe 0.1.0.13 → 0.1.0.15

raw patch · 5 files changed

+91/−29 lines, 5 files

Files

ewe.cabal view
@@ -2,9 +2,9 @@ -- see http://haskell.org/cabal/users-guide/  name:                ewe-version:             0.1.0.13+version:             0.1.0.15 synopsis:            An language to teach a programming-description:         Another implementions of the EWE programming language originally developed by Kent D. Lee. EWE was an extension of the RAM programming language created by Sethi.+description:         Another implemention of the EWE programming language originally created and developed by Kent D. Lee. EWE is an extension of the RAM programming language. RAM was created by Sethi. homepage:            http://github.com/jfcmacro/ewe license:             BSD3 license-file:        LICENSE
src/Language/EWE/AbsSyn.hs view
@@ -1,4 +1,13 @@-module Language.EWE.AbsSyn where+module Language.EWE.AbsSyn(MRef(..)+                          ,Cond(..)+                          ,Equ+                          ,Equates+                          ,Labels+                          ,Stmt(..)+                          ,Stmts+                          ,Prog(..)+                          ,Instr(..)+                          ,emptyProg) where  data AbsSyn = Empty @@ -54,3 +63,7 @@            | INI             deriving (Eq,Show) +emptyProg :: Prog+emptyProg = Prg { stms = []+                , equates = []+                }
src/Language/EWE/Parser.hs view
@@ -197,15 +197,15 @@  pNextRHS :: MRef -> Parser Instr pNextRHS mr1 =+  do pReserved "M"+     op <- pBrackets (pTokenB pMRefOrIdx')+     either (\(mr2,i) -> return $ IMRI mr1 mr2 i) (pEndRHS mr1) op+  <|>   (pLexeme pInt >>= \i -> return $ IMMI mr1 i)   <|>   (pLexeme pId >>= \id -> pEndRHS mr1 (MRefId id))   <|>   (pLexeme pStrLit >>= \str -> return $ IMMS mr1 str)-  <|>-  do pReserved "M"-     op <- pBrackets (pTokenB pMRefOrIdx')-     either (\(mr2,i) -> return $ IMRI mr1 mr2 i) (pEndRHS mr1) op    pEndRHS :: MRef -> MRef -> Parser Instr pEndRHS mr1 mr2 = option (IMMM mr1 mr2) (pPartialArith mr1 mr2)
src/Language/EWE/VM.hs view
@@ -1,5 +1,5 @@ -module Language.EWE.VM where+module Language.EWE.VM(runVM,execVM) where  import Language.EWE.AbsSyn import qualified Data.List as L@@ -7,7 +7,7 @@ import Control.Monad.Trans.State.Lazy import Control.Monad.Trans.Class(lift) import Data.Char (ord,chr)-import System.IO(hFlush, stdout)+import System.IO(hFlush, stdout,hPutStrLn,hPutStr) import System.Exit(exitWith,ExitCode(..))  type Memory = [(Int,Int)]@@ -23,10 +23,29 @@ data StateVM = StVM { mem :: Memory                     , prg :: Prog                     , pc  :: PC+                    } deriving (Show)++emptyStateVM = StVM { mem = emptyMemory+                    , pc  = initPC+                    , prg = emptyProg                     }  type StateVMM = StateT StateVM IO +runVM :: Prog -> IO () +runVM prg = evalStateT evalVM (initVM prg)++execVM :: Prog -> IO StateVM+execVM prg = execStateT evalVM (initVM prg)++evalVM :: StateVMM ()+evalVM = do+  lift $ hPutStrLn stdout "Iniciando maquina"+  evalInstr+  +initVM :: Prog -> StateVM+initVM p = emptyStateVM { prg = p }+ mRef :: MRef -> Equates -> Int mRef (MRefI i) _  = i mRef (MRefId s) m = M.fromMaybe (error "No found") $ L.lookup s m@@ -60,21 +79,26 @@              i <- lift $ readInt "Enter an integer:"              let st'' = execIRI i ci st              put st''+             evalInstr          (IWI _)   -> do              let (st'',i) = execIWI ci st              lift $ putStrLn (show i)              put st''+             evalInstr          (IRS _ _) -> do            s <- lift $ getLine            let st'' = execIRS s ci st            put st''+           evalInstr          (IWS _) -> do              let (st'',s) = execIWS ci st              lift $ putStrLn s              put st''-         (IH)      -> lift $ exitWith ExitSuccess-         (IB)      -> lift $ exitWith ExitSuccess-    else put st'+             evalInstr+         (IH)      -> return ()+         (IB)      -> return ()+    else do put st'+            evalInstr  execInstr :: Instr -> StateVM -> StateVM execInstr (INI)       state =
src/Main.hs view
@@ -10,41 +10,66 @@ import Data.Maybe(fromMaybe) import Language.EWE.Parser import Language.EWE.AbsSyn-import Language.EWE.VM+import Language.EWE.VM(runVM,execVM) -data Flag = EweVersion deriving Show+data Options =  Options { optShowVersion :: Bool+                        , optExec :: Bool+                        , optDebug :: Bool+                        } deriving Show -options :: [OptDescr Flag]+defaultOptions :: Options +defaultOptions = Options { optShowVersion = False+                         , optExec = False+                         , optDebug = False+                         }++options :: [OptDescr (Options -> Options)] options =-  [ Option ['V','?'] ["version"] (NoArg EweVersion) "show version number"+  [ Option ['V','?'] ["version"] +    (NoArg (\opts -> opts { optShowVersion  = True })) +    "show version number"+  , Option ['x'] ["exec"] +    (NoArg (\opts -> opts { optExec = True }))+    "execute the current file with ewe-vm"+  , Option ['d'] ["debug"] +    (NoArg (\opts -> opts { optDebug = True }))+    "show debug information"   ] -compilerOpts :: [String] -> IO ([Flag], [String])+compilerOpts :: [String] -> IO (Options, [String]) compilerOpts argv =   case getOpt Permute options argv of-    (o,n,[]) -> return (o,n)+    (o,n,[]) -> return (foldl (flip id) defaultOptions o,n)     (_,_,errs) -> ioError (userError (concat errs ++ usageInfo header options))     where header = "Usage: ewe [OPTION...] files..." -processFile :: FilePath -> IO ()-processFile fp = do+processFile :: Options -> FilePath -> IO ()+processFile opts fp = do   hPutStr stdout $ "Processing file: " ++ fp   fh   <- openFile fp ReadMode   s    <- hGetContents fh   let pRes = pEWE s fp   case pRes of     Left err   -> hPutStrLn stderr $ " Parsing error at " ++ (show err)-    Right prog -> hPutStrLn stdout $ " is Ok"+    Right prog -> do hPutStrLn stdout $ " is Ok"+                     if optExec opts +                       then do hPutStrLn stdout $ show prog+                               r <- execVM prog+                               if (optDebug opts) +                                then hPutStrLn stdout $ show r+                                else return ()+                       else return ()   hClose fh -processFlags :: [Flag] -> IO ()-processFlags = mapM_ processFlag+processStaticOptions :: Options -> IO ()+processStaticOptions opts = +  if optShowVersion opts +  then hPutStrLn stdout $ "ewe version: " ++ (showVersion version)+  else return ()                      -processFlag :: Flag -> IO ()-processFlag EweVersion = (hPutStrLn stdout $ "ewe version: " ++ (showVersion version)) >> exitSuccess-                          main :: IO () main = do-  (flgs, fls) <- (getArgs >>= compilerOpts)-  processFlags flgs-  mapM_ processFile fls+  (opts, fls) <- (getArgs >>= compilerOpts)+  processStaticOptions opts+  mapM_ (processFile opts) fls+  return ()