summaryrefslogtreecommitdiff
path: root/src/eval.c
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/eval.c
parente5dcf23d1cc67c15dbc75ba1659483c3ab2ef6c0 (diff)
floats fully implemented
Diffstat (limited to 'src/eval.c')
-rw-r--r--src/eval.c44
1 files changed, 39 insertions, 5 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);