From b92768c5ec266a8b3ba0d1352ac7ef7d915850e1 Mon Sep 17 00:00:00 2001 From: = <=> Date: Tue, 4 Aug 2020 18:30:40 +0100 Subject: [PATCH] Add script-calling. Tweak font size thresholds Other clean-up --- src/calc.c | 18 ++++++++++++++++-- src/calc.h | 3 ++- src/env.c | 31 ++++++++++++++++--------------- src/object.c | 3 ++- src/object.h | 3 ++- 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/calc.c b/src/calc.c index b19208c..9d17aa8 100644 --- a/src/calc.c +++ b/src/calc.c @@ -71,8 +71,8 @@ static void adjustFont() size++; } int f = size < 50 ? smallestFont - 3 : - size < 100 ? smallestFont - 2 : - size < 130 ? smallestFont - 1 : + size < 80 ? smallestFont - 2 : + size < 100 ? smallestFont - 1 : smallestFont; text_layer_set_font(s_input_text_layer, fonts_get_system_font(fonts[f])); @@ -206,6 +206,18 @@ static void code_window_load(Window *window) } } +static Object run_script(Object script_num, Object obj, struct Environment *e) { + int n = script_num.number - 1; + if(persist_exists(n)) { + char *code = malloc(sizeof(char) * SMAX_LENGTH); + persist_read_string(n, code, SMAX_LENGTH); + Object o = parseEval(code, e); + free(code); + return o; + } + return errorObject(SCRIPT_NOT_FOUND); +} + static void code_window_unload(Window *window) { // Save the current code text @@ -352,7 +364,9 @@ static struct Environment pebbleEnv() struct Environment e = defaultEnv(); // Needs two args addFunc("window", &add_window, &e); + addFunc("sc", &run_script, &e); parseEval("(def win (fn (a) (window a 1)))", &e); + parseEval("(def s (fn (a) (sc a 0)))", &e); return e; } diff --git a/src/calc.h b/src/calc.h index 420f0cc..6dbef7a 100644 --- a/src/calc.h +++ b/src/calc.h @@ -52,6 +52,7 @@ const char *tokens[] = { "a", "b", "c", "d", "e", "= ", "< ", "> ", "\"", + "s", "cat", "map", "fil", "fn", "def", "if", "\n", "...", @@ -65,7 +66,7 @@ const char *func_tokens[] = { }; const uint32_t inbox_size = 1024; -const uint32_t outbox_size = 64; +const uint32_t outbox_size = 1024; bool using_func_tokens = false; diff --git a/src/env.c b/src/env.c index d51272b..26a9573 100644 --- a/src/env.c +++ b/src/env.c @@ -188,23 +188,24 @@ struct Environment defaultEnv() e.objects = malloc(sizeof(Object) * MAX_ENV_ELM); e.size = MAX_ENV_ELM; - addFunc("+", &add, &e); - addFunc("-", &sub, &e); - addFunc("*", &mul, &e); - addFunc("/", &dvi, &e); - addFunc("%", &mod, &e); - addFunc("=", &equ, &e); - addFunc(">", >h, &e); - addFunc("<", <h, &e); + #define af(S, F) addFunc(S, F, &e) + af("+", &add); af("-", &sub); + af("*", &mul); af("/", &dvi); + af("%", &mod); - addFunc("cat", &catObjects, &e); - addFunc("fil", &filter, &e); + af("=", &equ); + af(">", >h); + af("<", <h); - addFunc("len", &len, &e); - addFunc("ap", &append, &e); - addFunc("at", &at, &e); - addFunc("rest", &rest, &e); - addFunc("rev", &reverse, &e); + af("cat", &catObjects); + af("fil", &filter); + + af("len", &len); + af("ap", &append); + af("at", &at); + af("rest", &rest); + af("rev", &reverse); + #undef af int i = 0; while(codes[i]) { diff --git a/src/object.c b/src/object.c index 22db730..5082fa3 100644 --- a/src/object.c +++ b/src/object.c @@ -217,7 +217,8 @@ static const char *errorText[] = { "UNSUPPORTED_NUMBER_TYPE", "NOT_A_SYMBOL", "ONLY_ONE_ARGUMENT", - "NOT_A_LIST" + "NOT_A_LIST", + "SCRIPT_NOT_FOUND" }; /** diff --git a/src/object.h b/src/object.h index b5bb1bd..79a0855 100644 --- a/src/object.h +++ b/src/object.h @@ -37,7 +37,8 @@ enum errorCode { UNSUPPORTED_NUMBER_TYPE, NOT_A_SYMBOL, ONLY_ONE_ARGUMENT, - NOT_A_LIST + NOT_A_LIST, + SCRIPT_NOT_FOUND }; #define MALLOC_FLAG 64