Fixes to run on Pebble hardware.
Correct cloneObject(TYPE_OTHER) behavior.
This commit is contained in:
parent
343568afda
commit
d625944e8d
70
src/calc.c
70
src/calc.c
|
@ -48,7 +48,7 @@ const char* tokens[] = {
|
||||||
"END"
|
"END"
|
||||||
};
|
};
|
||||||
|
|
||||||
// Currently selected button, starts on '('
|
// Currently-selected button, starts on '('
|
||||||
static int8_t selected_token = 1;
|
static int8_t selected_token = 1;
|
||||||
|
|
||||||
const char* func_tokens[] = {
|
const char* func_tokens[] = {
|
||||||
|
@ -57,27 +57,41 @@ const char* func_tokens[] = {
|
||||||
"sec ", "mnt ", "hr ", "hrt ", "vibe ", "sub ", "time "
|
"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
|
// // Size of messages to/from the phone app
|
||||||
const uint32_t inbox_size = 1024;
|
// const uint32_t inbox_size = 1024;
|
||||||
const uint32_t outbox_size = 1024;
|
// const uint32_t outbox_size = 1024;
|
||||||
|
|
||||||
/** Text Editing **/
|
/** Text Editing **/
|
||||||
|
|
||||||
// Get the number of tokens in the current list
|
// Get the number of tokens in the current list
|
||||||
static inline int8_t tokenCount()
|
static inline int8_t tokenCount()
|
||||||
{
|
{
|
||||||
return using_func_tokens ?
|
switch (tokenMode) {
|
||||||
sizeof(func_tokens) / sizeof(func_tokens[0]) :
|
case NORMAL:
|
||||||
sizeof(tokens) / sizeof(tokens[0]);
|
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
|
// Get current token from tokens[] or func_tokens[], as appropriate
|
||||||
static inline const char* getToken(int8_t n)
|
static inline const char* getToken(int8_t n)
|
||||||
{
|
{
|
||||||
|
//static char singleLetterString[2] = {'a', '\0'};
|
||||||
int8_t t = n % tokenCount();
|
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)
|
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));
|
text_layer_set_font(s_input_text_layer, fonts_get_system_font(f));
|
||||||
} else {
|
} 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);
|
text_layer_set_font(s_input_text_layer, tiny_font);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -180,7 +194,7 @@ static void click_backspace(ClickRecognizerRef recognizer, void* context)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!using_func_tokens) {
|
if (tokenMode == NORMAL) {
|
||||||
if (displayed_code[0] == '\0') {
|
if (displayed_code[0] == '\0') {
|
||||||
window_stack_remove(window_stack_get_top_window(), true);
|
window_stack_remove(window_stack_get_top_window(), true);
|
||||||
} else {
|
} else {
|
||||||
|
@ -189,7 +203,7 @@ static void click_backspace(ClickRecognizerRef recognizer, void* context)
|
||||||
updateText();
|
updateText();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
using_func_tokens = false;
|
tokenMode = NORMAL;
|
||||||
updateText();
|
updateText();
|
||||||
}
|
}
|
||||||
adjustFont();
|
adjustFont();
|
||||||
|
@ -200,8 +214,8 @@ static void add_token()
|
||||||
{
|
{
|
||||||
strcat(displayed_code, getToken(selected_token));
|
strcat(displayed_code, getToken(selected_token));
|
||||||
selected_token = 0;
|
selected_token = 0;
|
||||||
if (using_func_tokens) {
|
if (tokenMode == FUNC) {
|
||||||
using_func_tokens = false;
|
tokenMode = NORMAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateText();
|
updateText();
|
||||||
|
@ -235,10 +249,10 @@ static void calculate()
|
||||||
// Button press handler
|
// Button press handler
|
||||||
static void click_select(ClickRecognizerRef recognizer, void* context)
|
static void click_select(ClickRecognizerRef recognizer, void* context)
|
||||||
{
|
{
|
||||||
if (!using_func_tokens && selected_token == tokenCount() - 1) {
|
if (tokenMode == NORMAL && selected_token == tokenCount() - 1) {
|
||||||
calculate();
|
calculate();
|
||||||
} else if (!using_func_tokens && selected_token == tokenCount() - 2) {
|
} else if (tokenMode == NORMAL && selected_token == tokenCount() - 2) {
|
||||||
using_func_tokens = true;
|
tokenMode = FUNC;
|
||||||
selected_token = 0;
|
selected_token = 0;
|
||||||
updateText();
|
updateText();
|
||||||
} else {
|
} else {
|
||||||
|
@ -273,23 +287,31 @@ static void code_click_subscribe(void* context)
|
||||||
|
|
||||||
#ifdef LOW_MEM
|
#ifdef LOW_MEM
|
||||||
#define TIME_FUNC \
|
#define TIME_FUNC \
|
||||||
"(def time (fn ()(utl tt \n"\
|
"(def ti (fn ()(utl tt \n"\
|
||||||
" (cat \"Hey, it's \" \n"\
|
" (cat \"Hey, it's \" \n"\
|
||||||
" (hrt) \":\" (mnt)\n" \
|
" (hrt) \":\" (mnt)\n" \
|
||||||
"))))\n"
|
"))))\n"
|
||||||
#else
|
#else
|
||||||
#define TIME_FUNC \
|
#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"\
|
" (cat \"Hey, it's \"\n"\
|
||||||
" (hrt) \":\" (if (< m 10) \"0\" \"\") m)\n"\
|
" (hrt) \":\" (if (< m 10) \"0\" \"\") m)\n"\
|
||||||
"))))\n"
|
"))))\n"
|
||||||
#endif
|
#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 ww (cw))\n" \
|
||||||
"(def tt (atl ww \":\"))\n" \
|
"(def tt (atl ww (ti)))\n" \
|
||||||
"(pw ww)\n" \
|
"(pw ww) "
|
||||||
"(sub time 2)"
|
// "(utl tt (cat \"\" (ti)))"
|
||||||
|
// "(sub 2 (fn ()"
|
||||||
|
// "(utl tt (cat \"\" (time)))))"
|
||||||
|
|
||||||
// Remove to re-enable
|
// Remove to re-enable
|
||||||
#undef FORCE_TEXT
|
#undef FORCE_TEXT
|
||||||
|
@ -534,7 +556,7 @@ static void init(void)
|
||||||
.unload = script_menu_unload
|
.unload = script_menu_unload
|
||||||
});
|
});
|
||||||
window_stack_push(s_script_menu_window, true);
|
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);
|
app_message_register_inbox_received(inbox_received_callback);
|
||||||
tiny_font = fonts_load_custom_font(
|
tiny_font = fonts_load_custom_font(
|
||||||
resource_get_handle(RESOURCE_ID_FONT_TINY_11));
|
resource_get_handle(RESOURCE_ID_FONT_TINY_11));
|
||||||
|
|
14
src/env.c
14
src/env.c
|
@ -435,7 +435,7 @@ fn(segfault, "seg",
|
||||||
#define pf(_func) buildFuncSym(_func ## Symbol, &(_func))
|
#define pf(_func) buildFuncSym(_func ## Symbol, &(_func))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct Environment defaultEnv()
|
struct Environment defaultEnvPreAllocated(char** strings, Object* objects)
|
||||||
{
|
{
|
||||||
#ifndef STANDALONE
|
#ifndef STANDALONE
|
||||||
int helpInitialized = 0;
|
int helpInitialized = 0;
|
||||||
|
@ -450,8 +450,8 @@ struct Environment defaultEnv()
|
||||||
|
|
||||||
struct Environment e = {
|
struct Environment e = {
|
||||||
.outer = NULL,
|
.outer = NULL,
|
||||||
.strings = malloc(sizeof(char*) * MAX_ENV_ELM),
|
.strings = strings,
|
||||||
.objects = malloc(sizeof(Object) * MAX_ENV_ELM),
|
.objects = objects,
|
||||||
.capacity = MAX_ENV_ELM,
|
.capacity = MAX_ENV_ELM,
|
||||||
.refs = 1,
|
.refs = 1,
|
||||||
};
|
};
|
||||||
|
@ -476,6 +476,7 @@ struct Environment defaultEnv()
|
||||||
pf(reduce),
|
pf(reduce),
|
||||||
pf(mapO),
|
pf(mapO),
|
||||||
pf(at),
|
pf(at),
|
||||||
|
#ifndef PBL_PLATFORM_APLITE
|
||||||
pf(rest),
|
pf(rest),
|
||||||
pf(charAt),
|
pf(charAt),
|
||||||
pf(isNum),
|
pf(isNum),
|
||||||
|
@ -484,6 +485,7 @@ struct Environment defaultEnv()
|
||||||
pf(isErr),
|
pf(isErr),
|
||||||
pf(charVal),
|
pf(charVal),
|
||||||
pf(parseEvalO),
|
pf(parseEvalO),
|
||||||
|
#endif
|
||||||
pf(structAccess),
|
pf(structAccess),
|
||||||
pf(getTime),
|
pf(getTime),
|
||||||
#ifndef LOW_MEM
|
#ifndef LOW_MEM
|
||||||
|
@ -522,6 +524,12 @@ struct Environment defaultEnv()
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Environment defaultEnv()
|
||||||
|
{
|
||||||
|
return defaultEnvPreAllocated(malloc(sizeof(char*) * MAX_ENV_ELM), malloc(sizeof(Object) * MAX_ENV_ELM));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int getStructIndex(const char* name)
|
int getStructIndex(const char* name)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < dictionary.structCount; i++) {
|
for (int i = 0; i < dictionary.structCount; i++) {
|
||||||
|
|
|
@ -37,6 +37,8 @@ void shredDictionary();
|
||||||
|
|
||||||
struct Environment defaultEnv();
|
struct Environment defaultEnv();
|
||||||
|
|
||||||
|
struct Environment defaultEnvPreAllocated(char** strings, Object* objects);
|
||||||
|
|
||||||
int getStructIndex(const char* name);
|
int getStructIndex(const char* name);
|
||||||
|
|
||||||
struct StructDef* getStructAt(int i);
|
struct StructDef* getStructAt(int i);
|
||||||
|
|
|
@ -650,7 +650,7 @@ inline Object cloneObject(const Object src)
|
||||||
case TYPE_ERROR:
|
case TYPE_ERROR:
|
||||||
return errorWithContext(getErrorCode(src), src.error->context);
|
return errorWithContext(getErrorCode(src), src.error->context);
|
||||||
case TYPE_OTHER:
|
case TYPE_OTHER:
|
||||||
return cloneObject(src);
|
return cloneOther(src);
|
||||||
case TYPE_BOOL:
|
case TYPE_BOOL:
|
||||||
case TYPE_NUMBER:
|
case TYPE_NUMBER:
|
||||||
case TYPE_FUNC:
|
case TYPE_FUNC:
|
||||||
|
|
|
@ -124,27 +124,39 @@ static void subscriptionHandler(struct tm* tick_time, TimeUnits changed)
|
||||||
eval(&subscription, subscriptionEnv);
|
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 subscribe(Object* params, int length, struct Environment* env)
|
||||||
{
|
{
|
||||||
Object function = params[0];
|
Object subscription = cloneObject(params[1]);
|
||||||
Object time = params[1];
|
if (subscription.type == TYPE_LAMBDA) {
|
||||||
if (function.type == TYPE_LAMBDA) {
|
int unit = getUnit(params[0]);
|
||||||
subscription = cloneObject(function);
|
|
||||||
subscriptionEnv = env;
|
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);
|
tick_timer_service_subscribe(unit, subscriptionHandler);
|
||||||
return trueObject();
|
return trueObject();
|
||||||
|
} else {
|
||||||
|
printf("Is not lambda, is %d\n", subscription.type);
|
||||||
}
|
}
|
||||||
return falseObject();
|
return falseObject();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue