From cd36f96dfc03e3425d2d690254a8ce1b9178a751 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Tue, 28 Feb 2023 13:19:25 -0800 Subject: floats fully implemented --- src/eval.c | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) (limited to 'src/eval.c') 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); -- cgit v1.3.1