diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2018-03-07 15:25:07 -0500 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2018-03-07 15:25:07 -0500 |
commit | 952cb2d6dbd1b4cd76bb7c5dd46c1053631476a3 (patch) | |
tree | c1c1c89355d242489b551bd4390bdd2a68682926 /variable.c | |
parent | 0fb67b8d66ffd47ede0a0a5d09380943722414e9 (diff) |
big changes; no ints; things work better
Diffstat (limited to 'variable.c')
-rw-r--r-- | variable.c | 112 |
1 files changed, 26 insertions, 86 deletions
@@ -4,12 +4,10 @@ #include <stdlib.h> #include <memory.h> #include <string.h> +#include <shelpers.h> extern int atoi(const char *); -extern char *str_undef; -extern char *str_func; - char *fixstring(char *s) { char *n = malloc(strlen(s) + 1); @@ -27,105 +25,47 @@ char *fixstring(char *s) return n; } -variable *vmake(uint8_t fromc, uint8_t valtype, void *value) +variable *make_varn(variable *v, float value) { - variable *v = (variable *)malloc(sizeof(variable)); + if (v == 0) + v = (variable *)malloc(sizeof(variable)); v->used = 0; - v->fromc = fromc; - v->valtype = valtype; - v->value = 0; - v->svalue = 0; - switch (valtype) { - case STRING: - v->svalue = fixstring(value); - free(value); - break; - case INTEGER: - INT(v) = (int32_t)value; - isetstr(v); - break; - case FUNC: - v->value = (uint32_t)value; - v->svalue = str_func; - break; - case EXPR: - v->svalue = value; - break; - } + v->fromc = 0; + v->valtype = NUMBER; + v->value.f = value; return v; } -variable *vmakef(float value) +variable *make_vars(variable *v, const char *value) { - variable *v = (variable *)malloc(sizeof(variable)); + if (v == 0) + v = (variable *)malloc(sizeof(variable)); v->used = 0; v->fromc = 0; - v->valtype = FLOAT; - FLOAT(v) = value; - fsetstr(v); + v->valtype = STRING; + v->value.p = (value != 0) ? (uint32_t)fixstring(value) : 0; return v; } -void fsetstr(variable *f) +variable *make_varf(variable *v, uint8_t fromc, uint32_t func) { - if (f->svalue == 0 || f->svalue == str_undef) - f->svalue = (char *)malloc(32); - snprintf(f->svalue, 32, "%f", FLOAT(f)); -} - -void isetstr(variable *i) -{ - if (i->svalue == 0 || i->svalue == str_undef) - i->svalue = (char *)malloc(32); - snprintf(i->svalue, 32, "%d", (int)INT(i)); -} - -variable *itostring(variable *v) -{ - switch (v->valtype) { - case INTEGER: - v->valtype = STRING; - isetstr(v); - break; - case FLOAT: - v->valtype = STRING; - fsetstr(v); - break; - } - return v; -} - -variable *itoint(variable *v) -{ - switch (v->valtype) { - case STRING: - v->valtype = INTEGER; - INT(v) = atoi(v->svalue); - isetstr(v); - break; - case FLOAT: - v->valtype = INTEGER; - INT(v) = (int32_t)FLOAT(v); - isetstr(v); - break; - } + if (v == 0) + v = (variable *)malloc(sizeof(variable)); + v->used = 0; + v->fromc = fromc; + v->valtype = FUNC; + v->value.p = func; return v; } -variable *itofloat(variable *v) +variable *make_vare(variable *v, const char *expr) { - switch (v->valtype) { - case STRING: - v->valtype = FLOAT; - FLOAT(v) = strtof(v->svalue, 0); - fsetstr(v); - break; - case INTEGER: - v->valtype = FLOAT; - FLOAT(v) = (float)INT(v); - fsetstr(v); - break; - } + if (v == 0) + v = (variable *)malloc(sizeof(variable)); + v->used = 0; + v->fromc = 0; + v->valtype = EXPR; + v->value.p = (uint32_t)strclone(expr); return v; } |