From 391c80e4df40ddc21641aa06aa0224460a53ba90 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Mon, 24 Dec 2018 23:54:26 +0100 Subject: Add interactive shell --- isjit.icl | 72 +++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 18 deletions(-) (limited to 'isjit.icl') diff --git a/isjit.icl b/isjit.icl index 72d95dd..561d8bd 100644 --- a/isjit.icl +++ b/isjit.icl @@ -1,32 +1,68 @@ module isjit import StdEnv -import StdMaybe -import StdOverloadedList +import Data.Either +import Data.Maybe import System.CommandLine import Sjit.Compile import Sjit.Syntax import Sjit.Run -import Text.GenPrint -derive gPrint Instr - Start w +# (args,w) = getCommandLine w +# (_,cs) = bootstrap # (io,w) = stdio w -# io = Foldl (\io b -> io <<< " " <<< printToString b <<< "\n") (io <<< "Program blocks:\n") comp_state.blocks -# io = io <<< "Interpreted result: " <<< interpreted_result <<< "\n" -# io = io <<< "JIT-compiled result: " <<< jit_compiled_result <<< "\n" +# (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 -= setReturnCode (if (interpreted_result==jit_compiled_result) 0 1) w -where - interpreted_result = interpret comp_state - jit_compiled_result = exec comp_state += 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 -comp_state =: compile_all Nothing - [ {fun_name="id", fun_args=["x"], fun_expr=Var "x"} - , {fun_name="const", fun_args=["x","y"], fun_expr=Var "x"} - , {fun_name="seven", fun_args=[], fun_expr=App "const" [Int 7, Int 10]} - , {fun_name="main", fun_args=[], fun_expr=App "+" [App "seven" [], App "const" [Int 5, Int 10]]} - ] +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) -- cgit v1.2.3