From 9f95fa78463d7e6b047485bdce28f1a970a45fd2 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Mon, 17 Jul 2017 21:48:37 +0000 Subject: Initial commit --- sil.icl | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 sil.icl (limited to 'sil.icl') diff --git a/sil.icl b/sil.icl new file mode 100644 index 0000000..1ca0c81 --- /dev/null +++ b/sil.icl @@ -0,0 +1,87 @@ +module sil + +import StdBool +import StdChar +import StdFile +from StdFunc import o, seq +import StdList +import StdOverloaded +import StdString + +import Control.Applicative +import Control.Monad +import Data.Error +from Data.Func import $ +import Data.Functor +import System.CommandLine +import System.File + +import ABC.Assembler + +import qualified Sil.Compile as SC +from Sil.Compile import :: CompileError, instance toString CompileError +import Sil.Parse +import Sil.Parse.Parser +from Sil.Syntax import :: Program +from Sil.Util import :: PrintState, instance zero PrintState, + class PrettyPrinter(..), instance PrettyPrinter Program + +:: CLI = + { prettyprint :: Bool + , compile :: Bool + , inputfile :: String + } + +instance zero CLI +where + zero = + { prettyprint = False + , compile = False + , inputfile = "" + } + +Start w +# (io,w) = stdio w +# (cmd,w) = getCommandLine w +# (args,_) = runParser (arg until eof) $ tl cmd +| isError args + # io = io <<< toString (fromError args) <<< "\r\n" + # (_,w) = fclose io w + = w +# args = seq (fromOk args) zero +# (file,w) = readFile args.inputfile w +| isError file + # io = io <<< "Could not open '" <<< args.inputfile <<< "'.\r\n" + # (_,w) = fclose io w + = w +# prog = tokenise (fromString $ fromOk file) >>= parse +| isError prog + # io = io <<< toString (fromError prog) <<< "\r\n" + # (_,w) = fclose io w + = w +# io = if args.prettyprint + (io <<< print zero (fromOk prog) <<< "\r\n") + io +# io = if args.compile + (io <<< ('SC'.compile (fromOk prog)) <<< "\r\n") + io +# (_,w) = fclose io w += w +where + arg :: Parser String (CLI -> CLI) + arg = peek >>= \opt -> + ( item "--pretty-print" *> pure (\cli -> {cli & prettyprint=True}) + <|> item "--compile" *> pure (\cli -> {cli & compile=True}) + <|> (satisfy isFilename >>= \name -> pure (\cli -> {cli & inputfile=name})) + Invalid "command line argument" opt + ) + + isFilename :: (String -> Bool) + isFilename = all (\c -> isAlphanum c || isMember c ['.']) o fromString + +instance <<< (MaybeError e a) | <<< e & <<< a +where + <<< f (Ok a) = f <<< a + <<< f (Error e) = f <<< e + +instance <<< CompileError where <<< f e = f <<< toString e -- cgit v1.2.3