diff --git a/src/calc.c b/src/calc.c index 8307309..c6f87f4 100644 --- a/src/calc.c +++ b/src/calc.c @@ -48,7 +48,7 @@ const char* tokens[] = { "END" }; -// Currently selected button, starts on '(' +// Currently-selected button, starts on '(' static int8_t selected_token = 1; const char* func_tokens[] = { @@ -57,27 +57,41 @@ const char* func_tokens[] = { "sec ", "mnt ", "hr ", "hrt ", "vibe ", "sub ", "time " }; -bool using_func_tokens = false; +enum TokenMode { + NORMAL, + FUNC, +} tokenMode; -// Size of messages to/from the phone app -const uint32_t inbox_size = 1024; -const uint32_t outbox_size = 1024; +// // Size of messages to/from the phone app +// const uint32_t inbox_size = 1024; +// const uint32_t outbox_size = 1024; /** Text Editing **/ // Get the number of tokens in the current list static inline int8_t tokenCount() { - return using_func_tokens ? - sizeof(func_tokens) / sizeof(func_tokens[0]) : - sizeof(tokens) / sizeof(tokens[0]); + switch (tokenMode) { + case NORMAL: + return sizeof(tokens) / sizeof(tokens[0]); + case FUNC: + return sizeof(tokens) / sizeof(tokens[0]); + } + return 0; } // Get current token from tokens[] or func_tokens[], as appropriate static inline const char* getToken(int8_t n) { + //static char singleLetterString[2] = {'a', '\0'}; int8_t t = n % tokenCount(); - return using_func_tokens ? func_tokens[t] : tokens[t]; + switch (tokenMode) { + case NORMAL: + return tokens[t]; + case FUNC: + return func_tokens[t]; + } + return tokens[t]; } static void trim(size_t len) @@ -156,7 +170,7 @@ static void adjustFont() ]; text_layer_set_font(s_input_text_layer, fonts_get_system_font(f)); } else { - // Use custom extra small font for lots of text + // Use custom extra-small font for lots of text text_layer_set_font(s_input_text_layer, tiny_font); } } @@ -180,7 +194,7 @@ static void click_backspace(ClickRecognizerRef recognizer, void* context) return; } - if (!using_func_tokens) { + if (tokenMode == NORMAL) { if (displayed_code[0] == '\0') { window_stack_remove(window_stack_get_top_window(), true); } else { @@ -189,7 +203,7 @@ static void click_backspace(ClickRecognizerRef recognizer, void* context) updateText(); } } else { - using_func_tokens = false; + tokenMode = NORMAL; updateText(); } adjustFont(); @@ -200,8 +214,8 @@ static void add_token() { strcat(displayed_code, getToken(selected_token)); selected_token = 0; - if (using_func_tokens) { - using_func_tokens = false; + if (tokenMode == FUNC) { + tokenMode = NORMAL; } updateText(); @@ -235,10 +249,10 @@ static void calculate() // Button press handler static void click_select(ClickRecognizerRef recognizer, void* context) { - if (!using_func_tokens && selected_token == tokenCount() - 1) { + if (tokenMode == NORMAL && selected_token == tokenCount() - 1) { calculate(); - } else if (!using_func_tokens && selected_token == tokenCount() - 2) { - using_func_tokens = true; + } else if (tokenMode == NORMAL && selected_token == tokenCount() - 2) { + tokenMode = FUNC; selected_token = 0; updateText(); } else { @@ -273,23 +287,31 @@ static void code_click_subscribe(void* context) #ifdef LOW_MEM #define TIME_FUNC \ - "(def time (fn ()(utl tt \n"\ + "(def ti (fn ()(utl tt \n"\ " (cat \"Hey, it's \" \n"\ " (hrt) \":\" (mnt)\n" \ "))))\n" #else #define TIME_FUNC \ - "(def time (fn () ((def m (mnt)) (utl tt \n"\ + "(def ti (fn () ((def m (mnt)) (utl tt \n"\ " (cat \"Hey, it's \"\n"\ " (hrt) \":\" (if (< m 10) \"0\" \"\") m)\n"\ "))))\n" #endif -#define FORCE_TEXT TIME_FUNC \ +#define TIME_NEW \ + "(def ti (fn () (" \ + "(def t (time))" \ + "(cat \"Hey, it's \" t.minute \":\" t.sec)" \ + ")))" + +#define FORCE_TEXT TIME_NEW \ "(def ww (cw))\n" \ - "(def tt (atl ww \":\"))\n" \ - "(pw ww)\n" \ - "(sub time 2)" + "(def tt (atl ww (ti)))\n" \ + "(pw ww) " + // "(utl tt (cat \"\" (ti)))" + // "(sub 2 (fn ()" + // "(utl tt (cat \"\" (time)))))" // Remove to re-enable #undef FORCE_TEXT @@ -534,7 +556,7 @@ static void init(void) .unload = script_menu_unload }); window_stack_push(s_script_menu_window, true); - app_message_open(inbox_size, outbox_size); + //app_message_open(inbox_size, outbox_size); app_message_register_inbox_received(inbox_received_callback); tiny_font = fonts_load_custom_font( resource_get_handle(RESOURCE_ID_FONT_TINY_11)); diff --git a/src/env.c b/src/env.c index 8954a40..aa1441d 100644 --- a/src/env.c +++ b/src/env.c @@ -435,7 +435,7 @@ fn(segfault, "seg", #define pf(_func) buildFuncSym(_func ## Symbol, &(_func)) #endif -struct Environment defaultEnv() +struct Environment defaultEnvPreAllocated(char** strings, Object* objects) { #ifndef STANDALONE int helpInitialized = 0; @@ -450,8 +450,8 @@ struct Environment defaultEnv() struct Environment e = { .outer = NULL, - .strings = malloc(sizeof(char*) * MAX_ENV_ELM), - .objects = malloc(sizeof(Object) * MAX_ENV_ELM), + .strings = strings, + .objects = objects, .capacity = MAX_ENV_ELM, .refs = 1, }; @@ -476,6 +476,7 @@ struct Environment defaultEnv() pf(reduce), pf(mapO), pf(at), +#ifndef PBL_PLATFORM_APLITE pf(rest), pf(charAt), pf(isNum), @@ -484,6 +485,7 @@ struct Environment defaultEnv() pf(isErr), pf(charVal), pf(parseEvalO), +#endif pf(structAccess), pf(getTime), #ifndef LOW_MEM @@ -522,6 +524,12 @@ struct Environment defaultEnv() return e; } +struct Environment defaultEnv() +{ + return defaultEnvPreAllocated(malloc(sizeof(char*) * MAX_ENV_ELM), malloc(sizeof(Object) * MAX_ENV_ELM)); +} + + int getStructIndex(const char* name) { for (int i = 0; i < dictionary.structCount; i++) { diff --git a/src/env.h b/src/env.h index 8a94663..d850d85 100644 --- a/src/env.h +++ b/src/env.h @@ -37,6 +37,8 @@ void shredDictionary(); struct Environment defaultEnv(); +struct Environment defaultEnvPreAllocated(char** strings, Object* objects); + int getStructIndex(const char* name); struct StructDef* getStructAt(int i); diff --git a/src/object.c b/src/object.c index 6ca6cb9..e5515a6 100644 --- a/src/object.c +++ b/src/object.c @@ -650,7 +650,7 @@ inline Object cloneObject(const Object src) case TYPE_ERROR: return errorWithContext(getErrorCode(src), src.error->context); case TYPE_OTHER: - return cloneObject(src); + return cloneOther(src); case TYPE_BOOL: case TYPE_NUMBER: case TYPE_FUNC: diff --git a/src/pebbleobject.c b/src/pebbleobject.c index e26a712..c64f6b3 100644 --- a/src/pebbleobject.c +++ b/src/pebbleobject.c @@ -124,27 +124,39 @@ static void subscriptionHandler(struct tm* tick_time, TimeUnits changed) eval(&subscription, subscriptionEnv); } +int getUnit(Object time) +{ + if (time.type != TYPE_NUMBER) { + return MINUTE_UNIT; + } + switch (time.number) { + case 1: + return SECOND_UNIT; + case 2: + return MINUTE_UNIT; + case 3: + return HOUR_UNIT; + case 4: + return DAY_UNIT; + case 5: + return MONTH_UNIT; + case 6: + return YEAR_UNIT; + default: + return MINUTE_UNIT; + } +} + Object subscribe(Object* params, int length, struct Environment* env) { - Object function = params[0]; - Object time = params[1]; - if (function.type == TYPE_LAMBDA) { - subscription = cloneObject(function); + Object subscription = cloneObject(params[1]); + if (subscription.type == TYPE_LAMBDA) { + int unit = getUnit(params[0]); subscriptionEnv = env; - int unit = time.type != TYPE_NUMBER ? MINUTE_UNIT - : time.number == 1 ? SECOND_UNIT - : time.number == 2 ? MINUTE_UNIT - : time.number == 3 ? HOUR_UNIT - : time.number == 4 - ? DAY_UNIT - : time.number == - 5 ? MONTH_UNIT - : - time.number == - 6 ? YEAR_UNIT - : MINUTE_UNIT; tick_timer_service_subscribe(unit, subscriptionHandler); return trueObject(); + } else { + printf("Is not lambda, is %d\n", subscription.type); } return falseObject(); }