Patch to compile for Pebble devices.

Mostly throwing ifdef Standalone all over the place.
This commit is contained in:
= 2022-04-02 11:51:06 +01:00
parent 1a13fe4814
commit 323b3113d7
14 changed files with 160 additions and 94 deletions

View File

@ -54,7 +54,7 @@ static int8_t selected_token = 1;
const char* func_tokens[] = { const char* func_tokens[] = {
"window ", "window ",
"sc ", "cw ", "pw ", "rw ", "atl ", "utl ", "sc ", "cw ", "pw ", "rw ", "atl ", "utl ",
"sec ", "mnt ", "hr ", "hrt ", "vibe ", "sub " "sec ", "mnt ", "hr ", "hrt ", "vibe ", "sub ", "time "
}; };
bool using_func_tokens = false; bool using_func_tokens = false;
@ -96,7 +96,7 @@ static void updateText()
const char* token = getToken(selected_token); const char* token = getToken(selected_token);
const char* add = const char* add =
token[0] == ' ' ? "_" : // Display space as underscore token[0] == ' ' ? " " : // Display space as underscore
token[0] == '\n' ? "\\n" : // Display newline as \n token[0] == '\n' ? "\\n" : // Display newline as \n
token; // Display others literally token; // Display others literally
@ -224,8 +224,8 @@ static void calculate()
text_layer_set_font(s_result_text_layer, text_layer_set_font(s_result_text_layer,
fonts_get_system_font(FONT_KEY_GOTHIC_14_BOLD)); fonts_get_system_font(FONT_KEY_GOTHIC_14_BOLD));
} else { } else {
text_layer_set_font(s_result_text_layer, text_layer_set_font(s_result_text_layer, tiny_font);
fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD)); //fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD));
} }
snprintf(result_text, RESULT_LENGTH, RESULT_PREFIX "%s", temp); snprintf(result_text, RESULT_LENGTH, RESULT_PREFIX "%s", temp);
free(temp); free(temp);
@ -344,10 +344,10 @@ void debug_result(const char* d)
psleep(300); psleep(300);
} }
static Object run_script(Object script_num, Object obj, static Object run_script(Object* params, int length,
struct Environment* e) struct Environment* e)
{ {
int n = script_num.number - 1; int n = params[0].number - 1;
if (persist_exists(n)) { if (persist_exists(n)) {
char* code = malloc(sizeof(char) * SMAX_LENGTH); char* code = malloc(sizeof(char) * SMAX_LENGTH);
persist_read_string(n, code, SMAX_LENGTH); persist_read_string(n, code, SMAX_LENGTH);
@ -467,12 +467,12 @@ static void custom_load(Window* window)
window_stack_push(s_custom_window, true); window_stack_push(s_custom_window, true);
} }
Object add_window(Object obj1, Object _, struct Environment* env) Object add_window(Object* params, int length, struct Environment* env)
{ {
printf("ADD_WINDOW\n"); printf("ADD_WINDOW\n");
size_t length; size_t l;
char* temp = stringObj(&obj1, &length); char* temp = stringObj(&params[0], &l);
sprintf(header, "%s", temp); strcpy(header, temp);
if (!s_custom_window) { if (!s_custom_window) {
s_custom_window = window_create(); s_custom_window = window_create();
@ -503,23 +503,25 @@ static void inbox_received_callback(DictionaryIterator* iter, void* context)
} }
/** General **/ /** General **/
void af(const char* name, Object (* func)(Object*, int, struct Environment*), struct Environment* env)
{
Object o = newObject(TYPE_FUNC);
o.func = func;
addToEnv(env, name, o);
}
static struct Environment pebbleEnv() static struct Environment pebbleEnv()
{ {
struct Environment e = defaultEnv(); struct Environment e = defaultEnv();
addFunc("window", &add_window, &e); af("window", &add_window, &e);
addFunc("sc", &run_script, &e); af("sc", &run_script, &e);
addFunc("cw", &createWindow, &e); af("cw", &createWindow, &e);
addFunc("pw", &pushWindow, &e); af("pw", &pushWindow, &e);
addFunc("rw", &deleteWindow, &e); af("rw", &deleteWindow, &e);
addFunc("atl", &addTextLayer, &e); af("atl", &addTextLayer, &e);
addFunc("utl", &updateTextLayer, &e); af("utl", &updateTextLayer, &e);
addFunc("sec", &getSeconds, &e); af("vibe", &doVibe, &e);
addFunc("mnt", &getMinutes, &e); af("sub", &subscribe, &e);
addFunc("hr", &getHours, &e);
addFunc("hrt", &getTwelveHours, &e);
addFunc("vibe", &doVibe, &e);
addFunc("sub", &subscribe, &e);
return e; return e;
} }

View File

@ -8,6 +8,8 @@
#include "web.h" #include "web.h"
#include "plfunc.h" #include "plfunc.h"
#define printd(...) ;
struct Environment* globalEnv; struct Environment* globalEnv;
struct Environment* global() struct Environment* global()
@ -145,6 +147,7 @@ void addToEnv(struct Environment* env, const char* name, const Object obj)
addToEnvAt(old_size, env, name, obj); addToEnvAt(old_size, env, name, obj);
} }
#ifdef STANDALONE
void printEnv(struct Environment* env, int printPointers) void printEnv(struct Environment* env, int printPointers)
{ {
if (!env) { if (!env) {
@ -182,6 +185,8 @@ void printEnv(struct Environment* env, int printPointers)
} }
} }
#endif
void addFunc(const char* name, void addFunc(const char* name,
Object (* func)(Object*, int, struct Environment*), Object (* func)(Object*, int, struct Environment*),
struct Environment* env, struct Environment* env,
@ -232,6 +237,7 @@ struct symFunc {
Object (* func)(Object*, int, struct Environment*); Object (* func)(Object*, int, struct Environment*);
}; };
#ifdef STANDALONE
struct helpText { struct helpText {
const char* symbol; const char* symbol;
const char* help; const char* help;
@ -244,7 +250,9 @@ struct helpText helpTexts[100];
/// For any instances (e.g. segfault recovery) where defaultEnv() may be called /// For any instances (e.g. segfault recovery) where defaultEnv() may be called
/// multiple times. /// multiple times.
int helpInitialized = 0; int helpInitialized = 0;
#endif
#ifdef STANDALONE
struct symFunc buildFuncSym(const char* symbol, Object (* func)(Object*, int, struct Environment*), const char* help, struct symFunc buildFuncSym(const char* symbol, Object (* func)(Object*, int, struct Environment*), const char* help,
const char* const* tests, size_t testLength) const char* const* tests, size_t testLength)
{ {
@ -261,7 +269,17 @@ struct symFunc buildFuncSym(const char* symbol, Object (* func)(Object*, int, st
.sym = symbol, .sym = symbol,
}; };
} }
#else
struct symFunc buildFuncSym(const char* symbol, Object (* func)(Object*, int, struct Environment*))
{
return (struct symFunc) {
.func = func,
.sym = symbol,
};
}
#endif
#ifdef STANDALONE
const int black = 30; const int black = 30;
const int red = 31; const int red = 31;
const int green = 32; const int green = 32;
@ -350,14 +368,6 @@ fn(help, "?",
return nullTerminated("Help not found!"); return nullTerminated("Help not found!");
} }
fn(segfault, "seg",
"Induces a segfault."
)
{
int* p = NULL;
return numberObject(*p);
}
// Returns number of failures // Returns number of failures
int runTests(int detailed) int runTests(int detailed)
{ {
@ -409,11 +419,27 @@ int runTests(int detailed)
// fprintf(stderr, "TOTAL BYTES: %zu\n", getBytes()); // fprintf(stderr, "TOTAL BYTES: %zu\n", getBytes());
return failureCount; return failureCount;
} }
#endif
fn(segfault, "seg",
"Induces a segfault."
)
{
int* p = NULL;
return numberObject(*p);
}
#ifdef STANDALONE
#define pf(_func) buildFuncSym(_func ## Symbol, &(_func), _func ## Doc, _func ## Tests, array_length(_func ## Tests)) #define pf(_func) buildFuncSym(_func ## Symbol, &(_func), _func ## Doc, _func ## Tests, array_length(_func ## Tests))
#else
#define pf(_func) buildFuncSym(_func ## Symbol, &(_func))
#endif
struct Environment defaultEnv() struct Environment defaultEnv()
{ {
#ifndef STANDALONE
int helpInitialized = 0;
#endif
if (!helpInitialized) { if (!helpInitialized) {
dictionary = (struct Dictionary) { dictionary = (struct Dictionary) {
.structCount = 0, .structCount = 0,
@ -484,12 +510,12 @@ struct Environment defaultEnv()
#endif #endif
}; };
int i; unsigned i;
for (i = 0; i < sizeof(symFuncs) / sizeof(symFuncs[0]); i++) { for (i = 0; i < sizeof(symFuncs) / sizeof(symFuncs[0]); i++) {
addFunc(symFuncs[i].sym, symFuncs[i].func, &e, i); addFunc(symFuncs[i].sym, symFuncs[i].func, &e, i);
} }
for (; i < e.capacity; i++) { for (int j = i; j < e.capacity; j++) {
e.strings[i] = NULL; e.strings[j] = NULL;
} }
helpInitialized = 1; helpInitialized = 1;

View File

@ -285,14 +285,14 @@ int stringNObj(struct string* s, const Object* obj)
case TYPE_LAMBDA: { case TYPE_LAMBDA: {
#ifdef STANDALONE #ifdef STANDALONE
#ifdef DEBUG #ifdef DEBUG
s->cursor += sprintf(s->cursor, len, "\\x%d", obj->number); s->cursor += sprintf(s->cursor, "\\x%d", obj->number);
#endif #endif
stringNObj(s, &obj->lambda->params); stringNObj(s, &obj->lambda->params);
s->cursor += sprintf(s->cursor, " -> "); s->cursor += sprintf(s->cursor, " -> ");
stringNObj(s, &obj->lambda->body); stringNObj(s, &obj->lambda->body);
s->cursor += sprintf(s->cursor, ">"); s->cursor += sprintf(s->cursor, ">");
#else #else
s->cursor += sprintf(s->cursor, len, "\\x%d", obj->number); s->cursor += sprintf(s->cursor, "\\x%d", obj->number);
#endif #endif
break; break;
} }

View File

@ -5,9 +5,11 @@
#ifndef STANDALONE #ifndef STANDALONE
#include <pebble.h> #include <pebble.h>
#undef printf #undef sprintf
#define printf(...) APP_LOG(APP_LOG_LEVEL_DEBUG, __VA_ARGS__) #define sprintf(_dest, args...) snprintf(_dest, 999, args)
#else #else
#define xprintf(_dest, _format, ...) sprintf(_dest, _format, __VA_ARGS__)
#ifdef DEBUG #ifdef DEBUG
#define printd(...) printf(__VA_ARGS__) #define printd(...) printf(__VA_ARGS__)
#else #else
@ -243,6 +245,7 @@ struct Error noError();
Object constructLambda(const Object* params, const Object* body, struct Environment* env); Object constructLambda(const Object* params, const Object* body, struct Environment* env);
#ifdef STANDALONE
int getAllocations(); int getAllocations();
size_t getBytes(); size_t getBytes();
@ -254,4 +257,6 @@ void* smalloc(size_t size);
#define malloc(x) smalloc(x) #define malloc(x) smalloc(x)
#define calloc(x, y) scalloc(x, y) #define calloc(x, y) scalloc(x, y)
#endif // STANDALONE
#endif #endif

View File

@ -43,7 +43,7 @@ void custom_window_unload(Window* window)
window_destroy(window); window_destroy(window);
} }
Object createWindow(Object o1, Object o2, struct Environment* env) Object createWindow(Object* params, int length, struct Environment* env)
{ {
Window* window = window_create(); Window* window = window_create();
WindowHandlers wh = {.load = custom_window_load, WindowHandlers wh = {.load = custom_window_load,
@ -52,8 +52,9 @@ Object createWindow(Object o1, Object o2, struct Environment* env)
return pebbleOther(WINDOW, window, NULL, NULL); return pebbleOther(WINDOW, window, NULL, NULL);
} }
Object deleteWindow(Object window, Object o2, struct Environment* env) Object deleteWindow(Object* params, int length, struct Environment* env)
{ {
Object window = params[0];
/* Maintain a list of layers to delete? */ /* Maintain a list of layers to delete? */
if (getPebbleType(window) == WINDOW) { if (getPebbleType(window) == WINDOW) {
window_stack_remove(accessPebbleObject(window)->window, true); window_stack_remove(accessPebbleObject(window)->window, true);
@ -62,8 +63,9 @@ Object deleteWindow(Object window, Object o2, struct Environment* env)
return falseObject(); return falseObject();
} }
Object pushWindow(Object window, Object o2, struct Environment* env) Object pushWindow(Object* params, int length, struct Environment* env)
{ {
Object window = params[0];
if (getPebbleType(window) == WINDOW) { if (getPebbleType(window) == WINDOW) {
window_stack_push(accessPebbleObject(window)->window, true); window_stack_push(accessPebbleObject(window)->window, true);
return trueObject(); return trueObject();
@ -71,19 +73,26 @@ Object pushWindow(Object window, Object o2, struct Environment* env)
return falseObject(); return falseObject();
} }
Object updateTextLayer(Object textLayer, Object text, struct Environment* env) Object updateTextLayer(Object* params, int length, struct Environment* env)
{ {
Object textLayer = params[0];
Object text = params[1];
if (getPebbleType(textLayer) == TEXT_LAYER) { if (getPebbleType(textLayer) == TEXT_LAYER) {
struct PebbleObject* po = accessPebbleObject(textLayer); struct PebbleObject* po = accessPebbleObject(textLayer);
stringObj(po->textLayer->text, &text); size_t l;
char* string = stringObj(&text, &l);
snprintf(po->textLayer->text, 999, "%s", string);
free(string);
text_layer_set_text(po->textLayer->layer, po->textLayer->text); text_layer_set_text(po->textLayer->layer, po->textLayer->text);
return trueObject(); return trueObject();
} }
return falseObject(); return falseObject();
} }
Object addTextLayer(Object window, Object text, struct Environment* env) Object addTextLayer(Object* params, int length, struct Environment* env)
{ {
Object window = params[0];
Object text = params[1];
if (getPebbleType(window) != WINDOW) { if (getPebbleType(window) != WINDOW) {
return errorObject(0); return errorObject(0);
} }
@ -95,7 +104,10 @@ Object addTextLayer(Object window, Object text, struct Environment* env)
textLayer->text = calloc(sizeof(char), 100); textLayer->text = calloc(sizeof(char), 100);
textLayer->layer = text_layer_create(bounds); textLayer->layer = text_layer_create(bounds);
stringObj(textLayer->text, &text); size_t l;
char* string = stringObj(&text, &l);
snprintf(textLayer->text, 999, "%s", string);
free(string);
text_layer_set_text(textLayer->layer, textLayer->text); text_layer_set_text(textLayer->layer, textLayer->text);
text_layer_set_font(textLayer->layer, text_layer_set_font(textLayer->layer,
fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD)); fonts_get_system_font(FONT_KEY_BITHAM_42_BOLD));
@ -112,8 +124,10 @@ static void subscriptionHandler(struct tm* tick_time, TimeUnits changed)
eval(&subscription, subscriptionEnv); eval(&subscription, subscriptionEnv);
} }
Object subscribe(Object function, Object time, struct Environment* env) Object subscribe(Object* params, int length, struct Environment* env)
{ {
Object function = params[0];
Object time = params[1];
if (function.type == TYPE_LAMBDA) { if (function.type == TYPE_LAMBDA) {
subscription = cloneObject(function); subscription = cloneObject(function);
subscriptionEnv = env; subscriptionEnv = env;

View File

@ -21,14 +21,14 @@ struct PebbleObject {
}; };
}; };
Object createWindow(Object o1, Object o2, struct Environment* env); Object createWindow(Object* params, int length, struct Environment* env);
Object deleteWindow(Object window, Object o2, struct Environment* env); Object deleteWindow(Object* params, int length, struct Environment* env);
Object pushWindow(Object o1, Object o2, struct Environment* env); Object pushWindow(Object* params, int length, struct Environment* env);
Object addTextLayer(Object window, Object text, struct Environment* env); Object addTextLayer(Object* params, int length, struct Environment* env);
Object updateTextLayer(Object textLayer, Object text, struct Environment* env); Object updateTextLayer(Object* params, int length, struct Environment* env);
Object subscribe(Object function, Object time, struct Environment* env); Object subscribe(Object* params, int length, struct Environment* env);

View File

@ -500,8 +500,10 @@ Object parseEval(const char* input, struct Environment* env)
Object parsed = parse(tok).obj; Object parsed = parse(tok).obj;
if (parsed.type == TYPE_ERROR) { if (parsed.type == TYPE_ERROR) {
obj = parsed; // TODO Check necessity obj = parsed; // TODO Check necessity
#ifdef STANDALONE
obj.error->plContext = malloc(sizeof(struct Slice)); obj.error->plContext = malloc(sizeof(struct Slice));
*obj.error->plContext = *lastOpen; *obj.error->plContext = *lastOpen;
#endif
break; break;
} }
if (tok[i].text[0] == ')') { if (tok[i].text[0] == ')') {

View File

@ -12,6 +12,7 @@
#define UNPACK(...) __VA_ARGS__ #define UNPACK(...) __VA_ARGS__
#ifdef STANDALONE
#define fnn(_name, _docs, ...) \ #define fnn(_name, _docs, ...) \
static const char * const _name ## Doc = _docs; \ static const char * const _name ## Doc = _docs; \
unused static const char * const _name ## Tests[] = {__VA_ARGS__}; \ unused static const char * const _name ## Tests[] = {__VA_ARGS__}; \
@ -27,6 +28,19 @@ fnn(_name, _docs, __VA_ARGS__)
static const char * const _name ## Symbol = _symbol; \ static const char * const _name ## Symbol = _symbol; \
fnn(_name, _docs, __VA_ARGS__) fnn(_name, _docs, __VA_ARGS__)
#else
#define fnn(_name, _docs, ...) \
Object _name(Object* params, int length, struct Environment* env)
#define tfn(_name, _symbol, _type, _docs, ...) \
static const char * const _name ## Symbol = _symbol; \
fnn(_name, _docs, __VA_ARGS__)
#define fn(_name, _symbol, _docs, ...) \
static const char * const _name ## Symbol = _symbol; \
fnn(_name, _docs, __VA_ARGS__)
#endif
#define trueObject() boolObject(1) #define trueObject() boolObject(1)
#define falseObject() boolObject(0) #define falseObject() boolObject(0)
@ -64,6 +78,10 @@ Object simpleFuncEval(Object func, Object arg1, Object arg2, struct Environment*
Object typeCheck(const char* funcName, Object* params, int length, Object typeCheck(const char* funcName, Object* params, int length,
int (* typeChecks[])(Object), int typeLength, int* failed); int (* typeChecks[])(Object), int typeLength, int* failed);
#ifndef STANDALONE
#define DISABLE_TYPE_CHECKS
#endif
#ifndef DISABLE_TYPE_CHECKS #ifndef DISABLE_TYPE_CHECKS
#define checkTypes(FUNC) int FAILED; Object ERROR = typeCheck(#FUNC, params, length, FUNC ## TypeChecks, array_length(FUNC ## TypeChecks), &FAILED); \ #define checkTypes(FUNC) int FAILED; Object ERROR = typeCheck(#FUNC, params, length, FUNC ## TypeChecks, array_length(FUNC ## TypeChecks), &FAILED); \
if (FAILED) { \ if (FAILED) { \

View File

@ -2,11 +2,12 @@
#include <pebble.h> #include <pebble.h>
Object doVibe(Object patternList, Object o2, struct Environment* env) Object doVibe(Object* params, int length, struct Environment* env)
{ {
int length = listLength(&patternList); Object patternList = params[0];
uint32_t pattern[length]; int l = listLength(&patternList);
if (length > 0) { uint32_t pattern[l];
if (l > 0) {
int i = 0; int i = 0;
Object* pl = &patternList; Object* pl = &patternList;
FOR_POINTER_IN_LIST(pl) { FOR_POINTER_IN_LIST(pl) {
@ -16,7 +17,7 @@ Object doVibe(Object patternList, Object o2, struct Environment* env)
i++; i++;
} }
vibes_enqueue_custom_pattern( vibes_enqueue_custom_pattern(
(VibePattern) {.durations = pattern, .num_segments = length}); (VibePattern) {.durations = pattern, .num_segments = l});
return trueObject(); return trueObject();
} else { } else {
return errorObject(NOT_A_LIST); return errorObject(NOT_A_LIST);

View File

@ -1,11 +1,3 @@
#include "pebblisp.h" #include "pebblisp.h"
Object getSeconds(Object o1, Object o2, struct Environment* env); Object doVibe(Object* params, int length, struct Environment* env);
Object getMinutes(Object o1, Object o2, struct Environment* env);
Object getHours(Object o1, Object o2, struct Environment* env);
Object getTwelveHours(Object o1, Object o2, struct Environment* env);
Object doVibe(Object patternList, Object o2, struct Environment* env);

View File

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <time.h>
#include "plfunc.h" #include "plfunc.h"
@ -344,6 +345,23 @@ BASIC_COMPARISON(lessThan, <)
BASIC_COMPARISON(and, &&) BASIC_COMPARISON(and, &&)
int timeStructDefinition = -1;
Object getTime(unused Object* params, unused 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;
}
#ifdef STANDALONE #ifdef STANDALONE
#include <unistd.h> #include <unistd.h>
@ -465,24 +483,4 @@ Object getEnvVar(Object* params, unused int length, unused struct Environment* e
checkTypes(getEnvVar) checkTypes(getEnvVar)
return nullTerminated(getenv(params[0].string)); return nullTerminated(getenv(params[0].string));
} }
#include <time.h>
int timeStructDefinition = -1;
Object getTime(unused Object* params, unused 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 #endif // STANDALONE

View File

@ -189,6 +189,11 @@ fn(parseEvalO, "eval",
"(eval '(+ 5 5))", "10", "(eval '(+ 5 5))", "10",
); );
tfn(getTime, "time",
({ isStruct }),
"Get a struct of the current time with fields (minute hour sec)."
);
#ifdef STANDALONE #ifdef STANDALONE
fn(print, "prn", "Prints the string representation of the given object to stdout."); fn(print, "prn", "Prints the string representation of the given object to stdout.");
@ -253,11 +258,6 @@ tfn(getEnvVar, "env",
"(env HOME) => /home/sagevaillancourt" "(env HOME) => /home/sagevaillancourt"
); );
tfn(getTime, "time",
({ isStruct }),
"Get a struct of the current time with fields (minute hour sec)."
);
#endif // STANDALONE #endif // STANDALONE
#endif // PEBBLISP_PLFUNC_H #endif // PEBBLISP_PLFUNC_H

View File

@ -1,3 +1,5 @@
#ifdef STANDALONE
#include <string.h> #include <string.h>
#include <microhttpd.h> #include <microhttpd.h>
#include <stdio.h> #include <stdio.h>
@ -186,3 +188,5 @@ Object startServer(Object* params, int length, struct Environment* env)
} }
return numberObject(start(port)); return numberObject(start(port));
} }
#endif // STANDALONE

View File

@ -1,3 +1,5 @@
#ifdef STANDALONE
#include "pebblisp.h" #include "pebblisp.h"
fn(startServer, "serve", fn(startServer, "serve",
@ -22,3 +24,5 @@ fn(addPostRoute, "post",
" (serve)\n" " (serve)\n"
"Also see: (serve)\n" "Also see: (serve)\n"
); );
#endif