Remove in-memory duplication of pebbles' code text
This commit is contained in:
parent
723acc6b2e
commit
787aec6f95
47
src/calc.c
47
src/calc.c
|
@ -24,11 +24,9 @@ int current_script_num;
|
||||||
// PebbLisp environment
|
// PebbLisp environment
|
||||||
static struct Environment env;
|
static struct Environment env;
|
||||||
|
|
||||||
// Live code text
|
// The displayed/processed code text
|
||||||
char current_code_text[SMAX_LENGTH] = "";
|
|
||||||
|
|
||||||
// The actual displayed code text
|
|
||||||
char displayed_code[SMAX_LENGTH] = "";
|
char displayed_code[SMAX_LENGTH] = "";
|
||||||
|
int excess_chars = 1;
|
||||||
|
|
||||||
// The result of execution
|
// The result of execution
|
||||||
#define RESULT_PREFIX "R:"
|
#define RESULT_PREFIX "R:"
|
||||||
|
@ -54,9 +52,7 @@ const char *tokens[] = {
|
||||||
static int8_t selected_token = 1;
|
static int8_t selected_token = 1;
|
||||||
|
|
||||||
const char *func_tokens[] = {
|
const char *func_tokens[] = {
|
||||||
"spent ", "window ",
|
"window ",
|
||||||
"max ", "min ",
|
|
||||||
"sq ", "cube ", "exp ",
|
|
||||||
"sc ", "cw ", "pw ", "rw ", "atl ", "utl ",
|
"sc ", "cw ", "pw ", "rw ", "atl ", "utl ",
|
||||||
"sec ", "mnt ", "hr ", "hrt ", "vibe ", "sub "
|
"sec ", "mnt ", "hr ", "hrt ", "vibe ", "sub "
|
||||||
};
|
};
|
||||||
|
@ -84,19 +80,28 @@ static inline const char* getToken(int8_t n)
|
||||||
return using_func_tokens? func_tokens[t] : tokens[t];
|
return using_func_tokens? func_tokens[t] : tokens[t];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void trim(size_t len)
|
||||||
|
{
|
||||||
|
size_t end = strlen(displayed_code) - 1;
|
||||||
|
for (size_t i = 0; i < len; i++) {
|
||||||
|
displayed_code[end - i] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Update the current code text with the contents of `current_code_text`,
|
// Update the current code text with the contents of `current_code_text`,
|
||||||
// and add the current selected_token to the end
|
// and add the current selected_token to the end
|
||||||
static void updateText()
|
static void updateText()
|
||||||
{
|
{
|
||||||
|
trim(excess_chars);
|
||||||
const char *token = getToken(selected_token);
|
const char *token = getToken(selected_token);
|
||||||
|
|
||||||
strcpy(displayed_code, current_code_text);
|
const char* add =
|
||||||
|
|
||||||
strcat(displayed_code,
|
|
||||||
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
|
||||||
);
|
|
||||||
|
strcat(displayed_code, add);
|
||||||
|
excess_chars = strlen(add);
|
||||||
|
|
||||||
text_layer_set_text(s_input_text_layer, displayed_code);
|
text_layer_set_text(s_input_text_layer, displayed_code);
|
||||||
}
|
}
|
||||||
|
@ -174,10 +179,11 @@ static void click_backspace(ClickRecognizerRef recognizer, void *context)
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!using_func_tokens) {
|
if(!using_func_tokens) {
|
||||||
if(current_code_text[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 {
|
||||||
current_code_text[strlen(current_code_text) - 1] = '\0';
|
trim(excess_chars + 1);
|
||||||
|
excess_chars = 0;
|
||||||
updateText();
|
updateText();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -190,7 +196,7 @@ static void click_backspace(ClickRecognizerRef recognizer, void *context)
|
||||||
// Adds the current token string to the main code string
|
// Adds the current token string to the main code string
|
||||||
static void add_token()
|
static void add_token()
|
||||||
{
|
{
|
||||||
strcat(current_code_text, getToken(selected_token));
|
strcat(displayed_code, getToken(selected_token));
|
||||||
selected_token = 0;
|
selected_token = 0;
|
||||||
if(using_func_tokens) {
|
if(using_func_tokens) {
|
||||||
using_func_tokens = false;
|
using_func_tokens = false;
|
||||||
|
@ -205,9 +211,13 @@ static void add_token()
|
||||||
// Calculate result, display it and reset
|
// Calculate result, display it and reset
|
||||||
static void calculate()
|
static void calculate()
|
||||||
{
|
{
|
||||||
Object obj = parseEval(current_code_text, &env);
|
|
||||||
char temp[RESULT_LENGTH + 2] = "";
|
char temp[RESULT_LENGTH + 2] = "";
|
||||||
|
|
||||||
|
trim(excess_chars);
|
||||||
|
excess_chars = 0;
|
||||||
|
Object obj = parseEval(displayed_code, &env);
|
||||||
|
updateText();
|
||||||
|
|
||||||
stringNObj(temp, &obj, RESULT_LENGTH);
|
stringNObj(temp, &obj, RESULT_LENGTH);
|
||||||
if(obj.type == TYPE_ERROR) {
|
if(obj.type == TYPE_ERROR) {
|
||||||
text_layer_set_font(s_result_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_14_BOLD));
|
text_layer_set_font(s_result_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_14_BOLD));
|
||||||
|
@ -307,12 +317,12 @@ static void code_window_load(Window *window)
|
||||||
|
|
||||||
// If possible, load the previous code text
|
// If possible, load the previous code text
|
||||||
#ifdef FORCE_TEXT
|
#ifdef FORCE_TEXT
|
||||||
strncpy(current_code_text, FORCE_TEXT, SMAX_LENGTH);
|
strncpy(displayed_code, FORCE_TEXT, SMAX_LENGTH);
|
||||||
updateText();
|
updateText();
|
||||||
adjustFont();
|
adjustFont();
|
||||||
#else
|
#else
|
||||||
if(persist_exists(current_script_num)) {
|
if(persist_exists(current_script_num)) {
|
||||||
persist_read_string(current_script_num, current_code_text, SMAX_LENGTH);
|
persist_read_string(current_script_num, displayed_code, SMAX_LENGTH);
|
||||||
updateText();
|
updateText();
|
||||||
adjustFont();
|
adjustFont();
|
||||||
}
|
}
|
||||||
|
@ -469,7 +479,6 @@ static void inbox_received_callback(DictionaryIterator *iter, void *context) {
|
||||||
}
|
}
|
||||||
char *script_text = script_tuple->value->cstring;
|
char *script_text = script_tuple->value->cstring;
|
||||||
snprintf(displayed_code, sizeof(displayed_code), "%s", script_text);
|
snprintf(displayed_code, sizeof(displayed_code), "%s", script_text);
|
||||||
snprintf(current_code_text, sizeof(current_code_text), "%s", script_text);
|
|
||||||
updateText();
|
updateText();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue