toctave

t(iny)octave
Log | Files | Refs | README

commit 2c91e1330ad07ddd5fad1f8110b928c528f5ad78
parent 205df2979f1d31b3475d396b4ba26a499d2e3b39
Author: Mohammad-Reza Nabipoor <mnabipoor@gnu.org>
Date:   Sat, 28 May 2022 00:49:20 +0430

Change toctave_var to report failure

Diffstat:
Menv.cpp | 11++++++-----
Menv.hpp | 2+-
Mtoctave.jitter | 10+++++++---
3 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/env.cpp b/env.cpp @@ -3,7 +3,6 @@ #include <algorithm> #include <cassert> -#include <cmath> #include <utility> namespace { @@ -23,8 +22,8 @@ vars_find(env::Env& env, const std::string& name) } // anonymous namespace -extern "C" double -toctave_var(void* env, const char* name) +extern "C" bool +toctave_var(void* env, const char* name, double* val) { assert(env); @@ -32,12 +31,14 @@ toctave_var(void* env, const char* name) auto [found, it] = vars_find(*reinterpret_cast<env::Env*>(env), n); if (!found) - return std::nan(""); + return false; auto& var{ *it }; assert(var.name == n); - return var.val; + if (val) + *val = var.val; + return true; } extern "C" void diff --git a/env.hpp b/env.hpp @@ -6,7 +6,7 @@ extern "C" { - double toctave_var(void*, const char* name); + bool toctave_var(void*, const char* name, double* val); void toctave_var_set(void*, const char* name, double val); } diff --git a/toctave.jitter b/toctave.jitter @@ -46,7 +46,7 @@ early-header-c #include <string.h> #include <stdint.h> -double toctave_var(void* env, const char* name); +_Bool toctave_var(void* env, const char* name, double* val); void toctave_var_set(void* env, const char* name, double val); union double_uint64 @@ -121,12 +121,16 @@ end # Push the value of variable onto the stack # Stack: ( -- a) -instruction pushvar (?n var_literal_printer) +instruction pushvar (?n var_literal_printer) # , ?f) code const char* name = (const char*)nan_unbox_uint(JITTER_ARGN0); // For simplicity, we assume the variable is defined - double d = toctave_var(TOCTAVE_STATE_RUNTIME_FIELD(env), name); + double d; + _Bool found = toctave_var(TOCTAVE_STATE_RUNTIME_FIELD(env), name, &d); + // JITTER_BRANCH_FAST_IF_NONZERO(found, JITTER_ARGF1); + if (!found) + d = 0.0 / 0.0; // Put a NaN there TOCTAVE_PUSH_MAINSTACK(d); end end