diff --git a/src/env.c b/src/env.c index 7b9b7f4..b6371a1 100644 --- a/src/env.c +++ b/src/env.c @@ -279,17 +279,27 @@ void printColored(const char* code) { int c = 0; int depth = 0; + int isQuote = 0; printf("[%dm", getColor(depth)); while (code[c]) { - if (code[c] == '(') { + if (code[c] == '(' && !isQuote) { depth += 1; printf("[%dm", getColor(depth)); - } else if (code[c] == ')') { + } else if (code[c] == ')' && !isQuote) { depth -= 1; printf(")[%dm", getColor(depth)); c++; continue; - } else if (code[c] == '=' && code[c + 1] == '>') { + } else if (code[c] == '"') { + isQuote = !isQuote; + if (isQuote) { + printf("[%dm\"", teal); + } else { + printf("\"[%dm", getColor(depth)); + } + c++; + continue; + } else if (code[c] == '=' && code[c + 1] == '>' && !isQuote) { printf("=>[0;%dm", getColor(depth)); c += 2; continue; @@ -459,6 +469,8 @@ struct Environment defaultEnv() pf(printEnvO), pf(systemCall), pf(loadFile), + pf(cd), + pf(cwd), pf(takeInput), pf(readFileToObject), pf(getEnvVar), diff --git a/src/examples/pebblisp.pbl b/src/examples/pebblisp.pbl index 5f667ee..d0ed3fb 100644 --- a/src/examples/pebblisp.pbl +++ b/src/examples/pebblisp.pbl @@ -12,17 +12,15 @@ (def nl (ch 10)) -(def conf (cat (env "HOME") "/.pebblisp.pbl")) +(def config (cat (env "HOME") "/.pebblisp.pbl")) -(def reload (fn () (loadfile conf))) +(def reload (fn () (loadfile config))) (def hour (fn (ti) ( (def h (% ti.hour 12)) (if (= 0 h) 12 h) ))) -(def pwd (fn () (env "PWD"))) - (def zero (fn (num) (cat (if (< num 10) "0" "") num))) (def clock (fn (ti) (cat (hour ti) ":" (zero ti.minute) ":" (zero ti.sec)))) @@ -31,6 +29,6 @@ (def ti (time)) (cat nl - bold red "[sage] " blue (clock) " " reset cyan (pwd) nl + bold red "[sage] " blue (clock) " " reset cyan (cwd) nl bold green "pebblisp ~> " reset) ))) diff --git a/src/pebblisp.c b/src/pebblisp.c index 599e89e..8b72381 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -626,7 +626,7 @@ void repl(struct Environment* env) continue; } add_history(buf); - if (buf[0] == '?' && (buf[1] == ' ' || buf[1] == '\0')) { + if ((buf[0] == 'c' && buf[1] == 'd') || (buf[0] == '?' && (buf[1] == ' ' || buf[1] == '\0'))) { char* oldBuf = buf; buf = malloc(sizeof(char) * strlen(buf + 3)); sprintf(buf, "(%s)", oldBuf); @@ -706,7 +706,7 @@ int main(int argc, const char* argv[]) Object o = parseEval("(def prompt \"pebblisp::> \")", &env); cleanObject(&o); - const char * const home = getenv("HOME"); + const char* const home = getenv("HOME"); char config[strlen(home) + 15]; sprintf(config, "%s/.pebblisp.pbl", home); readFile(config, &env); diff --git a/src/plfunc.c b/src/plfunc.c index 974d874..e01d262 100644 --- a/src/plfunc.c +++ b/src/plfunc.c @@ -346,6 +346,8 @@ BASIC_COMPARISON(and, &&) #ifdef STANDALONE +#include + Object print(Object* params, int length, struct Environment* env) { for (int i = 0; i < length; i++) { @@ -391,6 +393,20 @@ Object loadFile(Object* params, int length, struct Environment* env) return numberObject(1); } +Object cd(Object* params, int length, struct Environment* env) +{ + checkTypes(cd) + + return numberObject(chdir(params[0].string)); +} + +Object cwd(Object* params, int length, struct Environment* env) +{ + char c[128]; + getcwd(c, sizeof(c)); + return nullTerminated(c); +} + Object systemCall(Object* params, int length, struct Environment* env) { checkTypes(systemCall) @@ -454,6 +470,7 @@ Object getEnvVar(Object* params, int length, struct Environment* env) #include int timeStructDefinition = -1; + Object getTime(Object* params, int length, struct Environment* env) { if (timeStructDefinition == -1) { diff --git a/src/plfunc.h b/src/plfunc.h index 999e339..1e4f56c 100644 --- a/src/plfunc.h +++ b/src/plfunc.h @@ -223,6 +223,16 @@ tfn(loadFile, "loadfile", "=> 0" ); +tfn(cd, "cd", + ({ isStringy, NULL }), + "Change the current directory." +); + +tfn(cwd, "cwd", + ({ isStringy }), + "Get the current directory." +); + /// @code /// () => STRING /// STRING => STRING