1
0
Fork 0
forked from fun/fun

More math opcode Fun. (0.37.43)

This commit is contained in:
Johannes Findeisen 2026-01-03 03:35:16 +01:00
commit c062b685e1
18 changed files with 501 additions and 2 deletions

View file

@ -193,6 +193,13 @@ static const char *opcode_name(OpCode op) {
case OP_CEIL: return "CEIL";
case OP_TRUNC: return "TRUNC";
case OP_ROUND: return "ROUND";
case OP_SIN: return "SIN";
case OP_COS: return "COS";
case OP_TAN: return "TAN";
case OP_EXP: return "EXP";
case OP_LOG: return "LOG";
case OP_LOG10: return "LOG10";
case OP_SQRT: return "SQRT";
default: return "???";
}
}

View file

@ -242,7 +242,16 @@ typedef enum {
OP_FLOOR, // pops x (int/float); pushes floor(x) (int if integral else float)
OP_CEIL, // pops x (int/float); pushes ceil(x) (int if integral else float)
OP_TRUNC, // pops x (int/float); pushes trunc(x) (int if integral else float)
OP_ROUND // pops x (int/float); pushes round(x) (half away from zero)
OP_ROUND, // pops x (int/float); pushes round(x) (half away from zero)
// C99 math.h transcendentals (float-aware)
OP_SIN, // pops x (int/float); pushes sin(x)
OP_COS, // pops x (int/float); pushes cos(x)
OP_TAN, // pops x (int/float); pushes tan(x)
OP_EXP, // pops x (int/float); pushes exp(x)
OP_LOG, // pops x (int/float); pushes natural log ln(x)
OP_LOG10, // pops x (int/float); pushes log10(x)
OP_SQRT // pops x (int/float); pushes sqrt(x)
} OpCode;
typedef struct {

View file

@ -39,6 +39,9 @@ int main(void) {
int cfn3_2 = bytecode_add_constant(bc, make_float(-3.2));
int cfn3_5 = bytecode_add_constant(bc, make_float(-3.5));
int cfn3_8 = bytecode_add_constant(bc, make_float(-3.8));
int cf0 = bytecode_add_constant(bc, make_float(0.0));
int cf1 = bytecode_add_constant(bc, make_float(1.0));
int c9 = bytecode_add_constant(bc, make_int(9));
// ---------- Arithmetic ----------
bytecode_add_instruction(bc, OP_LOAD_CONST, c42);
@ -168,6 +171,36 @@ int main(void) {
bytecode_add_instruction(bc, OP_FLOOR, 0);
bytecode_add_instruction(bc, OP_PRINT, 0);
// ---------- Transcendentals demo ----------
// sin(0)=0
bytecode_add_instruction(bc, OP_LOAD_CONST, cf0);
bytecode_add_instruction(bc, OP_SIN, 0);
bytecode_add_instruction(bc, OP_PRINT, 0);
// cos(0)=1
bytecode_add_instruction(bc, OP_LOAD_CONST, cf0);
bytecode_add_instruction(bc, OP_COS, 0);
bytecode_add_instruction(bc, OP_PRINT, 0);
// tan(0)=0
bytecode_add_instruction(bc, OP_LOAD_CONST, cf0);
bytecode_add_instruction(bc, OP_TAN, 0);
bytecode_add_instruction(bc, OP_PRINT, 0);
// exp(0)=1
bytecode_add_instruction(bc, OP_LOAD_CONST, cf0);
bytecode_add_instruction(bc, OP_EXP, 0);
bytecode_add_instruction(bc, OP_PRINT, 0);
// log(1)=0
bytecode_add_instruction(bc, OP_LOAD_CONST, cf1);
bytecode_add_instruction(bc, OP_LOG, 0);
bytecode_add_instruction(bc, OP_PRINT, 0);
// log10(1)=0
bytecode_add_instruction(bc, OP_LOAD_CONST, cf1);
bytecode_add_instruction(bc, OP_LOG10, 0);
bytecode_add_instruction(bc, OP_PRINT, 0);
// sqrt(9)=3
bytecode_add_instruction(bc, OP_LOAD_CONST, c9);
bytecode_add_instruction(bc, OP_SQRT, 0);
bytecode_add_instruction(bc, OP_PRINT, 0);
// ---------- HALT ----------
bytecode_add_instruction(bc, OP_HALT, 0);

View file

@ -1786,6 +1786,55 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
free(name);
return 1;
}
if (strcmp(name, "sin") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos) || !consume_char(src, len, pos, ')')) { parser_fail(*pos, "sin expects 1 arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_SIN, 0);
free(name);
return 1;
}
if (strcmp(name, "cos") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos) || !consume_char(src, len, pos, ')')) { parser_fail(*pos, "cos expects 1 arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_COS, 0);
free(name);
return 1;
}
if (strcmp(name, "tan") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos) || !consume_char(src, len, pos, ')')) { parser_fail(*pos, "tan expects 1 arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_TAN, 0);
free(name);
return 1;
}
if (strcmp(name, "exp") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos) || !consume_char(src, len, pos, ')')) { parser_fail(*pos, "exp expects 1 arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_EXP, 0);
free(name);
return 1;
}
if (strcmp(name, "log") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos) || !consume_char(src, len, pos, ')')) { parser_fail(*pos, "log expects 1 arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_LOG, 0);
free(name);
return 1;
}
if (strcmp(name, "log10") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos) || !consume_char(src, len, pos, ')')) { parser_fail(*pos, "log10 expects 1 arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_LOG10, 0);
free(name);
return 1;
}
if (strcmp(name, "sqrt") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos) || !consume_char(src, len, pos, ')')) { parser_fail(*pos, "sqrt expects 1 arg"); free(name); return 0; }
bytecode_add_instruction(bc, OP_SQRT, 0);
free(name);
return 1;
}
if (strcmp(name, "pow") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos) || !consume_char(src, len, pos, ',')) { parser_fail(*pos, "pow expects 2 args"); free(name); return 0; }

