genie-misc.c

     
   1  //! @file genie-misc.c
   2  //! @author J. Marcel van der Veer
   3  
   4  //! @section Copyright
   5  //!
   6  //! This file is part of Algol68G - an Algol 68 interpreter.
   7  //! Copyright 2001-2026 J. Marcel van der Veer [algol68g@algol68genie.nl].
   8  
   9  //! @section License
  10  //!
  11  //! This program is free software; you can redistribute it and/or modify it 
  12  //! under the terms of the GNU General Public License as published by the 
  13  //! Free Software Foundation; either version 3 of the License, or 
  14  //! (at your option) any later version.
  15  //!
  16  //! This program is distributed in the hope that it will be useful, but 
  17  //! WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  18  //! or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 
  19  //! more details. You should have received a copy of the GNU General Public 
  20  //! License along with this program. If not, see [http://www.gnu.org/licenses/].
  21  
  22  //! @section Synopsis
  23  //!
  24  //! Miscellaneous interpreter routines.
  25  
  26  #include "a68g.h"
  27  #include "a68g-conversion.h"
  28  #include "a68g-double.h"
  29  #include "a68g-genie.h"
  30  #include "a68g-mp.h"
  31  #include "a68g-prelude.h"
  32  
  33  #define VECTOR_SIZE 512
  34  #define FD_READ 0
  35  #define FD_WRITE 1
  36  
  37  //! @brief Nop for the genie, for instance '+' for INT or REAL.
  38  
  39  void genie_idle (NODE_T *p)
  40  {
  41    (void) p;
  42  }
  43  
  44  
  45  //! @brief Unimplemented feature handler.
  46  
  47  void genie_unimplemented (NODE_T *p)
  48  {
  49    diagnostic (A68G_RUNTIME_ERROR, p, ERROR_UNIMPLEMENTED);
  50    exit_genie (p, A68G_RUNTIME_ERROR);
  51  }
  52  
  53  
  54  //! @brief PROC sleep = (NUMBER) INT
  55  
  56  void genie_sleep (NODE_T *p)
  57  {
  58    DECREMENT_STACK_POINTER (p, SIZE (M_NUMBER));
  59    MOID_T *mode = (MOID_T *) (VALUE ((A68G_UNION *) STACK_TOP));
  60    if (mode == M_INT) {
  61      INT_T wait = ABS (VALUE ((A68G_INT *) (STACK_OFFSET (A68G_UNION_SIZE))));
  62      CHECK_INT_SHORTEN (p, wait);
  63      PRELUDE_ERROR (wait < 0, p, ERROR_INVALID_ARGUMENT, M_INT);
  64      while (wait > 0) {
  65        wait = (int) sleep ((unt) wait);
  66      }
  67      PUSH_VALUE (p, (INT_T) wait, A68G_INT);
  68      return;
  69    } else if (mode == M_REAL) {
  70      REAL_T z = ABS (VALUE ((A68G_REAL *) (STACK_OFFSET (A68G_UNION_SIZE))));
  71      CHECK_REAL (p, z, M_REAL);
  72      if (a68g_finite_real (z)) {
  73        PRELUDE_ERROR (z > (REAL_T) A68G_MAX_INT / (REAL_T) A68G_GIGA, p, ERROR_INVALID_ARGUMENT, M_REAL);
  74        INT_T wait = z * A68G_GIGA;
  75        PUSH_VALUE (p, (INT_T) a68g_sleep (wait), A68G_INT);
  76      }
  77      return;
  78    } else {
  79  #if (A68G_LEVEL >= 3)
  80      if (mode == M_LONG_INT) {
  81        DOUBLE_NUM_T k = abs_double_int (VALUE ((A68G_LONG_INT *) (STACK_OFFSET (A68G_UNION_SIZE))));
  82        PRELUDE_ERROR (HW (k) != 0, p, ERROR_INVALID_ARGUMENT, M_INT);
  83        PUSH_UNION (p, (void *) M_INT);
  84        PUSH_VALUE (p, LW (k), A68G_INT);
  85        INCREMENT_STACK_POINTER (p, SIZE (M_NUMBER) - (A68G_UNION_SIZE + SIZE (M_INT)));
  86        genie_sleep (p);
  87        return;
  88      } else if (mode == M_LONG_REAL) {
  89        DOUBLE_T z = ABS (DOUBLE_NUM_FLOAT ((VALUE ((A68G_LONG_REAL *) (STACK_OFFSET (A68G_UNION_SIZE))))));
  90        CHECK_DOUBLE_REAL (p, z, M_LONG_REAL);
  91        PUSH_UNION (p, (void *) M_REAL);
  92        PUSH_VALUE (p, (REAL_T) z, A68G_REAL);
  93        INCREMENT_STACK_POINTER (p, SIZE (M_NUMBER) - (A68G_UNION_SIZE + SIZE (M_REAL)));
  94        genie_sleep (p);
  95        return;
  96      }
  97  #endif
  98      if (mode == M_LONG_INT || mode == M_LONGER_INT) {
  99        MP_T *mp = (MP_T *) (STACK_OFFSET (A68G_UNION_SIZE));
 100        int limbs = LIMBS (mode);
 101        abs_mp (p, mp, mp, limbs);
 102        INT_T k = mp_to_int (p, mp, limbs);
 103        PUSH_UNION (p, (void *) M_INT);
 104        PUSH_VALUE (p, k, A68G_INT);
 105        INCREMENT_STACK_POINTER (p, SIZE (M_NUMBER) - (A68G_UNION_SIZE + SIZE (M_INT)));
 106        genie_sleep (p);
 107        return;
 108      } else if (mode == M_LONG_REAL || mode == M_LONGER_REAL) {
 109        MP_T *mp = (MP_T *) (STACK_OFFSET (A68G_UNION_SIZE));
 110        int limbs = LIMBS (mode);
 111        abs_mp (p, mp, mp, limbs);
 112        REAL_T u = mp_to_real (p, mp, limbs);
 113        PUSH_UNION (p, (void *) M_REAL);
 114        PUSH_VALUE (p, u, A68G_REAL);
 115        INCREMENT_STACK_POINTER (p, SIZE (M_NUMBER) - (A68G_UNION_SIZE + SIZE (M_REAL)));
 116        genie_sleep (p);
 117        return;
 118      } else {
 119        PUSH_VALUE (p, (INT_T) - 1, A68G_INT);
 120        return;
 121      }
 122    }
 123  }
 124  
 125  
 126  //! @brief PROC system = (STRING) INT
 127  
 128  void genie_system (NODE_T *p)
 129  {
 130    A68G_REF cmd;
 131    POP_REF (p, &cmd);
 132    CHECK_INIT (p, INITIALISED (&cmd), M_STRING);
 133    size_t size = 1 + a68g_string_size (p, cmd);
 134    A68G_REF ref_z = heap_generator (p, M_C_STRING, 1 + size);
 135    PUSH_VALUE (p, system (a_to_c_string (p, DEREF (char, &ref_z), cmd)), A68G_INT);
 136  }
 137  
 138  
 139  //! @brief PROC (PROC VOID) VOID on gc event
 140  
 141  void genie_on_gc_event (NODE_T *p)
 142  {
 143    POP_PROCEDURE (p, &A68G (on_gc_event));
 144  }
 145  
 146  
 147  //! @brief Generic procedure for OP AND BECOMES (+:=, -:=, ...).
 148  
 149  void genie_f_and_becomes (NODE_T *p, MOID_T *ref, GPROC *f)
 150  {
 151    MOID_T *mode = SUB (ref);
 152    size_t size = SIZE (mode);
 153    BYTE_T *src = STACK_OFFSET (-size), *addr;
 154    A68G_REF *dst = (A68G_REF *) STACK_OFFSET (-size - A68G_REF_SIZE);
 155    CHECK_REF (p, *dst, ref);
 156    addr = ADDRESS (dst);
 157    PUSH (p, addr, size);
 158    genie_check_initialisation (p, STACK_OFFSET (-size), mode);
 159    PUSH (p, src, size);
 160    (*f) (p);
 161    POP (p, addr, size);
 162    DECREMENT_STACK_POINTER (p, size);
 163  }
 164  
 165  
 166  //! @brief INT system heap pointer
 167  
 168  void genie_system_heap_pointer (NODE_T *p)
 169  {
 170    PUSH_VALUE (p, (int) (A68G_HP), A68G_INT);
 171  }
 172  
 173  
 174  //! @brief INT system stack pointer
 175  
 176  void genie_system_stack_pointer (NODE_T *p)
 177  {
 178    BYTE_T stack_offset;
 179    PUSH_VALUE (p, (int) (A68G (system_stack_offset) - &stack_offset), A68G_INT);
 180  }
 181  
     

This website is archived by the National Library of the Netherlands.

© J.M. van der Veer   •   jmvdveer@algol68genie.nl