module isjit import StdEnv import Data.Either import Data.Maybe import System.CommandLine import Sjit.Compile import Sjit.Syntax import Sjit.Run Start w # (args,w) = getCommandLine w # (_,cs) = bootstrap # (io,w) = stdio w # (io,w) = case args of [prog,file] # (_,f,w) = fopen file FReadText w # (f,io) = file_loop f io cs # (_,w) = fclose f w -> (io,w) [prog] -> (interactive_loop io cs, w) [prog:_] -> (io <<< "Usage: " <<< prog <<< " [FILE]\n", setReturnCode 1 w) # (_,w) = fclose io w = w interactive_loop :: !*File !CompileState -> *File interactive_loop io cs # io = io <<< "> " # (s,io) = freadline io # (s,cs) = handle_input s cs # io = case s of Nothing -> io Just s -> io <<< s <<< "\n" = case cs of Nothing -> io Just cs -> interactive_loop io cs file_loop :: !*File !*File !CompileState -> (!*File, !*File) file_loop f io cs # (e,f) = fend f | e = (f,io) # (s,f) = freadline f # (s,cs) = handle_input s cs # io = case s of Nothing -> io Just s -> io <<< s <<< "\n" = case cs of Nothing -> (f,io) Just cs -> file_loop f io cs handle_input :: !String !CompileState -> (!Maybe String, !Maybe CompileState) handle_input s cs | s == "" = (Just "", Nothing) | s == "quit\n" = (Nothing, Nothing) | otherwise = case parse_interactive_line s of Left e -> (Just e, Just cs) Right fun -> case compile fun cs of Left e -> (Just ("\033[0;31mError:\033[0m " +++ e), Just cs) Right cs | fun.fun_name <> "main" -> (Nothing, Just cs) # res = exec cs -> (Just (toString res), Just cs)