cleanup and implementation of builtin functions
This commit is contained in:
parent
78bb3321d8
commit
06f385d6b0
11 changed files with 621 additions and 302 deletions
154
src/builtin-functions.c
Normal file
154
src/builtin-functions.c
Normal file
|
@ -0,0 +1,154 @@
|
|||
/*
|
||||
* 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 "builtin-functions.h"
|
||||
|
||||
#include "value.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int BuiltinFunction_Equality(CallFrame* top_frame)
|
||||
{
|
||||
Value v1;
|
||||
Value v2;
|
||||
|
||||
CallFrame_StackPop(top_frame, &v1);
|
||||
if (CallFrame_StackPop(top_frame, &v2) != EXIT_SUCCESS) {
|
||||
// TODO: Error message
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
Value result;
|
||||
result.type = VALUETYPE_BOOLEAN;
|
||||
result.get.boolean = Value_Equal(&v1, &v2);
|
||||
|
||||
top_frame->instruction_pointer++;
|
||||
return CallFrame_StackPush(top_frame, &result);
|
||||
|
||||
}
|
||||
|
||||
int BuiltinFunction_Plus(CallFrame* top_frame)
|
||||
{
|
||||
Value v1;
|
||||
Value v2;
|
||||
|
||||
CallFrame_StackPop(top_frame, &v1);
|
||||
if (CallFrame_StackPop(top_frame, &v2) != EXIT_SUCCESS) {
|
||||
// TODO: Error message
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (v1.type != v2.type) {
|
||||
// TODO: Error message
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
Value result;
|
||||
result.type = v1.type;
|
||||
switch (v1.type) {
|
||||
case VALUETYPE_INT64:
|
||||
result.get.i64 = v2.get.i64 + v1.get.i64;
|
||||
break;
|
||||
case VALUETYPE_DOUBLE:
|
||||
result.get.f64 = v2.get.f64 + v1.get.f64;
|
||||
break;
|
||||
case VALUETYPE_BOOLEAN:
|
||||
result.get.boolean = v2.get.boolean + v1.get.boolean;
|
||||
break;
|
||||
}
|
||||
|
||||
top_frame->instruction_pointer++;
|
||||
return CallFrame_StackPush(top_frame, &result);
|
||||
}
|
||||
|
||||
int BuiltinFunction_Minus(CallFrame* top_frame)
|
||||
{
|
||||
Value v1;
|
||||
Value v2;
|
||||
|
||||
CallFrame_StackPop(top_frame, &v1);
|
||||
if (CallFrame_StackPop(top_frame, &v2) != EXIT_SUCCESS) {
|
||||
// TODO: Error message
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (v1.type != v2.type) {
|
||||
// TODO: Error message
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
Value result;
|
||||
result.type = v1.type;
|
||||
switch (v1.type) {
|
||||
case VALUETYPE_INT64:
|
||||
result.get.i64 = v2.get.i64 - v1.get.i64;
|
||||
break;
|
||||
case VALUETYPE_DOUBLE:
|
||||
result.get.f64 = v2.get.f64 - v1.get.f64;
|
||||
break;
|
||||
case VALUETYPE_BOOLEAN:
|
||||
result.get.boolean = v2.get.boolean - v1.get.boolean;
|
||||
break;
|
||||
}
|
||||
|
||||
top_frame->instruction_pointer++;
|
||||
return CallFrame_StackPush(top_frame, &result);
|
||||
}
|
||||
|
||||
|
||||
int BuiltinFunction_PrintLine(CallFrame* top_frame)
|
||||
{
|
||||
Value v1;
|
||||
|
||||
if (CallFrame_StackPop(top_frame, &v1) != EXIT_SUCCESS) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
switch (v1.type) {
|
||||
case VALUETYPE_INT64:
|
||||
printf("%i\n", v1.get.i64);
|
||||
break;
|
||||
case VALUETYPE_DOUBLE:
|
||||
printf("%f\n", v1.get.f64);
|
||||
break;
|
||||
case VALUETYPE_BOOLEAN:
|
||||
if (v1.get.boolean) {
|
||||
printf("true\n");
|
||||
} else {
|
||||
printf("false\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int BuiltinFunction_Duplicate(CallFrame* top_frame)
|
||||
{
|
||||
Value v1;
|
||||
|
||||
if (CallFrame_StackPop(top_frame, &v1) != EXIT_SUCCESS) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (CallFrame_StackPush(top_frame, &v1) || CallFrame_StackPush(top_frame, &v1)) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue