From 342eba3a0d50a2cf0118e1ca24b05dfca0603aeb Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Wed, 30 Mar 2022 16:00:53 -0400 Subject: [PATCH] Add basic (time) function and Time struct. Also add pebblisp.pbl config file to examples/ --- src/env.c | 7 ++++--- src/examples/pebblisp.pbl | 25 +++++++++++++++++++++++++ src/plfunc.c | 18 ++++++++++++++++++ src/plfunc.h | 4 ++++ 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 src/examples/pebblisp.pbl diff --git a/src/env.c b/src/env.c index 1752337..1ad2e12 100644 --- a/src/env.c +++ b/src/env.c @@ -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), diff --git a/src/examples/pebblisp.pbl b/src/examples/pebblisp.pbl new file mode 100644 index 0000000..9366aea --- /dev/null +++ b/src/examples/pebblisp.pbl @@ -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) +))) diff --git a/src/plfunc.c b/src/plfunc.c index af9f055..2b4f7f0 100644 --- a/src/plfunc.c +++ b/src/plfunc.c @@ -486,4 +486,22 @@ Object readFileToObject(Object* params, int length, struct Environment* env) return string; } +#include + +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 diff --git a/src/plfunc.h b/src/plfunc.h index ffbd2c4..3573faa 100644 --- a/src/plfunc.h +++ b/src/plfunc.h @@ -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 \ No newline at end of file