From 28447df15ac80d7dad1f4889d3d61fe7bfe42a12 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 8 Mar 2018 12:17:12 -0500 Subject: implicit multiply, negatives, solve --- variable.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'variable.c') 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 #include #include #include @@ -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; +} -- cgit v1.2.3