| 1 |
// https://github.com/camilstaps/iClean/raw/master/iclean.icl |
| 2 |
/** |
| 3 |
* Interactive Clean |
| 4 |
* |
| 5 |
* Clean program to easily compile and run one-line Clean expressions |
| 6 |
* |
| 7 |
* The MIT License (MIT) |
| 8 |
* |
| 9 |
* Copyright (c) 2015 Camil Staps <[email protected]> |
| 10 |
* |
| 11 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 |
* of this software and associated documentation files (the "Software"), to deal |
| 13 |
* in the Software without restriction, including without limitation the rights |
| 14 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 |
* copies of the Software, and to permit persons to whom the Software is |
| 16 |
* furnished to do so, subject to the following conditions: |
| 17 |
* |
| 18 |
* The above copyright notice and this permission notice shall be included in all |
| 19 |
* copies or substantial portions of the Software. |
| 20 |
* |
| 21 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 |
* SOFTWARE. |
| 28 |
*/ |
| 29 |
module iclean |
| 30 |
|
| 31 |
import StdEnv |
| 32 |
import ReadLine |
| 33 |
|
| 34 |
// SETTINGS |
| 35 |
temp_path :== "/tmp/" |
| 36 |
temp_module :== "iclean" |
| 37 |
readline_history :== ".iclean_history" |
| 38 |
// END SETTINGS |
| 39 |
|
| 40 |
temp_file :== temp_path +++ temp_module +++ ".icl" |
| 41 |
|
| 42 |
Start :: *World -> *World |
| 43 |
Start w |
| 44 |
# w = setReadLineName "iClean" w |
| 45 |
# w = usingHistory w |
| 46 |
# w = checkedWorldFunc readHistory "Couldn't read history file\n" readline_history w |
| 47 |
# w = loop w |
| 48 |
# w = checkedWorldFunc writeHistory "Couldn't write history file\n" readline_history w |
| 49 |
= w |
| 50 |
where |
| 51 |
loop :: !*World -> *World |
| 52 |
loop w |
| 53 |
# (s,w) = readLine "λ. " False w |
| 54 |
| isNothing s = print "\n" w |
| 55 |
# s = fromJust s |
| 56 |
| s == "" = loop (print "Use Ctrl-D to exit\n" w) |
| 57 |
# w = addHistory s w |
| 58 |
# w = writemodule s w |
| 59 |
# (r,w) = compile temp_path temp_module w |
| 60 |
| r <> 0 = loop w |
| 61 |
# w = run (temp_path +++ temp_module) w |
| 62 |
= loop w |
| 63 |
|
| 64 |
checkedWorldFunc :: (a *World -> (Bool, *World)) !String !a !*World -> *World |
| 65 |
checkedWorldFunc f err s w |
| 66 |
# (ok, w) = f s w |
| 67 |
| not ok = print err w |
| 68 |
| otherwise = w |
| 69 |
|
| 70 |
print :: String *World -> *World |
| 71 |
print s w |
| 72 |
# (io,w) = stdio w |
| 73 |
# io = fwrites s io |
| 74 |
# (ok,w) = fclose io w |
| 75 |
| not ok = abort "Couldn't close stdio\n" |
| 76 |
| otherwise = w |
| 77 |
|
| 78 |
writemodule :: String *World -> *World |
| 79 |
writemodule s w |
| 80 |
# (ok,f,w) = fopen temp_file FWriteText w |
| 81 |
| not ok = abort ("Couldn't open " +++ temp_file +++ " for writing.\n") |
| 82 |
# f = fwrites ("module " +++ temp_module +++ "\n") f |
| 83 |
# f = fwrites "import StdEnv\n" f |
| 84 |
# f = fwrites ("Start = " +++ s +++ "\n") f |
| 85 |
# (ok,w) = fclose f w |
| 86 |
| not ok = abort ("Couldn't close " +++ temp_file +++ "\n") |
| 87 |
| otherwise = w |
| 88 |
|
| 89 |
compile :: !String !String !*World -> *(!Int,*World) |
| 90 |
compile _ _ _ = code { |
| 91 |
ccall compile "SS:p:p" |
| 92 |
} |
| 93 |
|
| 94 |
run :: !String *World -> *World |
| 95 |
run _ _ = code { |
| 96 |
ccall run "S:V:p" |
| 97 |
} |
| 98 |
|