cleanup and implementation of builtin functions

This commit is contained in:
Alexander Acker 2024-10-08 17:17:10 +02:00
parent 78bb3321d8
commit 06f385d6b0
11 changed files with 621 additions and 302 deletions

72
src/value.c Normal file
View file

@ -0,0 +1,72 @@
/*
* This code is part of the programming language flup
* flup comes with ABSOLUTELY NO WARRANTY and is licensed under AGPL-3.0 or later.
* Copyright (C) 2024 VegOwOtenks
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "value.h"
bool Value_IsTruthy(Value* v)
{
switch (v->type) {
case VALUETYPE_INT64:
return v->get.i64 != 0;
case VALUETYPE_DOUBLE:
return v->get.f64 != 0.0;
case VALUETYPE_BOOLEAN:
return v->get.boolean;
}
return false;
}
bool Value_Equal(Value* v1, Value* v2)
{
if (v1->type != v2->type) return false;
switch (v1->type) {
case VALUETYPE_INT64:
return v1->get.i64 == v2->get.i64;
case VALUETYPE_DOUBLE:
return v1->get.f64 == v2->get.f64;
case VALUETYPE_BOOLEAN:
return v1->get.boolean = v2->get.boolean;
}
return false;
}
bool Value_TypeMatchesStringName(enum ValueType type, StringView type_name)
{
if (StringView_Equal(type_name, StringView_FromString("any"))) {
return true;
}
const enum ValueType type_mapping[] = {VALUETYPE_INT64, VALUETYPE_DOUBLE, VALUETYPE_BOOLEAN, VALUETYPE_BOOLEAN};
const char* name_mapping[] = {"int", "double", "bool", "boolean"};
for (size_t i = 0; i < sizeof(name_mapping) / sizeof(name_mapping[0]); i++) {
if (1 == 1
&& type == type_mapping[i]
&& StringView_Equal(type_name, StringView_FromString(name_mapping[i]))
) {
return true;
}
}
return false;
}