aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ExtraString.dcl11
-rw-r--r--ExtraString.icl21
-rw-r--r--Makefile2
-rw-r--r--compile.c12
-rw-r--r--iclean.icl51
5 files changed, 70 insertions, 27 deletions
diff --git a/ExtraString.dcl b/ExtraString.dcl
new file mode 100644
index 0000000..568bdb7
--- /dev/null
+++ b/ExtraString.dcl
@@ -0,0 +1,11 @@
+definition module ExtraString
+
+import StdOverloaded
+
+:: TextColor = Red | Green | None
+
+matches :: !String !String -> Bool // b starts with a
+skip :: !Int !String -> String // drop
+join :: !String ![a] -> String | toString a // join with delimiter
+color :: TextColor !String -> String // bash color
+
diff --git a/ExtraString.icl b/ExtraString.icl
new file mode 100644
index 0000000..740e8d2
--- /dev/null
+++ b/ExtraString.icl
@@ -0,0 +1,21 @@
+implementation module ExtraString
+
+import StdEnv
+
+matches :: !String !String -> Bool
+matches needle haystack = haystack % (0, size needle - 1) == needle
+
+skip :: !Int !String -> String
+skip i s = s % (i, size s - 1)
+
+join :: !String ![a] -> String | toString a
+join del [] = ""
+join del [s:ss] = toString s +++ del +++ join del ss
+
+:: TextColor = Red | Green | None
+
+color :: TextColor !String -> String
+color Red s = "\x1B[31m" +++ s +++ "\x1B[0m"
+color Green s = "\x1B[32m" +++ s +++ "\x1B[0m"
+color None s = "\x1B[0m" +++ s +++ "\x1B[0m"
+
diff --git a/Makefile b/Makefile
index 8d1313d..4ac4243 100644
--- a/Makefile
+++ b/Makefile
@@ -11,7 +11,7 @@ clean:
compile.o: compile.c
$(CC) $(CFLAGS) compile.c
-iclean: compile.o iclean.icl readline
+iclean: compile.o iclean.icl readline ExtraString.*
$(CLM) $(CLMFLAGS) iclean -o iclean
readline: FORCE
diff --git a/compile.c b/compile.c
index 0240698..9e2ac15 100644
--- a/compile.c
+++ b/compile.c
@@ -88,14 +88,14 @@ int64_t compile(CleanString path, CleanString module) {
*
* @return void
*/
-void run(CleanString executable) {
+int64_t run(CleanString executable) {
char* execchars;
int execlength;
// Copy CleanString to char[]
execlength = CleanStringLength(executable);
- execchars = calloc(sizeof(char), execlength + 1 + 4);
+ execchars = calloc(1, execlength + 1);
if (execchars == NULL) {
printf("Couldn't allocate memory\n");
exit(-1);
@@ -106,10 +106,10 @@ void run(CleanString executable) {
execchars[i] = CleanStringCharacters(executable)[i];
execchars[i] = 0x00;
- // Build & run command:
- // <module> -nt
- strcat(execchars, " -nt");
- system(execchars);
+ // Run command:
+ int r;
+ r = system(execchars);
free(execchars);
+ return r;
}
diff --git a/iclean.icl b/iclean.icl
index 9f51c05..30287fe 100644
--- a/iclean.icl
+++ b/iclean.icl
@@ -29,11 +29,13 @@ module iclean
import StdEnv
import ReadLine
+import ExtraString
// SETTINGS
temp_path :== "/tmp/"
temp_module :== "iclean"
readline_history :== ".iclean_history"
+template :== ["import StdEnv"]
// END SETTINGS
temp_file :== temp_path +++ temp_module +++ ".icl"
@@ -43,23 +45,31 @@ Start w
# w = setReadLineName "iClean" w
# w = usingHistory w
# w = checkedWorldFunc readHistory "Couldn't read history file\n" readline_history w
-# w = loop w
+# w = loop template True w
# w = checkedWorldFunc writeHistory "Couldn't write history file\n" readline_history w
= w
where
- loop :: !*World -> *World
- loop w
- # (s,w) = readLine "λ. " False w
- | isNothing s = print "\n" w
- # s = fromJust s
- | s == "" = loop (print "Use Ctrl-D to exit\n" w)
- # w = addHistory s w
- # w = writemodule s w
- # (r,w) = compile temp_path temp_module w
- | r <> 0 = loop w
- # w = run (temp_path +++ temp_module) w
- = loop w
+ loop :: ![String] Bool !*World -> *World
+ loop mem success w
+ # prompt = color (if success None Red) "λ. "
+ # (s,w) = readLine prompt False w
+ | isNothing s = print "\n" w
+ # s = fromJust s
+ | s == "" = loop mem False (print "Use Ctrl-D to exit\n" w)
+ # w = addHistory s w
+ | s == ":quit" || s == ":q" = w
+ | s == ":mem" = loop mem True (print (join "\n" mem) w)
+ | matches ":import " s = loop (mem ++ [skip 1 s]) True w
+ | matches ":def " s = loop (mem ++ [skip 5 s]) True w
+ | matches ":" s = loop mem False (print ("Unknown command " +++ s +++ "\n") w)
+ # w = writemodule mem s w
+ # (r,w) = compile temp_path temp_module w
+ | r <> 0 = loop mem False w
+ # (r,w) = run (temp_path +++ temp_module +++ " -nt") w
+ | r <> 0 = loop mem False w
+ = loop mem True w
+// iClean functions
checkedWorldFunc :: (a *World -> (Bool, *World)) !String !a !*World -> *World
checkedWorldFunc f err s w
# (ok, w) = f s w
@@ -74,24 +84,25 @@ print s w
| not ok = abort "Couldn't close stdio\n"
| otherwise = w
-writemodule :: String *World -> *World
-writemodule s w
+writemodule :: ![String] !String !*World -> *World
+writemodule mem s w
# (ok,f,w) = fopen temp_file FWriteText w
| not ok = abort ("Couldn't open " +++ temp_file +++ " for writing.\n")
# f = fwrites ("module " +++ temp_module +++ "\n") f
-# f = fwrites "import StdEnv\n" f
+# f = fwrites (join "\n" mem) f
# f = fwrites ("Start = " +++ s +++ "\n") f
# (ok,w) = fclose f w
| not ok = abort ("Couldn't close " +++ temp_file +++ "\n")
| otherwise = w
-compile :: !String !String !*World -> *(!Int,*World)
+// C functions
+compile :: !String !String !*World -> *(!Int,!*World)
compile _ _ _ = code {
- ccall compile "SS:p:p"
+ ccall compile "SS:p:A"
}
-run :: !String *World -> *World
+run :: !String !*World -> (!Int, !*World)
run _ _ = code {
- ccall run "S:V:p"
+ ccall run "S:I:A"
}