Add (cd) and (cwd).

Handle quotes in printColored().
This commit is contained in:
Sage Vaillancourt 2022-03-31 21:30:45 -04:00
parent b770a618e0
commit 980a1c42ab
5 changed files with 47 additions and 10 deletions

View File

@ -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),

View File

@ -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)
)))

View File

@ -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);

View File

@ -346,6 +346,8 @@ BASIC_COMPARISON(and, &&)
#ifdef STANDALONE
#include <unistd.h>
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 <time.h>
int timeStructDefinition = -1;
Object getTime(Object* params, int length, struct Environment* env)
{
if (timeStructDefinition == -1) {

View File

@ -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