Add basic (time) function and Time struct.

Also add pebblisp.pbl config file to examples/
This commit is contained in:
Sage Vaillancourt 2022-03-30 16:00:53 -04:00 committed by Sage Vaillancourt
parent 75ba0ac5ee
commit 342eba3a0d
4 changed files with 51 additions and 3 deletions

View File

@ -414,9 +414,6 @@ struct Environment defaultEnv()
pf(at),
pf(rest),
pf(charAt),
#ifndef LOW_MEM
pf(reverse),
#endif
pf(isNum),
pf(isList),
pf(isString),
@ -424,6 +421,10 @@ struct Environment defaultEnv()
pf(charVal),
pf(parseEvalO),
pf(possessive),
pf(getTime),
#ifndef LOW_MEM
pf(reverse),
#endif
#ifdef WEBSERVER
pf(addGetRoute),
pf(addPostRoute),

25
src/examples/pebblisp.pbl Normal file
View File

@ -0,0 +1,25 @@
(def red "")
(def green "")
(def blue "")
(def bold "")
(def reset "")
(def nl (ch 10))
(def hour (fn (ti) (
(def h (% ti's hour 12))
(if (= 0 h) 12 h)
)))
(def zero (fn (num) (cat (if (< num 10) "0" "") num)))
(def clock (fn (ti) (cat (hour ti) ":" (zero ti's minute) ":" (zero ti's sec))))
(def prompt (fn (a) (
(sys "echo")
(def ti (time))
(def m ti's minute)
(def s ti's sec)
(cat red bold "[sage] " blue (clock) nl green bold "pebblisp ~> " reset)
)))

View File

@ -486,4 +486,22 @@ Object readFileToObject(Object* params, int length, struct Environment* env)
return string;
}
#include <time.h>
int timeStructDefinition = -1;
Object getTime(Object* params, int length, struct Environment* env)
{
if (timeStructDefinition == -1) {
parseEval("(struct Time (minute hour sec))", env);
}
timeStructDefinition = getStructIndex("Time");
time_t t = time(NULL);
struct tm tm = *localtime(&t);
Object o = structObject(timeStructDefinition);
o.structObject->fields[0] = numberObject(tm.tm_min);
o.structObject->fields[1] = numberObject(tm.tm_hour);
o.structObject->fields[2] = numberObject(tm.tm_sec);
return o;
}
#endif // STANDALONE

View File

@ -240,6 +240,10 @@ tfn(readFileToObject, "rf",
"Read a file into a string object."
);
fn(getTime, "time",
"Get a struct of the current time with fields (minute hour sec)."
);
#endif // STANDALONE
#endif // PEBBLISP_PLFUNC_H