aboutsummaryrefslogtreecommitdiffstats
path: root/variable.c
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2018-03-08 12:17:12 -0500
committerClyne Sullivan <tullivan99@gmail.com>2018-03-08 12:17:12 -0500
commit28447df15ac80d7dad1f4889d3d61fe7bfe42a12 (patch)
treedcc38b9a21e9771af0ce3d243660a59bd4992c52 /variable.c
parent40e157ffa2ae741088d2fbea22c9042b85daf108 (diff)
implicit multiply, negatives, solve
Diffstat (limited to 'variable.c')
-rw-r--r--variable.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/variable.c b/variable.c
index c170f79..957a130 100644
--- a/variable.c
+++ b/variable.c
@@ -1,6 +1,7 @@
#include "variable.h"
#include "parser.h"
+#include <ctype.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
@@ -69,3 +70,66 @@ variable *make_vare(variable *v, const char *expr)
return v;
}
+int try_variable(char **name, const char *text)
+{
+ if (name == 0)
+ return 0;
+
+ int neg = 1;
+ int i = 0;
+
+ if (text[0] == '-') {
+ neg = -1;
+ i++;
+ }
+ if (!isalpha(text[i]))
+ return 0;
+
+ for (i++; isalnum(text[i]); i++);
+
+ int o = (neg < 0);
+ if (neg < 0)
+ i--;
+ *name = (char *)malloc(i + 1);
+ strncpy(*name, text + o, i);
+ (*name)[i] = '\0';
+ return (neg > 0) ? i : -(i + 1);
+}
+
+int try_number(variable *v, const char *text)
+{
+ if (v == 0)
+ return 0;
+
+ int decimal = -1;
+ char valid = 0;
+
+ int i = 0;
+ if (text[0] == '-')
+ i++;
+ do {
+ if (text[i] == '.') {
+ if (decimal >= 0) {
+ valid = 0;
+ break;
+ }
+ decimal = i;
+ } else if (isdigit(text[i])) {
+ valid |= 1;
+ } else {
+ break;
+ }
+ } while (text[++i] != '\0');
+
+ if (valid == 0)
+ return 0;
+
+ char *buf = (char *)malloc(i + 1);
+ strncpy(buf, text, i);
+ buf[i] = '\0';
+
+ make_varn(v, strtof(buf, 0));
+
+ free(buf);
+ return i;
+}