summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2023-02-28 13:19:25 -0800
committerKai Stevenson <kai@kaistevenson.com>2023-02-28 13:19:25 -0800
commitcd36f96dfc03e3425d2d690254a8ce1b9178a751 (patch)
treebef7c6fde6c433cc2daf7a672fadee90247589f1 /src
parente5dcf23d1cc67c15dbc75ba1659483c3ab2ef6c0 (diff)
floats fully implemented
Diffstat (limited to 'src')
-rw-r--r--src/eval.c44
-rw-r--r--src/parse.c21
2 files changed, 56 insertions, 9 deletions
diff --git a/src/eval.c b/src/eval.c
index 0b28ef4..8adcf8c 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -198,23 +198,56 @@ sym_t* try_eval_internal(sym_t* symbol, hashtable_t* symbolScope) {
}
else if (hash == get_hash("sum")) {
int sum = 0;
+ float fSum = 0;
+ char isFloat = 0;
for (int i = 0; i < symbol->argCount; i++) {
sym_t* v = eval(symbol->args[i], symbolScope);
- sum += symtoi(v);
+ if (v->litType == INT) {
+ sum += symtoi(v);
+ }
+ else if (v->litType == FLOAT) {
+ fSum += symtof(v);
+ isFloat = 1;
+ }
destroy_sym(v);
}
- return init_sym_int(sum);
+ if (!isFloat) {
+ return init_sym_int(sum);
+ }
+ else {
+ return init_sym_float(sum + fSum);
+ }
}
else if (hash == get_hash("sub")) {
sym_t* v1 = eval(symbol->args[0], symbolScope);
- int sum = symtoi(v1);
+ int sum = 0;
+ float fSum = 0;
+ char isFloat = 0;
+ if (v1->litType == INT) {
+ sum = symtoi(v1);
+ }
+ else if (v1->litType == FLOAT) {
+ fSum = symtof(v1);
+ isFloat = 1;
+ }
destroy_sym(v1);
for (int i = 1; i < symbol->argCount; i++) {
sym_t* v = eval(symbol->args[i], symbolScope);
- sum -= symtoi(v);
+ if (v->litType == INT) {
+ sum -= symtoi(v);
+ }
+ else if (v->litType == FLOAT) {
+ fSum -= symtof(v);
+ isFloat = 1;
+ }
destroy_sym(v);
}
- return init_sym_int(sum);
+ if (!isFloat) {
+ return init_sym_int(sum);
+ }
+ else {
+ return init_sym_float(sum + fSum);
+ }
}
else if (hash == get_hash("print")) {
sym_t* v = eval(symbol->args[0], symbolScope);
@@ -222,6 +255,7 @@ sym_t* try_eval_internal(sym_t* symbol, hashtable_t* symbolScope) {
printf("%d\n", symtoi(v));
}
else if (v->litType == FLOAT) {
+ printf("%.5f\n", symtof(v));
}
else if (v->litType == STRING) {
char* s = symtos(v);
diff --git a/src/parse.c b/src/parse.c
index 18425ac..d1b0f2e 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -24,7 +24,7 @@ sym_t* init_sym_int(int value) {
return init_sym(LITERAL, INT, vp);
}
sym_t* init_sym_float(float value) {
- int* vp = malloc(sizeof(float));
+ float* vp = malloc(sizeof(float));
*vp = value;
return init_sym(LITERAL, FLOAT, vp);
}
@@ -93,20 +93,23 @@ symset_t* parse_symbols(char** str) {
return symbolSet;
}
char* symbolName = calloc(30, sizeof(char));
+ char isFloat = 0;
int i = 0;
while (1) {
symbolName[i] = **str;
- if (**str == ',' || **str == ';') {
+ if (**str == '.') {
+ isFloat = 1;
+ }
+ else if (**str == ',' || **str == ';') {
(*str)++;
symbolName[i] = '\0';
sym_t* symbol;
- /* TODO: float */
/* string */
if (isalpha(symbolName[0])) {
symbol = init_sym_str(symbolName);
}
/* int */
- else {
+ else if (!isFloat) {
int v;
if (symbolName[0] == '-') {
v = atoi(symbolName + 1) * -1;
@@ -116,6 +119,16 @@ symset_t* parse_symbols(char** str) {
}
symbol = init_sym_int(v);
}
+ else {
+ float v;
+ if (symbolName[0] == '-') {
+ v = atof(symbolName + 1) * -1;
+ }
+ else {
+ v = atof(symbolName);
+ }
+ symbol = init_sym_float(v);
+ }
symset_add_symbol(symbolSet, symbol);
break;
}