View file

@ -678,6 +678,13 @@ void vm_run(VM *vm, Bytecode *entry) {
#include "vm/math/ceil.c"
#include "vm/math/trunc.c"
#include "vm/math/round.c"
#include "vm/math/sin.c"
#include "vm/math/cos.c"
#include "vm/math/tan.c"
#include "vm/math/exp.c"
#include "vm/math/log.c"
#include "vm/math/log10.c"
#include "vm/math/sqrt.c"
#include "vm/math/random_int.c"
#include "vm/math/random_seed.c"

31
src/vm/math/cos.c Normal file
View file

@ -0,0 +1,31 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-01-03
*/
/**
* @file cos.c
* @brief Implements the OP_COS opcode using C99 math.h cos().
*/
#include <math.h>
case OP_COS: {
Value v = pop_value(vm);
if (v.type == VAL_INT || v.type == VAL_FLOAT) {
double x = (v.type == VAL_FLOAT) ? v.d : (double)v.i;
double r = cos(x);
push_value(vm, make_float(r));
free_value(v);
} else {
fprintf(stderr, "Runtime type error: COS expects number, got %s\n", value_type_name(v.type));
exit(1);
}
break;
}

31
src/vm/math/exp.c Normal file
View file

@ -0,0 +1,31 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-01-03
*/
/**
* @file exp.c
* @brief Implements the OP_EXP opcode using C99 math.h exp().
*/
#include <math.h>
case OP_EXP: {
Value v = pop_value(vm);
if (v.type == VAL_INT || v.type == VAL_FLOAT) {
double x = (v.type == VAL_FLOAT) ? v.d : (double)v.i;
double r = exp(x);
push_value(vm, make_float(r));
free_value(v);
} else {
fprintf(stderr, "Runtime type error: EXP expects number, got %s\n", value_type_name(v.type));
exit(1);
}
break;
}

36
src/vm/math/log.c Normal file
View file

@ -0,0 +1,36 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-01-03
*/
/**
* @file log.c
* @brief Implements the OP_LOG opcode using C99 math.h log() (natural logarithm).
*/
#include <math.h>
case OP_LOG: {
Value v = pop_value(vm);
if (v.type == VAL_INT || v.type == VAL_FLOAT) {
double x = (v.type == VAL_FLOAT) ? v.d : (double)v.i;
if (x <= 0.0) {
/* Domain error: return NaN to signal invalid input */
push_value(vm, make_float(NAN));
} else {
double r = log(x);
push_value(vm, make_float(r));
}
free_value(v);
} else {
fprintf(stderr, "Runtime type error: LOG expects number, got %s\n", value_type_name(v.type));
exit(1);
}
break;
}

35
src/vm/math/log10.c Normal file
View file

@ -0,0 +1,35 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-01-03
*/
/**
* @file log10.c
* @brief Implements the OP_LOG10 opcode using C99 math.h log10().
*/
#include <math.h>
case OP_LOG10: {
Value v = pop_value(vm);
if (v.type == VAL_INT || v.type == VAL_FLOAT) {
double x = (v.type == VAL_FLOAT) ? v.d : (double)v.i;
if (x <= 0.0) {
push_value(vm, make_float(NAN));
} else {
double r = log10(x);
push_value(vm, make_float(r));
}
free_value(v);
} else {
fprintf(stderr, "Runtime type error: LOG10 expects number, got %s\n", value_type_name(v.type));
exit(1);
}
break;
}

31
src/vm/math/sin.c Normal file
View file

@ -0,0 +1,31 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-01-03
*/
/**
* @file sin.c
* @brief Implements the OP_SIN opcode using C99 math.h sin().
*/
#include <math.h>
case OP_SIN: {
Value v = pop_value(vm);
if (v.type == VAL_INT || v.type == VAL_FLOAT) {
double x = (v.type == VAL_FLOAT) ? v.d : (double)v.i;
double r = sin(x);
push_value(vm, make_float(r));
free_value(v);
} else {
fprintf(stderr, "Runtime type error: SIN expects number, got %s\n", value_type_name(v.type));
exit(1);
}
break;
}

45
src/vm/math/sqrt.c Normal file
View file

@ -0,0 +1,45 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-01-03
*/
/**
* @file sqrt.c
* @brief Implements the OP_SQRT opcode using C99 math.h sqrt().
*/
#include <math.h>
case OP_SQRT: {
Value v = pop_value(vm);
if (v.type == VAL_INT || v.type == VAL_FLOAT) {
double x = (v.type == VAL_FLOAT) ? v.d : (double)v.i;
if (x < 0.0) {
push_value(vm, make_float(NAN));
} else {
double r = sqrt(x);
/* preserve int if exactly integral */
if (r >= (double)INT64_MIN && r <= (double)INT64_MAX) {
int64_t ii = (int64_t)r;
if ((double)ii == r) {
push_value(vm, make_int(ii));
} else {
push_value(vm, make_float(r));
}
} else {
push_value(vm, make_float(r));
}
}
free_value(v);
} else {
fprintf(stderr, "Runtime type error: SQRT expects number, got %s\n", value_type_name(v.type));
exit(1);
}
break;
}

31
src/vm/math/tan.c Normal file
View file

@ -0,0 +1,31 @@
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2026-01-03
*/
/**
* @file tan.c
* @brief Implements the OP_TAN opcode using C99 math.h tan().
*/
#include <math.h>
case OP_TAN: {
Value v = pop_value(vm);
if (v.type == VAL_INT || v.type == VAL_FLOAT) {
double x = (v.type == VAL_FLOAT) ? v.d : (double)v.i;
double r = tan(x);
push_value(vm, make_float(r));
free_value(v);
} else {
fprintf(stderr, "Runtime type error: TAN expects number, got %s\n", value_type_name(v.type));
exit(1);
}
break;
}