1
0
Fork 0
forked from fun/fun

What is CPP? Holy shit... (0.38.4)

This commit is contained in:
Johannes Findeisen 2026-01-29 05:09:15 +01:00
commit d79815e366
10 changed files with 158 additions and 4 deletions

View file

@ -270,6 +270,9 @@ typedef enum {
OP_RUST_GET_SP, // pushes current VM stack pointer (via Rust reading VM memory)
OP_RUST_SET_EXIT, // pops int and sets VM exit_code (via Rust writing VM memory)
// C++ demo opcode(s)
OP_CPP_ADD, // pops b, a; pushes (a + b)
/* Notcurses TUI (optional) */
OP_NC_INIT, // initializes Notcurses; returns 1 on success, 0 on failure
OP_NC_SHUTDOWN, // shuts down Notcurses; returns 0

View file

@ -824,6 +824,16 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
free(name);
return 1;
}
if (strcmp(name, "cpp_add") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "cpp_add expects (a:int, b:int)"); free(name); return 0; }
if (!consume_char(src, len, pos, ',')) { parser_fail(*pos, "cpp_add expects two arguments"); free(name); return 0; }
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "cpp_add expects (a:int, b:int)"); free(name); return 0; }
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after cpp_add args"); free(name); return 0; }
bytecode_add_instruction(bc, OP_CPP_ADD, 0);
free(name);
return 1;
}
if (strcmp(name, "os_list_dir") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "os_list_dir expects (path)"); free(name); return 0; }

View file

@ -897,6 +897,22 @@ void vm_run(VM *vm, Bytecode *entry) {
#include "vm/sqlite/query.c"
#endif
/* C++ demo opcodes (guarded) */
#if defined(FUN_WITH_CPP)
case OP_CPP_ADD: {
int rc = fun_op_cpp_add(vm);
if (rc != 0) {
vm_raise_error(vm, "cpp_add failed");
}
break;
}
#else
case OP_CPP_ADD: {
vm_raise_error(vm, "CPP support is not enabled (build with -DFUN_WITH_CPP=ON)");
break;
}
#endif
/* libsql ops (independent) */
#ifdef FUN_WITH_LIBSQL
#include "vm/libsql/open.c"

View file

@ -58,6 +58,8 @@ static const char *opcode_names[] = {
"FMIN","FMAX",
/* Rust FFI demo */
"RUST_HELLO","RUST_HELLO_ARGS","RUST_HELLO_ARGS_RETURN","RUST_GET_SP","RUST_SET_EXIT",
/* C++ demo */
"CPP_ADD",
/* Notcurses TUI (optional) */
"NC_INIT","NC_SHUTDOWN","NC_CLEAR","NC_DRAW_TEXT","NC_GETCH"
};
@ -165,6 +167,9 @@ char *fun_rust_echo_string(const char *input);
/* Free a C string previously returned by fun_rust_echo_string. */
void fun_rust_string_free(char *ptr);
/* C++ demo opcode entry point (C ABI) */
int fun_op_cpp_add(struct VM *vm);
/* --- Extended C ABI for Rust to access VM internals (unsafe) --- */
/* Size helpers for Rust side to compute offsets and do pointer math */
size_t vm_sizeof(void);

28
src/vm/cpp/add.cpp Normal file
View file

@ -0,0 +1,28 @@
/*
* 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-29
*/
/*
* Minimal C++ opcode for Fun VM cpp_add
* Build is gated by -DFUN_WITH_CPP=ON (see CMake).
*/
#include <cstdint>
extern "C" {
#include "vm.h" // C header; provides VM, vm_pop_i64, vm_push_i64
}
extern "C" int fun_op_cpp_add(VM *vm) {
int64_t b = vm_pop_i64(vm);
int64_t a = vm_pop_i64(vm);
vm_push_i64(vm, a + b);
return 0; // success
}