feat: floating point numbers
This commit is contained in:
+40
-3
@@ -1,5 +1,7 @@
|
||||
#include "lisp.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#define func_arithmetic_ex(name, operator, operator_name) \
|
||||
int name(Nodes args, Scopes *scopes, Node *result) { \
|
||||
UNUSED(scopes); \
|
||||
@@ -14,8 +16,8 @@
|
||||
"else!\n"); \
|
||||
return 1; \
|
||||
} \
|
||||
long long a = args.items[0].as_number; \
|
||||
long long b = args.items[1].as_number; \
|
||||
double a = args.items[0].as_number; \
|
||||
double b = args.items[1].as_number; \
|
||||
result->as_number = a operator b; \
|
||||
result->kind = NODE_KIND_NUMBER; \
|
||||
return 0; \
|
||||
@@ -28,7 +30,6 @@ func_arithmetic(lisp_function_add, +);
|
||||
func_arithmetic(lisp_function_sub, -);
|
||||
func_arithmetic(lisp_function_div, /);
|
||||
func_arithmetic(lisp_function_mul, *);
|
||||
func_arithmetic(lisp_function_mod, %);
|
||||
func_arithmetic_ex(lisp_function_eq, ==, "=");
|
||||
func_arithmetic(lisp_function_lt, <);
|
||||
func_arithmetic(lisp_function_gt, >);
|
||||
@@ -151,3 +152,39 @@ int lisp_function_builtins(Nodes args, Scopes *scopes, Node *result) {
|
||||
result->kind = NODE_KIND_LIST;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lisp_function_floor(Nodes args, Scopes *scopes, Node *result) {
|
||||
UNUSED(scopes);
|
||||
|
||||
if (args.count != 1) {
|
||||
printf("'floor' expects one argument!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (args.items[0].kind != NODE_KIND_NUMBER) {
|
||||
printf("'floor' can only be used on numbers!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
result->kind = NODE_KIND_NUMBER;
|
||||
result->as_number = floor(args.items[0].as_number);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lisp_function_ceil(Nodes args, Scopes *scopes, Node *result) {
|
||||
UNUSED(scopes);
|
||||
|
||||
if (args.count != 1) {
|
||||
printf("'ceil' expects one argument!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (args.items[0].kind != NODE_KIND_NUMBER) {
|
||||
printf("'ceil' can only be used on numbers!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
result->kind = NODE_KIND_NUMBER;
|
||||
result->as_number = ceil(args.items[0].as_number);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user