Soft'N'Design Software
/
projects
/
calc.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (from parent 1:
85b4a72
)
all number supported
author
Laurent Mazet
<mazet@softndesign.org>
Tue, 27 Dec 2022 22:33:39 +0000
(23:33 +0100)
committer
Laurent Mazet
<mazet@softndesign.org>
Tue, 27 Dec 2022 22:33:39 +0000
(23:33 +0100)
parser.c
patch
|
blob
|
blame
|
history
diff --git
a/parser.c
b/parser.c
index 2ed8d9d845dd4f6749b021f582703fe9a92a168c..8ddd83ad62e14a80b1f152d00bb11ff7234c723f 100644
(file)
--- a/
parser.c
+++ b/
parser.c
@@
-13,10
+13,16
@@
int codecmp (char *ref, char *str)
int sig;
while (*ref != '\0') {
int sig;
while (*ref != '\0') {
- sig = *str++ - *ref++;
+ if (*ref == '\t') {
+ sig = (*str == '.') ? -1 : ((*str >= '0') && (*str <= '9'));
+ } else {
+ sig = *str - *ref;
+ }
if (sig != 0) {
return (sig > 0) ? 1 : -1;
}
if (sig != 0) {
return (sig > 0) ? 1 : -1;
}
+ str++;
+ ref++;
}
return 0;
}
return 0;
@@
-39,12
+45,10
@@
element_t *newelement (func_t function, int nbops)
/* functions */
/* functions */
-#define NB_OPERATORS
7
+#define NB_OPERATORS
5
keyword_t operators[NB_OPERATORS] = {
keyword_t operators[NB_OPERATORS] = {
- { "+ ", Add, 2, 1 },
- { "+\t", Add, 2, 1 },
- { "- ", Sub, 2, 1 },
+ { "+\t", Add, 2, 1 },
{ "-\t", Sub, 2, 1 },
{ "*", Mul, 2, 1 },
{ "/", Div, 2, 1 },
{ "-\t", Sub, 2, 1 },
{ "*", Mul, 2, 1 },
{ "/", Div, 2, 1 },
@@
-189,23
+193,10
@@
element_t *parser (char *str, char **next)
continue;
}
continue;
}
- /* last attend to detect addition and substraction */
-
- if (((*str == '-') || (*str == '+')) &&
- ((*(str + 1) >= '0') && (*(str + 1) <= '9')) &&
- ((root) && (root->func == Val))) {
- VERBOSE (INFO, PRINTOUT ("Oper: %d\n", Add));
- new = newelement (Add, 2);
- if (new == NULL) {
- return ERROR_OP;
- }
- new->ops[0] = root;
- root = new;
- }
-
/* look for number */
/* look for number */
- if (((*str >= '0') && (*str <= '9')) || (*str == '.')) {
+ if (((*str >= '0') && (*str <= '9')) ||
+ (*str == '.') || (*str == '+') || (*str == '-')) {
VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
char *pt;
float value = strtof (str, &pt);
VERBOSE (DEBUG, PRINTOUT ("start processing value\n"));
char *pt;
float value = strtof (str, &pt);
@@
-218,25
+209,20
@@
element_t *parser (char *str, char **next)
new->value = value;
if (root == NULL) {
root = new;
new->value = value;
if (root == NULL) {
root = new;
- } else {
- if (
root->func == Val
) {
- element_t *
set = newelement (Set, MAX_OPERANDS
);
- if (
set
== NULL) {
+ } else
if (root->func == Val)
{
+ if (
(*str == '+') || (*str == '-')
) {
+ element_t *
add = newelement (Add, 2
);
+ if (
add
== NULL) {
return ERROR_OP;
}
return ERROR_OP;
}
- set->ops[0] = root;
- root = set;
- }
- for (i = 0; i < root->nbops; i++) {
- if (root->ops[i] == NULL) {
- root->ops[i] = new;
- found = 1;
- break;
- }
- }
- if (!found) {
+ add->ops[0] = root;
+ add->ops[1] = new;
+ root = add;
+ } else {
return ERROR_OP;
}
return ERROR_OP;
}
+ } else {
+ return ERROR_OP;
}
str = pt;
found = 1;
}
str = pt;
found = 1;