packages feed

mathista-0.0.1: src/Main.hs

import System.Environment
import System.FilePath.Posix
import System.Exit
import Mathista.Parser
import Mathista.Compiler
import Mathista.Generator.C
import Mathista.Generator.Matlab


version :: String
version = "version 0.1.0"

usage :: String
usage = unlines [
            "The Mathista compiler",
            "",
            "USAGE:",
            "",
            "   mathista <command>",
            "",
            "COMMANDS:",
            "",
            "    compile out src     Compile a source file to the speicifed file type.",
            "    verison             Show version.",
            "    help                Show this message."
        ]


main :: IO ()
main = do
    args <- getArgs
    let args' = drop 1 args
    let cmd = if length(args) > 0
                  then args !! 0
                  else ""
    case cmd of
        "compile" -> compileCommand args'
        "run" -> putStrLn "not supported yet" >> exitWith (ExitFailure 1)
        "version" -> putStrLn version
        _ -> putStrLn usage


compileCommand :: [String] -> IO ()
compileCommand args
    | length(args) < 2 = putStrLn "mathista: specify a source and output file"
    | otherwise = do
          let out = args !! 0
          let src = args !! 1
          let name = takeBaseName out
          let generator = case takeExtension out of
                              ".m" -> generate_matlab
                              ".c" -> generate_c
                              _ -> error "mathista: invalid extension of output file"
          source <- readFile src
          let ast = case parse_str source of
                         Right s -> s
                         Left _ -> error "mathista: failed to parse"
          let code = generator name (compile ast)
          writeFile out code