feat: floating point numbers

This commit is contained in:
2026-07-21 22:16:50 +02:00
parent 62dc0f30eb
commit 6d022219c3
3 changed files with 58 additions and 17 deletions
+14 -12
View File
@@ -38,12 +38,10 @@ static bool node_is_true(Node node) {
}
void build_scopes(Scopes *scopes) {
// clang-format off
scope_add_function(&global_scope, "+", lisp_function_add);
scope_add_function(&global_scope, "-", lisp_function_sub);
scope_add_function(&global_scope, "/", lisp_function_div);
scope_add_function(&global_scope, "*", lisp_function_mul);
scope_add_function(&global_scope, "%", lisp_function_mod);
scope_add_function(&global_scope, "=", lisp_function_eq);
scope_add_function(&global_scope, "<", lisp_function_lt);
scope_add_function(&global_scope, ">", lisp_function_gt);
@@ -55,8 +53,9 @@ void build_scopes(Scopes *scopes) {
scope_add_function(&global_scope, "append", lisp_function_append);
scope_add_function(&global_scope, "length", lisp_function_length);
scope_add_function(&global_scope, "null?", lisp_function_isnull);
scope_add_function(&global_scope, "builtins", lisp_function_builtins);
// clang-format on
scope_add_function(&global_scope, "floor", lisp_function_floor);
scope_add_function(&global_scope, "ceil", lisp_function_ceil);
nob_da_append(scopes, &global_scope);
}
@@ -79,7 +78,7 @@ const char *token_kind_name(TokenKind kind) {
void node_print(Node node) {
switch (node.kind) {
case NODE_KIND_NUMBER: {
printf("%lld", node.as_number);
printf("%f", node.as_number);
} break;
case NODE_KIND_SYMBOL: {
printf(SV_Fmt, SV_Arg(node.as_symbol));
@@ -138,9 +137,11 @@ int tokenize(Nob_String_View content, Tokens *tokens) {
}
Nob_String_Builder sb = {0};
while (isdigit(c)) {
bool condition = isdigit(c);
while (condition) {
nob_sb_appendf(&sb, "%c", c);
c = content.data[++i];
condition = isdigit(c) || c == '.';
}
if (sb.count > 0) {
@@ -153,8 +154,8 @@ int tokenize(Nob_String_View content, Tokens *tokens) {
}
// symbols shouldn't start with a digit
bool condition = isascii(c) && !isdigit(c) && !isblank(c) &&
!iscntrl(c) && c != '(' && c != ')';
condition = isascii(c) && !isdigit(c) && !isblank(c) && !iscntrl(c) &&
c != '(' && c != ')';
while (condition) {
nob_sb_appendf(&sb, "%c", c);
c = content.data[++i];
@@ -221,10 +222,11 @@ int parse_one(Tokens *tokens, Node *node) {
return 1;
}
case TOKEN_KIND_NUMBER: {
long long num = strtoll(tok.data.data, NULL, 10);
// TODO:
// - error checking
// - double support
double num = strtod(tok.data.data, NULL);
if (errno != 0) {
printf("Failed to read number!\n");
return 1;
}
node->kind = NODE_KIND_NUMBER;
node->as_number = num;