From 49223129746bcb9981bba5b359af3f17731f6235 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sun, 25 Dec 2022 23:13:36 +0100 Subject: [PATCH] second preliminary version --- calc.c | 4 ++-- parser.c | 72 +++++++++++++++++++++++++++++++++++--------------------- 2 files changed, 47 insertions(+), 29 deletions(-) diff --git a/calc.c b/calc.c index 1c4099f..d2fdc8c 100644 --- a/calc.c +++ b/calc.c @@ -87,7 +87,7 @@ int main (int argc, char *argv[]) /* read from input stream */ while ((n = read (stdfdin, pt, BUFFER_SIZE - (pt - buffer))) != 0) { - VERBOSE (DEBUG, PRINTOUT ("read %d bytes\n", n)); + VERBOSE (INFO, PRINTOUT ("read %d bytes\n", n)); n += (pt - buffer); if ((n == 2) && (buffer[0] == '.')) { return 0; @@ -97,7 +97,7 @@ int main (int argc, char *argv[]) for (i = 0, j = 0; i < n; i++) { if (buffer[i] == '\n') { buffer[i] = 0; - VERBOSE (DEBUG, PRINTOUT ("line(%d): %s\n", j, buffer + j)); + VERBOSE (INFO, PRINTOUT ("line(%d): %s\n", j, buffer + j)); element_t *element = parser (buffer, NULL); if (element == (void *)(-1)) { VERBOSE (WARNING, PRINTOUT ("error while parsing: %s\n", buffer)); diff --git a/parser.c b/parser.c index 2190178..48fb980 100644 --- a/parser.c +++ b/parser.c @@ -45,7 +45,7 @@ keyword_t operators[NB_OPERATORS] = { { "+ ", Add, 2, 1 }, { "+\t", Add, 2, 1 }, { "- ", Sub, 2, 1 }, - { "- ", Sub, 2, 1 }, + { "-\t", Sub, 2, 1 }, { "*", Mul, 2, 1 }, { "/", Div, 2, 1 }, { "^", Pow, 2, 1 } @@ -69,11 +69,13 @@ element_t *parser (char *str, char **next) element_t *root = NULL; int i, j; + VERBOSE (DEBUG, PRINTOUT ("Starting parsing\n")); + /* main loop */ while (*str != '\0') { int found = 0; element_t *new = NULL; - VERBOSE (DEBUG, PRINTOUT ("Processing: %s\n", str)); + VERBOSE (INFO, PRINTOUT ("Processing: %s\n", str)); /* skip spaces and tabs */ @@ -98,16 +100,11 @@ element_t *parser (char *str, char **next) /* check for parent */ - if (*str == ')') { - if (next != NULL) { - *next = str + 1; - } - return root; - } if (*str == '(') { + VERBOSE (DEBUG, PRINTOUT ("start processing bracket\n")); new = parser (str + 1, &str); - if (new == (void *)(-1)) { - return new; + if (new == ERROR_OP) { + return ERROR_OP; } if (root) { for (i = 0, j = 0; i < root->nbops; i++) { @@ -128,9 +125,18 @@ element_t *parser (char *str, char **next) if (!found) { return ERROR_OP; } - } else { - return root; } + VERBOSE (DEBUG, PRINTOUT ("stop processing bracket\n")); + if (next != NULL) { + *next = str; + } + return root; + } + if (*str == ')') { + if (next != NULL) { + *next = str + 1; + } + return root; } /* look for operators */ @@ -138,34 +144,41 @@ element_t *parser (char *str, char **next) for (i = 0; i < NB_OPERATORS; i++) { keyword_t *operator = operators + i; if (codecmp (operator->keyword, str) == 0) { - if ((root) && (root->func == Val)) { - VERBOSE (DEBUG, PRINTOUT ("Oper: %d\n", operator->func)); + VERBOSE (DEBUG, PRINTOUT ("start processing operator\n")); + str += operator->offset; + if ((root) && (root->func != Set)) { + VERBOSE (INFO, PRINTOUT ("Oper: %d\n", operator->func)); new = newelement (operator->func, operator->nbops); if (new == NULL) { return ERROR_OP; } new->ops[0] = root; root = new; - } else { + new = parser (str, &str); + if (new == ERROR_OP) { + return ERROR_OP; + } + root->ops[1] = new; + } else { return ERROR_OP; } - str += operator->offset; found = 1; + VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n")); break; } } if (found) { - VERBOSE (DEBUG, PRINTOUT ("stop processing operator\n")); continue; } - + /* look for functions */ for (i = 0; i < NB_FUNCTIONS; i++) { keyword_t *function = functions + i; if (codecmp (function->keyword, str) == 0) { + VERBOSE (DEBUG, PRINTOUT ("start processing function\n")); if (root == NULL) { - VERBOSE (DEBUG, PRINTOUT ("Func: %d\n", function->func)); + VERBOSE (INFO, PRINTOUT ("Func: %d\n", function->func)); new = newelement (function->func, function->nbops); if (new == NULL) { return ERROR_OP; @@ -176,11 +189,11 @@ element_t *parser (char *str, char **next) } str += function->offset; found = 1; + VERBOSE (DEBUG, PRINTOUT ("stop processing function\n")); break; } } if (found) { - VERBOSE (DEBUG, PRINTOUT ("stop processing function\n")); continue; } @@ -189,7 +202,7 @@ element_t *parser (char *str, char **next) if (((*str == '-') || (*str == '+')) && ((*(str + 1) >= '0') && (*(str + 1) <= '9')) && ((root) && (root->func == Val))) { - VERBOSE (DEBUG, PRINTOUT ("Oper: %d\n", Add)); + VERBOSE (INFO, PRINTOUT ("Oper: %d\n", Add)); new = newelement (Add, 2); if (new == NULL) { return ERROR_OP; @@ -200,17 +213,17 @@ element_t *parser (char *str, char **next) /* look for number */ - if (((*str >= '0') && (*str <= '9')) || - (*str == '.') || (*str == '-') ||(*str == '+')) { + if (((*str >= '0') && (*str <= '9')) || (*str == '.')) { + VERBOSE (DEBUG, PRINTOUT ("start processing value\n")); char *pt; float value = strtof (str, &pt); - VERBOSE (DEBUG, PRINTOUT ("Value: %f\n", value)); + VERBOSE (INFO, PRINTOUT ("Value: %f\n", value)); if (str != pt) { new = newelement (Val, 1); - new->value = value; if (new == NULL) { return ERROR_OP; } + new->value = value; if (root == NULL) { root = new; } else { @@ -236,6 +249,7 @@ element_t *parser (char *str, char **next) str = pt; found = 1; } + VERBOSE (DEBUG, PRINTOUT ("stop processing value\n")); } /* error */ @@ -246,6 +260,9 @@ element_t *parser (char *str, char **next) } + if (next != NULL) { + *next = str; + } return root; } @@ -256,9 +273,10 @@ void print_element (element_t *root, int level) char *func = NULL; int i; - if (root == NULL) + if ((root == NULL) || (root == ERROR_OP)) { return; - + } + for (i = 0; i < level; i++) { PRINTOUT (" "); } -- 2.30.2