From 77ff84fd305de8d9ef25abdf5fa7acad1d5491f9 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Mon, 4 May 2020 15:03:35 +0100 Subject: [PATCH] Before breaking lists --- src/.pebblisp.c.swp | Bin 24576 -> 36864 bytes src/calc.c | 4 +- src/calc.h | 6 +- src/pebblisp.c | 161 +++++++++++++++++++++++++++++++++----------- src/pebblisp.h | 7 +- src/tags | 35 +++++----- src/tokens.c | 32 ++++++++- 7 files changed, 181 insertions(+), 64 deletions(-) diff --git a/src/.pebblisp.c.swp b/src/.pebblisp.c.swp index 7ab91f5ef611678aa0caa9b4de917a95edd9662f..c9633e6087dcfc768035b784c4160fa518c3cf05 100644 GIT binary patch literal 36864 zcmeI5f0SHReZU_93W-GoER@!AUM6ODX7*>+_J|2tN!Sep!!AuWfK9?OJM(7uk=dDL zW_CkJ5Jjno9<^2J0feHU2$l+m3P)@$1qG}}QK`_< z<`0kc&N-ibGwesjtec^p87s`!6QTg>o8j8gLqL8gLqL8gLqL8gLqL8u*pe zKy&U0?=n)p%%uL0&GnF$>k;Pn(dK%2i~Dbw-$$D36)oP|{FxNk6 zakp~!oBQ{)xF2eMPnc`QWWeUb+;$hI0jB|{0jB|{0jB|{0jB|{0jB|{0jB|{fnQ4v z6iS|VJVx@FVnDP0Z|(mdW9UBy_rqVpb#N`b7k0t9a5jvC4=2Hq@ISBhysyLO;CA>6 zct5-kE`T?~ac~s;_fekrHTWt#4DW)=U=GfJe)zB7#2r2b*TG(Bz!;ng-EbnD0LQ~m znP7Mh9)a88W3UHy!Z@sk*TD){4*$Nw^S%PNz<$^YTc8*6kb}cu89ce%^S%Lh!^fck zbvO^sgq83N6BS>Eo8TJQ1E<5$@D!6Me+$>cB#git;UsvG36%eZ7vORD4m<>R!be~p zCSeGUhyP$g<@@j%_y8=xDd58j_&F0Q--Ew{FT(BcS-1^mVI!OhE8#de91ewL@FnWP zU&9UXVfYXy2iwpT1cS0gP$k=ms{#oFB7 zZI!SXWNZD^!fZ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7krZE6=Hn%pPCnN;JWrC2VK26$ z5>^-x0S!WMQ!Pp7F^EpAg#}L|4XIEg7&qoh(vLWUgV=#K*iZ%&?gUHH#bEybVNM3n zg)%I_?I20|4u|1H8I~X!p$Ne|{1(V$IEPa>fSss8DRcj%|~ww%8g?a+9mE)>&h*Z#5{_vZ>n9)G5-Cn!cQW7GrmLv*yc9^F8xi$Hf#f4?%8=P@Q@lT0v`mP>J zsP{=HEd`>qGC{Ogn#CDik(h|TC&IHM)yrHB^#-H&wvmKnVKsz{DE)}Hf1tODBy+H* z!qDUAE!;O|dX|{_{X9P;rucW(X;@+?8|~3fd>AH44{!@N@dZxcJ-mfVWFiAP%)!rW z^V4`69e4@rkPjKrm|~v4ao;!RZH?ra)YfY}^%+`Kr>$C&$_YmoOQF0?H=WY1@$`de zgALE40)<$K6-a~;fw;j;XV8yktj8)OApsFk-r^drVifx{Z1BCD>_#i>D2ElLn2#s~ z;cf`2!8x43D{x{142Z%UTq2F1VGzxzf|6J>mO_ts=rE;bgI^|+Blrlf;Z?is~BYb*nvl@?=$jOluQC`3ocTsd}SXZkLF;qsk?F@5s%M;Tuhl3D=uu zjj4CniN}T{&l=L?wm&{3an=x9b+s}?3{*ywcy+omU4#6LY-+G?O*eaIS4!26%{F(P zD{H!-6zz=;+l%7($^@}!WlF&RS8X${%4gJ<6cmfNoEUK-A;NQ{Aka^oEG!5tS-Uzf z-zqwabbZC);(VcIMn!zGszSV8yg;NEMQIxyE_b6-=Dm`0oVI$0=qifqD|VD9x!y+R uzJ`)(rBm7jSslk1zQrI8;SFqq6>0FtC^ud3KEpGiZ3z|aS+Tx#y!S7|Vta%D diff --git a/src/calc.c b/src/calc.c index 0b77c17..1aa46da 100644 --- a/src/calc.c +++ b/src/calc.c @@ -56,7 +56,9 @@ static void enter(){ //Calculate result, display it and reset static void calculate(){ Object obj = parseEval(mytext, &env); - snprintf(resulttext, MAX_LENGTH, "R:%d", obj.number); + char temp[MAX_LENGTH-2] = ""; + stringObj(temp, &obj); + snprintf(resulttext, MAX_LENGTH, "R:%s", temp); selected_token = 0; //strcpy(mytext, HAHA); text_layer_set_text(s_result_text_layer, resulttext); diff --git a/src/calc.h b/src/calc.h index 7a23f11..9334e41 100644 --- a/src/calc.h +++ b/src/calc.h @@ -16,11 +16,13 @@ char resulttext[MAX_LENGTH] = ""; char *tokens[] = { " ", "(", ")", - "+", "-", "*", "/", + "+ ", "- ", "* ", "/ ", "1","2","3", "4","5","6", "7","8","9", "0", + "a", "b", "c", "d", "e", + "= ", "< ", "> ", "def", END_PHRASE }; -#define TOKEN_END 17 +#define TOKEN_END 26 diff --git a/src/pebblisp.c b/src/pebblisp.c index 9c8517d..617ba21 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -46,7 +46,7 @@ Object fetchFromEnvironment(const char *name, struct Environment *env) Result parse(struct Slice *slices) { - printf("parse() START\n"); + // printf("parse() START\n"); struct Slice *token = slices; struct Slice *rest; if(token->text != NULL) { @@ -62,12 +62,12 @@ Result parse(struct Slice *slices) } else { // todo error on closed paren return R(parseAtom(token), rest); } - printf("parse() END\n"); + // printf("parse() END\n"); } Result readSeq(struct Slice *tokens) { - printf("readSeq() START\n"); + // printf("readSeq() START\n"); Object res; res.forward = NULL; res.type = TYPE_LIST; @@ -81,21 +81,20 @@ Result readSeq(struct Slice *tokens) return R(res, rest); } Result r = parse(tokens); - printf("readSeq() before malloc\n"); + // printf("readSeq() before malloc\n"); march->forward = malloc(sizeof(struct Object)); - printf("readSeq() after malloc\n"); + // 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"); + // printf("readSeq() END\n"); } Object parseAtom(struct Slice *s) { - // printf("parseAtom()\n"); Object o; o.forward = NULL; if(isDigit(s->text[0])) { @@ -105,7 +104,12 @@ Object parseAtom(struct Slice *s) o.number *= 10; o.number += s->text[i] - '0'; } - + } else if (s->text[0] == 'T' && s->text[1] == '\0') { + o.type = TYPE_BOOL; + o.number = 1; + } else if (s->text[0] == 'F' && s->text[1] == '\0') { + o.type = TYPE_BOOL; + o.number = 0; } else { o.type = TYPE_SYMBOL; copySlice(o.name, s); @@ -113,58 +117,119 @@ Object parseAtom(struct Slice *s) return o; } -Object eval(Object *obj, struct Environment *env) +Object evalDefArgs(const Object *first, const Object *rest, + struct Environment *env) +{ + printf("evalDefArgs()\n"); + Object o; + return o; +} + +Object evalIfArgs(const Object *arg_forms, struct Environment *env) +{ + printf("evalIfArgs()\n"); + Object o; + + printf("test_form:\n"); + const Object *test_form = arg_forms; + printObj(test_form); + printObj(test_form->list); + //printObj(test_form->list->forward); + + printf("test_eval:\n"); + Object test_eval = eval(test_form, env); + printObj(&test_eval); + + printf("test_form END\n"); + + o.type = TYPE_NUMBER; + o.number = 222; + return o; +} + +Object evalBuiltIns(const Object *first, const Object *rest, int *found, + struct Environment *env) +{ + if(strcmp(first->name, "def") == 0) { + *found = 0; + return evalDefArgs(first, rest, env); + }else if(strcmp(first->name, "if") == 0) { + *found = 0; + return evalIfArgs(rest, env); + } + + Object o; + *found = -1; + return o; +} + +Object eval(const Object *obj, struct Environment *env) { printf("eval(): "); printObj(obj); Object o = *obj; switch(obj->type) { case TYPE_NUMBER: - { + case TYPE_BOOL: return *obj; - } + + case TYPE_SYMBOL: + return fetchFromEnvironment(obj->name, env); + case TYPE_LIST: { - // printf("TYPE_LIST\n"); Object first_form = *obj->forward; + + { // Try to eval built-ins + int i = -1; + Object built_in = + evalBuiltIns(&first_form, first_form.forward, &i, env); + + if(i == 0) + return built_in; + } + printf("\nNo built-ins found\n"); + Object first_eval = eval(&first_form, env); Object arg1 = eval(first_form.forward, env); Object arg2 = eval(first_form.forward->forward, env); + + printf("Evaluating func\n"); return first_eval.func(arg1, arg2); } - case TYPE_SYMBOL: - { - return fetchFromEnvironment(obj->name, env); - } default: ; } return o; } -char* stringObj(char *dest, Object *obj) +char* stringObj(char *dest, const Object *obj) { if(obj->type == TYPE_NUMBER) { snprintf(dest, MAX_TOK_LEN, "%d", obj->number); } else if(obj->type == TYPE_SYMBOL) { - return obj->name; + snprintf(dest, MAX_TOK_LEN, "%s", obj->name); + } else if(obj->type == TYPE_BOOL) { + snprintf(dest, MAX_TOK_LEN, "%s", obj->number ? "T" : "F"); } return dest; } -void printObj(Object *obj) +void printObj(const Object *obj) { if(obj->type == TYPE_NUMBER) { printf("TYPE_NUMBER"); } else if(obj->type == TYPE_LIST) { - printf("TYPE_NUMBER"); + printf("TYPE_LIST"); } else if(obj->type == TYPE_SYMBOL) { - printf("TYPE_NUMBER"); + printf("TYPE_SYMBOL"); + } else if(obj->type == TYPE_BOOL) { + printf("TYPE_BOOL"); } else { printf("TYPE_OTHER"); } - char temp[20]; + char temp[20] = ""; stringObj(temp, obj); printf(": %s\n", temp); } @@ -181,37 +246,51 @@ Object basicOp(Object *obj1, Object *obj2, const char op) { Object o; o.forward = NULL; + o.type = TYPE_NUMBER; switch(op){ case '+': o.number = obj1->number + obj2->number; - break; + return o; case '-': o.number = obj1->number - obj2->number; - break; + return o; case '*': o.number = obj1->number * obj2->number; - break; + return o; case '/': o.number = obj1->number / obj2->number; - break; - default: - o = *obj1; + return o; } + + o.type = TYPE_BOOL; + switch(op) { + case '=': + o.number = obj1->number == obj2->number; + return o; + case '>': + o.number = obj1->number > obj2->number; + return o; + case '<': + o.number = obj1->number < obj2->number; + return o; + } + + o = *obj1; return o; } -Object add(Object obj1, Object obj2) - { return basicOp(&obj1, &obj2, '+'); } +#define bopf(_name, _char) \ +Object _name(Object obj1, Object obj2) \ +{ \ + return basicOp(&obj1, &obj2, _char); \ +} -Object sub(Object obj1, Object obj2) - { return basicOp(&obj1, &obj2, '-'); } - -Object mul(Object obj1, Object obj2) - { return basicOp(&obj1, &obj2, '*'); } - -Object divi(Object obj1, Object obj2) - { return basicOp(&obj1, &obj2, '/'); } +bopf(add, '+'); +bopf(sub, '-'); +bopf(mul, '*'); +bopf(dvi, '/'); +bopf(equ, '='); void addFunc(const char *name, Object (*func)(Object, Object), struct Environment *env) @@ -242,7 +321,8 @@ struct Environment defaultEnv() { addFunc("+", &add, &e); addFunc("-", &sub, &e); addFunc("*", &mul, &e); - addFunc("/", &divi, &e); + addFunc("/", &dvi, &e); + addFunc("=", &equ, &e); /* e.strings[0] = malloc(sizeof("+")); @@ -287,7 +367,8 @@ int repl(struct Environment *env) printf("pebblisp>> "); fgets(input, 100, stdin); Object obj = parseEval(input, env); - printf("eval: %d\n", obj.number); + printObj(&obj); + //printf("eval: %d\n", obj.number); break; } } diff --git a/src/pebblisp.h b/src/pebblisp.h index f1891e1..3ff1664 100644 --- a/src/pebblisp.h +++ b/src/pebblisp.h @@ -18,6 +18,7 @@ struct Slice { typedef enum Type { TYPE_NUMBER, + TYPE_BOOL, TYPE_LIST, TYPE_FUNC, //TYPE_STRING, @@ -61,15 +62,15 @@ struct Environment { Object *objects; }; -Object eval(Object *obj, struct Environment *env); -char* stringObj(char *dest, Object *obj); +Object eval(const Object *obj, struct Environment *env); +char* stringObj(char *dest, const Object *obj); Result parse(struct Slice *slices); Result readSeq(struct Slice *slices); 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); +void printObj(const Object *obj); Result resultFromObjAndSlices(Object obj, struct Slice *slices); Object add(Object obj1, Object obj2); diff --git a/src/tags b/src/tags index 8d44738..04f9d44 100644 --- a/src/tags +++ b/src/tags @@ -15,13 +15,14 @@ MAX_TOK_LEN pebblisp.h 6;" d Object pebblisp.h /^struct Object {$/;" s Object pebblisp.h /^typedef struct Object Object;$/;" t typeref:struct:Object PEBBLISP_H pebblisp.h 2;" d -R pebblisp.h 74;" d +R pebblisp.h 78;" d Result pebblisp.h /^typedef struct Result {$/;" s Result pebblisp.h /^} Result;$/;" t typeref:struct:Result SMAX_LENGTH calc.h 4;" d -STANDALONE pebblisp.h 4;" d Slice pebblisp.h /^struct Slice {$/;" s TOKENS_H tokens.h 2;" d +TOKEN_END calc.h 28;" d +TYPE_BOOL pebblisp.h /^ TYPE_BOOL,$/;" e enum:Type TYPE_ENV pebblisp.h /^ TYPE_ENV,$/;" e enum:Type TYPE_ERROR pebblisp.h /^ TYPE_ERROR$/;" e enum:Type TYPE_FUNC pebblisp.h /^ TYPE_FUNC,$/;" e enum:Type @@ -31,16 +32,22 @@ TYPE_SYMBOL pebblisp.h /^ TYPE_SYMBOL,$/;" e enum:Type Type pebblisp.h /^typedef enum Type {$/;" g Type pebblisp.h /^} Type;$/;" t typeref:enum:Type _h_FIXED_ fixed.h 26;" d -add pebblisp.c /^Object add(Object obj1, Object obj2)$/;" f +addFunc pebblisp.c /^void addFunc(const char *name, Object (*func)(Object, Object), $/;" f +back_handler calc.c /^static void back_handler(ClickRecognizerRef recognizer, void *context) {$/;" f file: +basicOp pebblisp.c /^Object basicOp(Object *obj1, Object *obj2, const char op)$/;" f +bopf pebblisp.c 257;" d file: calculate calc.c /^static void calculate(){$/;" f file: -clear calc.c /^static void clear(bool full){$/;" f file: click_config_provider calc.c /^static void click_config_provider(void *context) {$/;" f file: copySlice pebblisp.c /^void copySlice(char * dest, struct Slice *src)$/;" f debugSlice pebblisp.c /^void debugSlice(struct Slice *s)$/;" f defaultEnv pebblisp.c /^struct Environment defaultEnv() {$/;" f deinit calc.c /^static void deinit(void) {$/;" f file: enter calc.c /^static void enter(){$/;" f file: -eval pebblisp.c /^Object eval(Object *obj, struct Environment *env)$/;" f +env calc.c /^static struct Environment env;$/;" v typeref:struct:Environment file: +eval pebblisp.c /^Object eval(const Object *obj, struct Environment *env)$/;" f +evalBuiltIns pebblisp.c /^Object evalBuiltIns(const Object *first, const Object *rest, int *found,$/;" f +evalIfArgs pebblisp.c /^Object evalIfArgs(const Object *first, const Object *rest, $/;" f +fetchFromEnvironment pebblisp.c /^Object fetchFromEnvironment(const char *name, struct Environment *env)$/;" f fixed fixed.h /^typedef int fixed;$/;" t fixed_add fixed.c /^fixed fixed_add(fixed lhs, fixed rhs, bool* overflow)$/;" f fixed_div fixed.c /^fixed fixed_div(fixed lhs, fixed rhs)$/;" f @@ -56,41 +63,39 @@ init calc.c /^static void init(void) {$/;" f file: int_to_fixed fixed.c /^fixed int_to_fixed(int n)$/;" f isDigit tokens.c /^int isDigit(const char c) {$/;" f isSingle tokens.c /^int isSingle(const char c) {$/;" f +isWhitespace tokens.c /^int isWhitespace(const char c) {$/;" f length pebblisp.h /^ int length;$/;" m struct:Slice list pebblisp.h /^ Object *list;$/;" m union:Object::__anon1 main calc.c /^int main(void) {$/;" f main pebblisp.c /^int main(void)$/;" f mytext calc.h /^char mytext[SMAX_LENGTH] = "";$/;" v name pebblisp.h /^ char name[MAX_TOK_LEN];$/;" m union:Object::__anon1 -num1 calc.c /^static char num1[MAX_LENGTH] = ""; \/\/First operand$/;" v file: -num1_is_ans calc.c /^static bool num1_is_ans = false; \/\/Is the previous result in num1? Used to allow further calculations on result.$/;" v file: -num2 calc.c /^static char num2[MAX_LENGTH] = ""; \/\/Second operand$/;" v file: number pebblisp.h /^ int number;$/;" m union:Object::__anon1 obj pebblisp.h /^ Object obj;$/;" m struct:Result objects pebblisp.h /^ Object *objects;$/;" m struct:Environment -operator calc.c /^static uint8_t operator = 0; \/\/Operator, where 0 is +, 1 is -, 2 is *, 3 is \/, 4 is ^$/;" v file: -operator_entered calc.c /^static bool operator_entered = false; \/\/Has the operator been entered yet$/;" v file: parse pebblisp.c /^Result parse(struct Slice *slices)$/;" f parseAtom pebblisp.c /^Object parseAtom(struct Slice *s)$/;" f parseEval pebblisp.c /^Object parseEval(const char *input, struct Environment *env)$/;" f -printf pebblisp.c 7;" d file: +printObj pebblisp.c /^void printObj(const Object *obj)$/;" f +printf pebblisp.c 8;" d file: +printf tokens.c 7;" d file: readSeq pebblisp.c /^Result readSeq(struct Slice *tokens)$/;" f repl pebblisp.c /^int repl(struct Environment *env)$/;" f resultFromObjAndSlices pebblisp.c /^Result resultFromObjAndSlices(Object obj, struct Slice *slices)$/;" f -result_text calc.c /^static char result_text[MAX_LENGTH] = ""; \/\/Results text layer buffer string$/;" v file: +resulttext calc.h /^char resulttext[MAX_LENGTH] = "";$/;" v s_input_text_layer calc.h /^TextLayer *s_input_text_layer;$/;" v +s_result_text_layer calc.h /^TextLayer *s_result_text_layer;$/;" v s_window calc.h /^Window *s_window;$/;" v select_handler calc.c /^static void select_handler(ClickRecognizerRef recognizer, void *context){$/;" f file: selected_token calc.c /^static int8_t selected_token = 1; \/\/Currently selected button, starts on '5'$/;" v file: -singleTokens tokens.c /^static const char singleTokens[] = "()+-*\/";$/;" v file: +singleTokens tokens.c /^static const char singleTokens[] = "()+-*\/=";$/;" v file: slices pebblisp.h /^ struct Slice *slices;$/;" m struct:Result typeref:struct:Result::Slice str_to_fixed fixed.c /^fixed str_to_fixed(const char* str, bool* overflow)$/;" f str_to_int fixed.c /^int str_to_int(const char *str, char **endptr, int maxnum) {$/;" f -stringObj pebblisp.c /^char* stringObj(char *dest, Object *obj)$/;" f +stringObj pebblisp.c /^char* stringObj(char *dest, const Object *obj)$/;" f strings pebblisp.h /^ char **strings;$/;" m struct:Environment temptext calc.h /^char temptext[SMAX_LENGTH] = "";$/;" v text pebblisp.h /^ const char *text;$/;" m struct:Slice -tokenFail pebblisp.h /^static const char* tokenFail = "Missing ')'\\n";$/;" v tokenize tokens.c /^struct Slice *tokenize(const char *input)$/;" f tokens calc.h /^char *tokens[] = {$/;" v type pebblisp.h /^ Type type;$/;" m struct:Object diff --git a/src/tokens.c b/src/tokens.c index 1e69075..8d7d8ff 100644 --- a/src/tokens.c +++ b/src/tokens.c @@ -16,7 +16,7 @@ */ // Is the char a standalone token? -static const char singleTokens[] = "()+-*/"; +static const char singleTokens[] = "()+-*/="; int isSingle(const char c) { int i = 0; while(singleTokens[i] != '\0'){ @@ -31,6 +31,14 @@ int isDigit(const char c) { return c >= '0' && c <= '9'; } +int isWhitespace(const char c) { + return c == ' ' || c == '\t' || c == '\n'; +} + +int notWhitespace(const char c) { + return !isWhitespace(c); +} + struct Slice *tokenize(const char *input) { if(!input) @@ -44,6 +52,12 @@ struct Slice *tokenize(const char *input) int parens = 0; while(input[i] != '\0') { printf("input: '%c'\n", input[i]); + + if(isWhitespace(input[i])) { + i++; + continue; + } + if(input[i] == '(') { parens++; } else if (input[i] == ')') { @@ -56,7 +70,18 @@ struct Slice *tokenize(const char *input) slice++; i++; - } else if(isDigit(input[i])) { + } else { + slices[slice].text = &input[i]; + //int (*check)(const char c) = isDigit(input[i])? + //&isDigit : ¬Whitespace; + int l = 1; + while(!isWhitespace(input[++i]) && !isSingle(input[i])) { + l++; + } + slices[slice].length = l; + slice++; + } + /* } else if(isDigit(input[i])) { slices[slice].text = &input[i]; int l = 1; @@ -66,9 +91,10 @@ struct Slice *tokenize(const char *input) slices[slice].length = l; slice++; - } else { // Whitespace or other uncaught + } else { // Other uncaught i++; } + */ } if(parens){