diff --git a/src/.pebblisp.c.swp b/src/.pebblisp.c.swp index 7ab91f5..c9633e6 100644 Binary files a/src/.pebblisp.c.swp and b/src/.pebblisp.c.swp differ 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){