Got results-as-operands working

This commit is contained in:
Sage Vaillancourt 2020-05-04 02:24:36 +01:00 committed by =
parent 17b6bf12b4
commit 9ab907d014
8 changed files with 64 additions and 15 deletions

View File

@ -3,7 +3,7 @@
"capabilities": [
""
],
"companyName": "Save V.",
"companyName": "Sage V.",
"enableMultiJS": false,
"longName": "PebbLisp",
"projectType": "native",

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.5 KiB

After

Width:  |  Height:  |  Size: 891 B

Binary file not shown.

Binary file not shown.

View File

@ -35,6 +35,17 @@ static void up_down_handler(ClickRecognizerRef recognizer, void *context){
updateText();
}
static void back_handler(ClickRecognizerRef recognizer, void *context) {
if(mytext[0] == '\0') {
window_stack_remove(window_stack_get_top_window(), true);
} else {
int i = 0;
while(mytext[++i] != '\0') { ; }
mytext[i-1] = '\0';
updateText();
}
}
// Adds the current string to the main string
static void enter(){
strcat(mytext, getToken(selected_token));
@ -45,10 +56,10 @@ static void enter(){
//Calculate result, display it and reset
static void calculate(){
Object obj = parseEval(mytext, &env);
snprintf(mytext, MAX_LENGTH, "R:%d", obj.number);
snprintf(resulttext, MAX_LENGTH, "R:%d", obj.number);
selected_token = 0;
//strcpy(mytext, HAHA);
updateText();
text_layer_set_text(s_result_text_layer, resulttext);
}
// Button press handler
@ -64,22 +75,31 @@ static void click_config_provider(void *context) {
window_single_repeating_click_subscribe(BUTTON_ID_UP, 100, up_down_handler);
window_single_repeating_click_subscribe(BUTTON_ID_DOWN, 100, up_down_handler);
window_single_click_subscribe(BUTTON_ID_SELECT, select_handler);
window_single_click_subscribe(BUTTON_ID_BACK, back_handler);
}
static void init(void) {
// Create a window and get information about the window
s_window = window_create();
Layer *window_layer = window_get_root_layer(s_window);
// Layer *window_layer = window_get_root_layer(s_window);
// Register click config provider
window_set_click_config_provider(s_window, click_config_provider);
// Input text layer setup
GRect text_bounds = GRect(6, 6, 132, 132);
GRect text_bounds = GRect(6, 6, 132, 127);
s_input_text_layer = text_layer_create(text_bounds);
text_layer_set_text(s_input_text_layer, getToken(1));
text_layer_set_font(s_input_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_24_BOLD));
text_layer_set_font(s_input_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
layer_add_child(window_get_root_layer(s_window), text_layer_get_layer(s_input_text_layer));
// Result text layer setup
GRect result_bounds = GRect(6, 128, 132, 132);
s_result_text_layer = text_layer_create(result_bounds);
text_layer_set_text(s_result_text_layer, "R: ");
text_layer_set_font(s_result_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_28_BOLD));
text_layer_set_text_alignment(s_result_text_layer, GTextAlignmentRight);
layer_add_child(window_get_root_layer(s_window), text_layer_get_layer(s_result_text_layer));
// Push the window, setting the window animation to 'true'
window_stack_push(s_window, true);
env = defaultEnv();

View File

@ -8,11 +8,11 @@
// Layers
Window *s_window;
TextLayer *s_input_text_layer;
const char *HAHA = "STONKS";
TextLayer *s_result_text_layer;
char mytext[SMAX_LENGTH] = "";
char temptext[SMAX_LENGTH] = "";
char resulttext[MAX_LENGTH] = "";
char *tokens[] = {
" ", "(", ")",

View File

@ -46,7 +46,7 @@ Object fetchFromEnvironment(const char *name, struct Environment *env)
Result parse(struct Slice *slices)
{
// printf("parse()\n");
printf("parse() START\n");
struct Slice *token = slices;
struct Slice *rest;
if(token->text != NULL) {
@ -62,11 +62,12 @@ Result parse(struct Slice *slices)
} else { // todo error on closed paren
return R(parseAtom(token), rest);
}
printf("parse() END\n");
}
Result readSeq(struct Slice *tokens)
{
// printf("readSeq()\n");
printf("readSeq() START\n");
Object res;
res.forward = NULL;
res.type = TYPE_LIST;
@ -80,13 +81,16 @@ Result readSeq(struct Slice *tokens)
return R(res, rest);
}
Result r = parse(tokens);
march->forward = malloc(sizeof(Object));
printf("readSeq() before malloc\n");
march->forward = malloc(sizeof(struct Object));
printf("readSeq() after malloc\n");
*march->forward = r.obj;
// char out[MAX_TOK_LEN];
// printf("stringObj: %s\n", stringObj(out, &r.obj));
tokens = r.slices;
march = march->forward;
}
printf("readSeq() END\n");
}
Object parseAtom(struct Slice *s)
@ -111,6 +115,8 @@ Object parseAtom(struct Slice *s)
Object eval(Object *obj, struct Environment *env)
{
printf("eval(): ");
printObj(obj);
Object o = *obj;
switch(obj->type) {
case TYPE_NUMBER:
@ -122,7 +128,9 @@ Object eval(Object *obj, struct Environment *env)
// printf("TYPE_LIST\n");
Object first_form = *obj->forward;
Object first_eval = eval(&first_form, env);
return first_eval.func(*first_form.forward, *first_form.forward->forward);
Object arg1 = eval(first_form.forward, env);
Object arg2 = eval(first_form.forward->forward, env);
return first_eval.func(arg1, arg2);
}
case TYPE_SYMBOL:
{
@ -145,6 +153,22 @@ char* stringObj(char *dest, Object *obj)
return dest;
}
void printObj(Object *obj)
{
if(obj->type == TYPE_NUMBER) {
printf("TYPE_NUMBER");
} else if(obj->type == TYPE_LIST) {
printf("TYPE_NUMBER");
} else if(obj->type == TYPE_SYMBOL) {
printf("TYPE_NUMBER");
} else {
printf("TYPE_OTHER");
}
char temp[20];
stringObj(temp, obj);
printf(": %s\n", temp);
}
Result resultFromObjAndSlices(Object obj, struct Slice *slices)
{
Result r;
@ -211,6 +235,9 @@ void addFunc(const char *name, Object (*func)(Object, Object),
struct Environment defaultEnv() {
struct Environment e;
e.strings = malloc(sizeof(char*) * MAX_ENV_ELM);
for(int i = 0; i < MAX_ENV_ELM; i++) {
e.strings[i] = NULL;
}
e.objects = malloc(sizeof(Object) * MAX_ENV_ELM);
addFunc("+", &add, &e);
addFunc("-", &sub, &e);
@ -248,6 +275,7 @@ Object parseEval(const char *input, struct Environment *env)
// printf("parseEval() parse()\n");
#endif
Object parsed = parse(tokens).obj;
free(tokens);
return eval(&parsed, env);
}

View File

@ -3,9 +3,9 @@
// #define STANDALONE
#define MAX_TOK_LEN 3
#define MAX_TOK_CNT 7 // 128
#define MAX_ENV_ELM 3 // 50
#define MAX_TOK_LEN 4
#define MAX_TOK_CNT 128 // 128
#define MAX_ENV_ELM 15 // 50
// static const char* tokenFail = "Missing ')'\n";
@ -69,6 +69,7 @@ Object parseAtom(struct Slice *slice);
void copySlice(char * dest, struct Slice *src);
Object parseEval(const char *input, struct Environment *env);
struct Environment defaultEnv();
void printObj(Object *obj);
Result resultFromObjAndSlices(Object obj, struct Slice *slices);
Object add(Object obj1, Object obj2);